aboutsummaryrefslogtreecommitdiff
path: root/src/bumpptr_alloc.c
blob: 6cb82df92a66d11d8ea690d34661450cdf42b5f3 (plain)
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include <assert.h>
#include <errno.h>
#include <stddef.h> /* NULL, size_t */
#include <stdint.h> /* uintptr_t */
#include <stdio.h> /* uintptr_t */
#include <unistd.h> /* sysconf(_SC_PAGESIZE) */
#include <string.h> /* memset */
#include <sys/mman.h> /* memset */

#define MIN_ALIGNMENT 16

#ifndef MEMSIZE
#define MEMSIZE 1024*4*1024*1024l
#endif

#define unlikely(x)     __builtin_expect((x),0)

#ifdef __cplusplus
extern "C" {
#endif

__thread void* mem_start = NULL;
__thread uintptr_t mem_end = 0;
__thread uintptr_t ptr = 0;

inline void* bump_up(size_t size, size_t align) {
	assert(align % 2 == 0);

	if (unlikely(mem_start == NULL)) {
		mem_start = mmap(NULL, MEMSIZE, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
		if(mem_start == MAP_FAILED) {
			perror("mmap");
			return NULL;
		}
		ptr = (uintptr_t)mem_start;
		mem_end = ptr + MEMSIZE;
	}

	// align ptr;
	uintptr_t aligned = (ptr + align - 1) & ~(align - 1);

	uintptr_t new_ptr = aligned + size;
	if (new_ptr > mem_end)
		return NULL;
	else {
		ptr = new_ptr;
		return (void*)aligned;
	}
}

void* malloc(size_t size) {
	return bump_up(size, MIN_ALIGNMENT);
}

void free(__attribute__ ((unused)) void* ptr) {
}

void* realloc(void* ptr, size_t size) {
	if(ptr == NULL)
		return malloc(size);

	void* new_ptr = bump_up(size, MIN_ALIGNMENT);
	// this may copies to much
	memcpy(new_ptr, ptr, size);
	return new_ptr;
}

void* memalign(size_t alignment, size_t size) {
	return bump_up(size, alignment);
}

int posix_memalign(void **memptr, size_t alignment, size_t size)
{
	void *out;

	if(memptr == NULL) {
		return 22;
	}

	if((alignment % sizeof(void*)) != 0) {
		return 22;
	}

	/* if not power of two */
	if(!((alignment != 0) && !(alignment & (alignment - 1)))) {
		return 22;
	}

	if(size == 0) {
		*memptr = NULL;
		return 0;
	}

	out = bump_up(size, alignment);
	if(out == NULL) {
		return 12;
	}

	*memptr = out;
	return 0;
}

void* calloc(size_t nmemb, size_t size)
{
	void *out;
	size_t fullsize = nmemb * size;

	if((size != 0) && ((fullsize / size) != nmemb)) {
		return NULL;
	}
	
	out = bump_up(fullsize, MIN_ALIGNMENT);
	if(out == NULL) {
		return NULL;
	}

	memset(out, 0, fullsize);
	return out;
}

void* valloc(size_t size)
{
	long ret = sysconf(_SC_PAGESIZE);
	if(ret == -1) {
		return NULL;
	}

	return memalign(ret, size);
}

void* pvalloc(size_t size)
{
	size_t ps, rem, allocsize;

	long ret = sysconf(_SC_PAGESIZE);
	if(ret == -1) {
		return NULL;
	}

	ps = ret;
	rem = size % ps;
	allocsize = size;
	if(rem != 0) {
		allocsize = ps + (size - rem);
	}

	return memalign(ps, allocsize);
}

void* aligned_alloc(size_t alignment, size_t size)
{
	if(alignment > size) {
		return NULL;
	}

	if((size % alignment) != 0) {
		return NULL;
	}

	return memalign(alignment, size);
}

int malloc_stats() {
	fprintf(stderr, "Bump pointer allocator by muhq\n");
	fprintf(stderr, "Memsize: %zu, start address: %p, bump pointer %p\n", MEMSIZE, mem_start, ptr);
	return 0;
}

#ifdef __cplusplus
}
#endif