-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalloc.h
More file actions
69 lines (62 loc) · 1.71 KB
/
alloc.h
File metadata and controls
69 lines (62 loc) · 1.71 KB
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
59
60
61
62
63
64
65
66
67
68
69
/* alloc.h */
#define _GNU_SOURCE
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <assert.h>
#include <errno.h>
#define public __attribute__((visibility("default")))
#define private static
#define packed __attribute__((__packed__))
#define unused __attribute__((__unused__))
#define Maxwords ((1024*1024*1024/4)-1)
#define ZeroWords 1073741823
#define ErrNoErr 0
#define ErrNoMem 1
#define ErrUnknown 2
#define Err2xFree 4
typedef unsigned char int8;
typedef unsigned short int int16;
typedef unsigned int int32;
typedef unsigned long long int int64;
// typedef unsigned _BitInt(128) int128;
typedef void heap;
typedef int32 word;
struct packed s_header {
word w:30;
bool alloced:1;
bool unused reserved:1;
};
typedef struct packed s_header header;
#define $1 (int8 *)
#define $2 (int16)
#define $4 (int32)
#define $8 (int64)
#define $16 (int128)
#define $c (char *)
#define $i (int)
#define $v (void *)
#define $h (header *)
/*
Use do-while loop so we can use reterr in a fn without {} (loop is considered as a single statement)
and did while(false) so the loop runs only one time
*/
#define reterr(x) do { \
errno = (x); \
return $v 0; \
} while(false)
// Pass x in parenthesis () not directly to support expressions too if they are passed not just numbers
#define findblock(x) findblock_($h memspace,(x),0)
#define show() show_($h memspace)
#define allock(x) alloc((x)*1024)
#define allocm(x) alloc((x)*1024*1024)
#define allocg(x) alloc((x)*1024*1024*1024)
public bool destroy(void*);
private void show_(header *);
private header *findblock_(header*,word,word);
private void *mkalloc(word, header*);
public void *alloc(int32);
void zero(int8*,int16);
int main(int,char**);