-
Notifications
You must be signed in to change notification settings - Fork 0
/
malloc.h
58 lines (49 loc) · 1.18 KB
/
malloc.h
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
* @file malloc.h
* @author Jens Munk Hansen <[email protected]>
* @date Thu Aug 3 20:11:01 2017
*
* @brief wrapper for malloc.h
*
*
*/
#pragma once
#include <sps/cenv.h>
#include <stdlib.h>
#include <stdio.h>
#include <malloc.h>
/**
* Return size of allocated memory
*
* @param data
*
* @return
*/
STATIC_INLINE_BEGIN size_t msize(void* data) {
#ifdef _WIN32
return _msize(data);
#elif __APPLE__
return malloc_size(data);
#elif defined(__GNUC__)
return malloc_usable_size(data);
#endif
}
#define SPS_MALLOC(theSize) sps_malloc(__FILE__, __LINE__, theSize)
static inline void *sps_malloc(const char *file,int line,size_t size) {
void *ptr = malloc(size);
if (!ptr) {
fprintf(stderr, "Could not allocate: %zu bytes (%s:%d)\n", size, file, line);
exit(EXIT_FAILURE);
}
return ptr;
}
#define SPS_CALLOC(count, theSize) sps_calloc(__FILE__, __LINE__, count, theSize)
static inline void *sps_calloc(const char *file, int line,
size_t count, size_t size) {
void *ptr = calloc(count, size);
if (!ptr) {
fprintf(stderr, "Could not allocate: %zu bytes (%s:%d)\n", size*count, file, line);
exit(EXIT_FAILURE);
}
return ptr;
}