Panda3D
 All Classes Functions Variables Enumerations
memoryHook.h
1 // Filename: memoryHook.h
2 // Created by: drose (28Jun07)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #ifndef MEMORYHOOK_H
16 #define MEMORYHOOK_H
17 
18 #include "dtoolbase.h"
19 #include "numeric_types.h"
20 #include "atomicAdjust.h"
21 #include "mutexImpl.h"
22 #include <map>
23 
24 class DeletedBufferChain;
25 
26 ////////////////////////////////////////////////////////////////////
27 // Class : MemoryHook
28 // Description : This class provides a wrapper around the various
29 // possible malloc schemes Panda might employ. It also
30 // exists to allow the MemoryUsage class in Panda to
31 // insert callback hooks to track the size of allocated
32 // pointers.
33 //
34 // The PANDA_MALLOC_* and PANDA_FREE_* macros are
35 // defined to vector through through this class (except
36 // in production builds) to facilitate that. Every
37 // memory allocation call in Panda should therefore use
38 // these macros instead of direct calls to malloc or
39 // free. (C++ new and delete operators may be employed
40 // for classes which inherit from MemoryBase; otherwise,
41 // use the PANDA_MALLOC macros.)
42 ////////////////////////////////////////////////////////////////////
43 class EXPCL_DTOOL MemoryHook {
44 public:
45  MemoryHook();
46  MemoryHook(const MemoryHook &copy);
47  virtual ~MemoryHook();
48 
49  virtual void *heap_alloc_single(size_t size);
50  virtual void heap_free_single(void *ptr);
51 
52  virtual void *heap_alloc_array(size_t size);
53  virtual void *heap_realloc_array(void *ptr, size_t size);
54  virtual void heap_free_array(void *ptr);
55 
56  INLINE void inc_heap(size_t size);
57  INLINE void dec_heap(size_t size);
58 
59  bool heap_trim(size_t pad);
60 
61  INLINE static size_t get_memory_alignment();
62  INLINE static size_t get_header_reserved_bytes();
63 
64  virtual void *mmap_alloc(size_t size, bool allow_exec);
65  virtual void mmap_free(void *ptr, size_t size);
66  INLINE size_t get_page_size() const;
67  INLINE size_t round_up_to_page_size(size_t size) const;
68 
69  virtual void mark_pointer(void *ptr, size_t orig_size, ReferenceCount *ref_ptr);
70 
71  DeletedBufferChain *get_deleted_chain(size_t buffer_size);
72 
73  virtual void alloc_fail(size_t attempted_size);
74 
75 private:
76  INLINE static size_t inflate_size(size_t size);
77  INLINE static void *alloc_to_ptr(void *alloc, size_t size);
78  INLINE static void *ptr_to_alloc(void *ptr, size_t &size);
79 
80 #ifdef DO_MEMORY_USAGE
81 protected:
82  TVOLATILE AtomicAdjust::Integer _total_heap_single_size;
83  TVOLATILE AtomicAdjust::Integer _total_heap_array_size;
84  TVOLATILE AtomicAdjust::Integer _requested_heap_size;
85  TVOLATILE AtomicAdjust::Integer _total_mmap_size;
86 
87  // If the allocated heap size crosses this threshold, we call
88  // overflow_heap_size().
89  size_t _max_heap_size;
90 
91  virtual void overflow_heap_size();
92 #endif // DO_MEMORY_USAGE
93 
94 private:
95  size_t _page_size;
96 
97  typedef map<size_t, DeletedBufferChain *> DeletedChains;
98  DeletedChains _deleted_chains;
99 
100  MutexImpl _lock;
101 };
102 
103 #include "memoryHook.I"
104 
105 #endif
A base class for all things that want to be reference-counted.
This class provides a wrapper around the various possible malloc schemes Panda might employ...
Definition: memoryHook.h:43
A fake mutex implementation for single-threaded applications that don&#39;t need any synchronization cont...
This template class can be used to provide faster allocation/deallocation for many Panda objects...