Panda3D
neverFreeMemory.I
1 // Filename: neverFreeMemory.I
2 // Created by: drose (14Jun07)
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 
16 ////////////////////////////////////////////////////////////////////
17 // Function: NeverFreeMemory::alloc
18 // Access: Public, Static
19 // Description: Returns a pointer to a newly-allocated block of
20 // memory of the indicated size.
21 ////////////////////////////////////////////////////////////////////
22 INLINE void *NeverFreeMemory::
23 alloc(size_t size) {
24  return get_global_ptr()->ns_alloc(size);
25 }
26 
27 ////////////////////////////////////////////////////////////////////
28 // Function: NeverFreeMemory::get_total_alloc
29 // Access: Published, Static
30 // Description: Returns the total number of bytes consumed by all the
31 // pages allocated internally by this object.
32 ////////////////////////////////////////////////////////////////////
33 INLINE size_t NeverFreeMemory::
35  return get_global_ptr()->_total_alloc;
36 }
37 
38 ////////////////////////////////////////////////////////////////////
39 // Function: NeverFreeMemory::get_total_used
40 // Access: Published, Static
41 // Description: Returns the total number of bytes requested by the
42 // application in calls to NeverFreeMemory::alloc().
43 ////////////////////////////////////////////////////////////////////
44 INLINE size_t NeverFreeMemory::
46  return get_global_ptr()->_total_used;
47 }
48 
49 ////////////////////////////////////////////////////////////////////
50 // Function: NeverFreeMemory::get_total_unused
51 // Access: Published, Static
52 // Description: Returns the difference between get_total_alloc() and
53 // get_total_used(). This represents bytes in allocated
54 // pages that have not (yet) been used by the
55 // application.
56 ////////////////////////////////////////////////////////////////////
57 INLINE size_t NeverFreeMemory::
59  NeverFreeMemory *global_ptr = get_global_ptr();
60  global_ptr->_lock.acquire();
61  size_t total_unused = global_ptr->_total_alloc - global_ptr->_total_used;
62  global_ptr->_lock.release();
63  return total_unused;
64 }
65 
66 ////////////////////////////////////////////////////////////////////
67 // Function: NeverFreeMemory::get_global_ptr
68 // Access: Private, Static
69 // Description:
70 ////////////////////////////////////////////////////////////////////
71 INLINE NeverFreeMemory *NeverFreeMemory::
72 get_global_ptr() {
73  if (_global_ptr == (NeverFreeMemory *)NULL) {
74  make_global_ptr();
75  }
76  return _global_ptr;
77 }
78 
79 ////////////////////////////////////////////////////////////////////
80 // Function: NeverFreeMemory::Page::Constructor
81 // Access: Public
82 // Description:
83 ////////////////////////////////////////////////////////////////////
84 INLINE NeverFreeMemory::Page::
85 Page(void *start, size_t size) :
86  _next((unsigned char *)start),
87  _remaining(size)
88 {
89 }
90 
91 ////////////////////////////////////////////////////////////////////
92 // Function: NeverFreeMemory::Page::operator <
93 // Access: Public
94 // Description:
95 ////////////////////////////////////////////////////////////////////
96 INLINE bool NeverFreeMemory::Page::
97 operator < (const NeverFreeMemory::Page &other) const {
98  return _remaining < other._remaining;
99 }
100 
101 ////////////////////////////////////////////////////////////////////
102 // Function: NeverFreeMemory::Page::alloc
103 // Access: Public
104 // Description:
105 ////////////////////////////////////////////////////////////////////
106 INLINE void *NeverFreeMemory::Page::
107 alloc(size_t size) {
108  assert(size <= _remaining);
109  void *result = _next;
110  _next += size;
111  _remaining -= size;
112  return result;
113 }
This class is used to allocate bytes of memory from a pool that is never intended to be freed...
static size_t get_total_unused()
Returns the difference between get_total_alloc() and get_total_used().
static size_t get_total_alloc()
Returns the total number of bytes consumed by all the pages allocated internally by this object...
static void * alloc(size_t size)
Returns a pointer to a newly-allocated block of memory of the indicated size.
static size_t get_total_used()
Returns the total number of bytes requested by the application in calls to NeverFreeMemory::alloc().