11#ifndef BLOCK_ALLOCATOR_H
22#define BLOCK_ALLOCATOR_H
3- template <class T ,u32 block_size>
3+ template <class T , u32 block_size>
44class CBlockAllocator
55{
6- u32 block_count;
7- u32 block_position;
8- T* current_block;
9- xr_vector<T*> blocks;
10- public:
11- IC T* add ()
12- {
13- if (block_position==block_size)next_block ();
14- ++block_position;
15- return ¤t_block[block_position-1 ];
16- }
17- IC void empty ()
18- {
19- block_count=0 ;
20- if (blocks.size ())
21- {
22- block_position=0 ;
23- current_block=blocks[0 ];
24- }
25- else
26- {
27- block_position=block_size;
28- }
29- }
30- CBlockAllocator ()
31- {
32- init ();
33- }
34- ~CBlockAllocator ()
35- {
36- clear ();
37- }
38- IC void init ()
39- {
40- block_position=block_size;
41- block_count=0 ;
42- current_block=NULL ;
43- }
44- IC void clear ()
45- {
46- xr_vector<T*>::iterator i=blocks.begin (),e=blocks.end ();
47- for (;i!=e;++i) xr_free (*i);
48- blocks.clear ();
49- init ();
50- }
51- private:
52- // ///////////////////////////////////////////////////////////////
53- IC void add_block ()
54- {
55- blocks.push_back (xr_alloc<T>(block_size));
56- };
57- IC void next_block ()
58- {
6+ u32 block_count;
7+ u32 block_position;
8+ T* current_block;
9+ xr_vector<T*> blocks;
5910
60- if (block_count==blocks.size ()) add_block ();
61- current_block=blocks[block_count];
62- ++block_count;
63- block_position=0 ;
64- }
65- // //////////////////////////////////////////////////////////////
6611public:
67- template <typename _Predicate>
68- IC void for_each (const _Predicate &pred)
69- {
70- if (! current_block) return ;
71- xr_vector<T*>::iterator i = blocks.begin ();
72- xr_vector<T*>::iterator e = blocks.begin ()+block_count;
73- u32 j;
74- for ( ; i != e; ++i)
75- {
76- for (j=0 ;j<block_size;++j)
77- pred.operator ()((*i)+j);
78- }
79- for (j=0 ;j<block_position;++j)
80- {
81- pred.operator ()(current_block+j);
82- }
83- // for_each(blocks.begin(),block.end(),pred);
84- }
85- private:
86- IC T* pointer (u32 position)
87- {
88- return blocks[position/block_size]+position%block_size;
89- }
90-
91- IC T& back ()
92- {
93- return current_block[block_position-1 ];
94- }
95-
96- IC T& back_pointer ()
97- {
98- return current_block+block_position-1 ;
99- }
12+ IC T* add ()
13+ {
14+ if (block_position == block_size) next_block ();
15+ ++block_position;
16+ return ¤t_block[block_position - 1 ];
17+ }
18+ IC void empty ()
19+ {
20+ block_count = 0 ;
21+ if (blocks.size ()) {
22+ block_position = 0 ;
23+ current_block = blocks[0 ];
24+ }
25+ else
26+ {
27+ block_position = block_size;
28+ }
29+ }
30+ CBlockAllocator () { init (); }
31+ ~CBlockAllocator () { clear (); }
32+ IC void init ()
33+ {
34+ block_position = block_size;
35+ block_count = 0 ;
36+ current_block = NULL ;
37+ }
38+ IC void clear ()
39+ {
40+ xr_vector<T *>::iterator i = blocks.begin (), e = blocks.end ();
41+ for (; i != e; ++i)
42+ xr_free (*i);
43+ blocks.clear ();
44+ init ();
45+ }
10046
101- IC T& operator [](u32 position)
102- {
103- return *pointer (position);
104- }
105-
106- IC void construct (u32 position)
107- {
108- xr_allocator_t <T> ().construct (pointer (position));
109- }
47+ private:
48+ // ///////////////////////////////////////////////////////////////
49+ IC void add_block () { blocks.push_back (xr_alloc<T>(block_size)); };
50+ IC void next_block ()
51+ {
52+ if (block_count == blocks.size ()) add_block ();
53+ current_block = blocks[block_count];
54+ ++block_count;
55+ block_position = 0 ;
56+ }
57+ // //////////////////////////////////////////////////////////////
58+ public:
59+ template <typename _Predicate>
60+ IC void for_each (const _Predicate& pred)
61+ {
62+ if (!current_block) return ;
63+ xr_vector<T*>::iterator i = blocks.begin ();
64+ xr_vector<T*>::iterator e = blocks.begin () + block_count;
65+ u32 j;
66+ for (; i != e; ++i)
67+ {
68+ for (j = 0 ; j < block_size; ++j)
69+ pred.operator ()((*i) + j);
70+ }
71+ for (j = 0 ; j < block_position; ++j)
72+ {
73+ pred.operator ()(current_block + j);
74+ }
75+ // for_each(blocks.begin(),block.end(),pred);
76+ }
11077
111- IC void construct_back ()
112- {
113- xr_allocator_t <T> ().construct (back_pointer ());
114- }
78+ private:
79+ IC T* pointer (u32 position) { return blocks[position / block_size] + position % block_size; }
80+ IC T& back () { return current_block[block_position - 1 ]; }
81+ IC T& back_pointer () { return current_block + block_position - 1 ; }
82+ IC T& operator [](u32 position) { return *pointer (position); }
83+ IC void construct (u32 position) { xr_allocator_t <T>().construct (pointer (position)); }
84+ IC void construct_back () { xr_allocator_t <T>().construct (back_pointer ()); }
11585};
11686
117-
11887#endif
0 commit comments