]> git.sur5r.net Git - cc65/blob - src/common/xmalloc.c
Moved the chiplib module into chip.c
[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 "debugflag.h"
41 #include "xmalloc.h"
42
43
44
45 /*****************************************************************************/
46 /*                                   code                                    */
47 /*****************************************************************************/
48
49
50
51 void* xmalloc (size_t Size)
52 /* Allocate memory, check for out of memory condition. Do some debugging */
53 {
54     /* Allocate memory */
55     void* P = malloc (Size);
56
57     /* Check for errors */
58     if (P == 0 && Size != 0) {
59         AbEnd ("Out of memory - requested block size = %lu", (unsigned long) Size);
60     }
61
62     /* Return a pointer to the block */
63     return P;
64 }
65
66
67
68 void* xrealloc (void* P, size_t Size)
69 /* Reallocate a memory block, check for out of memory */
70 {
71     /* Reallocate the block */
72     void* N = realloc (P, Size);
73
74     /* Check for errors */
75     if (N == 0 && Size != 0) {
76         AbEnd ("Out of memory in realloc - requested block size = %lu", (unsigned long) Size);
77     }
78
79     /* Return the pointer to the new block */
80     return N;
81 }
82
83
84
85 void xfree (void* Block)
86 /* Free the block, do some debugging */
87 {
88     free (Block);
89 }
90
91
92
93 char* xstrdup (const char* S)
94 /* Duplicate a string on the heap. The function checks for out of memory */
95 {
96     /* Allow dup'ing of NULL strings */
97     if (S) {
98
99         /* Get the length of the string */
100         unsigned Len = strlen (S) + 1;
101
102         /* Allocate memory and return a copy */
103         return memcpy (xmalloc (Len), S, Len);
104
105     } else {
106
107         /* Return a NULL pointer */
108         return 0;
109
110     }
111 }
112
113
114
115