2012-10-16 20:35:27 +04:00
|
|
|
#include "util/pool.hh"
|
2012-10-16 15:57:18 +04:00
|
|
|
|
2013-01-18 19:58:54 +04:00
|
|
|
#include "util/scoped.hh"
|
|
|
|
|
2012-10-16 15:57:18 +04:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2012-10-16 20:35:27 +04:00
|
|
|
namespace util {
|
2012-10-16 15:57:18 +04:00
|
|
|
|
|
|
|
Pool::Pool() {
|
|
|
|
current_ = NULL;
|
|
|
|
current_end_ = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
Pool::~Pool() {
|
|
|
|
FreeAll();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Pool::FreeAll() {
|
|
|
|
for (std::vector<void *>::const_iterator i(free_list_.begin()); i != free_list_.end(); ++i) {
|
|
|
|
free(*i);
|
|
|
|
}
|
|
|
|
free_list_.clear();
|
|
|
|
current_ = NULL;
|
|
|
|
current_end_ = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *Pool::More(std::size_t size) {
|
|
|
|
std::size_t amount = std::max(static_cast<size_t>(32) << free_list_.size(), size);
|
2013-01-18 19:58:54 +04:00
|
|
|
uint8_t *ret = static_cast<uint8_t*>(MallocOrThrow(amount));
|
2012-10-16 15:57:18 +04:00
|
|
|
free_list_.push_back(ret);
|
|
|
|
current_ = ret + size;
|
|
|
|
current_end_ = ret + amount;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-10-16 20:35:27 +04:00
|
|
|
} // namespace util
|