2014-07-22 01:58:24 +00:00
|
|
|
/*
|
|
|
|
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 {
|
2014-07-22 14:49:40 +00:00
|
|
|
/** \brief Auxiliary object for "recycling" allocated memory of fixed size */
|
|
|
|
class memory_pool {
|
2014-09-24 17:48:32 +00:00
|
|
|
unsigned m_size;
|
|
|
|
void * m_free_list;
|
2014-07-22 01:58:24 +00:00
|
|
|
public:
|
2014-09-24 17:48:32 +00:00
|
|
|
memory_pool(unsigned size):m_size(size), m_free_list(nullptr) {}
|
2014-09-24 19:51:04 +00:00
|
|
|
~memory_pool();
|
|
|
|
void * allocate();
|
2014-07-22 16:18:26 +00:00
|
|
|
void recycle(void * ptr) {
|
2014-07-22 01:58:24 +00:00
|
|
|
*(reinterpret_cast<void**>(ptr)) = m_free_list;
|
|
|
|
m_free_list = ptr;
|
|
|
|
}
|
|
|
|
};
|
2014-09-24 19:51:04 +00:00
|
|
|
|
|
|
|
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); \
|
|
|
|
}
|
2014-07-22 01:58:24 +00:00
|
|
|
}
|