comparison runtime/fixed_alloc.h @ 41:1b86a1ee500a

Added faster allocator for small objects
author Mike Pavone <pavone@retrodev.com>
date Sat, 10 Oct 2009 16:40:50 -0400
parents
children a24eb366195c
comparison
equal deleted inserted replaced
40:789a146a48e1 41:1b86a1ee500a
1 #ifndef FIXED_ALLOC_H_
2 #define FIXED_ALLOC_H_
3
4 #include <stddef.h>
5 #include "plat_types.h"
6 #include "block_alloc.h"
7
8 #define GET_BLOCK(ptr) ((void*)(((uint32_t)(ptr))&(~(BLOCK_SIZE-1))))
9
10 #define MAX_SIZE (BLOCK_SIZE/32)
11 #define STRIDE (BLOCK_SIZE/1024)
12 #define MIN_SIZE (BLOCK_SIZE/1024)
13 #define MAX_FREE 16
14
15 #define ADJUST_SIZE(requested) (((requested)+(STRIDE-1)) & (~(STRIDE-1)))
16
17
18 #pragma pack(push,1)
19 typedef struct mem_block {
20 struct mem_block *next;
21 struct mem_block *last;
22 uint16_t numfree;
23 uint16_t firstfree;
24 uint8_t bitmap[1];
25 } mem_block;
26 #pragma pack(pop)
27
28 //num_elements = (BLOCK_SIZE - sizeof(mem_block)+1)*8/(sizeof(element)*8+1)
29 typedef struct {
30 mem_block *freelist;
31 mem_block *inuse[(MAX_SIZE-MIN_SIZE)/STRIDE];
32 uint32_t freecount;
33 } mem_manager;
34
35 void fixed_alloc_init();
36 mem_manager * new_mem_manager();
37 void * falloc(size_t size, mem_manager * manager);
38 void ffree(void * ptr, size_t size, mem_manager * manager);
39
40 #endif //FIXED_ALLOC_H_