Workshop o mikrokontrolérech na SKSP 2024.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
988 B

3 months ago
/*
* UCW Library -- Memory Allocation
*
* (c) 2000 Martin Mares <mj@ucw.cz>
*
* This software may be freely distributed and used according to the terms
* of the GNU Lesser General Public License.
*/
#include <ucw/lib.h>
#include <stdlib.h>
#include <string.h>
void *
xmalloc(size_t size)
{
void *x = malloc(size);
if (!x)
die("Cannot allocate %zu bytes of memory", size);
return x;
}
void *
xmalloc_zero(size_t size)
{
void *x = xmalloc(size);
bzero(x, size);
return x;
}
void
xfree(void *ptr)
{
/*
* Maybe it is a little waste of resources to make this a function instead
* of a macro, but xmalloc() is not used for anything critical anyway,
* so let's prefer simplicity.
*/
free(ptr);
}
void *
xrealloc(void *old, size_t size)
{
/* We assume that realloc(NULL, x) works like malloc(x), which is true with the glibc. */
void *x = realloc(old, size);
if (!x && size)
die("Cannot reallocate %zu bytes of memory", size);
return x;
}