22#ifndef WIN32_LEAN_AND_MEAN
23#define WIN32_LEAN_AND_MEAN 1
35#define MAP_ANON 0x1000
43static_assert(MEMORY_HOOK_ALIGNMENT >=
sizeof(size_t),
44 "MEMORY_HOOK_ALIGNMENT should at least be sizeof(size_t)");
45static_assert(MEMORY_HOOK_ALIGNMENT >=
sizeof(
void *),
46 "MEMORY_HOOK_ALIGNMENT should at least be sizeof(void *)");
47static_assert(MEMORY_HOOK_ALIGNMENT * 8 >= NATIVE_WORDSIZE,
48 "MEMORY_HOOK_ALIGNMENT * 8 should at least be NATIVE_WORDSIZE");
49static_assert((MEMORY_HOOK_ALIGNMENT & (MEMORY_HOOK_ALIGNMENT - 1)) == 0,
50 "MEMORY_HOOK_ALIGNMENT should be a power of two");
54#elif defined(USE_MEMORY_MIMALLOC)
61#define call_malloc mi_malloc
62#define call_realloc mi_realloc
63#define call_free mi_free
64#undef MEMORY_HOOK_MALLOC_LOCK
66#elif defined(USE_MEMORY_DLMALLOC)
72#define DLMALLOC_EXPORT static
73#define USE_DL_PREFIX 1
79#define MALLOC_ALIGNMENT MEMORY_HOOK_ALIGNMENT
81#include "dlmalloc_src.cxx"
83#define call_malloc dlmalloc
84#define call_realloc dlrealloc
85#define call_free dlfree
86#define MEMORY_HOOK_MALLOC_LOCK 1
88#elif defined(USE_MEMORY_PTMALLOC2)
100#define USE_DL_PREFIX 1
103 #define MALLOC_DEBUG 2
105#include "ptmalloc2_smp_src.cxx"
107#define call_malloc dlmalloc
108#define call_realloc dlrealloc
109#define call_free dlfree
110#undef MEMORY_HOOK_MALLOC_LOCK
117#define call_malloc malloc
118#define call_realloc realloc
119#define call_free free
120#undef MEMORY_HOOK_MALLOC_LOCK
129inflate_size(
size_t size) {
130#if defined(MEMORY_HOOK_DO_ALIGN)
133 return size +
sizeof(uintptr_t) * 2 + MEMORY_HOOK_ALIGNMENT - 1;
134#elif defined(USE_MEMORY_DLMALLOC) || defined(USE_MEMORY_PTMALLOC2)
138#elif defined(DO_MEMORY_USAGE)
143 return size + MEMORY_HOOK_ALIGNMENT;
156alloc_to_ptr(
void *alloc,
size_t size) {
157#if defined(MEMORY_HOOK_DO_ALIGN)
159 uintptr_t *root = (uintptr_t *)((
char *)alloc +
sizeof(uintptr_t) * 2);
161 root = (uintptr_t *)(((uintptr_t)root + MEMORY_HOOK_ALIGNMENT - 1) & ~(MEMORY_HOOK_ALIGNMENT - 1));
163 root[-1] = (uintptr_t)alloc;
165#elif defined(USE_MEMORY_DLMALLOC) || defined(USE_MEMORY_PTMALLOC2)
167#elif defined(DO_MEMORY_USAGE)
168 size_t *root = (
size_t *)alloc;
170 return (
void *)((
char *)root + MEMORY_HOOK_ALIGNMENT);
182ptr_to_alloc(
void *ptr,
size_t &size) {
183#if defined(MEMORY_HOOK_DO_ALIGN)
184 uintptr_t *root = (uintptr_t *)ptr;
186 return (
void *)root[-1];
187#elif defined(USE_MEMORY_DLMALLOC) || defined(USE_MEMORY_PTMALLOC2)
188#ifdef DO_MEMORY_USAGE
192#elif defined(DO_MEMORY_USAGE)
193 size_t *root = (
size_t *)((
char *)ptr - MEMORY_HOOK_ALIGNMENT);
210 GetSystemInfo(&sysinfo);
212 _page_size = (size_t)sysinfo.dwPageSize;
217 _page_size = sysconf(_SC_PAGESIZE);
221 _total_heap_single_size = 0;
222 _total_heap_array_size = 0;
223 _requested_heap_size = 0;
224 _total_mmap_size = 0;
225 _max_heap_size = ~(size_t)0;
233 _total_heap_single_size(copy._total_heap_single_size),
234 _total_heap_array_size(copy._total_heap_array_size),
235 _requested_heap_size(copy._requested_heap_size),
236 _total_mmap_size(copy._total_mmap_size),
237 _max_heap_size(copy._max_heap_size),
238 _page_size(copy._page_size) {
241 _deleted_chains = copy._deleted_chains;
263 size_t inflated_size = inflate_size(size);
265#ifdef MEMORY_HOOK_MALLOC_LOCK
267 void *alloc = call_malloc(inflated_size);
270 void *alloc = call_malloc(inflated_size);
273 while (alloc ==
nullptr) {
275#ifdef MEMORY_HOOK_MALLOC_LOCK
277 alloc = call_malloc(inflated_size);
280 alloc = call_malloc(inflated_size);
284#ifdef DO_MEMORY_USAGE
287#if defined(USE_MEMORY_DLMALLOC) || defined(USE_MEMORY_PTMALLOC2)
290 inflated_size = size;
296 overflow_heap_size();
300 void *ptr = alloc_to_ptr(alloc, size);
302 assert(((uintptr_t)ptr % MEMORY_HOOK_ALIGNMENT) == 0);
303 assert(ptr >= alloc && (
char *)ptr + size <= (
char *)alloc + inflated_size);
314 void *alloc = ptr_to_alloc(ptr, size);
316#ifdef DO_MEMORY_USAGE
317 assert((
int)size <= _total_heap_single_size);
321#ifdef MEMORY_HOOK_MALLOC_LOCK
340 size_t inflated_size = inflate_size(size);
342#ifdef MEMORY_HOOK_MALLOC_LOCK
344 void *alloc = call_malloc(inflated_size);
347 void *alloc = call_malloc(inflated_size);
350 while (alloc ==
nullptr) {
352#ifdef MEMORY_HOOK_MALLOC_LOCK
354 alloc = call_malloc(inflated_size);
357 alloc = call_malloc(inflated_size);
361#ifdef DO_MEMORY_USAGE
364#if defined(USE_MEMORY_DLMALLOC) || defined(USE_MEMORY_PTMALLOC2)
367 inflated_size = size;
373 overflow_heap_size();
377 void *ptr = alloc_to_ptr(alloc, size);
379 assert(((uintptr_t)ptr % MEMORY_HOOK_ALIGNMENT) == 0);
380 assert(ptr >= alloc && (
char *)ptr + size <= (
char *)alloc + inflated_size);
391 void *alloc = ptr_to_alloc(ptr, orig_size);
393 size_t inflated_size = inflate_size(size);
395 void *alloc1 = alloc;
396#ifdef MEMORY_HOOK_MALLOC_LOCK
398 alloc1 = call_realloc(alloc1, inflated_size);
401 alloc1 = call_realloc(alloc1, inflated_size);
404 while (alloc1 ==
nullptr) {
410#ifdef MEMORY_HOOK_MALLOC_LOCK
412 alloc1 = call_realloc(alloc1, inflated_size);
415 alloc1 = call_realloc(alloc1, inflated_size);
419#ifdef DO_MEMORY_USAGE
420#if defined(USE_MEMORY_DLMALLOC) || defined(USE_MEMORY_PTMALLOC2)
423 inflated_size = size;
425 assert((AtomicAdjust::Integer)orig_size <= _total_heap_array_size);
426 AtomicAdjust::add(_total_heap_array_size, (AtomicAdjust::Integer)size-(AtomicAdjust::Integer)orig_size);
430#ifdef MEMORY_HOOK_DO_ALIGN
433 uintptr_t *root = (uintptr_t *)((
char *)alloc1 +
sizeof(uintptr_t) * 2);
434 root = (uintptr_t *)(((uintptr_t)root + MEMORY_HOOK_ALIGNMENT - 1) & ~(MEMORY_HOOK_ALIGNMENT - 1));
435 void *ptr1 = (
void *)root;
437 size_t orig_delta = (
char *)ptr - (
char *)alloc;
438 size_t new_delta = (
char *)ptr1 - (
char *)alloc1;
439 if (orig_delta != new_delta) {
440 memmove((
char *)alloc1 + new_delta, (
char *)alloc1 + orig_delta, std::min(size, orig_size));
444 root[-1] = (uintptr_t)alloc1;
446 void *ptr1 = alloc_to_ptr(alloc1, size);
450 assert(ptr1 >= alloc1 && (
char *)ptr1 + size <= (
char *)alloc1 + inflated_size);
451 assert(((uintptr_t)ptr1 % MEMORY_HOOK_ALIGNMENT) == 0);
462 void *alloc = ptr_to_alloc(ptr, size);
464#ifdef DO_MEMORY_USAGE
465 assert((
int)size <= _total_heap_array_size);
469#ifdef MEMORY_HOOK_MALLOC_LOCK
489 bool trimmed =
false;
491#if defined(USE_MEMORY_DLMALLOC) || defined(USE_MEMORY_PTMALLOC2)
496 if (dlmalloc_trim(pad)) {
504 if (_heapmin() == 0) {
525 assert((size % _page_size) == 0);
527#ifdef DO_MEMORY_USAGE
528 _total_mmap_size += size;
534 void *ptr = VirtualAlloc(
nullptr, size, MEM_COMMIT | MEM_RESERVE,
535 allow_exec ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE);
536 if (ptr ==
nullptr) {
537 DWORD err = GetLastError();
538 cerr <<
"Couldn't allocate memory page of size " << size <<
": ";
542 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
543 nullptr, err, 0, (LPTSTR)&buffer, 0,
nullptr);
545 cerr << (
char *)buffer <<
"\n";
547 cerr <<
"Error code " << err <<
"\n";
558 int prot = PROT_READ | PROT_WRITE;
562 void *ptr = mmap(
nullptr, size, prot, MAP_PRIVATE | MAP_ANON, -1, 0);
563 if (ptr == (
void *)-1) {
579 assert((size % _page_size) == 0);
581#ifdef DO_MEMORY_USAGE
582 assert((
int)size <= _total_mmap_size);
583 _total_mmap_size -= size;
587 VirtualFree(ptr, 0, MEM_RELEASE);
614 DeletedChains::iterator dci = _deleted_chains.find(buffer_size);
615 if (dci != _deleted_chains.end()) {
616 chain = (*dci).second;
620 _deleted_chains.insert(DeletedChains::value_type(buffer_size, chain));
641 cerr <<
"Out of memory allocating " << attempted_size <<
" bytes\n";
654overflow_heap_size() {
655#ifdef DO_MEMORY_USAGE
656 _max_heap_size = ~(size_t)0;
static Integer add(Integer &var, Integer delta)
Atomically computes var += delta.
static Integer get(const Integer &var)
Atomically retrieves the snapshot value of the indicated variable.
This template class can be used to provide faster allocation/deallocation for many Panda objects.
This class provides a wrapper around the various possible malloc schemes Panda might employ.
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...
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.
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...
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.
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.