Panda3D
Loading...
Searching...
No Matches
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 */
21alloc(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 */
29INLINE 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 */
38INLINE 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 */
48INLINE 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 */
60INLINE NeverFreeMemory *NeverFreeMemory::
61get_global_ptr() {
62 if (_global_ptr == nullptr) {
63 make_global_ptr();
64 }
65 return _global_ptr;
66}
67
68/**
69 *
70 */
71INLINE NeverFreeMemory::Page::
72Page(void *start, size_t size) :
73 _next((unsigned char *)start),
74 _remaining(size)
75{
76}
77
78/**
79 *
80 */
81INLINE bool NeverFreeMemory::Page::
82operator < (const NeverFreeMemory::Page &other) const {
83 if (_remaining != other._remaining) {
84 return _remaining < other._remaining;
85 } else {
86 return _next < other._next;
87 }
88}
89
90/**
91 *
92 */
93INLINE void *NeverFreeMemory::Page::
94alloc(size_t size) {
95 assert(size <= _remaining);
96 void *result = _next;
97 _next += size;
98 _remaining -= size;
99 return result;
100}
This class is used to allocate bytes of memory from a pool that is never intended to be freed.
static size_t get_total_used()
Returns the total number of bytes requested by the application in calls to NeverFreeMemory::alloc().
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_unused()
Returns the difference between get_total_alloc() and get_total_used().