2 Copyright (c) 2003-2006 by Juliusz Chroboczek
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 #define MB (1024 * 1024)
27 chunkCriticalMark
= 0,
28 chunkHighMark
= 8 * MB
;
33 CONFIG_VARIABLE(chunkLowMark
, CONFIG_INT
,
34 "Low mark for chunk memory (0 = auto).");
35 CONFIG_VARIABLE(chunkCriticalMark
, CONFIG_INT
,
36 "Critical mark for chunk memory (0 = auto).");
37 CONFIG_VARIABLE(chunkHighMark
, CONFIG_INT
,
38 "High mark for chunk memory.");
44 #define ROUND_CHUNKS(a) a = (((a) + CHUNK_SIZE - 1) / CHUNK_SIZE) * CHUNK_SIZE;
47 if(CHUNK_SIZE
!= 1 << log2_ceil(CHUNK_SIZE
)) {
48 do_log(L_ERROR
, "CHUNK SIZE %d is not a power of two.\n", CHUNK_SIZE
);
52 ROUND_CHUNKS(chunkHighMark
);
53 ROUND_CHUNKS(chunkCriticalMark
);
54 ROUND_CHUNKS(chunkLowMark
);
56 if(chunkHighMark
< 8 * CHUNK_SIZE
) {
57 chunkHighMark
= 8 * CHUNK_SIZE
;
58 do_log(L_WARN
, "Impossibly low chunkHighMark -- setting to %d.\n",
63 if(chunkLowMark
<= 0) q
= 1;
64 if(chunkLowMark
< 4 * CHUNK_SIZE
||
65 chunkLowMark
> chunkHighMark
- 4 * CHUNK_SIZE
) {
66 chunkLowMark
= MIN(chunkHighMark
- 4 * CHUNK_SIZE
,
67 chunkHighMark
* 3 / 4);
68 ROUND_CHUNKS(chunkLowMark
);
69 if(!q
) do_log(L_WARN
, "Inconsistent chunkLowMark -- setting to %d.\n",
74 if(chunkCriticalMark
<= 0) q
= 1;
75 if(chunkCriticalMark
>= chunkHighMark
- 2 * CHUNK_SIZE
||
76 chunkCriticalMark
<= chunkLowMark
+ 2 * CHUNK_SIZE
) {
78 MIN(chunkHighMark
- 2 * CHUNK_SIZE
,
79 chunkLowMark
+ (chunkHighMark
- chunkLowMark
) * 15 / 16);
80 ROUND_CHUNKS(chunkCriticalMark
);
81 if(!q
) do_log(L_WARN
, "Inconsistent chunkCriticalMark -- "
82 "setting to %d.\n", chunkCriticalMark
);
91 maybe_free_chunks(int arenas
, int force
)
93 if(force
|| used_chunks
>= CHUNKS(chunkHighMark
)) {
94 discardObjects(force
, force
);
100 if(used_chunks
>= CHUNKS(chunkLowMark
) && !objectExpiryScheduled
) {
101 TimeEventHandlerPtr event
;
102 event
= scheduleTimeEvent(1, discardObjectsHandler
, 0, NULL
);
104 objectExpiryScheduled
= 1;
115 do_log(L_WARN
, "Warning: using malloc(3) for chunk allocation.\n");
131 if(used_chunks
> CHUNKS(chunkHighMark
))
132 maybe_free_chunks(0, 0);
133 if(used_chunks
> CHUNKS(chunkHighMark
))
135 chunk
= malloc(CHUNK_SIZE
);
137 maybe_free_chunks(1, 1);
138 chunk
= malloc(CHUNK_SIZE
);
150 if(used_chunks
> CHUNKS(chunkHighMark
))
152 chunk
= malloc(CHUNK_SIZE
);
159 dispose_chunk(void *chunk
)
161 assert(chunk
!= NULL
);
173 totalChunkArenaSize()
175 return used_chunks
* CHUNK_SIZE
;
180 #define MAP_FAILED NULL
181 #define getpagesize() (64 * 1024)
183 alloc_arena(size_t size
)
185 return VirtualAlloc(NULL
, size
, MEM_COMMIT
| MEM_RESERVE
, PAGE_READWRITE
);
188 free_arena(void *addr
, size_t size
)
191 rc
= VirtualFree(addr
, size
, MEM_RELEASE
);
198 #define MAP_FAILED ((void*)((long int)-1))
201 alloc_arena(size_t size
)
203 return mmap(NULL
, size
, PROT_READ
| PROT_WRITE
,
204 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
207 free_arena(void *addr
, size_t size
)
209 return munmap(addr
, size
);
213 /* Memory is organised into a number of chunks of ARENA_CHUNKS chunks
214 each. Every arena is pointed at by a struct _ChunkArena. */
215 /* If currentArena is not NULL, it points at the last arena used,
216 which gives very fast dispose/get sequences. */
218 #define DEFINE_FFS(type, ffs_name) \
223 if(i == 0) return 0; \
225 while((i & 1) == 0) { \
232 #ifndef LONG_LONG_ARENA_BITMAPS
233 #ifndef LONG_ARENA_BITMAPS
237 typedef unsigned int ChunkBitmap
;
238 #define BITMAP_FFS(bitmap) (ffs(bitmap))
243 DEFINE_FFS(long, ffsl
)
245 typedef unsigned long ChunkBitmap
;
246 #define BITMAP_FFS(bitmap) (ffsl(bitmap))
252 DEFINE_FFS(long long, ffsll
)
254 typedef unsigned long long ChunkBitmap
;
255 #define BITMAP_FFS(bitmap) (ffsll(bitmap))
258 #define ARENA_CHUNKS ((int)sizeof(ChunkBitmap) * 8)
259 #define EMPTY_BITMAP (~(ChunkBitmap)0)
260 #define BITMAP_BIT(i) (((ChunkBitmap)1) << (i))
263 typedef struct _ChunkArena
{
266 } ChunkArenaRec
, *ChunkArenaPtr
;
268 static ChunkArenaPtr chunkArenas
, currentArena
;
269 static int numArenas
;
270 #define CHUNK_IN_ARENA(chunk, arena) \
271 ((arena)->chunks && \
272 (char*)(chunk) >= (arena)->chunks && \
273 (char*)(chunk) < (arena)->chunks + (ARENA_CHUNKS * CHUNK_SIZE))
275 #define CHUNK_ARENA_INDEX(chunk, arena) \
276 (((char*)(chunk) - (arena)->chunks) / CHUNK_SIZE)
284 pagesize
= getpagesize();
285 if((CHUNK_SIZE
* ARENA_CHUNKS
) % pagesize
!= 0) {
287 "The arena size %d (%d x %d) "
288 "is not a multiple of the page size %d.\n",
289 ARENA_CHUNKS
* CHUNK_SIZE
, ARENA_CHUNKS
, CHUNK_SIZE
, pagesize
);
293 (CHUNKS(chunkHighMark
) + (ARENA_CHUNKS
- 1)) / ARENA_CHUNKS
;
294 chunkArenas
= malloc(numArenas
* sizeof(ChunkArenaRec
));
295 if(chunkArenas
== NULL
) {
296 do_log(L_ERROR
, "Couldn't allocate chunk arenas.\n");
299 for(i
= 0; i
< numArenas
; i
++) {
300 chunkArenas
[i
].bitmap
= EMPTY_BITMAP
;
301 chunkArenas
[i
].chunks
= NULL
;
309 ChunkArenaPtr arena
= NULL
;
312 for(i
= 0; i
< numArenas
; i
++) {
313 arena
= &(chunkArenas
[i
]);
314 if(arena
->bitmap
!= 0)
320 assert(arena
!= NULL
);
324 p
= alloc_arena(CHUNK_SIZE
* ARENA_CHUNKS
);
325 if(p
== MAP_FAILED
) {
326 do_log_error(L_ERROR
, errno
, "Couldn't allocate chunk");
327 maybe_free_chunks(1, 1);
339 ChunkArenaPtr arena
= NULL
;
341 if(currentArena
&& currentArena
->bitmap
!= 0) {
342 arena
= currentArena
;
344 if(used_chunks
>= CHUNKS(chunkHighMark
))
345 maybe_free_chunks(0, 0);
347 if(used_chunks
>= CHUNKS(chunkHighMark
))
353 currentArena
= arena
;
355 i
= BITMAP_FFS(arena
->bitmap
) - 1;
356 arena
->bitmap
&= ~BITMAP_BIT(i
);
358 return arena
->chunks
+ CHUNK_SIZE
* i
;
365 ChunkArenaPtr arena
= NULL
;
367 if(currentArena
&& currentArena
->bitmap
!= 0) {
368 arena
= currentArena
;
370 if(used_chunks
>= CHUNKS(chunkHighMark
))
376 currentArena
= arena
;
378 i
= ffs(arena
->bitmap
) - 1;
379 arena
->bitmap
&= ~BITMAP_BIT(i
);
381 return arena
->chunks
+ CHUNK_SIZE
* i
;
385 dispose_chunk(void *chunk
)
387 ChunkArenaPtr arena
= NULL
;
390 assert(chunk
!= NULL
);
392 if(currentArena
&& CHUNK_IN_ARENA(chunk
, currentArena
)) {
393 arena
= currentArena
;
395 for(i
= 0; i
< numArenas
; i
++) {
396 arena
= &(chunkArenas
[i
]);
397 if(CHUNK_IN_ARENA(chunk
, arena
))
400 assert(arena
!= NULL
);
401 currentArena
= arena
;
404 i
= CHUNK_ARENA_INDEX(chunk
, arena
);
405 arena
->bitmap
|= BITMAP_BIT(i
);
415 for(i
= 0; i
< numArenas
; i
++) {
416 arena
= &(chunkArenas
[i
]);
417 if(arena
->bitmap
== EMPTY_BITMAP
&& arena
->chunks
) {
418 rc
= free_arena(arena
->chunks
, CHUNK_SIZE
* ARENA_CHUNKS
);
420 do_log_error(L_ERROR
, errno
, "Couldn't unmap memory");
423 arena
->chunks
= NULL
;
426 if(currentArena
&& currentArena
->chunks
== NULL
)
431 totalChunkArenaSize()
436 for(i
= 0; i
< numArenas
; i
++) {
437 arena
= &(chunkArenas
[i
]);
439 size
+= (CHUNK_SIZE
* ARENA_CHUNKS
);