]> git.sur5r.net Git - cc65/blob - src/common/xmalloc.c
Added VIC20 port changes from Steve Schmidtke
[cc65] / src / common / xmalloc.c
1 /*****************************************************************************/
2 /*                                                                           */
3 /*                                 xmalloc.c                                 */
4 /*                                                                           */
5 /*                       Memory allocation subroutines                       */
6 /*                                                                           */
7 /*                                                                           */
8 /*                                                                           */
9 /* (C) 2000-2001 Ullrich von Bassewitz                                       */
10 /*               Wacholderweg 14                                             */
11 /*               D-70597 Stuttgart                                           */
12 /* EMail:        uz@musoftware.de                                            */
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 #include "abend.h"
40 #include "xmalloc.h"
41
42
43
44 /*****************************************************************************/
45 /*                                   code                                    */
46 /*****************************************************************************/
47
48
49
50 void* xmalloc (size_t Size)
51 /* Allocate memory, check for out of memory condition. Do some debugging */
52 {
53     /* Allocate memory */
54     void* P = malloc (Size);
55
56     /* Check for errors */
57     if (P == 0 && Size != 0) {
58         AbEnd ("Out of memory - requested block size = %lu", (unsigned long) Size);
59     }
60
61     /* Return a pointer to the block */
62     return P;
63 }
64
65
66
67 void* xrealloc (void* P, size_t Size)
68 /* Reallocate a memory block, check for out of memory */
69 {
70     /* Reallocate the block */
71     void* N = realloc (P, Size);
72
73     /* Check for errors */
74     if (N == 0 && Size != 0) {
75         AbEnd ("Out of memory in realloc - requested block size = %lu", (unsigned long) Size);
76     }
77
78     /* Return the pointer to the new block */
79     return N;
80 }
81
82
83
84 void xfree (const void* Block)
85 /* Free the block, do some debugging */
86 {
87     free ((void*) Block);
88 }
89
90
91
92 char* xstrdup (const char* S)
93 /* Duplicate a string on the heap. The function checks for out of memory */
94 {
95     /* Allow dup'ing of NULL strings */
96     if (S) {
97
98         /* Get the length of the string */
99         unsigned Len = strlen (S) + 1;
100
101         /* Allocate memory and return a copy */
102         return memcpy (xmalloc (Len), S, Len);
103
104     } else {
105
106         /* Return a NULL pointer */
107         return 0;
108
109     }
110 }
111
112
113
114