2000-08-28 02:34:02 +08:00
|
|
|
/** \ingroup rpmio
|
|
|
|
* \file rpmio/rpmmalloc.c
|
|
|
|
*/
|
|
|
|
|
1999-09-23 00:34:08 +08:00
|
|
|
#include "system.h"
|
2000-12-13 04:03:45 +08:00
|
|
|
#include "debug.h"
|
1999-09-23 00:34:08 +08:00
|
|
|
|
2000-04-13 18:41:31 +08:00
|
|
|
#if !defined(EXIT_FAILURE)
|
|
|
|
#define EXIT_FAILURE 1
|
|
|
|
#endif
|
|
|
|
|
2009-09-16 22:28:22 +08:00
|
|
|
static void *vmefail(size_t size)
|
1999-09-23 00:34:08 +08:00
|
|
|
{
|
2000-11-01 00:18:34 +08:00
|
|
|
fprintf(stderr, _("memory alloc (%u bytes) returned NULL.\n"), (unsigned)size);
|
1999-09-29 23:59:55 +08:00
|
|
|
exit(EXIT_FAILURE);
|
1999-09-23 00:34:08 +08:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2009-09-16 22:28:22 +08:00
|
|
|
void * rmalloc (size_t size)
|
1999-09-23 00:34:08 +08:00
|
|
|
{
|
2000-03-01 07:22:44 +08:00
|
|
|
register void *value;
|
|
|
|
if (size == 0) size++;
|
|
|
|
value = malloc (size);
|
1999-09-23 00:34:08 +08:00
|
|
|
if (value == 0)
|
2000-04-26 03:41:37 +08:00
|
|
|
value = vmefail(size);
|
1999-09-23 00:34:08 +08:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2009-09-16 22:28:22 +08:00
|
|
|
void * rcalloc (size_t nmemb, size_t size)
|
1999-09-23 00:34:08 +08:00
|
|
|
{
|
2000-03-01 07:22:44 +08:00
|
|
|
register void *value;
|
|
|
|
if (size == 0) size++;
|
|
|
|
if (nmemb == 0) nmemb++;
|
|
|
|
value = calloc (nmemb, size);
|
1999-09-23 00:34:08 +08:00
|
|
|
if (value == 0)
|
2000-04-26 03:41:37 +08:00
|
|
|
value = vmefail(size);
|
1999-09-23 00:34:08 +08:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2009-09-16 22:28:22 +08:00
|
|
|
void * rrealloc (void *ptr, size_t size)
|
1999-09-23 00:34:08 +08:00
|
|
|
{
|
2000-03-01 07:22:44 +08:00
|
|
|
register void *value;
|
|
|
|
if (size == 0) size++;
|
|
|
|
value = realloc (ptr, size);
|
1999-09-23 00:34:08 +08:00
|
|
|
if (value == 0)
|
2000-04-26 03:41:37 +08:00
|
|
|
value = vmefail(size);
|
1999-09-23 00:34:08 +08:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2009-09-16 22:28:22 +08:00
|
|
|
char * rstrdup (const char *str)
|
1999-09-23 00:34:08 +08:00
|
|
|
{
|
2000-04-26 03:41:37 +08:00
|
|
|
size_t size = strlen(str) + 1;
|
|
|
|
char *newstr = (char *) malloc (size);
|
1999-09-23 00:34:08 +08:00
|
|
|
if (newstr == 0)
|
2000-04-26 03:41:37 +08:00
|
|
|
newstr = (char *) vmefail(size);
|
1999-09-23 00:34:08 +08:00
|
|
|
strcpy (newstr, str);
|
|
|
|
return newstr;
|
|
|
|
}
|
|
|
|
|
2009-09-16 22:28:22 +08:00
|
|
|
void * rfree (void *ptr)
|
|
|
|
{
|
|
|
|
free(ptr);
|
|
|
|
return NULL;
|
|
|
|
}
|