1 /***************************************************************************
2 *
3 * $Id: safe.c 152 2010-10-10 14:17:37Z Michael.McTernan $
4 *
5 * This file is part of timgen, a timing diagram renderer.
6 * Copyright (C) 2010 Michael C McTernan, Michael.McTernan.2001@cs.bris.ac.uk
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 **************************************************************************/
22
23 #define FILE_NAME SAFE
24
25 /*****************************************************************************
26 * Header Files
27 *****************************************************************************/
28
29 #include <stdlib.h>
30 #include <string.h>
31 #include <stdio.h>
32 #include "mscgen_config.h"
33 #include "mscgen_safe.h"
34
35 /*****************************************************************************
36 * Preprocessor Macros & Constants
37 *****************************************************************************/
38
39 /*****************************************************************************
40 * Typedefs
41 *****************************************************************************/
42
43 /*****************************************************************************
44 * Local Variable Definitions
45 *****************************************************************************/
46
47 /*****************************************************************************
48 * Global Variable Definitions
49 *****************************************************************************/
50
51 /*****************************************************************************
52 * Local Function Definitions
53 *****************************************************************************/
54
checkNotNull(void * p,const char * message)55 static void checkNotNull(void *p, const char *message)
56 {
57 if(!p)
58 {
59 fprintf(stderr, "Fatal error: %s\n", message);
60 exit(EXIT_FAILURE);
61 }
62 }
63
64 /*****************************************************************************
65 * Global Function Definitions
66 *****************************************************************************/
67
realloc_s(void * ptr,size_t size)68 void *realloc_s(void *ptr, size_t size)
69 {
70 void *r = realloc(ptr, size);
71
72 checkNotNull(r, "realloc() failed");
73
74 return r;
75 }
76
malloc_s(size_t size)77 void *malloc_s(size_t size)
78 {
79 void *r = malloc(size);
80
81 checkNotNull(r, "malloc() failed");
82
83 return r;
84 }
85
zalloc_s(size_t size)86 void *zalloc_s(size_t size)
87 {
88 void *r = malloc(size);
89
90 checkNotNull(r, "malloc() failed");
91 memset(r, 0, size);
92
93 return r;
94 }
95
strdup_s(const char * s)96 char *strdup_s(const char *s)
97 {
98 char *r = strdup(s);
99
100 checkNotNull(r, "strdup() failed");
101
102 return r;
103 }
104
mscgen_getenv_s(const char * name)105 const char *mscgen_getenv_s(const char *name)
106 {
107 char *r = getenv(name);
108
109 if(r == NULL) r = "";
110
111 return r;
112 }
113
114 /*****************************************************************************
115 * Unit Test Support
116 *****************************************************************************/
117
118
119 /* END OF FILE */
120