forked from stnmrshx/desrim
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdesrim_plat.c
234 lines (205 loc) · 5.74 KB
/
desrim_plat.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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*
* Desrim - Fast robust and scalable heap manager, like a shrimp beibeh... like a shrimp...
*
* Copyright (c) 2012, stnmrshx ([email protected])
* All rights reserved.
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those
* of the authors and should not be interpreted as representing official policies,
* either expressed or implied, of the FreeBSD Project.
*
*/
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <unistd.h>
#include <sys/mman.h>
#include "desrim.h"
pthread_key_t ds_threadkey;
pthread_mutex_t ds_init_lock = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t ds_heapcnt_lock = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t ds_large_arena_lock = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t ds_small_arena_lock = PTHREAD_MUTEX_INITIALIZER;
ds_localmem_t ds_large_arena_mem;
ds_localmem_t ds_small_arena_mem;
int ds_heapcnt[DS_NUM_HEAPS];
int ds_gotkey;
ds_size_t ds_sys_pagesize;
void ds_key_destruct(void *);
int ds_mmap(ds_size_t, void **);
void ds_plat_init(void)
{
ds_lm_init(&ds_large_arena_mem, 0, 0);
ds_lm_init(&ds_small_arena_mem, 0, 0);
ds_sys_pagesize = getpagesize();
if(pthread_key_create(&ds_threadkey, ds_key_destruct) == 0) {
ds_gotkey++;
}
else if(ds_err_func != NULL) {
ds_err_func(
"ds_plat_init: dafuq failed to create the thread key (%d): %s",
errno, strerror(errno));
}
}
void ds_key_destruct(void *arg)
{
(void)ds_lock(&ds_heapcnt_lock);
if(arg == NULL)
ds_heapcnt[0]--;
else
ds_heapcnt[(int)(long)arg - 1]--;
(void)ds_unlock(&ds_heapcnt_lock);
}
int ds_default_heapsel_func(void)
{
int i;
int heapmin;
int heapnum;
void *specval;
if(!ds_gotkey)
return 0;
specval = pthread_getspecific(ds_threadkey);
if(specval != NULL)
return((int)(long)specval - 1);
heapnum = 0;
(void)ds_lock(&ds_heapcnt_lock);
heapmin = ds_heapcnt[0];
for(i = 1; (i < ds_num_heaps) && (heapmin > 0); i++) {
if(ds_heapcnt[i] < heapmin) {
heapnum = i;
heapmin = ds_heapcnt[i];
}
}
ds_heapcnt[heapnum]++;
(void)ds_unlock(&ds_heapcnt_lock);
if(pthread_setspecific(ds_threadkey, (const void *)(long)(heapnum + 1)) != 0) {
(void)ds_lock(&ds_heapcnt_lock);
ds_heapcnt[heapnum]--;
ds_heapcnt[0]++;
(void)ds_unlock(&ds_heapcnt_lock);
return 0;
}
return heapnum;
}
void ds_lock_init(ds_lock_t *lock)
{
pthread_mutexattr_t mattr;
(void)pthread_mutexattr_init(&mattr);
(void)pthread_mutex_init(lock, &mattr);
(void)pthread_mutexattr_destroy(&mattr);
}
void ds_cond_init(ds_cond_t *cond)
{
pthread_condattr_t cattr;
(void)pthread_condattr_init(&cattr);
(void)pthread_cond_init(cond, &cattr);
(void)pthread_condattr_destroy(&cattr);
}
int ds_arena_alloc(ds_size_t size, void **ptr, ds_size_t *realsize, int flags)
{
int ret;
void *p;
ds_lock_t *lp;
ds_localmem_t *lm;
ds_size_t chunksz;
switch(size) {
case DS_LARGE_CHUNKSZ:
lm = &ds_large_arena_mem;
lp = &ds_large_arena_lock;
break;
#if DS_SMALL_CHUNKSZ != DS_LARGE_CHUNKSZ
case DS_SMALL_CHUNKSZ:
lm = &ds_small_arena_mem;
lp = &ds_small_arena_lock;
break;
#endif
default:
lm = 0;
lp = 0;
flags |= DS_ARENA_RAWALLOC;
break;
}
if(flags & DS_ARENA_RAWALLOC) {
size = ((size + ds_sys_pagesize - 1) / ds_sys_pagesize)
* ds_sys_pagesize;
ret = ds_mmap(size, ptr);
if(ret != 0)
return ret;
if(realsize)
*realsize = size;
return 0;
}
ds_lock(lp);
if(ds_lm_alloc(lm, size, ptr) != 0) {
if(size == DS_LARGE_CHUNKSZ)
chunksz = DS_LARGE_ARENA_CHUNKSZ;
else
chunksz = DS_SMALL_ARENA_CHUNKSZ;
ret = ds_mmap(chunksz, &p);
if(ret != 0) {
ds_unlock(lp);
return ret;
}
ds_lm_init(lm, chunksz, p);
(void)ds_lm_alloc(lm, size, ptr);
}
ds_unlock(lp);
if(realsize)
*realsize = size;
return 0;
}
void ds_arena_free(ds_size_t size, void *ptr)
{
int ret;
int err;
ret = munmap(ptr, size);
if((ret != 0) && (ds_err_func != NULL)) {
if(errno == 0)
err = ENOMEM;
else
err = errno;
ds_err_func(
"ds_munmap: dafuq, failed to free of %lx bytes (%d): %s",
(unsigned long)size, err, strerror(err));
}
}
int ds_mmap(ds_size_t size, void **ptr)
{
int err;
void *val;
val = mmap(0, size, (PROT_READ | PROT_WRITE | PROT_EXEC), (MAP_ANON | MAP_PRIVATE), -1, 0);
if(val == MAP_FAILED) {
if(errno == 0)
err = ENOMEM;
else
err = errno;
if(ds_err_func != NULL) {
ds_err_func(
"ds_mmap: dafuq failed to allocate of %lx bytes (%d): %s",
(unsigned long)size, err, strerror(err));
}
errno = err;
return err;
}
*ptr = val;
return 0;
}