Edinburgh Speech Tools  2.1-release
walloc.c
Go to the documentation of this file.
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 <stdint.h>
44 #include <stddef.h>
45 #include <sys/types.h>
46 #include "EST_unix.h"
47 #include <string.h>
48 #include "EST_cutils.h"
49 
50 void *safe_walloc(size_t size)
51 {
52  void *p;
53 
54  if (size == 0)
55  /* Some mallocs return NULL for size 0, which means you can't tell
56  if it failed or not. So we'll avoid that problem by never
57  asking for 0 bytes */
58  p = malloc(1);
59  else
60  p = malloc(size);
61 
62  if (p == NULL)
63  {
64  fprintf(stderr,"WALLOC: failed to malloc %zu bytes\n",size);
65  exit(-1); /* I'd rather not do this but this is the only safe */
66  /* thing to do */
67  }
68 
69  return p;
70 }
71 
72 void *safe_wrealloc(void *ptr, size_t size)
73 {
74  void *p;
75 
76  if (ptr == 0)
77  p = safe_walloc(size);
78  else if (size == 0)
79  /* Some mallocs return NULL for size 0, which means you can't tell
80  if it failed or not. So we'll avoid that problem by never
81  asking for 0 bytes */
82  p = realloc(ptr, 1);
83  else
84  p = realloc(ptr, size);
85 
86  if ((p == NULL) && (size != 0))
87  {
88  fprintf(stderr,"WREALLOC: failed to malloc %zu bytes\n",size);
89  exit(-1); /* I'd rather not do this but this is the only safe */
90  /* thing to do */
91  }
92 
93  return p;
94 }
95 
96 void *safe_wcalloc(size_t num, size_t size)
97 {
98  void *p;
99  /* Some mallocs return NULL for size 0, which means you can't tell
100  if it failed or not. So we'll avoid that problem by never
101  asking for 0 bytes */
102  if (num == 0 || size == 0) {
103  p = calloc(1, 1);
104  } else {
105  p = calloc(num, size);
106  }
107 
108  if (p == NULL)
109  {
110  fprintf(stderr,"WCALLOC: failed to calloc %zu elements of %zu bytes each\n",num, size);
111  exit(-1); /* I'd rather not do this but this is the only safe */
112  /* thing to do */
113  }
114  return p;
115 }
116 
117 char *wstrdup(const char *s)
118 {
119  size_t strsize=strlen(s);
120  if (strsize < SIZE_MAX) {
121  strsize++; /* We can finish copy with a zero */
122  } else {
123  fprintf(stderr,"String is not null terminated! (too long)\n");
124  exit(-1);
125  }
126  char *t = safe_wcalloc(sizeof(char), strsize);
127  memcpy(t, s, strsize);
128  return t;
129 }
130 
131 void wfree(void *p)
132 {
133  if (p != NULL)
134  free(p);
135 }
136 
void wfree(void *p)
Definition: walloc.c:131
NULL
Definition: EST_WFST.cc:55
char * wstrdup(const char *s)
Definition: walloc.c:117
void * safe_walloc(size_t size)
Definition: walloc.c:50
void * safe_wrealloc(void *ptr, size_t size)
Definition: walloc.c:72
void * safe_wcalloc(size_t num, size_t size)
Definition: walloc.c:96