lean2/src/util/memory_pool.h
Leonardo de Moura 516c0c73b9 refactor(*): remove dependency to thread_local C++11 keyword, the
current compilers have several bugs associated with it

We use the simpler __thread (gcc and clang) and
__declspec(thread) (visual studio).
2014-09-24 12:51:04 -07:00

34 lines
1 KiB
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 */
class memory_pool {
unsigned m_size;
void * m_free_list;
public:
memory_pool(unsigned size):m_size(size), m_free_list(nullptr) {}
~memory_pool();
void * allocate();
void recycle(void * ptr) {
*(reinterpret_cast<void**>(ptr)) = m_free_list;
m_free_list = ptr;
}
};
memory_pool * allocate_thread_memory_pool(unsigned sz);
#define DEF_THREAD_MEMORY_POOL(NAME, SZ) \
LEAN_THREAD_PTR(memory_pool, NAME ## _tlocal); \
memory_pool & NAME() { \
if (!NAME ## _tlocal) \
NAME ## _tlocal = allocate_thread_memory_pool(SZ); \
return *(NAME ## _tlocal); \
}
}