array.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*************************************************************************
  2. * *
  3. * Open Dynamics Engine, Copyright (C) 2001,2002 Russell L. Smith. *
  4. * All rights reserved. Email: [email protected] Web: www.q12.org *
  5. * *
  6. * This library is free software; you can redistribute it and/or *
  7. * modify it under the terms of EITHER: *
  8. * (1) The GNU Lesser General Public License as published by the Free *
  9. * Software Foundation; either version 2.1 of the License, or (at *
  10. * your option) any later version. The text of the GNU Lesser *
  11. * General Public License is included with this library in the *
  12. * file LICENSE.TXT. *
  13. * (2) The BSD-style license that is included with this library in *
  14. * the file LICENSE-BSD.TXT. *
  15. * *
  16. * This library is distributed in the hope that it will be useful, *
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of *
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files *
  19. * LICENSE.TXT and LICENSE-BSD.TXT for more details. *
  20. * *
  21. *************************************************************************/
  22. #include <ode/odeconfig.h>
  23. #include <ode/memory.h>
  24. #include <ode/error.h>
  25. #include "config.h"
  26. #include "array.h"
  27. static inline int roundUpToPowerOfTwo (int x)
  28. {
  29. int i = 1;
  30. while (i < x) i <<= 1;
  31. return i;
  32. }
  33. void dArrayBase::_freeAll (int sizeofT)
  34. {
  35. if (_data) {
  36. if (_data == this+1) return; // if constructLocalArray() was called
  37. dFree (_data,_anum * sizeofT);
  38. }
  39. }
  40. void dArrayBase::_setSize (int newsize, int sizeofT)
  41. {
  42. if (newsize < 0) return;
  43. if (newsize > _anum) {
  44. if (_data == this+1) {
  45. // this is a no-no, because constructLocalArray() was called
  46. dDebug (0,"setSize() out of space in LOCAL array");
  47. }
  48. int newanum = roundUpToPowerOfTwo (newsize);
  49. if (_data) _data = dRealloc (_data, _anum*sizeofT, newanum*sizeofT);
  50. else _data = dAlloc (newanum*sizeofT);
  51. _anum = newanum;
  52. }
  53. _size = newsize;
  54. }
  55. void * dArrayBase::operator new (size_t size)
  56. {
  57. return dAlloc (size);
  58. }
  59. void dArrayBase::operator delete (void *ptr, size_t size)
  60. {
  61. dFree (ptr,size);
  62. }
  63. void dArrayBase::constructLocalArray (int __anum)
  64. {
  65. _size = 0;
  66. _anum = __anum;
  67. _data = this+1;
  68. }