1/*****************************************************************************/
2/* */
3/* xmalloc.c */
4/* */
5/* Memory allocation subroutines */
6/* */
7/* */
8/* */
9/* (C) 2000-2006 Ullrich von Bassewitz */
10/* Römerstrasse 52 */
11/* D-70794 Filderstadt */
12/* EMail: uz@cc65.org */
13/* */
14/* */
15/* This software is provided 'as-is', without any expressed or implied */
16/* warranty. In no event will the authors be held liable for any damages */
17/* arising from the use of this software. */
18/* */
19/* Permission is granted to anyone to use this software for any purpose, */
20/* including commercial applications, and to alter it and redistribute it */
21/* freely, subject to the following restrictions: */
22/* */
23/* 1. The origin of this software must not be misrepresented; you must not */
24/* claim that you wrote the original software. If you use this software */
25/* in a product, an acknowledgment in the product documentation would be */
26/* appreciated but is not required. */
27/* 2. Altered source versions must be plainly marked as such, and must not */
28/* be misrepresented as being the original software. */
29/* 3. This notice may not be removed or altered from any source */
30/* distribution. */
31/* */
32/*****************************************************************************/
33
34
35
36#include <stdlib.h>
37#include <string.h>
38
39/* common */
40#include "abend.h"
41#include "debugflag.h"
42#include "xmalloc.h"
43
44
45
46/*****************************************************************************/
47/* code */
48/*****************************************************************************/
49
50
51
52void* xmalloc (size_t Size)
53/* Allocate memory, check for out of memory condition. Do some debugging */
54{
55 void* P = 0;
56
57 /* Allow zero sized requests and return NULL in this case */
58 if (Size) {
59
60 /* Allocate memory */
61 P = malloc (Size);
62
63 /* Check for errors */
64 if (P == 0) {
65 AbEnd ("Out of memory - requested block size = %lu",
66 (unsigned long) Size);
67 }
68 }
69
70 /* Return a pointer to the block */
71 return P;
72}
73
74
75
76void* xrealloc (void* P, size_t Size)
77/* Reallocate a memory block, check for out of memory */
78{
79 /* Reallocate the block */
80 void* N = realloc (P, Size);
81
82 /* Check for errors */
83 if (N == 0 && Size != 0) {
84 AbEnd ("Out of memory in realloc - requested block size = %lu", (unsigned long) Size);
85 }
86
87 /* Return the pointer to the new block */
88 return N;
89}
90
91
92
93void xfree (void* Block)
94/* Free the block, do some debugging */
95{
96 free (Block);
97}
98
99
100
101char* xstrdup (const char* S)
102/* Duplicate a string on the heap. The function checks for out of memory */
103{
104 /* Allow dup'ing of NULL strings */
105 if (S) {
106
107 /* Get the length of the string */
108 unsigned Len = strlen (S) + 1;
109
110 /* Allocate memory and return a copy */
111 return memcpy (xmalloc (Len), S, Len);
112
113 } else {
114
115 /* Return a NULL pointer */
116 return 0;
117
118 }
119}
120
121
122
123void* xdup (const void* Buf, size_t Size)
124/* Create a copy of Buf on the heap and return a pointer to it. */
125{
126 return memcpy (xmalloc (Size), Buf, Size);
127}
128