Panda3D
deletedBufferChain.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 deletedBufferChain.h
10  * @author drose
11  * @date 2007-07-20
12  */
13 
14 #ifndef DELETEDBUFFERCHAIN_H
15 #define DELETEDBUFFERCHAIN_H
16 
17 #include "dtoolbase.h"
18 #include "neverFreeMemory.h"
19 #include "mutexImpl.h"
20 #include "atomicAdjust.h"
21 #include "numeric_types.h"
22 #include "typeHandle.h"
23 #include <assert.h>
24 
25 // Though it's tempting, it doesn't seem to be possible to implement
26 // DeletedBufferChain via the atomic exchange operation. Specifically, a
27 // pointer may be removed from the head of the chain, then the same pointer
28 // reinserted in the chain, while another thread is waiting; and that thread
29 // will not detect the change. So instead, we always use a mutex.
30 
31 #ifndef NDEBUG
32 // In development mode, we define USE_DELETEDCHAINFLAG, which triggers the
33 // piggyback of an additional word of data on every allocated block, so we can
34 // ensure that an object is not double-deleted and that the deleted chain
35 // remains intact.
36 #define USE_DELETEDCHAINFLAG 1
37 #endif // NDEBUG
38 
39 #ifdef USE_DELETEDCHAINFLAG
40 enum DeletedChainFlag {
41  DCF_deleted = 0xfeedba0f,
42  DCF_alive = 0x12487654,
43 };
44 #endif
45 
46 /**
47  * This template class can be used to provide faster allocation/deallocation
48  * for many Panda objects. It works by maintaining a linked list of deleted
49  * buffers that are all of the same size; when a new object is allocated that
50  * matches that size, the same space is just reused.
51  *
52  * This class manages untyped buffers of a fixed size. It can be used
53  * directly; or it also serves as a backbone for DeletedChain, which is a
54  * template class that manages object allocations.
55  *
56  * Use MemoryHook to get a new DeletedBufferChain of a particular size.
57  */
58 class EXPCL_DTOOL_DTOOLBASE DeletedBufferChain {
59 protected:
60  DeletedBufferChain(size_t buffer_size);
61 
62 public:
63  void *allocate(size_t size, TypeHandle type_handle);
64  void deallocate(void *ptr, TypeHandle type_handle);
65 
66  INLINE bool validate(void *ptr);
67  INLINE size_t get_buffer_size() const;
68 
69 private:
70  class ObjectNode {
71  public:
72 #ifdef USE_DELETEDCHAINFLAG
73  // In development mode, we piggyback this extra data. This is maintained
74  // out-of-band from the actual pointer returned, so we can safely use this
75  // flag to indicate the difference between allocated and freed pointers.
76  TVOLATILE AtomicAdjust::Integer _flag;
77 #endif
78 
79  // This pointer sits within the buffer, in the same space referenced by
80  // the actual pointer returned (unlike _flag, above). It's only used when
81  // the buffer is deleted, so there's no harm in sharing space with the
82  // undeleted buffer.
83  ObjectNode *_next;
84  };
85 
86  static INLINE void *node_to_buffer(ObjectNode *node);
87  static INLINE ObjectNode *buffer_to_node(void *buffer);
88 
89  ObjectNode *_deleted_chain;
90 
91  MutexImpl _lock;
92  size_t _buffer_size;
93 
94 #ifndef USE_DELETEDCHAINFLAG
95  // Without DELETEDCHAINFLAG, we don't even store the _flag member at all.
96  static const size_t flag_reserved_bytes = 0;
97 
98 #else
99  // Otherwise, we need space for the integer.
100  static const size_t flag_reserved_bytes = sizeof(AtomicAdjust::Integer);
101 #endif // USE_DELETEDCHAINFLAG
102 
103  friend class MemoryHook;
104 };
105 
106 #include "deletedBufferChain.I"
107 
108 #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.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
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...
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:81
PANDA 3D SOFTWARE Copyright (c) Carnegie Mellon University.
This template class can be used to provide faster allocation/deallocation for many Panda objects.