Skip to content

Commit 62fb883

Browse files
initial commit
0 parents  commit 62fb883

File tree

8 files changed

+123
-0
lines changed

8 files changed

+123
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
main

LICENSE.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
2+
Version 2, December 2004
3+
4+
Copyright (C) 2004 Sam Hocevar <[email protected]>
5+
6+
Everyone is permitted to copy and distribute verbatim or modified
7+
copies of this license document, and changing it is allowed as long
8+
as the name is changed.
9+
10+
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
11+
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
12+
13+
0. You just DO WHAT THE FUCK YOU WANT TO.

README.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# randalloc
2+
randalloc is a 🚀 **BLAZING FAST**, 🚀 **MEMORY SAFE** and 🚀 **THREAD SAFE**
3+
memory allocator. It works like this:
4+
```c
5+
void* malloc(size_t s) { return (void*)rand(); }
6+
```
7+
8+
Actually, _a bit more_ is required to actually make `rand` work like that. Go
9+
read [`randalloc.c`](https://github.com/portasynthinca3/randalloc/blob/master/randalloc.c),
10+
it's just 36 lines long.
11+
12+
## Run it!
13+
```
14+
git clone https://github.com/portasynthinca3/randalloc.git
15+
cd randalloc
16+
cat main.c # just kinda look at what the code does
17+
sh randalloc.sh
18+
```
19+
20+
## Use it!
21+
- Yank [`randalloc.c`](https://github.com/portasynthinca3/randalloc/blob/master/randalloc.c)
22+
from this repo and paste it into your project.
23+
- Call `_init_alloc` as close to entry as possible.
24+
- Compile your project with `-Wl,--wrap=malloc -Wl,--wrap=free -Wl,--wrap=realloc`.
25+
- Enjoy 🚀 **BLAZING FASTNESS**, 🚀 **MEMORY SAFETY** and 🚀 **THREAD
26+
SAFETY** in your very own project that you chose to write in a legacy
27+
language!
28+
29+
## Performance
30+
The allocator runs in O(1) time. And that constant is quite small, not gonna lie.
31+
32+
Have I measured it? No. But it's fast; like look at it!
33+
34+
## Memory safety
35+
Assuming you are fortunate enough that your allocations don't overlap, your code
36+
will be memory safe!
37+
38+
## Thread safety
39+
This allocator does not need to take any extra precautions to ensure thread
40+
safety. That not only means it's 🚀 **BLAZING SAFE**, it's also 🚀 **THREAD
41+
FAST**!
42+
43+
Wait, no, I think I might have mixed up something.
44+
45+
## Why?
46+
- I just discovered the `--wrap` linker flag and my mind was absolutely
47+
**blown**.
48+
- I saw the original implementation in someone's Discord status.
49+
- Yes, I am a little child.
50+
51+
## Licensing
52+
This project is hereby licensed under the [WTFPL](http://www.wtfpl.net/txt/copying/).

main.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <stdlib.h>
2+
#include <string.h>
3+
#include <stdio.h>
4+
5+
void _init_alloc(void);
6+
7+
int main(int argc, const char** argv) {
8+
_init_alloc();
9+
10+
const char* static_str = "Hello, World!";
11+
char* dynamic = malloc(strlen(static_str) + 1);
12+
strcpy(dynamic, static_str);
13+
printf("%p: %s", dynamic, dynamic);
14+
}

randalloc.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include <stdint.h>
2+
#include <stdlib.h>
3+
#include <unistd.h>
4+
#include <time.h>
5+
#include <string.h>
6+
7+
#define RANDALLOC_SIZE (1*1024*1024)
8+
_Static_assert(RANDALLOC_SIZE < RAND_MAX);
9+
10+
static void* rand_start;
11+
12+
void _init_alloc(void) {
13+
srand(time(NULL));
14+
rand_start = sbrk(0);
15+
sbrk(RANDALLOC_SIZE);
16+
}
17+
18+
void* __wrap_malloc(size_t size) {
19+
int offset = rand() % RANDALLOC_SIZE;
20+
return (uint8_t*)rand_start + offset;
21+
}
22+
23+
void __wrap_free(void* ptr) {
24+
// BLAZING FAST
25+
}
26+
27+
void* __wrap_calloc(size_t nmemb, size_t size) {
28+
return malloc(nmemb * size);
29+
}
30+
31+
void* __wrap_realloc(void* ptr, size_t size) {
32+
void* new = malloc(size);
33+
memcpy(new, ptr, size);
34+
free(ptr);
35+
return new;
36+
}

randalloc.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env sh
2+
gcc -O0 -g -Wl,--wrap=malloc -Wl,--wrap=free -Wl,--wrap=realloc main.c randalloc.c -o main
3+
./main

regular.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
void _init_alloc(void) { /* stub */ }

regular.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/usr/bin/env sh
2+
gcc -O0 -g main.c regular.c -o main
3+
./main

0 commit comments

Comments
 (0)