lean2/src/util/memory_pool.h
Leonardo de Moura c8b6f0c7fb refactor(util): rename fixed_size_allocator to memory_pool
Signed-off-by: Leonardo de Moura <leonardo@microsoft.com>
2014-07-22 07:49:40 -07:00

40 lines
981 B
C++

/*
Copyright (c) 2014 Microsoft Corporation. All rights reserved.
Released under Apache 2.0 license as described in the file LICENSE.
Author: Leonardo de Moura
*/
#pragma once
#include "util/memory.h"
namespace lean {
/** \brief Auxiliary object for "recycling" allocated memory of fixed size */
template<unsigned Size>
class memory_pool {
void * m_free_list;
public:
memory_pool():m_free_list(nullptr) {}
~memory_pool() {
while (m_free_list != nullptr) {
void * r = m_free_list;
m_free_list = *(reinterpret_cast<void **>(r));
free(r);
}
}
void * allocate() {
if (m_free_list != nullptr) {
void * r = m_free_list;
m_free_list = *(reinterpret_cast<void **>(r));
return r;
} else {
return malloc(Size);
}
}
void recyle(void * ptr) {
*(reinterpret_cast<void**>(ptr)) = m_free_list;
m_free_list = ptr;
}
};
}