Panda3D
|
This class provides a wrapper around the various possible malloc schemes Panda might employ. More...
#include "memoryHook.h"
Public Member Functions | |
MemoryHook (const MemoryHook ©) | |
virtual void | alloc_fail (size_t attempted_size) |
This callback method is called whenever a low-level call to call_malloc() has returned NULL, indicating failure. More... | |
void | dec_heap (size_t size) |
Called by our alternative malloc implementations (dlmalloc and ptmalloc2) to indicate they have returned size bytes to the system from the heap. More... | |
DeletedBufferChain * | get_deleted_chain (size_t buffer_size) |
Returns a pointer to a global DeletedBufferChain object suitable for allocating arrays of the indicated size. More... | |
size_t | get_page_size () const |
Returns the operating system page size. More... | |
virtual void * | heap_alloc_array (size_t size) |
Allocates a block of memory from the heap, similar to malloc(). More... | |
virtual void * | heap_alloc_single (size_t size) |
Allocates a block of memory from the heap, similar to malloc(). More... | |
virtual void | heap_free_array (void *ptr) |
Releases a block of memory previously allocated via heap_alloc_array. More... | |
virtual void | heap_free_single (void *ptr) |
Releases a block of memory previously allocated via heap_alloc_single. More... | |
virtual void * | heap_realloc_array (void *ptr, size_t size) |
Resizes a block of memory previously returned from heap_alloc_array. More... | |
bool | heap_trim (size_t pad) |
Attempts to release memory back to the system, if possible. More... | |
void | inc_heap (size_t size) |
Called by our alternative malloc implementations (dlmalloc and ptmalloc2) to indicate they have requested size bytes from the system for the heap. More... | |
virtual void | mark_pointer (void *ptr, size_t orig_size, ReferenceCount *ref_ptr) |
This special method exists only to provide a callback hook into MemoryUsage. More... | |
virtual void * | mmap_alloc (size_t size, bool allow_exec) |
Allocates a raw page or pages of memory directly from the OS. More... | |
virtual void | mmap_free (void *ptr, size_t size) |
Frees a block of memory previously allocated via mmap_alloc(). More... | |
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 call to mmap_alloc(). More... | |
Static Public Member Functions | |
static size_t | get_header_reserved_bytes () |
Returns the number of additional bytes that are reserved at the beginning of every allocated block to store a size_t. More... | |
static size_t | get_memory_alignment () |
Returns the global memory alignment. More... | |
This class provides a wrapper around the various possible malloc schemes Panda might employ.
It also exists to allow the MemoryUsage class in Panda to insert callback hooks to track the size of allocated pointers.
The PANDA_MALLOC_* and PANDA_FREE_* macros are defined to vector through through this class (except in production builds) to facilitate that. Every memory allocation call in Panda should therefore use these macros instead of direct calls to malloc or free. (C++ new and delete operators may be employed for classes which inherit from MemoryBase; otherwise, use the PANDA_MALLOC macros.)
Definition at line 43 of file memoryHook.h.
|
virtual |
This callback method is called whenever a low-level call to call_malloc() has returned NULL, indicating failure.
Since this method is called very low-level, and may be in the middle of any number of critical sections, it will be difficult for this callback initiate any emergency high-level operation to make more memory available. However, this module is set up to assume that that's what this method does, and will make another alloc attempt after it returns. Probably the only sensible thing this method can do, however, is just to display a message and abort.
Definition at line 13932 of file memoryHook.cxx.
Referenced by get_deleted_chain().
|
inline |
Called by our alternative malloc implementations (dlmalloc and ptmalloc2) to indicate they have returned size bytes to the system from the heap.
Definition at line 38 of file memoryHook.I.
References AtomicAdjustDummyImpl::add(), and get_memory_alignment().
Referenced by inc_heap().
DeletedBufferChain * MemoryHook::get_deleted_chain | ( | size_t | buffer_size | ) |
Returns a pointer to a global DeletedBufferChain object suitable for allocating arrays of the indicated size.
There is one unique DeletedBufferChain object for every different size.
Definition at line 13897 of file memoryHook.cxx.
References alloc_fail().
Referenced by MovieVideoCursor::complete_pointers(), mark_pointer(), and SimpleHashMap< const RenderState *, Composition, pointer_hash >::validate().
|
inlinestatic |
Returns the number of additional bytes that are reserved at the beginning of every allocated block to store a size_t.
Definition at line 79 of file memoryHook.I.
References get_page_size().
Referenced by get_memory_alignment(), and round_up_to_page_size().
|
inlinestatic |
Returns the global memory alignment.
This is the number of bytes at which each allocated memory pointer will be aligned.
Definition at line 53 of file memoryHook.I.
References get_header_reserved_bytes().
Referenced by dec_heap(), and round_up_to_page_size().
|
inline |
Returns the operating system page size.
This is the minimum granularity required for calls to mmap_alloc(). Also see round_up_to_page_size().
Definition at line 116 of file memoryHook.I.
References round_up_to_page_size().
Referenced by get_header_reserved_bytes().
|
virtual |
Allocates a block of memory from the heap, similar to malloc().
This will never return NULL; it will abort instead if memory is not available.
This particular function should be used to allocate memory for an array of objects, as opposed to a single object. The only difference is in the bookkeeping.
Definition at line 13627 of file memoryHook.cxx.
References AtomicAdjustDummyImpl::add(), AtomicAdjustDummyImpl::get(), and heap_realloc_array().
Referenced by heap_free_single().
|
virtual |
Allocates a block of memory from the heap, similar to malloc().
This will never return NULL; it will abort instead if memory is not available.
This particular function should be used to allocate memory for a single object, as opposed to an array. The only difference is in the bookkeeping.
Definition at line 13551 of file memoryHook.cxx.
References AtomicAdjustDummyImpl::add(), AtomicAdjustDummyImpl::get(), and heap_free_single().
|
virtual |
Releases a block of memory previously allocated via heap_alloc_array.
Definition at line 13728 of file memoryHook.cxx.
References AtomicAdjustDummyImpl::add(), and heap_trim().
Referenced by heap_realloc_array().
|
virtual |
Releases a block of memory previously allocated via heap_alloc_single.
Definition at line 13596 of file memoryHook.cxx.
References AtomicAdjustDummyImpl::add(), and heap_alloc_array().
Referenced by heap_alloc_single().
|
virtual |
Resizes a block of memory previously returned from heap_alloc_array.
Definition at line 13672 of file memoryHook.cxx.
References AtomicAdjustDummyImpl::add(), and heap_free_array().
Referenced by heap_alloc_array().
bool MemoryHook::heap_trim | ( | size_t | pad | ) |
Attempts to release memory back to the system, if possible.
The pad argument is the minimum amount of unused memory to keep in the heap (against future allocations). Any memory above that may be released to the system, reducing the memory size of this process. There is no guarantee that any memory may be released.
Returns true if any memory was actually released, false otherwise.
Definition at line 13761 of file memoryHook.cxx.
References mmap_alloc().
Referenced by heap_free_array(), and PandaSystem::heap_trim().
|
inline |
Called by our alternative malloc implementations (dlmalloc and ptmalloc2) to indicate they have requested size bytes from the system for the heap.
Definition at line 24 of file memoryHook.I.
References AtomicAdjustDummyImpl::add(), and dec_heap().
|
virtual |
This special method exists only to provide a callback hook into MemoryUsage.
It indicates that the indicated pointer, allocated from somewhere other than a call to heap_alloc(), now contains a pointer to the indicated ReferenceCount object. If orig_size is 0, it indicates that the ReferenceCount object has been destroyed.
Definition at line 13885 of file memoryHook.cxx.
References get_deleted_chain().
Referenced by mmap_free().
|
virtual |
Allocates a raw page or pages of memory directly from the OS.
This will be in a different address space from the memory allocated by heap_alloc(), and so it won't contribute to fragmentation of that memory.
The allocation size must be an integer multiple of the page size. Use round_to_page_size() if there is any doubt.
If allow_exec is true, the memory will be flagged so that it is legal to execute code that has been written to this memory.
Definition at line 13802 of file memoryHook.cxx.
References mmap_free().
Referenced by VertexDataPage::flush_threads(), and heap_trim().
|
virtual |
Frees a block of memory previously allocated via mmap_alloc().
You must know how large the block was.
Definition at line 13858 of file memoryHook.cxx.
References mark_pointer().
Referenced by VertexDataPage::flush_threads(), and mmap_alloc().
|
inline |
Rounds the indicated size request up to the next larger multiple of page_size, to qualify it for a call to mmap_alloc().
Definition at line 128 of file memoryHook.I.
References get_header_reserved_bytes(), and get_memory_alignment().
Referenced by get_page_size().