aboutsummaryrefslogtreecommitdiff
path: root/memory.c
blob: 14dddf556b03e3cfc83c4647b55a9211403b37a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#define _GNU_SOURCE
#include <stdio.h>
#include <stdarg.h>
#include <assert.h>
#include "global.h"
#include "memory.h"

void* check_after_allocation(const char *func,size_t num,size_t sz,void *ptr){
	if(ptr==NULL){
		die("Allocation failed: %s(%zu * %zuB = %zu)",func,num,sz,num*sz);
	}
	return ptr;
}

void* check_after_allocation_str(const char *func,void *ptr){
	if(ptr==NULL){
		die("Allocation failed: %s()",func);
	}
	return ptr;
}

__attribute__((format (printf, 2, 3)))
int memory_asprintf_wrapper(char **ret,const char *format,...){
	assert(ret!=NULL);
	va_list ap;
	va_start(ap,format);
	int len=vasprintf(ret,format,ap);
	va_end(ap);
	check_after_allocation_str("asprintf",*ret);
	return len;
}