17 INLINE SimpleAllocator::
18 SimpleAllocator(
size_t max_size,
Mutex &lock) :
22 _contiguous(max_size),
35 alloc(
size_t size,
size_t alignment) {
37 return do_alloc(size, alignment);
107 INLINE
bool SimpleAllocator::
108 do_is_empty()
const {
109 return (_next ==
this);
118 INLINE
void SimpleAllocator::
132 if (space > _contiguous) {
134 changed_contiguous();
142 INLINE SimpleAllocatorBlock::
144 size_t start,
size_t size) :
154 INLINE SimpleAllocatorBlock::
156 _allocator(from._allocator)
158 if (_allocator ==
nullptr) {
163 _start = from._start;
165 LinkedListNode::operator = (std::move(from));
166 from._allocator =
nullptr;
184 _allocator = from._allocator;
185 if (_allocator ==
nullptr) {
192 _start = from._start;
194 LinkedListNode::operator = (std::move(from));
195 from._allocator =
nullptr;
204 if (_allocator !=
nullptr) {
225 nassertr(_allocator !=
nullptr, 0);
235 nassertr(_allocator !=
nullptr, 0);
244 return (_allocator !=
nullptr);
253 nassertr(_allocator !=
nullptr, 0);
255 return do_get_max_size();
264 nassertr(_allocator !=
nullptr,
false);
266 return do_realloc(size);
275 nassertr(_allocator !=
nullptr,
nullptr);
285 INLINE
void SimpleAllocatorBlock::
287 nassertv(_allocator !=
nullptr);
289 _allocator->_total_size -= _size;
292 _allocator->mark_contiguous(prev);
293 _allocator =
nullptr;
302 INLINE
size_t SimpleAllocatorBlock::
303 do_get_max_size()
const {
305 if (_next == _allocator) {
306 end = _allocator->_max_size;
319 INLINE
bool SimpleAllocatorBlock::
320 do_realloc(
size_t size) {
321 if (size > do_get_max_size()) {
325 _allocator->_total_size -= _size;
326 _allocator->_total_size += size;
331 _allocator->mark_contiguous(
this);
~SimpleAllocatorBlock()
The block automatically frees itself when it destructs.
An implementation of a very simple block allocator.
bool is_empty() const
Returns true if there are no blocks allocated on this page, or false if there is at least one.
A standard mutex, or mutual exclusion lock.
A lightweight C++ object whose constructor calls acquire() and whose destructor calls release() on a ...
bool realloc(size_t size)
Changes the size of this block to the specified size.
SimpleAllocatorBlock * alloc(size_t size, size_t alignment=1)
Allocates a new block.
bool is_free() const
Returns true if the block has been freed, false if it is still valid.
This just stores the pointers to implement a doubly-linked list of some kind of object.
A single block as returned from SimpleAllocator::alloc().
size_t get_contiguous() const
Returns an upper-bound estimate of the size of the largest contiguous block that may be allocated.
size_t get_max_size() const
Returns the available space for allocated objects.
SimpleAllocatorBlock * get_next_block() const
Returns a pointer to the next allocated block in the chain, or NULL if there are no more allocated bl...
SimpleAllocatorBlock * get_first_block() const
Returns a pointer to the first allocated block, or NULL if there are no allocated blocks.
void free()
Releases the allocated space.
size_t get_total_size() const
Returns the total size of allocated objects.
size_t get_start() const
Returns the starting point of this block.
SimpleAllocator * get_allocator() const
Returns the SimpleAllocator object that owns this block.
size_t get_max_size() const
Returns the maximum size this block can be reallocated to, as limited by the following block.
void set_max_size(size_t max_size)
Changes the available space for allocated objects.
size_t get_size() const
Returns the size of this block.