-
Notifications
You must be signed in to change notification settings - Fork 0
/
block_list.hpp
59 lines (42 loc) · 1.35 KB
/
block_list.hpp
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
50
51
52
53
54
55
56
57
58
59
#ifndef _BLOCK_LIST_BASE_H
#define _BLOCK_LIST_BASE_H
// Simple collection classes for storing dynamic lists in blocks. Due to
// nature of blocks no reallocation can take place but more chunks can
// be added from the block.
#include "block_malloc.hpp"
#define BLOCK_LIST_CHUNK 8
class BlockListBaseIter;
class BlockListBase {
friend class BlockListBaseIter;
void* data[BLOCK_LIST_CHUNK];
unsigned int used;
BlockListBase* next;
BlockListBase* tail;
void addToChunk(void* item) {
data[used++] = item;
}
public:
BlockListBase();
void* operator new(size_t size, Block* block);
void add(Block* block, void* item);
BlockListBaseIter iter();
};
class BlockListBaseIter {
friend class BlockListBase;
BlockListBase* current;
unsigned int position;
public:
BlockListBaseIter(BlockListBase* start);
void* next();
};
template <typename T> class BlockListIter: public BlockListBaseIter {
public:
BlockListIter(const BlockListBaseIter& base) : BlockListBaseIter(base){}
T* next() { return static_cast<T*>(BlockListBaseIter::next());}
};
template <typename T> class BlockList: public BlockListBase {
public:
void add(Block* block, T* item) {BlockListBase::add(block, item);}
BlockListIter<T> iter() { return static_cast<BlockListIter<T>> (BlockListBase::iter());}
};
#endif