46 #include "EST_Chunk.h" 48 EST_Chunk::EST_Chunk ()
55 EST_Chunk::~EST_Chunk ()
59 cerr <<
"deleting chunk with non-zero count\n";
73 #if !defined(__CHUNK_INLINE_AGGRESSIVELY__) 75 void EST_Chunk:: operator ++ ()
78 if (++count > MAX_CHUNK_COUNT)
80 cerr<<
"max count exceeded\n";
85 if (count < MAX_CHUNK_COUNT)
91 void EST_Chunk::operator -- ()
95 cerr<<
"negative count\n";
106 void *EST_Chunk::operator
new (
size_t size,
int bytes)
109 if (bytes > MAX_CHUNK_SIZE)
111 cerr<<
"trying to make chunk of size "<<bytes<<
"\n";
114 #if defined(__CHUNK_USE_WALLOC__) 115 void *it = walloc(
char, size+bytes);
118 void *it =
new char[size + bytes];
128 void EST_Chunk::operator
delete (
void *it)
131 #if defined(__CHUNK_USE_WALLOC__) 145 #if !defined(__CHUNK_INLINE_AGGRESSIVELY__) 147 EST_ChunkPtr::EST_ChunkPtr (
EST_Chunk *chp)
161 EST_ChunkPtr::~EST_ChunkPtr (
void)
191 EST_ChunkPtr::operator
const char*()
const 194 return &(ptr->memory[0]);
199 EST_ChunkPtr::operator
char const*()
201 return ptr?&(ptr->memory[0]):(
const char *)NULL;
204 EST_ChunkPtr::operator
char*()
210 CHUNK_WARN(
"getting writable version of shared chunk");
211 cp_make_updatable(*
this);
214 return &(ptr->memory[0]);
220 char &EST_ChunkPtr::operator () (
int i) {
223 CHUNK_WARN(
"getting writable version of shared chunk");
224 cp_make_updatable(*
this);
226 return ptr->memory[i];
244 EST_ChunkPtr chunk_allocate(
int bytes,
const char *initial,
int initial_len)
246 if (initial_len >= bytes)
248 cerr<<
"initialiser too long\n";
254 memcpy(cp->memory, initial, initial_len);
256 cp->memory[initial_len] =
'\0';
263 if (initial_len >= bytes)
265 cerr<<
"initialiser too long\n";
271 memcpy(cp->memory, initial.ptr->memory + initial_start, initial_len);
273 cp->memory[initial_len] =
'\0';
286 void cp_make_updatable(
EST_ChunkPtr &cp, EST_Chunk::EST_chunk_size inuse)
288 if (cp.ptr && cp.ptr->count > 1)
293 memcpy(newchunk->memory, cp.ptr->memory, inuse);
301 if (cp.ptr && cp.ptr->count > 1)
305 memcpy(newchunk->memory, cp.ptr->memory, cp.ptr->size);
318 void grow_chunk(
EST_ChunkPtr &cp, EST_Chunk::EST_chunk_size newsize)
320 if (!cp.ptr || cp.ptr->size < newsize)
323 cp_make_updatable(cp);
325 memcpy(newchunk->memory, cp.ptr->memory, cp.ptr->size);
330 void grow_chunk(
EST_ChunkPtr &cp, EST_Chunk::EST_chunk_size inuse, EST_Chunk::EST_chunk_size newsize)
332 if (!cp.ptr || cp.ptr->size < newsize)
335 cp_make_updatable(cp, inuse);
337 memcpy(newchunk->memory, cp.ptr->memory, inuse);
342 ostream &operator << (ostream &s,
const EST_Chunk &ch)
348 memcpy(buff, ch.memory, ch.size);
353 memcpy(buff, ch.memory, 20);
357 return (s<<
"[" << ch.size <<
"!" << ch.count <<
"!" << buff <<
"]");