2000-05-28 13:40:48 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
/* */
|
2000-06-14 09:32:22 +00:00
|
|
|
/* xmalloc.c */
|
|
|
|
/* */
|
|
|
|
/* Memory allocation subroutines */
|
|
|
|
/* */
|
|
|
|
/* */
|
|
|
|
/* */
|
2001-03-26 21:46:37 +00:00
|
|
|
/* (C) 2000-2001 Ullrich von Bassewitz */
|
|
|
|
/* Wacholderweg 14 */
|
|
|
|
/* D-70597 Stuttgart */
|
|
|
|
/* EMail: uz@musoftware.de */
|
2000-06-14 09:32:22 +00:00
|
|
|
/* */
|
|
|
|
/* */
|
2000-05-28 13:40:48 +00:00
|
|
|
/* This software is provided 'as-is', without any expressed or implied */
|
|
|
|
/* warranty. In no event will the authors be held liable for any damages */
|
|
|
|
/* arising from the use of this software. */
|
2000-06-14 09:32:22 +00:00
|
|
|
/* */
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Permission is granted to anyone to use this software for any purpose, */
|
|
|
|
/* including commercial applications, and to alter it and redistribute it */
|
|
|
|
/* freely, subject to the following restrictions: */
|
2000-06-14 09:32:22 +00:00
|
|
|
/* */
|
2000-05-28 13:40:48 +00:00
|
|
|
/* 1. The origin of this software must not be misrepresented; you must not */
|
|
|
|
/* claim that you wrote the original software. If you use this software */
|
|
|
|
/* in a product, an acknowledgment in the product documentation would be */
|
|
|
|
/* appreciated but is not required. */
|
|
|
|
/* 2. Altered source versions must be plainly marked as such, and must not */
|
|
|
|
/* be misrepresented as being the original software. */
|
|
|
|
/* 3. This notice may not be removed or altered from any source */
|
|
|
|
/* distribution. */
|
2000-06-14 09:32:22 +00:00
|
|
|
/* */
|
2000-05-28 13:40:48 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2000-06-14 09:32:22 +00:00
|
|
|
#include "abend.h"
|
|
|
|
#include "xmalloc.h"
|
2000-05-28 13:40:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/*****************************************************************************/
|
2000-06-14 09:32:22 +00:00
|
|
|
/* code */
|
2000-05-28 13:40:48 +00:00
|
|
|
/*****************************************************************************/
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-06-14 09:32:22 +00:00
|
|
|
void* xmalloc (size_t Size)
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Allocate memory, check for out of memory condition. Do some debugging */
|
|
|
|
{
|
2000-06-14 09:32:22 +00:00
|
|
|
/* Allocate memory */
|
|
|
|
void* P = malloc (Size);
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2000-06-14 09:32:22 +00:00
|
|
|
/* Check for errors */
|
|
|
|
if (P == 0 && Size != 0) {
|
|
|
|
AbEnd ("Out of memory - requested block size = %lu", (unsigned long) Size);
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Return a pointer to the block */
|
2000-06-14 09:32:22 +00:00
|
|
|
return P;
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-03-26 21:46:37 +00:00
|
|
|
void* xrealloc (void* P, size_t Size)
|
|
|
|
/* Reallocate a memory block, check for out of memory */
|
|
|
|
{
|
|
|
|
/* Reallocate the block */
|
|
|
|
void* N = realloc (P, Size);
|
|
|
|
|
|
|
|
/* Check for errors */
|
|
|
|
if (N == 0 && Size != 0) {
|
|
|
|
AbEnd ("Out of memory in realloc - requested block size = %lu", (unsigned long) Size);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Return the pointer to the new block */
|
|
|
|
return N;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-06-14 09:32:22 +00:00
|
|
|
void xfree (const void* Block)
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Free the block, do some debugging */
|
|
|
|
{
|
2000-06-14 09:32:22 +00:00
|
|
|
free ((void*) Block);
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-06-14 09:32:22 +00:00
|
|
|
char* xstrdup (const char* S)
|
2000-05-28 13:40:48 +00:00
|
|
|
/* Duplicate a string on the heap. The function checks for out of memory */
|
|
|
|
{
|
2001-03-09 21:59:23 +00:00
|
|
|
/* Allow dup'ing of NULL strings */
|
|
|
|
if (S) {
|
2000-05-28 13:40:48 +00:00
|
|
|
|
2001-03-09 21:59:23 +00:00
|
|
|
/* Get the length of the string */
|
|
|
|
unsigned Len = strlen (S) + 1;
|
|
|
|
|
|
|
|
/* Allocate memory and return a copy */
|
|
|
|
return memcpy (xmalloc (Len), S, Len);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
/* Return a NULL pointer */
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
}
|
2000-05-28 13:40:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-06-14 09:32:22 +00:00
|
|
|
|