00001 #include "util/pool.hh"
00002
00003 #include "util/scoped.hh"
00004
00005 #include <cstdlib>
00006
00007 #include <algorithm>
00008
00009 namespace util {
00010
00011 Pool::Pool() {
00012 current_ = NULL;
00013 current_end_ = NULL;
00014 }
00015
00016 Pool::~Pool() {
00017 FreeAll();
00018 }
00019
00020 void Pool::FreeAll() {
00021 for (std::vector<void *>::const_iterator i(free_list_.begin()); i != free_list_.end(); ++i) {
00022 free(*i);
00023 }
00024 free_list_.clear();
00025 current_ = NULL;
00026 current_end_ = NULL;
00027 }
00028
00029 void *Pool::More(std::size_t size) {
00030 std::size_t amount = std::max(static_cast<size_t>(32) << free_list_.size(), size);
00031 uint8_t *ret = static_cast<uint8_t*>(MallocOrThrow(amount));
00032 free_list_.push_back(ret);
00033 current_ = ret + size;
00034 current_end_ = ret + amount;
00035 return ret;
00036 }
00037
00038 }