|
#ifndef RAVA_SAFE_ALLOC_H
|
|
#define RAVA_SAFE_ALLOC_H
|
|
|
|
#include <stdlib.h>
|
|
#include <stdio.h>
|
|
|
|
#define RAVA_ERROR_BUFFER_SIZE 512
|
|
|
|
static inline void* rava_safe_realloc(void *ptr, size_t size) {
|
|
if (size == 0) {
|
|
free(ptr);
|
|
return NULL;
|
|
}
|
|
void *new_ptr = realloc(ptr, size);
|
|
if (!new_ptr) {
|
|
free(ptr);
|
|
return NULL;
|
|
}
|
|
return new_ptr;
|
|
}
|
|
|
|
static inline void* rava_safe_malloc(size_t size) {
|
|
if (size == 0) return NULL;
|
|
return malloc(size);
|
|
}
|
|
|
|
static inline void* rava_safe_calloc(size_t count, size_t size) {
|
|
if (count == 0 || size == 0) return NULL;
|
|
return calloc(count, size);
|
|
}
|
|
|
|
#endif
|