62 _max = _chunk->top();
63 set_size_in_bytes(init_size);
64 }
65
66 Arena::Arena() {
67 _first = _chunk = new (Chunk::init_size) Chunk(Chunk::init_size);
68 _hwm = _chunk->bottom(); // Save the cached hwm, max
69 _max = _chunk->top();
70 set_size_in_bytes(Chunk::init_size);
71 }
72
73 Arena::Arena( Arena *a )
74 : _chunk(a->_chunk), _hwm(a->_hwm), _max(a->_max), _first(a->_first) {
75 set_size_in_bytes(a->size_in_bytes());
76 }
77
78 //------------------------------used-------------------------------------------
79 // Total of all Chunks in arena
80 size_t Arena::used() const {
81 size_t sum = _chunk->_len - (_max-_hwm); // Size leftover in this Chunk
82 register Chunk *k = _first;
83 while( k != _chunk) { // Whilst have Chunks in a row
84 sum += k->_len; // Total size of this Chunk
85 k = k->_next; // Bump along to next Chunk
86 }
87 return sum; // Return total consumed space.
88 }
89
90 //------------------------------grow-------------------------------------------
91 // Grow a new Chunk
92 void* Arena::grow( size_t x ) {
93 // Get minimal required size. Either real big, or even bigger for giant objs
94 size_t len = max(x, Chunk::size);
95
96 register Chunk *k = _chunk; // Get filled-up chunk address
97 _chunk = new (len) Chunk(len);
98
99 if( k ) k->_next = _chunk; // Append new chunk to end of linked list
100 else _first = _chunk;
101 _hwm = _chunk->bottom(); // Save the cached hwm, max
102 _max = _chunk->top();
103 set_size_in_bytes(size_in_bytes() + len);
104 void* result = _hwm;
105 _hwm += x;
106 return result;
107 }
108
109 //------------------------------calloc-----------------------------------------
110 // Allocate zeroed storage in Arena
111 void *Arena::Acalloc( size_t items, size_t x ) {
112 size_t z = items*x; // Total size needed
113 void *ptr = Amalloc(z); // Get space
114 memset( ptr, 0, z ); // Zap space
115 return ptr; // Return space
116 }
|
62 _max = _chunk->top();
63 set_size_in_bytes(init_size);
64 }
65
66 Arena::Arena() {
67 _first = _chunk = new (Chunk::init_size) Chunk(Chunk::init_size);
68 _hwm = _chunk->bottom(); // Save the cached hwm, max
69 _max = _chunk->top();
70 set_size_in_bytes(Chunk::init_size);
71 }
72
73 Arena::Arena( Arena *a )
74 : _chunk(a->_chunk), _hwm(a->_hwm), _max(a->_max), _first(a->_first) {
75 set_size_in_bytes(a->size_in_bytes());
76 }
77
78 //------------------------------used-------------------------------------------
79 // Total of all Chunks in arena
80 size_t Arena::used() const {
81 size_t sum = _chunk->_len - (_max-_hwm); // Size leftover in this Chunk
82 Chunk *k = _first;
83 while( k != _chunk) { // Whilst have Chunks in a row
84 sum += k->_len; // Total size of this Chunk
85 k = k->_next; // Bump along to next Chunk
86 }
87 return sum; // Return total consumed space.
88 }
89
90 //------------------------------grow-------------------------------------------
91 // Grow a new Chunk
92 void* Arena::grow( size_t x ) {
93 // Get minimal required size. Either real big, or even bigger for giant objs
94 size_t len = max(x, Chunk::size);
95
96 Chunk *k = _chunk; // Get filled-up chunk address
97 _chunk = new (len) Chunk(len);
98
99 if( k ) k->_next = _chunk; // Append new chunk to end of linked list
100 else _first = _chunk;
101 _hwm = _chunk->bottom(); // Save the cached hwm, max
102 _max = _chunk->top();
103 set_size_in_bytes(size_in_bytes() + len);
104 void* result = _hwm;
105 _hwm += x;
106 return result;
107 }
108
109 //------------------------------calloc-----------------------------------------
110 // Allocate zeroed storage in Arena
111 void *Arena::Acalloc( size_t items, size_t x ) {
112 size_t z = items*x; // Total size needed
113 void *ptr = Amalloc(z); // Get space
114 memset( ptr, 0, z ); // Zap space
115 return ptr; // Return space
116 }
|