• Main Page
  • Related Pages
  • Data Structures
  • Files
  • File List
  • Globals

src/libsphinxbase/util/ckd_alloc.c

00001 /* -*- c-basic-offset: 4; indent-tabs-mode: nil -*- */
00002 /* ====================================================================
00003  * Copyright (c) 1999-2004 Carnegie Mellon University.  All rights
00004  * reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions
00008  * are met:
00009  *
00010  * 1. Redistributions of source code must retain the above copyright
00011  *    notice, this list of conditions and the following disclaimer. 
00012  *
00013  * 2. Redistributions in binary form must reproduce the above copyright
00014  *    notice, this list of conditions and the following disclaimer in
00015  *    the documentation and/or other materials provided with the
00016  *    distribution.
00017  *
00018  * This work was supported in part by funding from the Defense Advanced 
00019  * Research Projects Agency and the National Science Foundation of the 
00020  * United States of America, and the CMU Sphinx Speech Consortium.
00021  *
00022  * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND 
00023  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
00024  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
00025  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
00026  * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00027  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
00028  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
00029  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
00030  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
00031  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
00032  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033  *
00034  * ====================================================================
00035  *
00036  */
00037 /*
00038  * ckd_alloc.c -- Memory allocation package.
00039  *
00040  * **********************************************
00041  * CMU ARPA Speech Project
00042  *
00043  * Copyright (c) 1999 Carnegie Mellon University.
00044  * ALL RIGHTS RESERVED.
00045  * **********************************************
00046  * 
00047  * HISTORY
00048  * $Log: ckd_alloc.c,v $
00049  * Revision 1.6  2005/06/22 02:59:25  arthchan2003
00050  * Added  keyword
00051  *
00052  * Revision 1.3  2005/03/30 01:22:48  archan
00053  * Fixed mistakes in last updates. Add
00054  *
00055  * 
00056  * 19-Jun-97    M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University
00057  *              Removed file,line arguments from free functions.
00058  *              Removed debugging stuff.
00059  * 
00060  * 01-Jan-96    M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University
00061  *              Created.
00062  */
00063 
00064 
00065 /*********************************************************************
00066  *
00067  * $Header: /cvsroot/cmusphinx/sphinx3/src/libutil/ckd_alloc.c,v 1.6 2005/06/22 02:59:25 arthchan2003 Exp $
00068  *
00069  * Carnegie Mellon ARPA Speech Group
00070  *
00071  * Copyright (c) 1994 Carnegie Mellon University.
00072  * All rights reserved.
00073  *
00074  *********************************************************************
00075  *
00076  * file: ckd_alloc.c
00077  * 
00078  * traceability: 
00079  * 
00080  * description: 
00081  * 
00082  * author: 
00083  * 
00084  *********************************************************************/
00085 
00086 
00087 #include <stdio.h>
00088 #include <stdlib.h>
00089 #include <string.h>
00090 #include <assert.h>
00091 #include <stdarg.h>
00092 
00093 #ifdef _MSC_VER
00094 #pragma warning (disable: 4996)
00095 #endif
00096 
00097 #include "ckd_alloc.h"
00098 #include "err.h"
00099 
00105 static jmp_buf *ckd_target;
00106 static int jmp_abort;
00107 
00108 jmp_buf *
00109 ckd_set_jump(jmp_buf *env, int abort)
00110 {
00111     jmp_buf *old;
00112 
00113     if (abort)
00114         jmp_abort = 1;
00115 
00116     old = ckd_target;
00117     ckd_target = env;
00118     return old;
00119 }
00120 
00121 void
00122 ckd_fail(char *format, ...)
00123 {
00124     va_list args;
00125 
00126     va_start(args, format);
00127     vfprintf(stderr, format, args);
00128     va_end(args);
00129 
00130     if (jmp_abort)
00131         abort();
00132     else if (ckd_target)
00133         longjmp(*ckd_target, 1);
00134     else
00135         exit(-1);
00136 }
00137 
00138 void *
00139 __ckd_calloc__(size_t n_elem, size_t elem_size,
00140                const char *caller_file, int caller_line)
00141 {
00142     void *mem;
00143 
00144 #if defined(__ADSPBLACKFIN__) && !defined(__linux__)
00145     if ((mem = heap_calloc(heap_lookup(1),n_elem, elem_size)) == NULL)
00146         if ((mem = heap_calloc(heap_lookup(0),n_elem, elem_size)) == NULL) 
00147         {
00148                 ckd_fail("calloc(%d,%d) failed from %s(%d), free space: %d\n", n_elem,
00149                 elem_size, caller_file, caller_line,space_unused());
00150         }
00151 #else
00152     if ((mem = calloc(n_elem, elem_size)) == NULL) {
00153         ckd_fail("calloc(%d,%d) failed from %s(%d)\n", n_elem,
00154                 elem_size, caller_file, caller_line);
00155         }
00156 #endif
00157         
00158 
00159     return mem;
00160 }
00161 
00162 
00163 void *
00164 __ckd_malloc__(size_t size, const char *caller_file, int caller_line)
00165 {
00166     void *mem;
00167 
00168 #if defined(__ADSPBLACKFIN__) && !defined(__linux__)
00169     if ((mem = heap_malloc(heap_lookup(0),size)) == NULL)
00170         if ((mem = heap_malloc(heap_lookup(1),size)) == NULL) 
00171 #else
00172     if ((mem = malloc(size)) == NULL)
00173 #endif
00174                 ckd_fail("malloc(%d) failed from %s(%d)\n", size,
00175                 caller_file, caller_line);
00176                 
00177     return mem;
00178 }
00179 
00180 
00181 void *
00182 __ckd_realloc__(void *ptr, size_t new_size,
00183                 const char *caller_file, int caller_line)
00184 {
00185     void *mem;
00186 #if defined(__ADSPBLACKFIN__) && !defined(__linux__)
00187     if ((mem = heap_realloc(heap_lookup(0),ptr, new_size)) == NULL) {
00188 #else
00189     if ((mem = realloc(ptr, new_size)) == NULL) {
00190 #endif
00191         ckd_fail("malloc(%d) failed from %s(%d)\n", new_size,
00192                 caller_file, caller_line);
00193     }
00194 
00195     return mem;
00196 }
00197 
00198 
00199 char *
00200 __ckd_salloc__(const char *orig, const char *caller_file,
00201                int caller_line)
00202 {
00203     size_t len;
00204     char *buf;
00205 
00206     len = strlen(orig) + 1;
00207     buf = (char *) __ckd_malloc__(len, caller_file, caller_line);
00208 
00209     strcpy(buf, orig);
00210     return (buf);
00211 }
00212 
00213 
00214 void *
00215 __ckd_calloc_2d__(size_t d1, size_t d2, size_t elemsize,
00216                   const char *caller_file, int caller_line)
00217 {
00218     char **ref, *mem;
00219     size_t i, offset;
00220 
00221     mem =
00222         (char *) __ckd_calloc__(d1 * d2, elemsize, caller_file,
00223                                 caller_line);
00224     ref =
00225         (char **) __ckd_malloc__(d1 * sizeof(void *), caller_file,
00226                                  caller_line);
00227 
00228     for (i = 0, offset = 0; i < d1; i++, offset += d2 * elemsize)
00229         ref[i] = mem + offset;
00230 
00231     return ref;
00232 }
00233 
00234 
00235 void
00236 ckd_free(void *ptr)
00237 {
00238     if (ptr)
00239 #if defined(__ADSPBLACKFIN__) && !defined(__linux__)
00240         heap_free(0,ptr);
00241 #else
00242                 free(ptr);
00243 #endif
00244 }
00245 
00246 void
00247 ckd_free_2d(void *tmpptr)
00248 {
00249     void **ptr = (void **)tmpptr;
00250     if (ptr)
00251         ckd_free(ptr[0]);
00252     ckd_free(ptr);
00253 }
00254 
00255 
00256 void *
00257 __ckd_calloc_3d__(size_t d1, size_t d2, size_t d3, size_t elemsize,
00258                   const char *caller_file, int caller_line)
00259 {
00260     char ***ref1, **ref2, *mem;
00261     size_t i, j, offset;
00262 
00263     mem =
00264         (char *) __ckd_calloc__(d1 * d2 * d3, elemsize, caller_file,
00265                                 caller_line);
00266     ref1 =
00267         (char ***) __ckd_malloc__(d1 * sizeof(void **), caller_file,
00268                                   caller_line);
00269     ref2 =
00270         (char **) __ckd_malloc__(d1 * d2 * sizeof(void *), caller_file,
00271                                  caller_line);
00272 
00273     for (i = 0, offset = 0; i < d1; i++, offset += d2)
00274         ref1[i] = ref2 + offset;
00275 
00276     offset = 0;
00277     for (i = 0; i < d1; i++) {
00278         for (j = 0; j < d2; j++) {
00279             ref1[i][j] = mem + offset;
00280             offset += d3 * elemsize;
00281         }
00282     }
00283 
00284     return ref1;
00285 }
00286 
00287 
00288 void
00289 ckd_free_3d(void *inptr)
00290 {
00291     void ***ptr = (void ***)inptr;
00292 
00293     if (ptr && ptr[0])
00294         ckd_free(ptr[0][0]);
00295     if (ptr)
00296         ckd_free(ptr[0]);
00297     ckd_free(ptr);
00298 }
00299 
00300 /* Layers a 3d array access structure over a preallocated storage area */
00301 void *
00302 __ckd_alloc_3d_ptr(size_t d1,
00303                    size_t d2,
00304                    size_t d3,
00305                    void *store,
00306                    size_t elem_size,
00307                    char *file,
00308                    int line)
00309 {
00310     void **tmp1;
00311     void ***out;
00312     size_t i, j;
00313     
00314     tmp1 = __ckd_calloc__(d1 * d2, sizeof(void *), file, line);
00315 
00316     out  = __ckd_calloc__(d1, sizeof(void **), file, line);
00317     
00318     for (i = 0, j = 0; i < d1*d2; i++, j += d3) {
00319         tmp1[i] = &((char *)store)[j*elem_size];
00320     }
00321     
00322     for (i = 0, j = 0; i < d1; i++, j += d2) {
00323         out[i] = &tmp1[j];
00324     }
00325     
00326     return out;
00327 }
00328 
00329 void *
00330 __ckd_alloc_2d_ptr(size_t d1,
00331                    size_t d2,
00332                    void *store,
00333                    size_t elem_size,
00334                    char *file,
00335                    int line)
00336 {
00337     void **out;
00338     size_t i, j;
00339     
00340     out = __ckd_calloc__(d1, sizeof(void *), file, line);
00341     
00342     for (i = 0, j = 0; i < d1; i++, j += d2) {
00343         out[i] = &((char *)store)[j*elem_size];
00344     }
00345 
00346     return out;
00347 }

Generated on Fri Jan 14 2011 for SphinxBase by  doxygen 1.7.1