Panda3D
neverFreeMemory.I
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 neverFreeMemory.I
10  * @author drose
11  * @date 2007-06-14
12  */
13 
14 /**
15  * Returns a pointer to a newly-allocated block of memory of the indicated
16  * size.
17  *
18  * Please note that the resulting pointer is not aligned to any boundary.
19  */
20 INLINE void *NeverFreeMemory::
21 alloc(size_t size) {
22  return get_global_ptr()->ns_alloc(size);
23 }
24 
25 /**
26  * Returns the total number of bytes consumed by all the pages allocated
27  * internally by this object.
28  */
29 INLINE size_t NeverFreeMemory::
31  return get_global_ptr()->_total_alloc;
32 }
33 
34 /**
35  * Returns the total number of bytes requested by the application in calls to
36  * NeverFreeMemory::alloc().
37  */
38 INLINE size_t NeverFreeMemory::
40  return get_global_ptr()->_total_used;
41 }
42 
43 /**
44  * Returns the difference between get_total_alloc() and get_total_used().
45  * This represents bytes in allocated pages that have not (yet) been used by
46  * the application.
47  */
48 INLINE size_t NeverFreeMemory::
50  NeverFreeMemory *global_ptr = get_global_ptr();
51  global_ptr->_lock.lock();
52  size_t total_unused = global_ptr->_total_alloc - global_ptr->_total_used;
53  global_ptr->_lock.unlock();
54  return total_unused;
55 }
56 
57 /**
58  *
59  */
60 INLINE NeverFreeMemory *NeverFreeMemory::
61 get_global_ptr() {
62  if (_global_ptr == nullptr) {
63  make_global_ptr();
64  }
65  return _global_ptr;
66 }
67 
68 /**
69  *
70  */
71 INLINE NeverFreeMemory::Page::
72 Page(void *start, size_t size) :
73  _next((unsigned char *)start),
74  _remaining(size)
75 {
76 }
77 
78 /**
79  *
80  */
81 INLINE bool NeverFreeMemory::Page::
82 operator < (const NeverFreeMemory::Page &other) const {
83  return _remaining < other._remaining;
84 }
85 
86 /**
87  *
88  */
89 INLINE void *NeverFreeMemory::Page::
90 alloc(size_t size) {
91  assert(size <= _remaining);
92  void *result = _next;
93  _next += size;
94  _remaining -= size;
95  return result;
96 }
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().