-
Notifications
You must be signed in to change notification settings - Fork 0
/
yolomalloc.c
62 lines (51 loc) · 1.29 KB
/
yolomalloc.c
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
/*======================*
%$ /!\ yolomalloc.c /!\ $%
%$ DANGER Swag overflow $%
*======================*/
/* Feature test macros */
#define _DEFAULT_SOURCE /* for sbrk */
/* Includes */
#include <unistd.h>
#include <stdio.h>
#include "yolomalloc.h"
/* Defines */
#define YOLO_WORD (sizeof(intptr_t))
#define YOLO_DWORD (2 * YOLO_WORD)
#define YOLO_CHUNK 8192
/* Globals */
void *yolo_start = NULL;
void *yolo_end = NULL;
int yolo_init() {
yolo_start = sbrk(0);
if (yolo_start == (void*) -1) {
yolo_start = NULL;
yolo_end = NULL;
return -1;
}
yolo_end = yolo_start;
return 0;
}
/* Ya know, allocating stuff and all */
void *yolomalloc(size_t size) {
if (size == 0) {
return NULL;
}
if (yolo_start == NULL || yolo_end == NULL) {
if (yolo_init() != 0) {
return NULL;
}
}
intptr_t size_w = (size + (YOLO_WORD-1)) / YOLO_WORD; // size in words
intptr_t align_dw = ((size_w + 1) / 2) * 2; // round size_w to dword
void *ptr = sbrk(align_dw);
if (ptr == (void*) -1) {
return NULL;
}
yolo_end = ptr + align_dw;
return ptr;
}
void yolofree(void *ptr) {
/*----------------------------------*
* THE YOLO IS STRONG WITH THIS ONE *
*----------------------------------*/
}