Panda3D
Public Member Functions | Static Public Member Functions | List of all members
MemoryHook Class Reference

This class provides a wrapper around the various possible malloc schemes Panda might employ. More...

#include "memoryHook.h"

Inheritance diagram for MemoryHook:
MemoryUsage

Public Member Functions

 MemoryHook (const MemoryHook &copy)
 
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...
 
DeletedBufferChainget_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 constexpr size_t get_memory_alignment ()
 
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 allocated for it. More...
 

Detailed Description

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 37 of file memoryHook.h.

Member Function Documentation

◆ alloc_fail()

void MemoryHook::alloc_fail ( size_t  attempted_size)
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 628 of file memoryHook.cxx.

◆ dec_heap()

void MemoryHook::dec_heap ( size_t  size)
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 30 of file memoryHook.I.

References AtomicAdjustDummyImpl::add().

◆ get_deleted_chain()

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 598 of file memoryHook.cxx.

◆ get_page_size()

size_t MemoryHook::get_page_size ( ) const
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 42 of file memoryHook.I.

◆ get_ptr_size()

size_t MemoryHook::get_ptr_size ( void *  ptr)
inlinestatic

Given a pointer that was returned by a MemoryHook allocation, returns the number of bytes that were allocated for it.

This may be slightly larger than the number of bytes requested. The behavior of this function is undefined if the given pointer was not returned by the MemoryHook allocator or was already freed. May return 0 if not compiling with DO_MEMORY_USAGE.

This is only defined publicly so TypeHandle can get at it; it really shouldn't be used outside of dtoolbase.

Definition at line 67 of file memoryHook.I.

◆ heap_alloc_array()

void * MemoryHook::heap_alloc_array ( size_t  size)
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 327 of file memoryHook.cxx.

◆ heap_alloc_single()

void * MemoryHook::heap_alloc_single ( size_t  size)
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 250 of file memoryHook.cxx.

◆ heap_free_array()

void MemoryHook::heap_free_array ( void *  ptr)
virtual

Releases a block of memory previously allocated via heap_alloc_array.

Definition at line 448 of file memoryHook.cxx.

◆ heap_free_single()

void MemoryHook::heap_free_single ( void *  ptr)
virtual

Releases a block of memory previously allocated via heap_alloc_single.

Definition at line 300 of file memoryHook.cxx.

◆ heap_realloc_array()

void * MemoryHook::heap_realloc_array ( void *  ptr,
size_t  size 
)
virtual

Resizes a block of memory previously returned from heap_alloc_array.

Definition at line 377 of file memoryHook.cxx.

◆ heap_trim()

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 476 of file memoryHook.cxx.

◆ inc_heap()

void MemoryHook::inc_heap ( size_t  size)
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 19 of file memoryHook.I.

References AtomicAdjustDummyImpl::add().

◆ mark_pointer()

void MemoryHook::mark_pointer ( void *  ptr,
size_t  orig_size,
ReferenceCount ref_ptr 
)
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 589 of file memoryHook.cxx.

◆ mmap_alloc()

void * MemoryHook::mmap_alloc ( size_t  size,
bool  allow_exec 
)
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 512 of file memoryHook.cxx.

◆ mmap_free()

void MemoryHook::mmap_free ( void *  ptr,
size_t  size 
)
virtual

Frees a block of memory previously allocated via mmap_alloc().

You must know how large the block was.

Definition at line 566 of file memoryHook.cxx.

◆ round_up_to_page_size()

size_t MemoryHook::round_up_to_page_size ( size_t  size) const
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 51 of file memoryHook.I.


The documentation for this class was generated from the following files: