Edinburgh Speech Tools 2.4-release
walloc.c
1 /************************************************************************/
2 /* */
3 /* Centre for Speech Technology Research */
4 /* University of Edinburgh, UK */
5 /* Copyright (c) 1996,1997 */
6 /* All Rights Reserved. */
7 /* */
8 /* Permission is hereby granted, free of charge, to use and distribute */
9 /* this software and its documentation without restriction, including */
10 /* without limitation the rights to use, copy, modify, merge, publish, */
11 /* distribute, sublicense, and/or sell copies of this work, and to */
12 /* permit persons to whom this work is furnished to do so, subject to */
13 /* the following conditions: */
14 /* 1. The code must retain the above copyright notice, this list of */
15 /* conditions and the following disclaimer. */
16 /* 2. Any modifications must be clearly marked as such. */
17 /* 3. Original authors' names are not deleted. */
18 /* 4. The authors' names are not used to endorse or promote products */
19 /* derived from this software without specific prior written */
20 /* permission. */
21 /* */
22 /* THE UNIVERSITY OF EDINBURGH AND THE CONTRIBUTORS TO THIS WORK */
23 /* DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING */
24 /* ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT */
25 /* SHALL THE UNIVERSITY OF EDINBURGH NOR THE CONTRIBUTORS BE LIABLE */
26 /* FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES */
27 /* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN */
28 /* AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, */
29 /* ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF */
30 /* THIS SOFTWARE. */
31 /* */
32 /************************************************************************/
33 /* Author: Richard Caley (rjc@cstr.ed.ac.uk) */
34 /* Date: Wed Apr 9 1997 */
35 /************************************************************************/
36 /* */
37 /* Allocation routines which check for errors. */
38 /* */
39 /************************************************************************/
40
41#include <stdio.h>
42#include <stdlib.h>
43#include <sys/types.h>
44#include "EST_unix.h"
45#include <string.h>
46#include "EST_cutils.h"
47
48/* #define CST_DEBUG_MALLOC 1 */
49
50#ifdef CST_DEBUG_MALLOC
51/* use the debug malloc in flite */
52#include "cst_alloc.h"
53
54void *safe_walloc(int size)
55{
56 return cst_safe_alloc(size);
57}
58void *safe_wrealloc(void *ptr, int size)
59{
60 return cst_safe_realloc(ptr,size);
61}
62void *safe_wcalloc(int size)
63{
64 return cst_safe_calloc(size);
65}
66void wfree(void *p)
67{
68 cst_free(p);
69 return;
70}
71char *wstrdup(const char *s)
72{
73 char *t = cst_alloc(char,strlen(s)+1);
74 strcpy(t,s);
75 return t;
76}
77
78void debug_memory_summary(void)
79{
80 cst_alloc_debug_summary();
81}
82
83#else
84void *safe_walloc(int size)
85{
86 char *p;
87
88 if (size == 0)
89 /* Some mallocs return NULL for size 0, which means you can't tell
90 if it failed or not. So we'll avoid that problem by never
91 asking for 0 bytes */
92 p = calloc(1,1);
93 else
94 p = calloc(size,1);
95
96 if (p == NULL)
97 {
98 fprintf(stderr,"WALLOC: failed to malloc %d bytes\n",size);
99 exit(-1); /* I'd rather not do this but this is the only safe */
100 /* thing to do */
101 }
102
103 return p;
104}
105
106void *safe_wrealloc(void *ptr, int size)
107{
108 char *p;
109
110 if (ptr == 0)
111 p = safe_walloc(size);
112 else if (size == 0)
113 /* Some mallocs return NULL for size 0, which means you can't tell
114 if it failed or not. So we'll avoid that problem by never
115 asking for 0 bytes */
116 p = realloc(ptr, 1);
117 else
118 p = realloc(ptr, size);
119
120 if ((p == NULL) && (size != 0))
121 {
122 fprintf(stderr,"WREALLOC: failed to malloc %d bytes\n",size);
123 exit(-1); /* I'd rather not do this but this is the only safe */
124 /* thing to do */
125 }
126
127 return p;
128}
129
130void *safe_wcalloc(int size)
131{
132 char *p = safe_walloc(size);
133
134 memset(p,0,size);
135
136 return p;
137}
138
139char *wstrdup(const char *s)
140{
141 char *t = walloc(char,strlen(s)+1);
142 strcpy(t,s);
143 return t;
144}
145
146void wfree(void *p)
147{
148 if (p != NULL)
149 free(p);
150}
151
152#endif