Panda3D

memoryHook.h

00001 // Filename: memoryHook.h
00002 // Created by:  drose (28Jun07)
00003 //
00004 ////////////////////////////////////////////////////////////////////
00005 //
00006 // PANDA 3D SOFTWARE
00007 // Copyright (c) Carnegie Mellon University.  All rights reserved.
00008 //
00009 // All use of this software is subject to the terms of the revised BSD
00010 // license.  You should have received a copy of this license along
00011 // with this source code in a file named "LICENSE."
00012 //
00013 ////////////////////////////////////////////////////////////////////
00014 
00015 #ifndef MEMORYHOOK_H
00016 #define MEMORYHOOK_H
00017 
00018 #include "dtoolbase.h"
00019 #include "numeric_types.h"
00020 #include "atomicAdjust.h"
00021 #include "mutexImpl.h"
00022 #include <map>
00023 
00024 class DeletedBufferChain;
00025 
00026 ////////////////////////////////////////////////////////////////////
00027 //       Class : MemoryHook
00028 // Description : This class provides a wrapper around the various
00029 //               possible malloc schemes Panda might employ.  It also
00030 //               exists to allow the MemoryUsage class in Panda to
00031 //               insert callback hooks to track the size of allocated
00032 //               pointers.
00033 //
00034 //               The PANDA_MALLOC_* and PANDA_FREE_* macros are
00035 //               defined to vector through through this class (except
00036 //               in production builds) to facilitate that.  Every
00037 //               memory allocation call in Panda should therefore use
00038 //               these macros instead of direct calls to malloc or
00039 //               free.  (C++ new and delete operators may be employed
00040 //               for classes which inherit from MemoryBase; otherwise,
00041 //               use the PANDA_MALLOC macros.)
00042 ////////////////////////////////////////////////////////////////////
00043 class EXPCL_DTOOL MemoryHook {
00044 public:
00045   MemoryHook();
00046   MemoryHook(const MemoryHook &copy);
00047   virtual ~MemoryHook();
00048 
00049   virtual void *heap_alloc_single(size_t size);
00050   virtual void heap_free_single(void *ptr);
00051 
00052   virtual void *heap_alloc_array(size_t size);
00053   virtual void *heap_realloc_array(void *ptr, size_t size);
00054   virtual void heap_free_array(void *ptr);
00055 
00056   INLINE void inc_heap(size_t size);
00057   INLINE void dec_heap(size_t size);
00058 
00059   bool heap_trim(size_t pad);
00060 
00061   virtual void *mmap_alloc(size_t size, bool allow_exec);
00062   virtual void mmap_free(void *ptr, size_t size);
00063   INLINE size_t get_page_size() const;
00064   INLINE size_t round_up_to_page_size(size_t size) const;
00065 
00066   virtual void mark_pointer(void *ptr, size_t orig_size, ReferenceCount *ref_ptr);
00067 
00068   DeletedBufferChain *get_deleted_chain(size_t buffer_size);
00069 
00070   virtual void alloc_fail();
00071 
00072 private:
00073   INLINE static size_t inflate_size(size_t size);
00074   INLINE static void *alloc_to_ptr(void *alloc, size_t size);
00075   INLINE static void *ptr_to_alloc(void *ptr, size_t &size);
00076 
00077 #ifdef DO_MEMORY_USAGE
00078 protected:
00079   TVOLATILE AtomicAdjust::Integer _total_heap_single_size;
00080   TVOLATILE AtomicAdjust::Integer _total_heap_array_size;
00081   TVOLATILE AtomicAdjust::Integer _requested_heap_size;
00082   TVOLATILE AtomicAdjust::Integer _total_mmap_size;
00083 
00084   // If the allocated heap size crosses this threshold, we call
00085   // overflow_heap_size().
00086   size_t _max_heap_size;
00087 
00088   virtual void overflow_heap_size();
00089 #endif  // DO_MEMORY_USAGE
00090 
00091 private:
00092   size_t _page_size;
00093 
00094   typedef map<size_t, DeletedBufferChain *> DeletedChains;
00095   DeletedChains _deleted_chains;
00096 
00097   MutexImpl _lock;
00098 };
00099 
00100 #include "memoryHook.I"
00101 
00102 #endif
 All Classes Functions Variables Enumerations