2022-10-12 02:03:57 +00:00
|
|
|
#include "oslib.h"
|
|
|
|
|
|
|
|
void *xmalloc(const char *what, int size) {
|
2022-10-12 17:07:57 +00:00
|
|
|
void *ret;
|
|
|
|
|
|
|
|
ret = malloc(size ? size : 1);
|
|
|
|
if (!ret) {
|
|
|
|
fprintf(
|
|
|
|
stderr,
|
|
|
|
"*** Out of memory when allocating %d bytes%s%s",
|
|
|
|
size,
|
|
|
|
what ? " for " : "",
|
|
|
|
what ? what : "");
|
|
|
|
exit(-23);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2022-10-12 02:03:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void *xcalloc(const char *what, int size) {
|
2022-10-12 17:07:57 +00:00
|
|
|
void *ret;
|
2022-10-12 02:03:57 +00:00
|
|
|
|
2022-10-12 17:07:57 +00:00
|
|
|
ret = xmalloc(what, size);
|
|
|
|
memset(ret, 0, size);
|
|
|
|
return ret;
|
2022-10-12 02:03:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void *xrealloc(const char *what, void *old, int size) {
|
2022-10-12 17:07:57 +00:00
|
|
|
void *ret;
|
|
|
|
|
|
|
|
ret = realloc(old, size ? size : 1);
|
|
|
|
if (!ret) {
|
|
|
|
fprintf(
|
|
|
|
stderr,
|
|
|
|
"*** Out of memory when resizing buffer to %d bytes%s%s",
|
|
|
|
size,
|
|
|
|
what ? " for " : "",
|
|
|
|
what ? what : "");
|
|
|
|
exit(-23);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
2022-10-12 02:03:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
char *xstrdup(const char *str) {
|
2022-10-12 17:07:57 +00:00
|
|
|
return strcpy(xmalloc(0, strlen(str) + 1), str);
|
2022-10-12 02:03:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void xfree(void *ptr) {
|
2022-10-12 17:07:57 +00:00
|
|
|
if (ptr)
|
|
|
|
free(ptr);
|
2022-10-12 02:03:57 +00:00
|
|
|
}
|