├── LICENSE ├── Makefile ├── README.md ├── bpa.c ├── bpa.h └── testbpa.c /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2013 codingnow.com 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 8 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | testbpa : testbpa.c bpa.c 2 | gcc -g -Wall -o $@ $^ -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Bump Pointer Allocator 2 | === 3 | 4 | A bump pointer allocator . 5 | 6 | read testbpa.c 7 | -------------------------------------------------------------------------------- /bpa.c: -------------------------------------------------------------------------------- 1 | #include "bpa.h" 2 | 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | #define ALIGN_SIZE 8 10 | #define COLLECT_STEP_LENGTH 0x1000 11 | #define HEAP_HEADER_SIZE ((sizeof(struct bpa_heap) + ALIGN_SIZE - 1) / ALIGN_SIZE) 12 | #define HEAP_SLICE(H, off) (struct slice *)((char *)H + off * ALIGN_SIZE) 13 | #define SLICE_ID(S) ((id *)((uintptr_t)(S) + S->sz * ALIGN_SIZE - sizeof(id))) 14 | #define MAX_SLOT 0x10000 15 | 16 | typedef uint32_t pointer_t; 17 | 18 | struct bpa_heap { 19 | pointer_t sz; 20 | pointer_t offset; 21 | pointer_t collect; 22 | id cid; 23 | pointer_t slot[MAX_SLOT]; 24 | }; 25 | 26 | // the size of slice is at the end of slice, because sizeof(struct slice) align to 8 would be better. 27 | struct slice { 28 | int ref; 29 | pointer_t sz; 30 | }; 31 | 32 | struct bpa_heap * 33 | bpa_init(void *buffer, size_t sz) { 34 | struct bpa_heap *H = buffer; 35 | sz /= ALIGN_SIZE; 36 | if (sz != (pointer_t)sz) 37 | return NULL; 38 | if (sz <= HEAP_HEADER_SIZE) { 39 | return NULL; 40 | } 41 | H->sz = sz; 42 | H->offset = HEAP_HEADER_SIZE; 43 | H->collect = 0; 44 | H->cid = 0; 45 | 46 | return H; 47 | } 48 | 49 | static int 50 | alloc_slot(struct bpa_heap *H) { 51 | int i; 52 | for (i=0;icid; 54 | if (cid == 0) { 55 | cid = ++ H->cid; 56 | } 57 | int slot = cid % MAX_SLOT; 58 | if (H->slot[slot] == 0) { 59 | return slot; 60 | } 61 | } 62 | return -1; 63 | } 64 | 65 | id 66 | bpa_create(struct bpa_heap *H, size_t sz) { 67 | pointer_t rsz = (sz + sizeof(struct slice) + sizeof(id) + ALIGN_SIZE - 1) / ALIGN_SIZE; 68 | if (H->offset + rsz > H->sz) 69 | return 0; 70 | int slot = alloc_slot(H); 71 | if (slot < 0) { 72 | return 0; 73 | } 74 | id cid = H->cid; 75 | H->slot[slot] = H->offset; 76 | struct slice * s = HEAP_SLICE(H, H->offset); 77 | H->offset += rsz; 78 | s->ref = 0; 79 | s->sz = rsz; 80 | *SLICE_ID(s) = cid; 81 | 82 | return cid; 83 | } 84 | 85 | void * 86 | bpa_grab(struct bpa_heap *H, id uid) { 87 | pointer_t off = H->slot[uid % MAX_SLOT]; 88 | if (off == 0) { 89 | return NULL; 90 | } 91 | struct slice *s = HEAP_SLICE(H, off); 92 | if (*SLICE_ID(s) != uid) { 93 | return NULL; 94 | } 95 | ++s->ref; 96 | return s+1; 97 | } 98 | 99 | void 100 | bpa_release(struct bpa_heap *H, id uid) { 101 | pointer_t off = H->slot[uid % MAX_SLOT]; 102 | if (off == 0) { 103 | assert(0); 104 | return; 105 | } 106 | struct slice *s = HEAP_SLICE(H, off); 107 | if (*SLICE_ID(s) != uid) { 108 | assert(0); 109 | return; 110 | } 111 | assert(s->ref > 0); 112 | --s->ref; 113 | } 114 | 115 | void * 116 | bpa_get_ptr(struct bpa_heap *H, id uid) { 117 | pointer_t off = H->slot[uid % MAX_SLOT]; 118 | if (off == 0) { 119 | return NULL; 120 | } 121 | struct slice *s = HEAP_SLICE(H, off); 122 | if (*SLICE_ID(s) != uid) { 123 | return NULL; 124 | } 125 | return s+1; 126 | } 127 | 128 | id 129 | bpa_get_id(struct bpa_heap *H, void *ptr) { 130 | assert((ptr > (void *)H) && ((char *)ptr < (char *)H + H->offset * ALIGN_SIZE)); 131 | struct slice * s = (struct slice *)ptr - 1; 132 | return *SLICE_ID(s); 133 | } 134 | 135 | static void 136 | collect(struct bpa_heap *H) { 137 | pointer_t p = H->collect; 138 | assert(p); 139 | int length = 0; 140 | pointer_t dst = H->collect; 141 | for (;;) { 142 | if (p >= H->offset) { 143 | H->offset = dst; 144 | H->collect = 0; 145 | return; 146 | } 147 | if (length > COLLECT_STEP_LENGTH) { 148 | H->collect = dst; 149 | if (p != dst) { 150 | struct slice *s = HEAP_SLICE(H, dst); 151 | s->ref = 0; 152 | s->sz = p - dst; 153 | *SLICE_ID(s) = 0; 154 | } 155 | break; 156 | } 157 | struct slice *s = HEAP_SLICE(H, p); 158 | pointer_t sz = s->sz; 159 | id uid = *SLICE_ID(s); 160 | length += sizeof(struct slice) ; 161 | if (s->ref == 0) { 162 | if (uid) { 163 | H->slot[uid % MAX_SLOT] = 0; 164 | } 165 | } else { 166 | if (dst != p) { 167 | H->slot[uid % MAX_SLOT] = dst; 168 | memmove(HEAP_SLICE(H, dst), s, sz * ALIGN_SIZE); 169 | length += sz * ALIGN_SIZE; 170 | } 171 | dst += sz; 172 | } 173 | p += sz; 174 | } 175 | } 176 | 177 | void 178 | bpa_collect(struct bpa_heap * H, int step) { 179 | int i; 180 | if (step == 0 && H->collect !=0) { 181 | // finish last cycle 182 | do { 183 | collect(H); 184 | } while(H->collect != 0); 185 | } 186 | if (H->collect == 0) { 187 | H->collect = HEAP_HEADER_SIZE; 188 | } 189 | 190 | for (i=0;step == 0 || icollect == 0) { 193 | break; 194 | } 195 | } 196 | } 197 | 198 | // for debug 199 | #include 200 | 201 | void 202 | bpa_dump(struct bpa_heap * H) { 203 | printf("Heap size = %d K , %u used\n", H->sz * 8 / 1024, H->offset * 8); 204 | int i; 205 | for (i=0;islot[i]; 207 | if (pt) { 208 | printf("\tslot [%d] offset = 0x%x\n", i,pt); 209 | } 210 | } 211 | pointer_t offset = HEAP_HEADER_SIZE; 212 | while (offset < H->offset) { 213 | struct slice * s= HEAP_SLICE(H, offset); 214 | printf("\tslice [%d] offset = 0x%x ref = %d sz = %u\n", 215 | *SLICE_ID(s), 216 | offset, 217 | s->ref, 218 | s->sz * 8); 219 | offset += s->sz; 220 | } 221 | } 222 | -------------------------------------------------------------------------------- /bpa.h: -------------------------------------------------------------------------------- 1 | #ifndef bump_pointer_allocator_h 2 | #define bump_pointer_allocator_h 3 | 4 | #include 5 | #include 6 | 7 | typedef uint32_t id; 8 | struct bpa_heap; 9 | 10 | struct bpa_heap * bpa_init(void *buffer, size_t sz); 11 | 12 | id bpa_create(struct bpa_heap *, size_t sz); 13 | void * bpa_grab(struct bpa_heap *, id); 14 | void bpa_release(struct bpa_heap *, id); 15 | 16 | void * bpa_get_ptr(struct bpa_heap *, id); 17 | id bpa_get_id(struct bpa_heap *, void *); 18 | 19 | // 4K bytes per step 20 | // step 0 for full collect 21 | void bpa_collect(struct bpa_heap *, int step); 22 | 23 | // for debug 24 | void bpa_dump(struct bpa_heap *); 25 | 26 | #endif 27 | -------------------------------------------------------------------------------- /testbpa.c: -------------------------------------------------------------------------------- 1 | #include "bpa.h" 2 | #include 3 | #include 4 | #include 5 | #include 6 | 7 | static void 8 | set_id(struct bpa_heap *H, id uid, int v, size_t s) { 9 | char * buf = bpa_get_ptr(H, uid); 10 | memset(buf, v, s); 11 | } 12 | 13 | static void 14 | dump_id(struct bpa_heap *H, id uid, size_t s) { 15 | printf("id = %x\n", uid); 16 | char * buf = bpa_get_ptr(H, uid); 17 | if (buf == NULL) { 18 | printf("not exist\n"); 19 | return; 20 | } 21 | size_t i; 22 | for (i=0;i