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;
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)
292 memcpy(newchunk->memory, cp.ptr->memory, inuse);
300 if (cp.ptr && cp.ptr->count > 1)
304 memcpy(newchunk->memory, cp.ptr->memory, cp.ptr->size);
317 void grow_chunk(
EST_ChunkPtr &cp, EST_Chunk::EST_chunk_size newsize)
319 if (!cp.ptr || cp.ptr->size < newsize)
322 cp_make_updatable(cp);
324 memcpy(newchunk->memory, cp.ptr->memory, cp.ptr->size);
329 void grow_chunk(
EST_ChunkPtr &cp, EST_Chunk::EST_chunk_size inuse, EST_Chunk::EST_chunk_size newsize)
331 if (!cp.ptr || cp.ptr->size < newsize)
334 cp_make_updatable(cp, inuse);
336 memcpy(newchunk->memory, cp.ptr->memory, inuse);
341 ostream &operator << (ostream &s,
const EST_Chunk &ch)
347 memcpy(buff, ch.memory, ch.size);
352 memcpy(buff, ch.memory, 20);
356 return (s<<
"[" << ch.size <<
"!" << ch.count <<
"!" << buff <<
"]");