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