pallocator.T 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Filename: pallocator.T
  2. // Created by: drose (05Jun01)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. //
  6. // PANDA 3D SOFTWARE
  7. // Copyright (c) 2001, Disney Enterprises, Inc. All rights reserved
  8. //
  9. // All use of this software is subject to the terms of the Panda 3d
  10. // Software license. You should have received a copy of this license
  11. // along with this source code; you will also find a current copy of
  12. // the license at http://www.panda3d.org/license.txt .
  13. //
  14. // To contact the maintainers of this program write to
  15. // [email protected] .
  16. //
  17. ////////////////////////////////////////////////////////////////////
  18. template<class Type>
  19. INLINE pallocator_single<Type>::
  20. pallocator_single(TypeHandle type_handle) throw() :
  21. _type_handle(type_handle)
  22. {
  23. }
  24. template<class Type>
  25. INLINE TYPENAME pallocator_single<Type>::pointer pallocator_single<Type>::
  26. allocate(TYPENAME pallocator_single<Type>::size_type n, TYPENAME allocator<void>::const_pointer) {
  27. TAU_PROFILE("pallocator_single:allocate()", " ", TAU_USER);
  28. // This doesn't support allocating arrays.
  29. assert(n == 1);
  30. return StaticDeletedChain<Type>::allocate(sizeof(Type), _type_handle);
  31. }
  32. template<class Type>
  33. INLINE void pallocator_single<Type>::
  34. deallocate(TYPENAME pallocator_single<Type>::pointer p, TYPENAME pallocator_single<Type>::size_type) {
  35. TAU_PROFILE("pallocator_single:deallocate()", " ", TAU_USER);
  36. StaticDeletedChain<Type>::deallocate(p, _type_handle);
  37. }
  38. template<class Type>
  39. INLINE pallocator_array<Type>::
  40. pallocator_array(TypeHandle type_handle) throw() :
  41. _type_handle(type_handle)
  42. {
  43. }
  44. template<class Type>
  45. INLINE TYPENAME pallocator_array<Type>::pointer pallocator_array<Type>::
  46. allocate(TYPENAME pallocator_array<Type>::size_type n, TYPENAME allocator<void>::const_pointer) {
  47. TAU_PROFILE("pallocator_array:allocate()", " ", TAU_USER);
  48. #ifdef DO_MEMORY_USAGE
  49. size_t alloc_size = n * sizeof(Type);
  50. // We also need to store the total number of bytes we allocated.
  51. alloc_size += sizeof(size_t);
  52. _type_handle.inc_memory_usage(TypeHandle::MC_array, (int)alloc_size);
  53. void *ptr = (TYPENAME pallocator_array<Type>::pointer)PANDA_MALLOC_ARRAY(alloc_size);
  54. *((size_t *)ptr) = alloc_size;
  55. return (TYPENAME pallocator_array<Type>::pointer)(((size_t *)ptr) + 1);
  56. #else
  57. return (TYPENAME pallocator_array<Type>::pointer)malloc(n * sizeof(Type));
  58. #endif // DO_MEMORY_USAGE
  59. }
  60. template<class Type>
  61. INLINE void pallocator_array<Type>::
  62. deallocate(TYPENAME pallocator_array<Type>::pointer p, TYPENAME pallocator_array<Type>::size_type) {
  63. TAU_PROFILE("pallocator_array:deallocate()", " ", TAU_USER);
  64. #ifdef DO_MEMORY_USAGE
  65. // Now we need to recover the total number of bytes.
  66. size_t alloc_size = *(((size_t *)p) - 1);
  67. _type_handle.dec_memory_usage(TypeHandle::MC_array, (int)alloc_size);
  68. PANDA_FREE_ARRAY(((size_t *)p) - 1);
  69. #else
  70. free(p);
  71. #endif // DO_MEMORY_USAGE
  72. }