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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343
|
/************************************************************************/
/* */
/* Centre for Speech Technology Research */
/* University of Edinburgh, UK */
/* Copyright (c) 1997 */
/* All Rights Reserved. */
/* */
/* Permission is hereby granted, free of charge, to use and distribute */
/* this software and its documentation without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of this work, and to */
/* permit persons to whom this work is furnished to do so, subject to */
/* the following conditions: */
/* 1. The code must retain the above copyright notice, this list of */
/* conditions and the following disclaimer. */
/* 2. Any modifications must be clearly marked as such. */
/* 3. Original authors' names are not deleted. */
/* 4. The authors' names are not used to endorse or promote products */
/* derived from this software without specific prior written */
/* permission. */
/* */
/* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */
/* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */
/* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */
/* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */
/* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */
/* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */
/* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */
/* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */
/* THIS SOFTWARE. */
/* */
/************************************************************************/
/* */
/* Author: Richard Caley (rjc@cstr.ed.ac.uk) */
/* Date: February 1997 */
/* -------------------------------------------------------------------- */
/* */
/* Use counted memory chunks and smart pointers to them. */
/* */
/************************************************************************/
#include <stdlib.h>
#include <iostream>
#include <string.h>
#include "EST_Chunk.h"
EST_Chunk::EST_Chunk ()
{
count = 0;
memory[0] = '\0';
// cerr<<"created " << hex << (int)&memory << "," << dec << size <<"\n";
}
EST_Chunk::~EST_Chunk ()
{
if (count > 0)
{
cerr << "deleting chunk with non-zero count\n";
exit(1);
}
// cerr << "deleted "<< hex << (int)&memory << "," << dec << size <<"\n";
}
// private address-of operator - up to friends to keep use counts correct.
EST_Chunk *EST_Chunk::operator & ()
{
return this;
}
#if !defined(__CHUNK_INLINE_AGGRESSIVELY__)
void EST_Chunk:: operator ++ ()
{
if (++count > MAX_CHUNK_COUNT)
{
cerr<<"max count exceeded\n";
exit(1);
}
}
void EST_Chunk::operator -- ()
{
if (count-- == 0)
{
cerr<<"negative count\n";
exit(1);
}
else if (count == 0)
{
// cerr<<"deleting\n";
delete this;
}
}
#endif
void *EST_Chunk::operator new (size_t size, int bytes)
{
if (bytes > MAX_CHUNK_SIZE)
{
cerr<<"trying to make chunk of size "<<bytes<<"\n";
}
#if defined(__CHUNK_USE_WALLOC__)
void *it = walloc(char, size+bytes);
#else
void *it = new char[size + bytes];
#endif
// cerr<<"allocated "<<bytes+size<<" byte for chunk\n";
((EST_Chunk *)it) -> size = bytes;
return it;
}
void EST_Chunk::operator delete (void *it)
{
#if defined(__CHUNK_USE_WALLOC__)
wfree(it);
#else
delete it;
#endif
}
/************************************************************************/
/* */
/* Now the smart pointers. */
/* */
/************************************************************************/
#if !defined(__CHUNK_INLINE_AGGRESSIVELY__)
EST_ChunkPtr::EST_ChunkPtr (EST_Chunk *chp)
{
ptr=chp;
if (ptr)
++ *ptr;
}
EST_ChunkPtr::EST_ChunkPtr (const EST_ChunkPtr &cp)
{
ptr=cp.ptr;
if (ptr)
++ *ptr;
}
EST_ChunkPtr::~EST_ChunkPtr (void)
{
if (ptr)
-- *ptr;
}
EST_ChunkPtr &EST_ChunkPtr::operator = (EST_ChunkPtr cp)
{
// doing it in this order means self assignment is safe.
if (cp.ptr)
++ *(cp.ptr);
if (ptr)
-- *ptr;
ptr=cp.ptr;
return *this;
}
EST_ChunkPtr &EST_ChunkPtr::operator = (EST_Chunk *chp)
{
// doing it in this order means self assignment is safe.
if (chp)
++ *chp;
if (ptr)
-- *ptr;
ptr=chp;
return *this;
}
EST_ChunkPtr::operator const char*() const
{
if (ptr)
return &(ptr->memory[0]);
else
return NULL;
}
EST_ChunkPtr::operator char*()
{
if (ptr)
{
if (ptr->count > 1)
{
CHUNK_WARN("getting writable version of shared chunk");
make_updatable(*this);
}
return &(ptr->memory[0]);
}
else
return NULL;
}
char &EST_ChunkPtr::operator () (int i) {
if (ptr->count>1)
{
CHUNK_WARN("getting writable version of shared chunk");
make_updatable(*this);
}
return ptr->memory[i];
}
#endif
/************************************************************************/
/* */
/* Friend function to allocate a chunk in a non count-aware program. */
/* */
/************************************************************************/
EST_ChunkPtr chunk_allocate(int bytes)
{
EST_Chunk *cp = new(bytes) EST_Chunk;
return (EST_ChunkPtr)cp;
}
EST_ChunkPtr chunk_allocate(int bytes, const char *initial, int initial_len)
{
if (initial_len >= bytes)
{
cerr<<"initialiser too long\n";
abort();
}
EST_Chunk *cp = new(bytes) EST_Chunk;
memcpy(cp->memory, initial, initial_len);
cp->memory[initial_len] = '\0';
return (EST_ChunkPtr)cp;
}
EST_ChunkPtr chunk_allocate(int bytes, const EST_ChunkPtr &initial, int initial_start, int initial_len)
{
if (initial_len >= bytes)
{
cerr<<"initialiser too long\n";
abort();
}
EST_Chunk *cp = new(bytes) EST_Chunk;
memcpy(cp->memory, initial.ptr->memory + initial_start, initial_len);
cp->memory[initial_len] = '\0';
return (EST_ChunkPtr)cp;
}
/************************************************************************/
/* */
/* And one which ensures that a chunk is not shared. Do this before */
/* writing to the memory. The first version is told how much of the */
/* memory to copy, the second just copies it all. */
/* */
/************************************************************************/
void make_updatable(EST_ChunkPtr &cp, EST_Chunk::EST_chunk_size inuse)
{
if (cp.ptr && cp.ptr->count > 1)
{
EST_Chunk *newchunk = new(cp.ptr->size) EST_Chunk;
memcpy(newchunk->memory, cp.ptr->memory, inuse);
cp = newchunk;
}
}
void make_updatable(EST_ChunkPtr &cp)
{
if (cp.ptr && cp.ptr->count > 1)
{
EST_Chunk *newchunk = new(cp.ptr->size) EST_Chunk;
memcpy(newchunk->memory, cp.ptr->memory, cp.ptr->size);
cp = newchunk;
}
}
/************************************************************************/
/* */
/* Make more room in a chunk. If the chunk is already big enough and */
/* is unshared then nothing is done. */
/* */
/************************************************************************/
void grow_chunk(EST_ChunkPtr &cp, EST_Chunk::EST_chunk_size newsize)
{
if (!cp.ptr || cp.ptr->size < newsize)
{
if (cp.ptr)
make_updatable(cp);
EST_Chunk *newchunk = new(newsize) EST_Chunk;
memcpy(newchunk->memory, cp.ptr->memory, cp.ptr->size);
cp = newchunk;
}
}
void grow_chunk(EST_ChunkPtr &cp, EST_Chunk::EST_chunk_size inuse, EST_Chunk::EST_chunk_size newsize)
{
if (!cp.ptr || cp.ptr->size < newsize)
{
if (cp.ptr)
make_updatable(cp, inuse);
EST_Chunk *newchunk = new(newsize) EST_Chunk;
memcpy(newchunk->memory, cp.ptr->memory, inuse);
cp = newchunk;
}
}
ostream &operator << (ostream &s, const EST_Chunk &ch)
{
char buff[21];
if (ch.size<20)
{
memcpy(buff, ch.memory, ch.size);
buff[ch.size]='\0';
}
else
{
memcpy(buff, ch.memory, 20);
buff[20]='\0';
}
return (s<< "[" << ch.size << "!" << ch.count << "!" << buff << "]");
}
|