aboutsummaryrefslogtreecommitdiff
path: root/include/08.heap/heap06.c
diff options
context:
space:
mode:
authorAleksa Vučković <aleksav013@gmail.com>2022-01-10 22:15:15 +0100
committerAleksa Vučković <aleksav013@gmail.com>2022-01-10 22:43:56 +0100
commit0bff8199e4a800d5ea1bd422dcf06643d6daf008 (patch)
tree15108a0073d66afc026c0a2225474c2d61e71b34 /include/08.heap/heap06.c
parent97d99fa196d4b732cfd6635dcb8b0a9c9228c88d (diff)
include/setup.sh
Diffstat (limited to 'include/08.heap/heap06.c')
-rw-r--r--include/08.heap/heap06.c58
1 files changed, 0 insertions, 58 deletions
diff --git a/include/08.heap/heap06.c b/include/08.heap/heap06.c
deleted file mode 100644
index 5ad2787..0000000
--- a/include/08.heap/heap06.c
+++ /dev/null
@@ -1,58 +0,0 @@
-void *k_heapBMAlloc(KHEAPBM *heap, uint32_t size) {
- KHEAPBLOCKBM *b;
- uint8_t *bm;
- uint32_t bcnt;
- uint32_t x, y, z;
- uint32_t bneed;
- uint8_t nid;
-
- /* iterate blocks */
- for (b = heap->fblock; b; b = b->next) {
- //printf("size:%d,used:%d,bsize:%d,lfb:%d\n",b->size,b->used,b->bsize,b->lfb);
- /* check if block has enough room */
- if (b->size - (b->used * b->bsize) >= size) {
-
- bcnt = b->size / b->bsize;
- bneed = (size / b->bsize) * b->bsize < size ? size / b->bsize + 1 : size / b->bsize;
- bm = (uint8_t*)&b[1];
- //printf("bcnt:%d,bneed:%d,bm:%d\n",bcnt,bneed,bm);
-
- for (x = (b->lfb + 1 >= bcnt ? 0 : b->lfb + 1); x != b->lfb; ++x) {
- /* just wrap around */
- if (x >= bcnt) {
- x = 0;
- }
-
- if (bm[x] == 0) {
- /* count free blocks */
- for (y = 0; bm[x + y] == 0 && y < bneed && (x + y) < bcnt; ++y);
-
- /* we have enough, now allocate them */
- if (y == bneed) {
- /* find ID that does not match left or right */
- nid = k_heapBMGetNID(bm[x - 1], bm[x + y]);
-
- /* allocate by setting id */
- for (z = 0; z < y; ++z) {
- bm[x + z] = nid;
- }
-
- /* optimization */
- b->lfb = (x + bneed) - 2;
-
- /* count used blocks NOT bytes */
- b->used += y;
-
- return (void*)(x * b->bsize + (uintptr_t)&b[1]);
- }
-
- /* x will be incremented by one ONCE more in our FOR loop */
- x += (y - 1);
- continue;
- }
- }
- }
- }
-
- return 0;
-}