1 // Distributed under the MIT license. Copyright (c) 2010, Ivan Vashchaev
5 #include "block_allocator.h"
7 block_allocator::block_allocator(size_t blocksize): m_head(0), m_blocksize(blocksize)
11 block_allocator::~block_allocator()
15 block *temp = m_head->next;
21 void block_allocator::swap(block_allocator &rhs)
23 std::swap(m_blocksize, rhs.m_blocksize);
24 std::swap(m_head, rhs.m_head);
27 void *block_allocator::malloc(size_t size)
29 if ((m_head && m_head->used + size > m_head->size) || !m_head)
31 // calc needed size for allocation
32 size_t alloc_size = std::max(sizeof(block) + size, m_blocksize);
35 char *buffer = (char *)::malloc(alloc_size);
36 block *b = reinterpret_cast<block *>(buffer);
38 b->used = sizeof(block);
44 void *ptr = m_head->buffer + m_head->used;
49 void block_allocator::free()
51 block_allocator(0).swap(*this);