Panda3D
Public Member Functions | Static Public Member Functions
MemoryHook Class Reference

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

#include "memoryHook.h"

List of all members.

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.
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.
DeletedBufferChainget_deleted_chain (size_t buffer_size)
 Returns a pointer to a global DeletedBufferChain object suitable for allocating arrays of the indicated size.
size_t get_page_size () const
 Returns the operating system page size.
virtual void * heap_alloc_array (size_t size)
 Allocates a block of memory from the heap, similar to malloc().
virtual void * heap_alloc_single (size_t size)
 Allocates a block of memory from the heap, similar to malloc().
virtual void heap_free_array (void *ptr)
 Releases a block of memory previously allocated via heap_alloc_array.
virtual void heap_free_single (void *ptr)
 Releases a block of memory previously allocated via heap_alloc_single.
virtual void * heap_realloc_array (void *ptr, size_t size)
 Resizes a block of memory previously returned from heap_alloc_array.
bool heap_trim (size_t pad)
 Attempts to release memory back to the system, if possible.
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.
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.
virtual void * mmap_alloc (size_t size, bool allow_exec)
 Allocates a raw page or pages of memory directly from the OS.
virtual void mmap_free (void *ptr, size_t size)
 Frees a block of memory previously allocated via mmap_alloc().
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().

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.
static size_t get_memory_alignment ()
 Returns the global memory alignment.

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


Member Function Documentation

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

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 38 of file memoryHook.I.

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

size_t MemoryHook::get_header_reserved_bytes ( ) [inline, static]

Returns the number of additional bytes that are reserved at the beginning of every allocated block to store a size_t.

Definition at line 74 of file memoryHook.I.

size_t MemoryHook::get_memory_alignment ( ) [inline, static]

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.

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 106 of file memoryHook.I.

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

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

void MemoryHook::heap_free_array ( void *  ptr) [virtual]

Releases a block of memory previously allocated via heap_alloc_array.

Definition at line 13707 of file memoryHook.cxx.

void MemoryHook::heap_free_single ( void *  ptr) [virtual]

Releases a block of memory previously allocated via heap_alloc_single.

Definition at line 13575 of file memoryHook.cxx.

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

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

Referenced by PandaSystem::heap_trim().

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 24 of file memoryHook.I.

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

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

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

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 118 of file memoryHook.I.


The documentation for this class was generated from the following files:
 All Classes Functions Variables Enumerations