Panda3D
|
00001 // Filename: memoryHook.I 00002 // Created by: drose (28Jun07) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) Carnegie Mellon University. All rights reserved. 00008 // 00009 // All use of this software is subject to the terms of the revised BSD 00010 // license. You should have received a copy of this license along 00011 // with this source code in a file named "LICENSE." 00012 // 00013 //////////////////////////////////////////////////////////////////// 00014 00015 00016 //////////////////////////////////////////////////////////////////// 00017 // Function: MemoryHook::inc_heap 00018 // Access: Public 00019 // Description: Called by our alternative malloc implementations 00020 // (dlmalloc and ptmalloc2) to indicate they have 00021 // requested size bytes from the system for the heap. 00022 //////////////////////////////////////////////////////////////////// 00023 INLINE void MemoryHook:: 00024 inc_heap(size_t size) { 00025 #ifdef DO_MEMORY_USAGE 00026 AtomicAdjust::add(_requested_heap_size, (AtomicAdjust::Integer)size); 00027 #endif // DO_MEMORY_USAGE 00028 } 00029 00030 //////////////////////////////////////////////////////////////////// 00031 // Function: MemoryHook::dec_heap 00032 // Access: Public 00033 // Description: Called by our alternative malloc implementations 00034 // (dlmalloc and ptmalloc2) to indicate they have 00035 // returned size bytes to the system from the heap. 00036 //////////////////////////////////////////////////////////////////// 00037 INLINE void MemoryHook:: 00038 dec_heap(size_t size) { 00039 #ifdef DO_MEMORY_USAGE 00040 //assert((int)size <= _requested_heap_size); 00041 AtomicAdjust::add(_requested_heap_size, -(AtomicAdjust::Integer)size); 00042 #endif // DO_MEMORY_USAGE 00043 } 00044 00045 //////////////////////////////////////////////////////////////////// 00046 // Function: MemoryHook::get_page_size 00047 // Access: Public 00048 // Description: Returns the operating system page size. This is the 00049 // minimum granularity required for calls to 00050 // mmap_alloc(). Also see round_up_to_page_size(). 00051 //////////////////////////////////////////////////////////////////// 00052 INLINE size_t MemoryHook:: 00053 get_page_size() const { 00054 return _page_size; 00055 } 00056 00057 //////////////////////////////////////////////////////////////////// 00058 // Function: MemoryHook::round_up_to_page_size 00059 // Access: Public 00060 // Description: Rounds the indicated size request up to the next 00061 // larger multiple of page_size, to qualify it for a 00062 // call to mmap_alloc(). 00063 //////////////////////////////////////////////////////////////////// 00064 INLINE size_t MemoryHook:: 00065 round_up_to_page_size(size_t size) const { 00066 return ((size + _page_size - 1) / _page_size) * _page_size; 00067 } 00068 00069 //////////////////////////////////////////////////////////////////// 00070 // Function: MemoryHook::inflate_size 00071 // Access: Private, Static 00072 // Description: Increments the amount of requested size as necessary 00073 // to accommodate the extra data we might piggyback on 00074 // each allocated block. 00075 //////////////////////////////////////////////////////////////////// 00076 INLINE size_t MemoryHook:: 00077 inflate_size(size_t size) { 00078 #ifdef DO_MEMORY_USAGE 00079 return size + sizeof(size_t); 00080 #else 00081 return size; 00082 #endif // DO_MEMORY_USAGE 00083 } 00084 00085 //////////////////////////////////////////////////////////////////// 00086 // Function: MemoryHook::alloc_to_ptr 00087 // Access: Private, Static 00088 // Description: Converts an allocated pointer to a pointer returnable 00089 // to the application. Stuffs size in the first n bytes 00090 // of the allocated space. 00091 //////////////////////////////////////////////////////////////////// 00092 INLINE void *MemoryHook:: 00093 alloc_to_ptr(void *alloc, size_t size) { 00094 #ifdef DO_MEMORY_USAGE 00095 size_t *root = (size_t *)alloc; 00096 root[0] = size; 00097 return (void *)(root + 1); 00098 #else 00099 return alloc; 00100 #endif // DO_MEMORY_USAGE 00101 } 00102 00103 //////////////////////////////////////////////////////////////////// 00104 // Function: MemoryHook::ptr_to_alloc 00105 // Access: Private, Static 00106 // Description: Converts an application pointer back to the original 00107 // allocated pointer. Extracts size from the first n 00108 // bytes of the allocated space. 00109 //////////////////////////////////////////////////////////////////// 00110 INLINE void *MemoryHook:: 00111 ptr_to_alloc(void *ptr, size_t &size) { 00112 #ifdef DO_MEMORY_USAGE 00113 size_t *root = (size_t *)ptr; 00114 root -= 1; 00115 size = root[0]; 00116 return (void *)root; 00117 #else 00118 return ptr; 00119 #endif // DO_MEMORY_USAGE 00120 }