From ab34a7c656a52e671b4919a5cb4ec044d3826384 Mon Sep 17 00:00:00 2001 From: David Rose Date: Fri, 29 Jun 2007 18:37:18 +0000 Subject: [PATCH] windows niceties --- dtool/src/dtoolbase/memoryHook.cxx | 20 ++++++++--- panda/src/express/memoryUsage.I | 16 ++++++--- panda/src/express/memoryUsage.cxx | 56 ++++++++++++------------------ 3 files changed, 51 insertions(+), 41 deletions(-) diff --git a/dtool/src/dtoolbase/memoryHook.cxx b/dtool/src/dtoolbase/memoryHook.cxx index 887862dab5..11ad35365b 100644 --- a/dtool/src/dtoolbase/memoryHook.cxx +++ b/dtool/src/dtoolbase/memoryHook.cxx @@ -295,13 +295,25 @@ heap_free_array(void *ptr) { //////////////////////////////////////////////////////////////////// bool MemoryHook:: heap_trim(size_t pad) { + bool trimmed = false; + #if defined(USE_MEMORY_DLMALLOC) || (defined(USE_MEMORY_PTMALLOC2) && !defined(linux)) - return (dlmalloc_trim(pad) != 0); -#else // Since malloc_trim() isn't standard C, we can't be sure it exists - // on a given platform. - return 0; + // on a given platform. But if we're using ALTERNATIVE_MALLOC, we + // know we have dlmalloc_trim. + if (dlmalloc_trim(pad)) { + trimmed = true; + } #endif + +#ifdef WIN32 + // Also, on Windows we have _heapmin(). + if (_heapmin() == 0) { + trimmed = true; + } +#endif + + return trimmed; } //////////////////////////////////////////////////////////////////// diff --git a/panda/src/express/memoryUsage.I b/panda/src/express/memoryUsage.I index 5a3c2db87f..3a81abc457 100644 --- a/panda/src/express/memoryUsage.I +++ b/panda/src/express/memoryUsage.I @@ -97,8 +97,8 @@ is_tracking() { // Function: MemoryUsage::is_counting // Access: Public, Static // Description: Returns true if the MemoryUsage object is currently -// at least counting memory (e.g. count-memory-usage is -// configured #t), even if it's not fully tracking it. +// at least counting memory (e.g. this is a Windows +// debug build), even if it's not fully tracking it. //////////////////////////////////////////////////////////////////// INLINE bool MemoryUsage:: is_counting() { @@ -213,10 +213,18 @@ INLINE size_t MemoryUsage:: get_external_size() { MemoryUsage *mu = get_global_ptr(); if (mu->_count_memory_usage) { + // We can only possibly know this with memory counting, which + // tracks every malloc call. + #if defined(USE_MEMORY_DLMALLOC) || defined(USE_MEMORY_PTMALLOC2) - return mu->_total_size - mu->_total_heap_single_size - mu->_total_heap_array_size - mu->_interpreter_size; -#else + // With alternative malloc, none of the Panda allocated memory + // shows up in total_size, so anything there is either interpreter + // or external. return mu->_total_size - mu->_interpreter_size; +#else + // Without alternative malloc, the Panda allocated memory is also + // included in total_size, so we have to subtract it out. + return mu->_total_size - mu->_total_heap_single_size - mu->_total_heap_array_size - mu->_interpreter_size; #endif } else { return 0; diff --git a/panda/src/express/memoryUsage.cxx b/panda/src/express/memoryUsage.cxx index 6d3a9287cc..4a2053b8ca 100644 --- a/panda/src/express/memoryUsage.cxx +++ b/panda/src/express/memoryUsage.cxx @@ -336,30 +336,28 @@ win32_malloc_hook(int alloc_type, void *ptr, size_t size, int block_use, long request, const unsigned char *filename, int line) { MemoryUsage *mu = get_global_ptr(); - if (mu->_count_memory_usage) { - int increment = 0; - switch (alloc_type) { - case _HOOK_ALLOC: - increment = size; - break; - - case _HOOK_REALLOC: - increment = size - _msize(ptr); - break; - - case _HOOK_FREE: - increment = - ((int)_msize(ptr)); - break; - } + int increment = 0; + switch (alloc_type) { + case _HOOK_ALLOC: + increment = size; + break; - mu->_total_size += increment; + case _HOOK_REALLOC: + increment = size - _msize(ptr); + break; + + case _HOOK_FREE: + increment = - ((int)_msize(ptr)); + break; + } + + mu->_total_size += increment; #ifdef TRACK_IN_INTERPRETER - if (in_interpreter) { - mu->_interpreter_size += increment; - } -#endif + if (in_interpreter) { + mu->_interpreter_size += increment; } +#endif return true; } @@ -384,26 +382,18 @@ MemoryUsage(const MemoryHook ©) : MemoryHook(copy) { PRC_DESC("Set this to true to enable full-force tracking of C++ allocations " "and recordkeeping by type. It's quite expensive.")); -#if defined(WIN32_VC) && defined(_DEBUG) - _count_memory_usage = ConfigVariableBool - ("count-memory-usage", _track_memory_usage, - PRC_DESC("This is a much lighter-weight version of track-memory-usage, and it " - "only tracks the total memory allocation. However, it only exists in " - "certain build environments (in particular, only in an Opt1 or " - "Opt2 build on Windows.")); - -#else _count_memory_usage = false; -#endif #ifdef USE_MEMORY_NOWRAPPERS #error Cannot compile MemoryUsage without malloc wrappers! #endif #if defined(WIN32_VC) && defined(_DEBUG) - if (_count_memory_usage) { - _CrtSetAllocHook(&win32_malloc_hook); - } + // On a debug Windows build, we can set this malloc hook which + // allows tracking every malloc call, even from subordinate + // libraries. + _CrtSetAllocHook(&win32_malloc_hook); + _count_memory_usage = true; #endif _info_set_dirty = false;