1 /*****************************************************************************/
5 /* Memory allocation subroutines */
9 /* (C) 2000-2006 Ullrich von Bassewitz */
10 /* Römerstrasse 52 */
11 /* D-70794 Filderstadt */
12 /* EMail: uz@cc65.org */
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. */
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: */
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 */
32 /*****************************************************************************/
41 #include "debugflag.h"
46 /*****************************************************************************/
48 /*****************************************************************************/
52 void* xmalloc (size_t Size)
53 /* Allocate memory, check for out of memory condition. Do some debugging */
57 /* Allow zero sized requests and return NULL in this case */
63 /* Check for errors */
65 AbEnd ("Out of memory - requested block size = %lu",
66 (unsigned long) Size);
70 /* Return a pointer to the block */
76 void* xrealloc (void* P, size_t Size)
77 /* Reallocate a memory block, check for out of memory */
79 /* Reallocate the block */
80 void* N = realloc (P, Size);
82 /* Check for errors */
83 if (N == 0 && Size != 0) {
84 AbEnd ("Out of memory in realloc - requested block size = %lu", (unsigned long) Size);
87 /* Return the pointer to the new block */
93 void xfree (void* Block)
94 /* Free the block, do some debugging */
101 char* xstrdup (const char* S)
102 /* Duplicate a string on the heap. The function checks for out of memory */
104 /* Allow dup'ing of NULL strings */
107 /* Get the length of the string */
108 unsigned Len = strlen (S) + 1;
110 /* Allocate memory and return a copy */
111 return memcpy (xmalloc (Len), S, Len);
115 /* Return a NULL pointer */
123 void* xdup (const void* Buf, size_t Size)
124 /* Create a copy of Buf on the heap and return a pointer to it. */
126 return memcpy (xmalloc (Size), Buf, Size);