autoupdater: add safe allocation functions
safe_malloc() and safe_realloc() are wrappers around malloc() and realloc() than abort the process if the memory allocation fails. Signed-off-by: Tobias Schramm <tobleminer@gmail.com> [Matthias Schiffer: add safe_realloc()]
This commit is contained in:
parent
0b61fee98e
commit
3566cabef5
|
@ -100,3 +100,23 @@ float get_uptime(void) {
|
|||
fputs("autoupdater: error: unable to determine uptime\n", stderr);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
void * safe_malloc(size_t size) {
|
||||
void *ret = malloc(size);
|
||||
if (!ret) {
|
||||
fprintf(stderr, "autoupdater: error: failed to allocate memory\n");
|
||||
abort();
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
void * safe_realloc(void *ptr, size_t size) {
|
||||
void *ret = realloc(ptr, size);
|
||||
if (!ret) {
|
||||
fprintf(stderr, "autoupdater: error: failed to allocate memory\n");
|
||||
abort();
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
|
|
@ -24,7 +24,12 @@
|
|||
*/
|
||||
#pragma once
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
|
||||
void run_dir(const char *dir);
|
||||
void randomize(void);
|
||||
float get_uptime(void);
|
||||
|
||||
void * safe_malloc(size_t size);
|
||||
void * safe_realloc(void *ptr, size_t size);
|
||||
|
|
Loading…
Reference in New Issue