-
Notifications
You must be signed in to change notification settings - Fork 0
/
block_list.cpp
49 lines (41 loc) · 1014 Bytes
/
block_list.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include "block_list.hpp"
BlockListBase::BlockListBase()
: used(0)
, next(0)
, tail(this)
{
for(int i=0; i<BLOCK_LIST_CHUNK; ++i) data[i] = 0;
}
void* BlockListBase::operator new(size_t size, Block* block){
return block->allocate(size);
}
void BlockListBase::add(Block* block, void* item){
if(tail->used >= BLOCK_LIST_CHUNK) {
BlockListBase* n = new(block) BlockListBase();
tail->next = n;
tail = n;
}
tail->addToChunk(item);
}
BlockListBaseIter BlockListBase::iter(){
return BlockListBaseIter(this);
}
BlockListBaseIter::BlockListBaseIter(BlockListBase* start)
: current(start)
, position(0)
{ }
void* BlockListBaseIter::next() {
if(position == BLOCK_LIST_CHUNK){
// try next chunk
current = current->next;
position = 0;
if(current == 0) return 0;
}
if(position < current->used){
void* item = current->data[position];
++position;
return item;
} else {
return 0;
}
}