mirror of
https://github.com/labwc/labwc.git
synced 2025-11-05 13:29:58 -05:00
common: Add additional memory utilities (xzalloc() etc.)
This commit is contained in:
parent
b89f7bfc0d
commit
cb40cdc36c
35 changed files with 193 additions and 167 deletions
47
src/common/mem.c
Normal file
47
src/common/mem.c
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
// SPDX-License-Identifier: GPL-2.0-only
|
||||
#define _POSIX_C_SOURCE 200809L
|
||||
#include <assert.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include "common/mem.h"
|
||||
|
||||
static void
|
||||
die_if_null(void *ptr)
|
||||
{
|
||||
if (!ptr) {
|
||||
perror("Failed to allocate memory");
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
|
||||
void *
|
||||
xzalloc(size_t size)
|
||||
{
|
||||
if (!size) {
|
||||
return NULL;
|
||||
}
|
||||
void *ptr = calloc(1, size);
|
||||
die_if_null(ptr);
|
||||
return ptr;
|
||||
}
|
||||
|
||||
void *
|
||||
xrealloc(void *ptr, size_t size)
|
||||
{
|
||||
if (!size) {
|
||||
free(ptr);
|
||||
return NULL;
|
||||
}
|
||||
ptr = realloc(ptr, size);
|
||||
die_if_null(ptr);
|
||||
return ptr;
|
||||
}
|
||||
|
||||
char *
|
||||
xstrdup(const char *str)
|
||||
{
|
||||
assert(str);
|
||||
char *copy = strdup(str);
|
||||
die_if_null(copy);
|
||||
return copy;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue