Panda3D
Loading...
Searching...
No Matches
memoryHook.h
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.h
10 * @author drose
11 * @date 2007-06-28
12 */
13
14#ifndef MEMORYHOOK_H
15#define MEMORYHOOK_H
16
17#include "dtoolbase.h"
18#include "numeric_types.h"
19#include "atomicAdjust.h"
20#include "mutexImpl.h"
21#include <map>
22
24
25/**
26 * This class provides a wrapper around the various possible malloc schemes
27 * Panda might employ. It also exists to allow the MemoryUsage class in Panda
28 * to insert callback hooks to track the size of allocated pointers.
29 *
30 * The PANDA_MALLOC_* and PANDA_FREE_* macros are defined to vector through
31 * through this class (except in production builds) to facilitate that. Every
32 * memory allocation call in Panda should therefore use these macros instead
33 * of direct calls to malloc or free. (C++ new and delete operators may be
34 * employed for classes which inherit from MemoryBase; otherwise, use the
35 * PANDA_MALLOC macros.)
36 */
37class EXPCL_DTOOL_DTOOLBASE MemoryHook {
38public:
39 MemoryHook();
40 MemoryHook(const MemoryHook &copy);
41 virtual ~MemoryHook();
42
43 virtual void *heap_alloc_single(size_t size);
44 virtual void heap_free_single(void *ptr);
45
46 virtual void *heap_alloc_array(size_t size);
47 virtual void *heap_realloc_array(void *ptr, size_t size);
48 virtual void heap_free_array(void *ptr);
49
50 INLINE void inc_heap(size_t size);
51 INLINE void dec_heap(size_t size);
52
53 bool heap_trim(size_t pad);
54
55 constexpr static size_t get_memory_alignment() {
56 return MEMORY_HOOK_ALIGNMENT;
57 }
58
59 virtual void *mmap_alloc(size_t size, bool allow_exec);
60 virtual void mmap_free(void *ptr, size_t size);
61 INLINE size_t get_page_size() const;
62 INLINE size_t round_up_to_page_size(size_t size) const;
63
64 virtual void mark_pointer(void *ptr, size_t orig_size, ReferenceCount *ref_ptr);
65
66 DeletedBufferChain *get_deleted_chain(size_t buffer_size);
67
68 virtual void alloc_fail(size_t attempted_size);
69
70 INLINE static size_t get_ptr_size(void *ptr);
71
72protected:
73 TVOLATILE AtomicAdjust::Integer _total_heap_single_size;
74 TVOLATILE AtomicAdjust::Integer _total_heap_array_size;
75 TVOLATILE AtomicAdjust::Integer _requested_heap_size;
76 TVOLATILE AtomicAdjust::Integer _total_mmap_size;
77
78 // If the allocated heap size crosses this threshold, we call
79 // overflow_heap_size().
80 size_t _max_heap_size;
81
82 virtual void overflow_heap_size();
83
84private:
85 size_t _page_size;
86
87 typedef std::map<size_t, DeletedBufferChain *> DeletedChains;
88 DeletedChains _deleted_chains;
89
90 mutable MutexImpl _lock;
91};
92
93#include "memoryHook.I"
94
95#endif
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This template class can be used to provide faster allocation/deallocation for many Panda objects.
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().
virtual void alloc_fail(size_t attempted_size)
This callback method is called whenever a low-level call to call_malloc() has returned NULL,...
virtual void * heap_alloc_array(size_t size)
Allocates a block of memory from the heap, similar to malloc().
DeletedBufferChain * get_deleted_chain(size_t buffer_size)
Returns a pointer to a global DeletedBufferChain object suitable for allocating arrays of the indicat...
void inc_heap(size_t size)
Called by our alternative malloc implementations (dlmalloc and ptmalloc2) to indicate they have reque...
Definition memoryHook.I:19
virtual void heap_free_array(void *ptr)
Releases a block of memory previously allocated via heap_alloc_array.
virtual void * heap_alloc_single(size_t size)
Allocates a block of memory from the heap, similar to malloc().
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.
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
virtual void heap_free_single(void *ptr)
Releases a block of memory previously allocated via heap_alloc_single.
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
bool heap_trim(size_t pad)
Attempts to release memory back to the system, if possible.
virtual void * heap_realloc_array(void *ptr, size_t size)
Resizes a block of memory previously returned from heap_alloc_array.
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 get_page_size() const
Returns the operating system page size.
Definition memoryHook.I:42
A base class for all things that want to be reference-counted.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.