Panda3D
 All Classes Functions Variables Enumerations
memoryHook.I
1 // Filename: memoryHook.I
2 // Created by: drose (28Jun07)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 
16 ////////////////////////////////////////////////////////////////////
17 // Function: MemoryHook::inc_heap
18 // Access: Public
19 // Description: Called by our alternative malloc implementations
20 // (dlmalloc and ptmalloc2) to indicate they have
21 // requested size bytes from the system for the heap.
22 ////////////////////////////////////////////////////////////////////
23 INLINE void MemoryHook::
24 inc_heap(size_t size) {
25 #ifdef DO_MEMORY_USAGE
26  AtomicAdjust::add(_requested_heap_size, (AtomicAdjust::Integer)size);
27 #endif // DO_MEMORY_USAGE
28 }
29 
30 ////////////////////////////////////////////////////////////////////
31 // Function: MemoryHook::dec_heap
32 // Access: Public
33 // Description: Called by our alternative malloc implementations
34 // (dlmalloc and ptmalloc2) to indicate they have
35 // returned size bytes to the system from the heap.
36 ////////////////////////////////////////////////////////////////////
37 INLINE void MemoryHook::
38 dec_heap(size_t size) {
39 #ifdef DO_MEMORY_USAGE
40  //assert((int)size <= _requested_heap_size);
41  AtomicAdjust::add(_requested_heap_size, -(AtomicAdjust::Integer)size);
42 #endif // DO_MEMORY_USAGE
43 }
44 
45 ////////////////////////////////////////////////////////////////////
46 // Function: MemoryHook::get_memory_alignment
47 // Access: Public, Static
48 // Description: Returns the global memory alignment. This is the
49 // number of bytes at which each allocated memory
50 // pointer will be aligned.
51 ////////////////////////////////////////////////////////////////////
52 INLINE size_t MemoryHook::
54 #ifdef LINMATH_ALIGN
55  // We require 16-byte alignment of certain structures, to support
56  // SSE2. We don't strictly have to align *everything*, but it's just
57  // easier to do so.
58  const size_t alignment_size = 16;
59 #else
60  // Otherwise, use word alignment.
61  const size_t alignment_size = sizeof(void *);
62 #endif
63  return alignment_size;
64 }
65 
66 ////////////////////////////////////////////////////////////////////
67 // Function: MemoryHook::get_header_reserved_bytes
68 // Access: Public, Static
69 // Description: Returns the number of additional bytes that are
70 // reserved at the beginning of every allocated block to
71 // store a size_t.
72 ////////////////////////////////////////////////////////////////////
73 INLINE size_t MemoryHook::
75  // We need to figure out the minimum amount of additional space we
76  // need in order to place a single word at the start of each
77  // allocated block, to store the size of the block.
78 
79 #ifdef LINMATH_ALIGN
80  // If we're doing SSE2 alignment, we must reserve a full 16-byte
81  // block, since anything less than that will spoil the alignment.
82  static const size_t header_reserved_bytes = 16;
83 
84 #elif defined(MEMORY_HOOK_DO_ALIGN)
85  // If we're just aligning to words, we reserve a block as big as two
86  // words, to allow us wiggle room to align the word precisely within
87  // that block.
88  static const size_t header_reserved_bytes = sizeof(size_t) + sizeof(size_t);
89 
90 #else
91  // If we're not aligning, we just need space for the word itself.
92  static const size_t header_reserved_bytes = sizeof(size_t);
93 #endif
94 
95  return header_reserved_bytes;
96 }
97 
98 ////////////////////////////////////////////////////////////////////
99 // Function: MemoryHook::get_page_size
100 // Access: Public
101 // Description: Returns the operating system page size. This is the
102 // minimum granularity required for calls to
103 // mmap_alloc(). Also see round_up_to_page_size().
104 ////////////////////////////////////////////////////////////////////
105 INLINE size_t MemoryHook::
106 get_page_size() const {
107  return _page_size;
108 }
109 
110 ////////////////////////////////////////////////////////////////////
111 // Function: MemoryHook::round_up_to_page_size
112 // Access: Public
113 // Description: Rounds the indicated size request up to the next
114 // larger multiple of page_size, to qualify it for a
115 // call to mmap_alloc().
116 ////////////////////////////////////////////////////////////////////
117 INLINE size_t MemoryHook::
118 round_up_to_page_size(size_t size) const {
119  return ((size + _page_size - 1) / _page_size) * _page_size;
120 }
121 
122 ////////////////////////////////////////////////////////////////////
123 // Function: MemoryHook::inflate_size
124 // Access: Private, Static
125 // Description: Increments the amount of requested size as necessary
126 // to accommodate the extra data we might piggyback on
127 // each allocated block.
128 ////////////////////////////////////////////////////////////////////
129 INLINE size_t MemoryHook::
130 inflate_size(size_t size) {
131 #if defined(MEMORY_HOOK_DO_ALIGN)
132  // If we're aligning, we need to request the header size, plus extra
133  // bytes to give us wiggle room to adjust the pointer.
134  return size + get_header_reserved_bytes() + get_memory_alignment() - 1;
135 #elif defined(DO_MEMORY_USAGE)
136  // If we're not aligning, but we're tracking memory allocations, we
137  // just need the header size extra (this gives us a place to store
138  // the size of the allocated block).
139  return size + get_header_reserved_bytes();
140 #else
141  // If we're not doing any of that, we can just allocate the precise
142  // requested amount.
143  return size;
144 #endif // DO_MEMORY_USAGE
145 }
146 
147 ////////////////////////////////////////////////////////////////////
148 // Function: MemoryHook::alloc_to_ptr
149 // Access: Private, Static
150 // Description: Converts an allocated pointer to a pointer returnable
151 // to the application. Stuffs size in the first n bytes
152 // of the allocated space.
153 ////////////////////////////////////////////////////////////////////
154 INLINE void *MemoryHook::
155 alloc_to_ptr(void *alloc, size_t size) {
156 #if defined(MEMORY_HOOK_DO_ALIGN)
157  size_t alignment = get_memory_alignment();
158  // Move the allocated pointer up to the next even alignment.
159  size_t *root = (size_t *)((((size_t)alloc + alignment - 1) / alignment) * alignment);
160  assert(alloc <= root && (size_t)((char *)root - (char *)alloc) < alignment);
161  root[0] = size;
162  root[1] = (size_t)alloc; // Save the pointer we originally allocated.
163  return (void *)((char *)root + get_header_reserved_bytes());
164 #elif defined(DO_MEMORY_USAGE)
165  size_t *root = (size_t *)alloc;
166  root[0] = size;
167  return (void *)((char *)root + get_header_reserved_bytes());
168 #else
169  return alloc;
170 #endif // DO_MEMORY_USAGE
171 }
172 
173 ////////////////////////////////////////////////////////////////////
174 // Function: MemoryHook::ptr_to_alloc
175 // Access: Private, Static
176 // Description: Converts an application pointer back to the original
177 // allocated pointer. Extracts size from the first n
178 // bytes of the allocated space.
179 ////////////////////////////////////////////////////////////////////
180 INLINE void *MemoryHook::
181 ptr_to_alloc(void *ptr, size_t &size) {
182 #if defined(MEMORY_HOOK_DO_ALIGN)
183  size_t *root = (size_t *)((char *)ptr - get_header_reserved_bytes());
184  size = root[0];
185  void *alloc = (void *)root[1]; // Get the pointer we originally allocated.
186  assert(alloc <= root && (size_t)((char *)root - (char *)alloc) < get_memory_alignment());
187  return alloc;
188 #elif defined(DO_MEMORY_USAGE)
189  size_t *root = (size_t *)((char *)ptr - get_header_reserved_bytes());
190  size = root[0];
191  return (void *)root;
192 #else
193  return ptr;
194 #endif // DO_MEMORY_USAGE
195 }
void inc_heap(size_t size)
Called by our alternative malloc implementations (dlmalloc and ptmalloc2) to indicate they have reque...
Definition: memoryHook.I:24
static void add(Integer &var, Integer delta)
Atomically computes var += delta.
static size_t get_header_reserved_bytes()
Returns the number of additional bytes that are reserved at the beginning of every allocated block to...
Definition: memoryHook.I:74
size_t round_up_to_page_size(size_t size) const
Rounds the indicated size request up to the next larger multiple of page_size, to qualify it for a ca...
Definition: memoryHook.I:118
void dec_heap(size_t size)
Called by our alternative malloc implementations (dlmalloc and ptmalloc2) to indicate they have retur...
Definition: memoryHook.I:38
size_t get_page_size() const
Returns the operating system page size.
Definition: memoryHook.I:106
static size_t get_memory_alignment()
Returns the global memory alignment.
Definition: memoryHook.I:53