1 /******************************************************************************
2 Memory.c
3
4 Change Control: DDMMYYYY
5 Michael Still File created 09012001
6
7 Purpose:
8 Abstract the out of memory tests so that calling functions can asusme that
9 memory was returned by malloc et al.
10
11 Copyright (C) Michael Still 2000 - 2002
12
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
17
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 ******************************************************************************/
27
28 #if defined _WINDOWS
29 #include "stdafx.h"
30 #else
31 #include <panda/constants.h>
32 #include <panda/functions.h>
33 #endif
34
35 #include <stdio.h>
36
37 /******************************************************************************
38 DOCBOOK START
39
40 FUNCTION panda_xmalloc
41 PURPOSE allocate some memory
42
43 SYNOPSIS START
44 #include<panda/constants.h>
45 #include<panda/functions.h>
46 void *panda_xmalloc (size_t size);
47 SYNOPSIS END
48
49 DESCRIPTION <command>PANDA INTERNAL</command>. Allocate some memory, performing all of the error checking so we don't have to elsewhere.
50
51 RETURNS A void * to the memory allocated.
52
53 EXAMPLE START
54 #include<panda/constants.h>
55 #include<panda/functions.h>
56
57 char *memory;
58
59 memory = (char *) panda_xmalloc(42);
60 EXAMPLE END
61 DOCBOOK END
62 ******************************************************************************/
63
64 void *
panda_xmalloc(size_t size)65 panda_xmalloc (size_t size)
66 {
67 void *buffer;
68
69 if ((buffer = malloc (size)) == NULL)
70 {
71 panda_error (panda_true, "panda_xmalloc failed to allocate memory");
72 }
73
74 return buffer;
75 }
76
77 /******************************************************************************
78 DOCBOOK START
79
80 FUNCTION panda_xrealloc
81 PURPOSE allocate some memory, resizing it if already exists
82
83 SYNOPSIS START
84 #include<panda/constants.h>
85 #include<panda/functions.h>
86 void *panda_xmalloc (void *now, size_t size);
87 SYNOPSIS END
88
89 DESCRIPTION <command>PANDA INTERNAL</command>. Allocate some memory, performing all of the error checking so we don't have to elsewhere. Resize the memory if it already exists.
90
91 RETURNS A void * to the memory allocated.
92
93 EXAMPLE START
94 #include<panda/constants.h>
95 #include<panda/functions.h>
96
97 char *memory;
98
99 memory = (char *) panda_xrealloc(memory, 42);
100 EXAMPLE END
101 DOCBOOK END
102 ******************************************************************************/
103
104 void *
panda_xrealloc(void * memory,size_t size)105 panda_xrealloc (void *memory, size_t size)
106 {
107 void *buffer;
108
109 if ((buffer = realloc (memory, size)) == NULL)
110 {
111 panda_error (panda_true, "panda_xrealloc failed to allocate memory");
112 }
113
114 return buffer;
115 }
116
117 /******************************************************************************
118 DOCBOOK START
119
120 FUNCTION panda_xfree
121 PURPOSE deallocate memory in a safe manner
122
123 SYNOPSIS START
124 #include<panda/constants.h>
125 #include<panda/functions.h>
126 void panda_xfree (void *p);
127 SYNOPSIS END
128
129 DESCRIPTION <command>PANDA INTERNAL</command>. Deallocate a block of memory in a safe manner. For instance, some systems will not allow you to free a NULL pointer...
130
131 RETURNS Nothing
132
133 EXAMPLE START
134 #include<panda/constants.h>
135 #include<panda/functions.h>
136
137 char *p;
138 p = panda_xmalloc(42);
139 panda_xrealloc(p);
140 EXAMPLE END
141 DOCBOOK END
142 ******************************************************************************/
143
144 void
panda_xfree(void * memory)145 panda_xfree (void *memory)
146 {
147 if (memory != NULL)
148 free (memory);
149 }
150