1 /* ====================================================================
2 * Copyright (c) 1999-2001 Carnegie Mellon University. All rights
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
17 * This work was supported in part by funding from the Defense Advanced
18 * Research Projects Agency and the National Science Foundation of the
19 * United States of America, and the CMU Sphinx Speech Consortium.
21 * THIS SOFTWARE IS PROVIDED BY CARNEGIE MELLON UNIVERSITY ``AS IS'' AND
22 * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL CARNEGIE MELLON UNIVERSITY
25 * NOR ITS EMPLOYEES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * ====================================================================
36 /* $Header: /cvsroot/cmusphinx/sphinx2/src/libsphinx2/CM_funcs.c,v 1.8 2004/05/18 19:02:40 egouvea Exp $
38 * CMPSL functions to make error handling a little easier.
46 #include <posixwin32.h>
49 #include <sys/types.h>
52 #include "CM_macros.h"
55 _CM_fopen (char const *file
, char const *mode
, char const *srcfile
, int32 srcline
)
57 FILE *fs
= fopen (file
, mode
);
60 fprintf (stdout
, "%s(%d): fopen(%s,%s) failed\n",
61 srcfile
, srcline
, file
, mode
);
70 _CM_fopenp (char const *dirl
, char const *file
, char const *mode
,
71 char const *srcfile
, int32 srcline
)
76 sprintf (buffer
, "%s/%s", dirl
, file
);
77 fs
= (FILE *) fopen (buffer
, mode
);
80 fprintf (stdout
, "%s(%d): fopen(%s,%s) failed\n", srcfile
, srcline
,
89 void *_CM_calloc (int32 cnt
, int32 size
, char const *file
, int32 line
)
96 ret
= calloc ((size_t)cnt
, (size_t)size
);
98 fprintf (stdout
, "%s(%d): calloc(%d,%d) failed\n", file
, line
, cnt
, size
);
104 void *_CM_2dcalloc (int32 rcnt
, int32 ccnt
, int32 size
,
105 char const *srcfile
, int32 srcline
)
106 /*------------------------------------------------------------*
107 * DESCRIPTION - allocate row pointers and data in one chunk
114 if ((rcnt
== 0) || (ccnt
== 0))
117 ret
= calloc ((size_t)(rcnt
* ccnt
* size
) +
118 rcnt
* sizeof(caddr_t
), 1);
119 rowPtr
= (caddr_t
*) ret
;
122 fprintf (stdout
, "%s(%d): CM_2dcalloc(%d,%d,%d) failed\n", srcfile
, srcline
,
127 for (r
= 0; r
< rcnt
; r
++)
128 rowPtr
[r
] = (caddr_t
)(ret
+ (rcnt
* sizeof(caddr_t
)) + (r
* ccnt
* size
));
133 void *_CM_3dcalloc (int32 lcnt
, int32 rcnt
, int32 ccnt
, int32 size
,
134 char const *srcfile
, int32 srcline
)
135 /*------------------------------------------------------------*
136 * DESCRIPTION - allocate row pointers and data in one chunk
144 ret
= (char *) calloc ((size_t)(lcnt
* rcnt
* ccnt
* size
) +
145 (size_t)(lcnt
* rcnt
* sizeof(caddr_t
)) +
146 (size_t)(lcnt
* sizeof(caddr_t
)), 1);
147 rowPtr
= (caddr_t
*) ret
;
148 lvlPtr
= (caddr_t
*) ret
;
151 fprintf (stdout
, "%s(%d): CM_3dcalloc(%d,%d,%d) failed\n",
152 srcfile
, srcline
, rcnt
, ccnt
, size
);
156 for (l
= 0; l
< lcnt
; l
++) {
157 lvlPtr
[l
] = ret
+ (lcnt
* sizeof(caddr_t
)) + (rcnt
* sizeof(caddr_t
) * l
);
158 rowPtr
= (caddr_t
*) lvlPtr
[l
];
159 for (r
= 0; r
< rcnt
; r
++) {
160 rowPtr
[r
] = (caddr_t
)(ret
+ (lcnt
* sizeof(caddr_t
)) +
161 (lcnt
* rcnt
* sizeof(caddr_t
)) +
162 (l
* rcnt
* ccnt
* size
) +
170 void *_CM_recalloc (void *ptr
, int32 cnt
, int32 size
,
171 char const *srcfile
, int32 srcline
)
176 ret
= calloc ((size_t)cnt
, (size_t)size
);
178 ret
= realloc (ptr
, (size_t)size
* (size_t)cnt
);
181 fprintf (stdout
, "%s(%d): recalloc(0x%lX,%d,%d) failed\n", srcfile
, srcline
,
182 (unsigned long) ptr
, cnt
, size
);