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"
|
2009-12-23 18:55:45 +08:00
|
|
|
|
2010-01-05 21:04:20 +08:00
|
|
|
#include <stdlib.h>
|
2010-01-05 21:17:34 +08:00
|
|
|
#include <string.h>
|
2010-01-05 22:06:02 +08:00
|
|
|
#include <stdio.h>
|
2022-02-08 15:23:58 +08:00
|
|
|
#include <stdint.h>
|
2009-12-23 18:55:45 +08:00
|
|
|
|
2000-12-13 04:03:45 +08:00
|
|
|
#include "debug.h"
|
1999-09-23 00:34:08 +08:00
|
|
|
|
2009-09-17 16:36:10 +08:00
|
|
|
static rpmMemFailFunc failfunc = NULL;
|
|
|
|
static void *failfunc_data = NULL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Give memfail callback a chance to try to give us memory or perform
|
|
|
|
* it's own cleanup. If we dont get memory we die anyway as rpm doesn't
|
|
|
|
* check for NULL returns from allocations.
|
|
|
|
*/
|
2009-09-16 22:28:22 +08:00
|
|
|
static void *vmefail(size_t size)
|
1999-09-23 00:34:08 +08:00
|
|
|
{
|
2009-09-17 16:36:10 +08:00
|
|
|
void *val = failfunc ? (*failfunc)(size, failfunc_data) : NULL;
|
|
|
|
if (val == NULL) {
|
|
|
|
fprintf(stderr, _("memory alloc (%u bytes) returned NULL.\n"),
|
|
|
|
(unsigned)size);
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
2010-09-21 18:41:25 +08:00
|
|
|
rpmMemFailFunc rpmSetMemFail(rpmMemFailFunc func, void *data)
|
2009-09-17 16:36:10 +08:00
|
|
|
{
|
2010-09-21 18:41:25 +08:00
|
|
|
rpmMemFailFunc ofunc = failfunc;
|
2009-09-17 16:36:10 +08:00
|
|
|
failfunc = func;
|
|
|
|
failfunc_data = data;
|
|
|
|
return ofunc;
|
1999-09-23 00:34:08 +08:00
|
|
|
}
|
|
|
|
|
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);
|
2009-09-17 17:40:29 +08:00
|
|
|
if (value == NULL)
|
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);
|
2009-09-17 17:40:29 +08:00
|
|
|
if (value == NULL)
|
2000-04-26 03:41:37 +08:00
|
|
|
value = vmefail(size);
|
1999-09-23 00:34:08 +08:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2022-02-08 15:23:58 +08:00
|
|
|
void * rreallocn (void *ptr, size_t nmemb, size_t size)
|
|
|
|
{
|
|
|
|
register void *value = NULL;
|
|
|
|
if (size == 0) size++;
|
|
|
|
if (nmemb == 0) nmemb++;
|
|
|
|
if (nmemb < SIZE_MAX / size)
|
|
|
|
value = realloc(ptr, nmemb * size);
|
|
|
|
if (value == NULL)
|
|
|
|
value = vmefail(size);
|
|
|
|
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);
|
2009-09-17 17:40:29 +08:00
|
|
|
if (value == NULL)
|
2000-04-26 03:41:37 +08:00
|
|
|
value = vmefail(size);
|
1999-09-23 00:34:08 +08:00
|
|
|
return value;
|
|
|
|
}
|
|
|
|
|
2020-11-05 20:07:06 +08:00
|
|
|
char * rstrndup(const char *str, size_t n)
|
1999-09-23 00:34:08 +08:00
|
|
|
{
|
2020-11-05 20:07:06 +08:00
|
|
|
size_t size = n + 1;
|
2000-04-26 03:41:37 +08:00
|
|
|
char *newstr = (char *) malloc (size);
|
2009-09-17 17:40:29 +08:00
|
|
|
if (newstr == NULL)
|
2000-04-26 03:41:37 +08:00
|
|
|
newstr = (char *) vmefail(size);
|
2020-11-05 20:07:06 +08:00
|
|
|
strncpy(newstr, str, n);
|
|
|
|
newstr[n] = '\0';
|
1999-09-23 00:34:08 +08:00
|
|
|
return newstr;
|
|
|
|
}
|
|
|
|
|
2020-11-05 20:07:06 +08:00
|
|
|
char * rstrdup (const char *str)
|
|
|
|
{
|
|
|
|
return rstrndup(str, strlen(str));
|
|
|
|
}
|
|
|
|
|
2009-09-16 22:28:22 +08:00
|
|
|
void * rfree (void *ptr)
|
|
|
|
{
|
|
|
|
free(ptr);
|
|
|
|
return NULL;
|
|
|
|
}
|