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   INLINE static size_t get_memory_alignment();
00062   INLINE static size_t get_header_reserved_bytes();
00063 
00064   virtual void *mmap_alloc(size_t size, bool allow_exec);
00065   virtual void mmap_free(void *ptr, size_t size);
00066   INLINE size_t get_page_size() const;
00067   INLINE size_t round_up_to_page_size(size_t size) const;
00068 
00069   virtual void mark_pointer(void *ptr, size_t orig_size, ReferenceCount *ref_ptr);
00070 
00071   DeletedBufferChain *get_deleted_chain(size_t buffer_size);
00072 
00073   virtual void alloc_fail(size_t attempted_size);
00074 
00075 private:
00076   INLINE static size_t inflate_size(size_t size);
00077   INLINE static void *alloc_to_ptr(void *alloc, size_t size);
00078   INLINE static void *ptr_to_alloc(void *ptr, size_t &size);
00079 
00080 #ifdef DO_MEMORY_USAGE
00081 protected:
00082   TVOLATILE AtomicAdjust::Integer _total_heap_single_size;
00083   TVOLATILE AtomicAdjust::Integer _total_heap_array_size;
00084   TVOLATILE AtomicAdjust::Integer _requested_heap_size;
00085   TVOLATILE AtomicAdjust::Integer _total_mmap_size;
00086 
00087   // If the allocated heap size crosses this threshold, we call
00088   // overflow_heap_size().
00089   size_t _max_heap_size;
00090 
00091   virtual void overflow_heap_size();
00092 #endif  // DO_MEMORY_USAGE
00093 
00094 private:
00095   size_t _page_size;
00096 
00097   typedef map<size_t, DeletedBufferChain *> DeletedChains;
00098   DeletedChains _deleted_chains;
00099 
00100   MutexImpl _lock;
00101 };
00102 
00103 #include "memoryHook.I"
00104 
00105 #endif
 All Classes Functions Variables Enumerations