// Filename: pallocator.T // Created by: drose (05Jun01) // //////////////////////////////////////////////////////////////////// // // PANDA 3D SOFTWARE // Copyright (c) Carnegie Mellon University. All rights reserved. // // All use of this software is subject to the terms of the revised BSD // license. You should have received a copy of this license along // with this source code in a file named "LICENSE." // //////////////////////////////////////////////////////////////////// template INLINE pallocator_single:: pallocator_single(TypeHandle type_handle) throw() : _type_handle(type_handle) { } template INLINE TYPENAME pallocator_single::pointer pallocator_single:: allocate(TYPENAME pallocator_single::size_type n, TYPENAME allocator::const_pointer) { TAU_PROFILE("pallocator_single:allocate()", " ", TAU_USER); // This doesn't support allocating arrays. assert(n == 1); return StaticDeletedChain::allocate(sizeof(Type), _type_handle); } template INLINE void pallocator_single:: deallocate(TYPENAME pallocator_single::pointer p, TYPENAME pallocator_single::size_type) { TAU_PROFILE("pallocator_single:deallocate()", " ", TAU_USER); StaticDeletedChain::deallocate(p, _type_handle); } template INLINE pallocator_array:: pallocator_array(TypeHandle type_handle) throw() : _type_handle(type_handle) { } template INLINE TYPENAME pallocator_array::pointer pallocator_array:: allocate(TYPENAME pallocator_array::size_type n, TYPENAME allocator::const_pointer) { TAU_PROFILE("pallocator_array:allocate()", " ", TAU_USER); #ifdef DO_MEMORY_USAGE size_t alloc_size = n * sizeof(Type); // We also need to store the total number of bytes we allocated. alloc_size += sizeof(size_t); _type_handle.inc_memory_usage(TypeHandle::MC_array, (int)alloc_size); void *ptr = (TYPENAME pallocator_array::pointer)PANDA_MALLOC_ARRAY(alloc_size); *((size_t *)ptr) = alloc_size; return (TYPENAME pallocator_array::pointer)(((size_t *)ptr) + 1); #else return (TYPENAME pallocator_array::pointer)malloc(n * sizeof(Type)); #endif // DO_MEMORY_USAGE } template INLINE void pallocator_array:: deallocate(TYPENAME pallocator_array::pointer p, TYPENAME pallocator_array::size_type) { TAU_PROFILE("pallocator_array:deallocate()", " ", TAU_USER); #ifdef DO_MEMORY_USAGE // Now we need to recover the total number of bytes. size_t alloc_size = *(((size_t *)p) - 1); _type_handle.dec_memory_usage(TypeHandle::MC_array, (int)alloc_size); PANDA_FREE_ARRAY(((size_t *)p) - 1); #else free(p); #endif // DO_MEMORY_USAGE }