1 // Distributed under the MIT license. Copyright (c) 2010, Ivan Vashchaev
7 #include "block_allocator.h"
12 block_allocator::block_allocator(size_t blocksize): m_head(0), m_blocksize(blocksize)
16 block_allocator::~block_allocator()
20 block *temp = m_head->next;
26 void block_allocator::swap(block_allocator &rhs)
28 std::swap(m_blocksize, rhs.m_blocksize);
29 std::swap(m_head, rhs.m_head);
32 void *block_allocator::malloc(size_t size)
34 if ((m_head && m_head->used + size > m_head->size) || !m_head)
36 // calc needed size for allocation
37 size_t alloc_size = std::max(sizeof(block) + size, m_blocksize);
40 char *buffer = (char *)::malloc(alloc_size);
41 block *b = reinterpret_cast<block *>(buffer);
43 b->used = sizeof(block);
49 void *ptr = m_head->buffer + m_head->used;
54 void block_allocator::free()
56 block_allocator(0).swap(*this);