Skip to content
Snippets Groups Projects
Commit 779bf765 authored by Marek Vavrusa's avatar Marek Vavrusa Committed by Jan Kadlec
Browse files

Implemented xmalloc()/xrealloc() memory patterns.

The idea is to die gracefully if memory allocation fails.
Could revert this behavior later on.
parent c0504c8a
Branches
Tags
No related merge requests found
......@@ -21,8 +21,30 @@
#include <config.h>
#include <stdarg.h>
#include "common/log.h"
#include "common/slab/alloc-common.h"
void* xmalloc(size_t l)
{
void *p = malloc(l);
if (p == NULL) {
log_server_fatal("Failed to allocate %zu bytes.\n", l);
abort();
}
return p;
}
void *xrealloc(void *p, size_t l)
{
p = realloc(p, l);
if (p == NULL) {
log_server_fatal("Failed to reallocate to %zu bytes from %p.\n",
l, p);
abort();
}
return p;
}
int mreserve(char **p, size_t tlen, size_t min, size_t allow, size_t *reserved)
{
......
......@@ -27,6 +27,12 @@
#ifndef _KNOTD_COMMON_MALLOC_H_
#define _KNOTD_COMMON_MALLOC_H_
/*! \brief Allocate memory or die. */
void* xmalloc(size_t l);
/*! \brief Reallocate memory or die. */
void *xrealloc(void *p, size_t l);
/*!
* \brief Reserve new or trim excessive memory.
*
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment