File size: 10,173 Bytes
37a92a9 | 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 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 | /* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
/* ====================================================================
* Copyright (c) 1999-2004 Carnegie Mellon University. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* This work was supported in part by funding from the Defense Advanced
* Research Projects Agency and the National Science Foundation of the
* United States of America, and the CMU Sphinx Speech Consortium.
*
* THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
* ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
* NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* ====================================================================
*
*/
/*
* ckd_alloc.c -- Memory allocation package.
*
* **********************************************
* CMU ARPA Speech Project
*
* Copyright (c) 1999 Carnegie Mellon University.
* ALL RIGHTS RESERVED.
* **********************************************
*
* HISTORY
* $Log: ckd_alloc.c,v $
* Revision 1.6 2005/06/22 02:59:25 arthchan2003
* Added keyword
*
* Revision 1.3 2005/03/30 01:22:48 archan
* Fixed mistakes in last updates. Add
*
*
* 19-Jun-97 M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University
* Removed file,line arguments from free functions.
* Removed debugging stuff.
*
* 01-Jan-96 M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University
* Created.
*/
/*********************************************************************
*
* $Header: /cvsroot/cmusphinx/sphinx3/src/libutil/ckd_alloc.c,v 1.6 2005/06/22 02:59:25 arthchan2003 Exp $
*
* Carnegie Mellon ARPA Speech Group
*
* Copyright (c) 1994 Carnegie Mellon University.
* All rights reserved.
*
*********************************************************************
*
* file: ckd_alloc.c
*
* traceability:
*
* description:
*
* author:
*
*********************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <stdarg.h>
#ifdef _MSC_VER
#pragma warning (disable: 4996)
#endif
#include <pocketsphinx/err.h>
#include "util/ckd_alloc.h"
/**
* Target for longjmp() on failure.
*
* FIXME: This should be in thread-local storage.
*/
static jmp_buf *ckd_target;
static int jmp_abort;
jmp_buf *
ckd_set_jump(jmp_buf *env, int abort)
{
jmp_buf *old;
if (abort)
jmp_abort = 1;
old = ckd_target;
ckd_target = env;
return old;
}
void
ckd_fail(char *format, ...)
{
va_list args;
va_start(args, format);
vfprintf(stderr, format, args);
va_end(args);
if (jmp_abort)
/* abort() doesn't exist in Windows CE */
#if defined(_WIN32_WCE)
exit(-1);
#else
abort();
#endif
else if (ckd_target)
longjmp(*ckd_target, 1);
else
exit(-1);
}
void *
__ckd_calloc__(size_t n_elem, size_t elem_size,
const char *caller_file, int caller_line)
{
void *mem;
#if defined(__ADSPBLACKFIN__) && !defined(__linux__)
if ((mem = heap_calloc(heap_lookup(1),n_elem, elem_size)) == NULL)
if ((mem = heap_calloc(heap_lookup(0),n_elem, elem_size)) == NULL)
{
ckd_fail("calloc(%d,%d) failed from %s(%d), free space: %d\n", n_elem,
elem_size, caller_file, caller_line,space_unused());
}
#else
if ((mem = calloc(n_elem, elem_size)) == NULL) {
ckd_fail("calloc(%d,%d) failed from %s(%d)\n", n_elem,
elem_size, caller_file, caller_line);
}
#endif
return mem;
}
void *
__ckd_malloc__(size_t size, const char *caller_file, int caller_line)
{
void *mem;
#if defined(__ADSPBLACKFIN__) && !defined(__linux__)
if ((mem = heap_malloc(heap_lookup(0),size)) == NULL)
if ((mem = heap_malloc(heap_lookup(1),size)) == NULL)
#else
if ((mem = malloc(size)) == NULL)
#endif
ckd_fail("malloc(%d) failed from %s(%d)\n", size,
caller_file, caller_line);
return mem;
}
void *
__ckd_realloc__(void *ptr, size_t new_size,
const char *caller_file, int caller_line)
{
void *mem;
#if defined(__ADSPBLACKFIN__) && !defined(__linux__)
if ((mem = heap_realloc(heap_lookup(0),ptr, new_size)) == NULL) {
#else
if ((mem = realloc(ptr, new_size)) == NULL) {
#endif
ckd_fail("malloc(%d) failed from %s(%d)\n", new_size,
caller_file, caller_line);
}
return mem;
}
char *
__ckd_salloc__(const char *orig, const char *caller_file,
int caller_line)
{
size_t len;
char *buf;
if (!orig)
return NULL;
len = strlen(orig) + 1;
buf = (char *) __ckd_malloc__(len, caller_file, caller_line);
strcpy(buf, orig);
return (buf);
}
void *
__ckd_calloc_2d__(size_t d1, size_t d2, size_t elemsize,
const char *caller_file, int caller_line)
{
char **ref, *mem;
size_t i, offset;
mem =
(char *) __ckd_calloc__(d1 * d2, elemsize, caller_file,
caller_line);
ref =
(char **) __ckd_malloc__(d1 * sizeof(void *), caller_file,
caller_line);
for (i = 0, offset = 0; i < d1; i++, offset += d2 * elemsize)
ref[i] = mem + offset;
return ref;
}
void
ckd_free(void *ptr)
{
#if defined(__ADSPBLACKFIN__) && !defined(__linux__)
if (ptr)
heap_free(0,ptr);
#else
free(ptr);
#endif
}
void
ckd_free_2d(void *tmpptr)
{
void **ptr = (void **)tmpptr;
if (ptr)
ckd_free(ptr[0]);
ckd_free(ptr);
}
void *
__ckd_calloc_3d__(size_t d1, size_t d2, size_t d3, size_t elemsize,
const char *caller_file, int caller_line)
{
char ***ref1, **ref2, *mem;
size_t i, j, offset;
mem =
(char *) __ckd_calloc__(d1 * d2 * d3, elemsize, caller_file,
caller_line);
ref1 =
(char ***) __ckd_malloc__(d1 * sizeof(void **), caller_file,
caller_line);
ref2 =
(char **) __ckd_malloc__(d1 * d2 * sizeof(void *), caller_file,
caller_line);
for (i = 0, offset = 0; i < d1; i++, offset += d2)
ref1[i] = ref2 + offset;
offset = 0;
for (i = 0; i < d1; i++) {
for (j = 0; j < d2; j++) {
ref1[i][j] = mem + offset;
offset += d3 * elemsize;
}
}
return ref1;
}
void
ckd_free_3d(void *inptr)
{
void ***ptr = (void ***)inptr;
if (ptr && ptr[0])
ckd_free(ptr[0][0]);
if (ptr)
ckd_free(ptr[0]);
ckd_free(ptr);
}
void ****
__ckd_calloc_4d__(size_t d1,
size_t d2,
size_t d3,
size_t d4,
size_t elem_size,
char *file,
int line)
{
void *store;
void **tmp1;
void ***tmp2;
void ****out;
size_t i, j;
store = calloc(d1 * d2 * d3 * d4, elem_size);
if (store == NULL) {
E_FATAL("ckd_calloc_4d failed for caller at %s(%d) at %s(%d)\n",
file, line, __FILE__, __LINE__);
}
tmp1 = calloc(d1 * d2 * d3, sizeof(void *));
if (tmp1 == NULL) {
E_FATAL("ckd_calloc_4d failed for caller at %s(%d) at %s(%d)\n",
file, line, __FILE__, __LINE__);
}
tmp2 = ckd_calloc(d1 * d2, sizeof(void **));
if (tmp2 == NULL) {
E_FATAL("ckd_calloc_4d failed for caller at %s(%d) at %s(%d)\n",
file, line, __FILE__, __LINE__);
}
out = ckd_calloc(d1, sizeof(void ***));
if (out == NULL) {
E_FATAL("ckd_calloc_4d failed for caller at %s(%d) at %s(%d)\n",
file, line, __FILE__, __LINE__);
}
for (i = 0, j = 0; i < d1*d2*d3; i++, j += d4) {
tmp1[i] = &((char *)store)[j*elem_size];
}
for (i = 0, j = 0; i < d1*d2; i++, j += d3) {
tmp2[i] = &tmp1[j];
}
for (i = 0, j = 0; i < d1; i++, j += d2) {
out[i] = &tmp2[j];
}
return out;
}
void
ckd_free_4d(void *inptr)
{
void ****ptr = (void ****)inptr;
if (ptr == NULL)
return;
/* free the underlying store */
ckd_free(ptr[0][0][0]);
/* free the access overhead */
ckd_free(ptr[0][0]);
ckd_free(ptr[0]);
ckd_free(ptr);
}
/* Layers a 3d array access structure over a preallocated storage area */
void *
__ckd_alloc_3d_ptr(size_t d1,
size_t d2,
size_t d3,
void *store,
size_t elem_size,
char *file,
int line)
{
void **tmp1;
void ***out;
size_t i, j;
tmp1 = __ckd_calloc__(d1 * d2, sizeof(void *), file, line);
out = __ckd_calloc__(d1, sizeof(void **), file, line);
for (i = 0, j = 0; i < d1*d2; i++, j += d3) {
tmp1[i] = &((char *)store)[j*elem_size];
}
for (i = 0, j = 0; i < d1; i++, j += d2) {
out[i] = &tmp1[j];
}
return out;
}
void *
__ckd_alloc_2d_ptr(size_t d1,
size_t d2,
void *store,
size_t elem_size,
char *file,
int line)
{
void **out;
size_t i, j;
out = __ckd_calloc__(d1, sizeof(void *), file, line);
for (i = 0, j = 0; i < d1; i++, j += d2) {
out[i] = &((char *)store)[j*elem_size];
}
return out;
}
/* vim: set ts=4 sw=4: */
|