fix(util): add missing file
Signed-off-by: Leonardo de Moura <leonardo@microsoft.com>
This commit is contained in:
parent
79ea7c5910
commit
77537d43a3
1 changed files with 39 additions and 0 deletions
39
src/util/fixed_size_allocator.h
Normal file
39
src/util/fixed_size_allocator.h
Normal file
|
@ -0,0 +1,39 @@
|
|||
/*
|
||||
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 {
|
||||
template<unsigned Size>
|
||||
class fixed_size_allocator {
|
||||
void * m_free_list;
|
||||
public:
|
||||
fixed_size_allocator():m_free_list(nullptr) {}
|
||||
~fixed_size_allocator() {
|
||||
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;
|
||||
}
|
||||
};
|
||||
}
|
Loading…
Reference in a new issue