31 #include "MyVector.hpp" 44 static size_t const UNIT_SIZE =
sizeof(Unit);
45 static size_t const BLOCK_UNITS = 400000 / UNIT_SIZE;
46 static size_t const MAX_ELEMENT_UNIS = BLOCK_UNITS / 10;
53 : blockList(0), nextUnit(BLOCK_UNITS) {
57 : blockList(0), nextUnit(BLOCK_UNITS) {
81 blockList = o.blockList;
82 nextUnit = o.nextUnit;
91 return blockList == 0;
95 while (blockList != 0) {
96 Unit* block = blockList;
97 blockList = blockList->next;
100 nextUnit = BLOCK_UNITS;
104 if (blockList == 0)
return;
105 while (blockList->next != 0) {
106 Unit* block = blockList;
107 blockList = blockList->next;
114 if (blockList != 0) {
115 Unit** rear = &o.blockList;
117 rear = &(*rear)->next;
122 blockList = o.blockList;
123 nextUnit = o.nextUnit;
126 o.nextUnit = BLOCK_UNITS;
129 void* alloc(
size_t n) {
130 size_t const elementUnits = (n + UNIT_SIZE - 1) / UNIT_SIZE;
132 if (elementUnits > MAX_ELEMENT_UNIS) {
133 size_t m = elementUnits + 1;
134 Unit* block =
new Unit[m];
135 if (blockList == 0) {
140 block->next = blockList->next;
141 blockList->next = block;
146 if (nextUnit + elementUnits > BLOCK_UNITS) {
147 Unit* block =
new Unit[BLOCK_UNITS];
148 block->next = blockList;
151 assert(nextUnit + elementUnits <= BLOCK_UNITS);
154 Unit* p = blockList + nextUnit;
155 nextUnit += elementUnits;
160 T* allocate(
size_t n = 1) {
161 return static_cast<T*
>(alloc(
sizeof(T) * n));
165 class Allocator:
public std::allocator<T> {
171 typedef Allocator<U> other;
182 Allocator(Allocator
const& o)
throw ()
186 Allocator& operator=(Allocator
const& o) {
192 Allocator(Allocator<U>
const& o)
throw ()
196 ~Allocator()
throw () {
199 T* allocate(
size_t n,
const void* = 0) {
200 return pool->allocate<T>(n);
203 void deallocate(T*,
size_t) {
208 Allocator<T> allocator() {
209 return Allocator<T>(*this);
220 for (Unit* p = o.blockList; p != 0; p = p->next) {
223 return os <<
"MemoryPool(" << n <<
")";
230 typedef MyVector<MemoryPool> MemoryPools;
233 inline void MyVector<MemoryPool>::moveElement(
MemoryPool& from,
friend std::ostream & operator<<(std::ostream &os, MemoryPool const &o)
Send an object to an output stream.