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.h -- 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.h,v $
49 * Revision 1.10 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.
59 * 01-Jan-96 M K Ravishankar (rkm@cs.cmu.edu) at Carnegie Mellon University
64 /*********************************************************************
66 * $Header: /cvsroot/cmusphinx/sphinx3/src/libutil/ckd_alloc.h,v 1.10 2005/06/22 02:59:25 arthchan2003 Exp $
68 * Carnegie Mellon ARPA Speech Group
70 * Copyright (c) 1994 Carnegie Mellon University.
71 * All rights reserved.
73 *********************************************************************
83 *********************************************************************/
86 #ifndef _LIBUTIL_CKD_ALLOC_H_
87 #define _LIBUTIL_CKD_ALLOC_H_
92 /* Win32/WinCE DLL gunk */
93 #include <sphinxbase/sphinxbase_export.h>
94 #include <sphinxbase/prim_type.h>
97 *\brief Sphinx's memory allocation/deallocation routines.
99 *Implementation of efficient memory allocation deallocation for
100 *multiple dimensional arrays.
113 * Control behaviour of the program when allocation fails.
115 * Although your program is probably toast when memory allocation
116 * fails, it is also probably a good idea to be able to catch these
117 * errors and alert the user in some way. Either that, or you might
118 * want the program to call abort() so that you can debug the failed
119 * code. This function allows you to control that behaviour.
121 * @param env Pointer to a <code>jmp_buf</code> initialized with
122 * setjmp(), or NULL to remove a previously set jump target.
123 * @param abort If non-zero, the program will call abort() when
124 * allocation fails rather than exiting or calling longjmp().
125 * @return Pointer to a previously set <code>jmp_buf</code>, if any.
127 jmp_buf *ckd_set_jump(jmp_buf *env, int abort);
130 * Fail (with a message) according to behaviour specified by ckd_set_jump().
132 void ckd_fail(char *format, ...);
135 * The following functions are similar to the malloc family, except
136 * that they have two additional parameters, caller_file and
137 * caller_line, for error reporting. All functions print a diagnostic
138 * message if any error occurs, with any other behaviour determined by
139 * ckd_set_jump(), above.
143 void *__ckd_calloc__(size_t n_elem, size_t elem_size,
144 const char *caller_file, int caller_line);
147 void *__ckd_malloc__(size_t size,
148 const char *caller_file, int caller_line);
151 void *__ckd_realloc__(void *ptr, size_t new_size,
152 const char *caller_file, int caller_line);
155 * Like strdup, except that if an error occurs it prints a diagnostic message
159 char *__ckd_salloc__(const char *origstr,
160 const char *caller_file, int caller_line);
163 * Allocate a 2-D array and return ptr to it (ie, ptr to vector of ptrs).
164 * The data area is allocated in one block so it can also be treated as a 1-D array.
167 void *__ckd_calloc_2d__(size_t d1, size_t d2, /* In: #elements in the 2 dimensions */
168 size_t elemsize, /* In: Size (#bytes) of each element */
169 const char *caller_file, int caller_line); /* In */
172 * Allocate a 3-D array and return ptr to it.
173 * The data area is allocated in one block so it can also be treated as a 1-D array.
176 void *__ckd_calloc_3d__(size_t d1, size_t d2, size_t d3, /* In: #elems in the dims */
177 size_t elemsize, /* In: Size (#bytes) per element */
178 const char *caller_file, int caller_line); /* In */
181 * Allocate a 34D array and return ptr to it.
182 * The data area is allocated in one block so it can also be treated as a 1-D array.
185 void ****__ckd_calloc_4d__(size_t d1,
194 * Overlay a 3-D array over a previously allocated storage area.
197 void * __ckd_alloc_3d_ptr(size_t d1,
206 * Overlay a s-D array over a previously allocated storage area.
209 void *__ckd_alloc_2d_ptr(size_t d1,
217 * Test and free a 1-D array
220 void ckd_free(void *ptr);
223 * Free a 2-D array (ptr) previously allocated by ckd_calloc_2d
226 void ckd_free_2d(void *ptr);
229 * Free a 3-D array (ptr) previously allocated by ckd_calloc_3d
232 void ckd_free_3d(void *ptr);
235 * Free a 4-D array (ptr) previously allocated by ckd_calloc_4d
238 void ckd_free_4d(void *ptr);
241 * Macros to simplify the use of above functions.
242 * One should use these, rather than target functions directly.
246 * Macro for __ckd_calloc__
248 #define ckd_calloc(n,sz) __ckd_calloc__((n),(sz),__FILE__,__LINE__)
251 * Macro for __ckd_malloc__
253 #define ckd_malloc(sz) __ckd_malloc__((sz),__FILE__,__LINE__)
256 * Macro for __ckd_realloc__
258 #define ckd_realloc(ptr,sz) __ckd_realloc__(ptr,(sz),__FILE__,__LINE__)
261 * Macro for __ckd_salloc__
264 #define ckd_salloc(ptr) __ckd_salloc__(ptr,__FILE__,__LINE__)
267 * Macro for __ckd_calloc_2d__
270 #define ckd_calloc_2d(d1,d2,sz) __ckd_calloc_2d__((d1),(d2),(sz),__FILE__,__LINE__)
273 * Macro for __ckd_calloc_3d__
276 #define ckd_calloc_3d(d1,d2,d3,sz) __ckd_calloc_3d__((d1),(d2),(d3),(sz),__FILE__,__LINE__)
279 * Macro for __ckd_calloc_4d__
281 #define ckd_calloc_4d(d1, d2, d3, d4, s) __ckd_calloc_4d__((d1), (d2), (d3), (d4), (s), __FILE__, __LINE__)
284 * Macro for __ckd_alloc_2d_ptr__
287 #define ckd_alloc_2d_ptr(d1, d2, bf, sz) __ckd_alloc_2d_ptr((d1), (d2), (bf), (sz), __FILE__, __LINE__)
290 * Free only the pointer arrays allocated with ckd_alloc_2d_ptr().
292 #define ckd_free_2d_ptr(bf) ckd_free(bf)
295 * Macro for __ckd_alloc_3d_ptr__
298 #define ckd_alloc_3d_ptr(d1, d2, d3, bf, sz) __ckd_alloc_3d_ptr((d1), (d2), (d3), (bf), (sz), __FILE__, __LINE__)
301 * Free only the pointer arrays allocated with ckd_alloc_3d_ptr().
303 #define ckd_free_3d_ptr(bf) ckd_free_2d(bf)