Panda3D
memoryHook.I
Go to the documentation of this file.
1 /**
2  * PANDA 3D SOFTWARE
3  * Copyright (c) Carnegie Mellon University. All rights reserved.
4  *
5  * All use of this software is subject to the terms of the revised BSD
6  * license. You should have received a copy of this license along
7  * with this source code in a file named "LICENSE."
8  *
9  * @file memoryHook.I
10  * @author drose
11  * @date 2007-06-28
12  */
13 
14 /**
15  * Called by our alternative malloc implementations (dlmalloc and ptmalloc2)
16  * to indicate they have requested size bytes from the system for the heap.
17  */
18 INLINE void MemoryHook::
19 inc_heap(size_t size) {
20 #ifdef DO_MEMORY_USAGE
21  AtomicAdjust::add(_requested_heap_size, (AtomicAdjust::Integer)size);
22 #endif // DO_MEMORY_USAGE
23 }
24 
25 /**
26  * Called by our alternative malloc implementations (dlmalloc and ptmalloc2)
27  * to indicate they have returned size bytes to the system from the heap.
28  */
29 INLINE void MemoryHook::
30 dec_heap(size_t size) {
31 #ifdef DO_MEMORY_USAGE
32  // assert((int)size <= _requested_heap_size);
33  AtomicAdjust::add(_requested_heap_size, -(AtomicAdjust::Integer)size);
34 #endif // DO_MEMORY_USAGE
35 }
36 
37 /**
38  * Returns the operating system page size. This is the minimum granularity
39  * required for calls to mmap_alloc(). Also see round_up_to_page_size().
40  */
41 INLINE size_t MemoryHook::
42 get_page_size() const {
43  return _page_size;
44 }
45 
46 /**
47  * Rounds the indicated size request up to the next larger multiple of
48  * page_size, to qualify it for a call to mmap_alloc().
49  */
50 INLINE size_t MemoryHook::
51 round_up_to_page_size(size_t size) const {
52  return ((size + _page_size - 1) / _page_size) * _page_size;
53 }
54 
55 /**
56  * Given a pointer that was returned by a MemoryHook allocation, returns the
57  * number of bytes that were allocated for it. This may be slightly larger
58  * than the number of bytes requested.
59  * The behavior of this function is undefined if the given pointer was not
60  * returned by the MemoryHook allocator or was already freed.
61  * May return 0 if not compiling with DO_MEMORY_USAGE.
62  *
63  * This is only defined publicly so TypeHandle can get at it; it really
64  * shouldn't be used outside of dtoolbase.
65  */
66 INLINE size_t MemoryHook::
67 get_ptr_size(void *ptr) {
68 #if defined(MEMORY_HOOK_DO_ALIGN)
69  uintptr_t *root = (uintptr_t *)ptr;
70  return (size_t)root[-2];
71 #elif defined(USE_MEMORY_DLMALLOC) || defined(USE_MEMORY_PTMALLOC2)
72  // If we are using dlmalloc, we know how it stores the size.
73  size_t *root = (size_t *)ptr;
74  return (root[-1] & ~0x7) - sizeof(size_t);
75 #elif defined(DO_MEMORY_USAGE)
76  size_t *root = (size_t *)((char *)ptr - MEMORY_HOOK_ALIGNMENT);
77  return *root;
78 #else
79  return 0;
80 #endif // DO_MEMORY_USAGE
81 }
void inc_heap(size_t size)
Called by our alternative malloc implementations (dlmalloc and ptmalloc2) to indicate they have reque...
Definition: memoryHook.I:19
static Integer add(Integer &var, Integer delta)
Atomically computes var += delta.
size_t get_page_size() const
Returns the operating system page size.
Definition: memoryHook.I:42
void dec_heap(size_t size)
Called by our alternative malloc implementations (dlmalloc and ptmalloc2) to indicate they have retur...
Definition: memoryHook.I:30
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:51
static size_t get_ptr_size(void *ptr)
Given a pointer that was returned by a MemoryHook allocation, returns the number of bytes that were a...
Definition: memoryHook.I:67