2012-10-16 15:57:18 +04:00
|
|
|
// Very simple pool. It can only allocate memory. And all of the memory it
|
|
|
|
// allocates must be freed at the same time.
|
|
|
|
|
2012-10-16 20:35:27 +04:00
|
|
|
#ifndef UTIL_POOL__
|
|
|
|
#define UTIL_POOL__
|
2012-10-16 15:57:18 +04:00
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
2012-10-16 20:35:27 +04:00
|
|
|
namespace util {
|
2012-10-16 15:57:18 +04:00
|
|
|
|
2012-10-16 20:35:27 +04:00
|
|
|
class Pool {
|
2012-10-16 15:57:18 +04:00
|
|
|
public:
|
|
|
|
Pool();
|
|
|
|
|
|
|
|
~Pool();
|
|
|
|
|
2012-10-16 20:58:38 +04:00
|
|
|
void *Allocate(std::size_t size) {
|
2012-10-16 15:57:18 +04:00
|
|
|
void *ret = current_;
|
|
|
|
current_ += size;
|
|
|
|
if (current_ < current_end_) {
|
|
|
|
return ret;
|
|
|
|
} else {
|
|
|
|
return More(size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FreeAll();
|
|
|
|
|
|
|
|
private:
|
2012-10-16 20:58:38 +04:00
|
|
|
void *More(std::size_t size);
|
2012-10-16 15:57:18 +04:00
|
|
|
|
|
|
|
std::vector<void *> free_list_;
|
|
|
|
|
|
|
|
uint8_t *current_, *current_end_;
|
2012-10-16 20:35:27 +04:00
|
|
|
|
|
|
|
// no copying
|
|
|
|
Pool(const Pool &);
|
|
|
|
Pool &operator=(const Pool &);
|
2012-10-16 15:57:18 +04:00
|
|
|
};
|
|
|
|
|
2012-10-16 20:35:27 +04:00
|
|
|
} // namespace util
|
2012-10-16 15:57:18 +04:00
|
|
|
|
2012-10-16 20:35:27 +04:00
|
|
|
#endif // UTIL_POOL__
|