-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmem.h
54 lines (48 loc) · 1.31 KB
/
mem.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
#ifndef MEM_H
#define MEM_H
#include <stdio.h>
#include <stdlib.h>
// bit control
#define UNFLAG_R(flag, len) flag >> len << len // unflag bits by length from front bit
// memory
#define CHECK_MEM_ERR(v) \
if (!v) \
{ \
fprintf(stderr, "Insufficient memory"); \
exit(EXIT_FAILURE); \
}
#define BYTE_TO_BINARY_PATTERN "%c%c%c%c%c%c%c%c"
#define BYTE_TO_BINARY(byte) \
(byte & 0x80 ? '1' : '0'), \
(byte & 0x40 ? '1' : '0'), \
(byte & 0x20 ? '1' : '0'), \
(byte & 0x10 ? '1' : '0'), \
(byte & 0x08 ? '1' : '0'), \
(byte & 0x04 ? '1' : '0'), \
(byte & 0x02 ? '1' : '0'), \
(byte & 0x01 ? '1' : '0')
// #define print_uint32_t(head, bin) printf(head BYTE_TO_BINARY_PATTERN" "BYTE_TO_BINARY_PATTERN" "BYTE_TO_BINARY_PATTERN" "BYTE_TO_BINARY_PATTERN"\n", \
// BYTE_TO_BINARY(bin>>24), BYTE_TO_BINARY(bin>>16), BYTE_TO_BINARY(bin>>8), BYTE_TO_BINARY(bin));
#define print_uint32_t(head, bin) NULL
// malloc
static inline void* malloc_s(size_t t)
{
void *n = malloc(t);
CHECK_MEM_ERR(n);
return n;
}
// calloc
static inline void* calloc_s(size_t num, size_t size)
{
void *n = calloc(num, size);
CHECK_MEM_ERR(n);
return n;
}
// realloc
static inline void* realloc_s(void *ptr, size_t size)
{
void *n = realloc(ptr, size);
CHECK_MEM_ERR(n);
return n;
}
#endif