Panda3D

Loading...
Searching...
No Matches
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:

## 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.
DeletedBufferChain * | get_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 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.

## 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.

## Constructor & Destructor Documentation

## ◆ MemoryHook() [1/2]

MemoryHook::MemoryHook | ( |  | ) |

Definition at line 204 of file memoryHook.cxx.

## ◆ MemoryHook() [2/2]

MemoryHook::MemoryHook | ( | const MemoryHook & | copy | ) |

Definition at line 231 of file memoryHook.cxx.

## ◆ ~MemoryHook()

MemoryHook::~MemoryHook | ( |  | ) |
| virtual

Definition at line 248 of file memoryHook.cxx.

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

Referenced by heap_alloc_array(), heap_alloc_single(), and heap_realloc_array().

## ◆ 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 29 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 609 of file memoryHook.cxx.

## ◆ get_memory_alignment()

static constexpr size_t MemoryHook::get_memory_alignment | ( |  | ) |
| inline static constexpr

Definition at line 55 of file memoryHook.h.

## ◆ 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 41 of file memoryHook.I.

## ◆ get_ptr_size()

size_t MemoryHook::get_ptr_size | ( | void * | ptr | ) |
| inline static

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

Referenced by TypeHandle::allocate_array(), TypeHandle::deallocate_array(), heap_alloc_array(), heap_alloc_single(), heap_realloc_array(), and TypeHandle::reallocate_array().

## ◆ 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 338 of file memoryHook.cxx.

References AtomicAdjustDummyImpl::add(), alloc_fail(), AtomicAdjustDummyImpl::get(), and get_ptr_size().

## ◆ 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 261 of file memoryHook.cxx.

References AtomicAdjustDummyImpl::add(), alloc_fail(), AtomicAdjustDummyImpl::get(), and get_ptr_size().

## ◆ 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 459 of file memoryHook.cxx.

References AtomicAdjustDummyImpl::add().

## ◆ 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 311 of file memoryHook.cxx.

References AtomicAdjustDummyImpl::add().

## ◆ 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 388 of file memoryHook.cxx.

References AtomicAdjustDummyImpl::add(), alloc_fail(), and get_ptr_size().

## ◆ 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 487 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 18 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 600 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 523 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 577 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 50 of file memoryHook.I.

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

- dtool/src/dtoolbase/memoryHook.h
- dtool/src/dtoolbase/memoryHook.cxx
- dtool/src/dtoolbase/memoryHook.I