1 /* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* ====================================================================
3 * Copyright (c) 1999-2004 Carnegie Mellon University. All rights
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
18 * This work was supported in part by funding from the Defense Advanced
19 * Research Projects Agency and the National Science Foundation of the
20 * United States of America, and the CMU Sphinx Speech Consortium.
22 * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
23 * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
26 * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 * ====================================================================
38 * ckd_alloc.c -- Memory allocation package.
40 * **********************************************
41 * CMU ARPA Speech Project
43 * Copyright (c) 1999 Carnegie Mellon University.
44 * ALL RIGHTS RESERVED.
45 * **********************************************
48 * $Log: ckd_alloc.c,v $
49 * Revision 1.6 2005/06/22 02:59:25 arthchan2003
52 * Revision 1.3 2005/03/30 01:22:48 archan
53 * Fixed mistakes in last updates. Add
56 * 19-Jun-97 M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University
57 * Removed file,line arguments from free functions.
58 * Removed debugging stuff.
60 * 01-Jan-96 M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University
65 /*********************************************************************
67 * $Header: /cvsroot/cmusphinx/sphinx3/src/libutil/ckd_alloc.c,v 1.6 2005/06/22 02:59:25 arthchan2003 Exp $
69 * Carnegie Mellon ARPA Speech Group
71 * Copyright (c) 1994 Carnegie Mellon University.
72 * All rights reserved.
74 *********************************************************************
84 *********************************************************************/
94 #pragma warning (disable: 4996)
97 #include "sphinxbase/ckd_alloc.h"
98 #include "sphinxbase/err.h"
101 * Target for longjmp() on failure.
103 * FIXME: This should be in thread-local storage.
105 static jmp_buf *ckd_target;
106 static int jmp_abort;
109 ckd_set_jump(jmp_buf *env, int abort)
122 ckd_fail(char *format, ...)
126 va_start(args, format);
127 vfprintf(stderr, format, args);
131 /* Silvio Moioli: abort() doesn't exist in Windows CE */
132 #if defined(_WIN32_WCE)
138 longjmp(*ckd_target, 1);
144 __ckd_calloc__(size_t n_elem, size_t elem_size,
145 const char *caller_file, int caller_line)
149 #if defined(__ADSPBLACKFIN__) && !defined(__linux__)
150 if ((mem = heap_calloc(heap_lookup(1),n_elem, elem_size)) == NULL)
151 if ((mem = heap_calloc(heap_lookup(0),n_elem, elem_size)) == NULL)
153 ckd_fail("calloc(%d,%d) failed from %s(%d), free space: %d\n", n_elem,
154 elem_size, caller_file, caller_line,space_unused());
157 if ((mem = calloc(n_elem, elem_size)) == NULL) {
158 ckd_fail("calloc(%d,%d) failed from %s(%d)\n", n_elem,
159 elem_size, caller_file, caller_line);
169 __ckd_malloc__(size_t size, const char *caller_file, int caller_line)
173 #if defined(__ADSPBLACKFIN__) && !defined(__linux__)
174 if ((mem = heap_malloc(heap_lookup(0),size)) == NULL)
175 if ((mem = heap_malloc(heap_lookup(1),size)) == NULL)
177 if ((mem = malloc(size)) == NULL)
179 ckd_fail("malloc(%d) failed from %s(%d)\n", size,
180 caller_file, caller_line);
187 __ckd_realloc__(void *ptr, size_t new_size,
188 const char *caller_file, int caller_line)
191 #if defined(__ADSPBLACKFIN__) && !defined(__linux__)
192 if ((mem = heap_realloc(heap_lookup(0),ptr, new_size)) == NULL) {
194 if ((mem = realloc(ptr, new_size)) == NULL) {
196 ckd_fail("malloc(%d) failed from %s(%d)\n", new_size,
197 caller_file, caller_line);
205 __ckd_salloc__(const char *orig, const char *caller_file,
211 len = strlen(orig) + 1;
212 buf = (char *) __ckd_malloc__(len, caller_file, caller_line);
220 __ckd_calloc_2d__(size_t d1, size_t d2, size_t elemsize,
221 const char *caller_file, int caller_line)
227 (char *) __ckd_calloc__(d1 * d2, elemsize, caller_file,
230 (char **) __ckd_malloc__(d1 * sizeof(void *), caller_file,
233 for (i = 0, offset = 0; i < d1; i++, offset += d2 * elemsize)
234 ref[i] = mem + offset;
244 #if defined(__ADSPBLACKFIN__) && !defined(__linux__)
252 ckd_free_2d(void *tmpptr)
254 void **ptr = (void **)tmpptr;
262 __ckd_calloc_3d__(size_t d1, size_t d2, size_t d3, size_t elemsize,
263 const char *caller_file, int caller_line)
265 char ***ref1, **ref2, *mem;
269 (char *) __ckd_calloc__(d1 * d2 * d3, elemsize, caller_file,
272 (char ***) __ckd_malloc__(d1 * sizeof(void **), caller_file,
275 (char **) __ckd_malloc__(d1 * d2 * sizeof(void *), caller_file,
278 for (i = 0, offset = 0; i < d1; i++, offset += d2)
279 ref1[i] = ref2 + offset;
282 for (i = 0; i < d1; i++) {
283 for (j = 0; j < d2; j++) {
284 ref1[i][j] = mem + offset;
285 offset += d3 * elemsize;
294 ckd_free_3d(void *inptr)
296 void ***ptr = (void ***)inptr;
307 __ckd_calloc_4d__(size_t d1,
321 store = calloc(d1 * d2 * d3 * d4, elem_size);
323 E_FATAL("ckd_calloc_4d failed for caller at %s(%d) at %s(%d)\n",
324 file, line, __FILE__, __LINE__);
327 tmp1 = calloc(d1 * d2 * d3, sizeof(void *));
329 E_FATAL("ckd_calloc_4d failed for caller at %s(%d) at %s(%d)\n",
330 file, line, __FILE__, __LINE__);
333 tmp2 = ckd_calloc(d1 * d2, sizeof(void **));
335 E_FATAL("ckd_calloc_4d failed for caller at %s(%d) at %s(%d)\n",
336 file, line, __FILE__, __LINE__);
339 out = ckd_calloc(d1, sizeof(void ***));
341 E_FATAL("ckd_calloc_4d failed for caller at %s(%d) at %s(%d)\n",
342 file, line, __FILE__, __LINE__);
345 for (i = 0, j = 0; i < d1*d2*d3; i++, j += d4) {
346 tmp1[i] = &((char *)store)[j*elem_size];
349 for (i = 0, j = 0; i < d1*d2; i++, j += d3) {
353 for (i = 0, j = 0; i < d1; i++, j += d2) {
361 ckd_free_4d(void *inptr)
363 void ****ptr = (void ****)inptr;
366 /* free the underlying store */
367 ckd_free(ptr[0][0][0]);
369 /* free the access overhead */
375 /* Layers a 3d array access structure over a preallocated storage area */
377 __ckd_alloc_3d_ptr(size_t d1,
389 tmp1 = __ckd_calloc__(d1 * d2, sizeof(void *), file, line);
391 out = __ckd_calloc__(d1, sizeof(void **), file, line);
393 for (i = 0, j = 0; i < d1*d2; i++, j += d3) {
394 tmp1[i] = &((char *)store)[j*elem_size];
397 for (i = 0, j = 0; i < d1; i++, j += d2) {
405 __ckd_alloc_2d_ptr(size_t d1,
415 out = __ckd_calloc__(d1, sizeof(void *), file, line);
417 for (i = 0, j = 0; i < d1; i++, j += d2) {
418 out[i] = &((char *)store)[j*elem_size];