浏览代码

windows niceties

David Rose 18 年之前
父节点
当前提交
ab34a7c656
共有 3 个文件被更改,包括 51 次插入41 次删除
  1. 16 4
      dtool/src/dtoolbase/memoryHook.cxx
  2. 12 4
      panda/src/express/memoryUsage.I
  3. 23 33
      panda/src/express/memoryUsage.cxx

+ 16 - 4
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;
 }
 
 ////////////////////////////////////////////////////////////////////

+ 12 - 4
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;

+ 23 - 33
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 &copy) : 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;