diff options
| author | Aleksa Vučković <aleksav013@gmail.com> | 2022-01-31 11:16:38 +0100 |
|---|---|---|
| committer | Aleksa Vučković <aleksav013@gmail.com> | 2022-01-31 14:03:24 +0100 |
| commit | 03035c98b4ba3297b837e96080e78e0aac1e86db (patch) | |
| tree | 55b5f69f83bc8b77ec229062f702c0a4f960e87f /include/08.heap | |
| parent | 11e780fd0f7bed3594de9a7170fadc88c4d48be8 (diff) | |
Promene
Diffstat (limited to 'include/08.heap')
| -rw-r--r-- | include/08.heap/deo1 | 1 | ||||
| -rw-r--r-- | include/08.heap/deo12 | 5 | ||||
| -rw-r--r-- | include/08.heap/deo14 | 4 | ||||
| -rw-r--r-- | include/08.heap/deo19 | 4 | ||||
| -rw-r--r-- | include/08.heap/deo2 | 7 | ||||
| -rw-r--r-- | include/08.heap/deo3 | 3 | ||||
| -rw-r--r-- | include/08.heap/deo4 | 3 | ||||
| -rw-r--r-- | include/08.heap/heap.c | 77 | ||||
| -rw-r--r-- | include/08.heap/heap1.c | 2 | ||||
| -rw-r--r-- | include/08.heap/heap10.c | 7 | ||||
| -rw-r--r-- | include/08.heap/heap11.c (renamed from include/08.heap/deo13) | 3 | ||||
| -rw-r--r-- | include/08.heap/heap12.c | 6 | ||||
| -rw-r--r-- | include/08.heap/heap13.c (renamed from include/08.heap/deo15) | 3 | ||||
| -rw-r--r-- | include/08.heap/heap14.c (renamed from include/08.heap/deo16) | 7 | ||||
| -rw-r--r-- | include/08.heap/heap15.c (renamed from include/08.heap/deo17) | 6 | ||||
| -rw-r--r-- | include/08.heap/heap16.c (renamed from include/08.heap/deo18) | 3 | ||||
| -rw-r--r-- | include/08.heap/heap17.c | 2 | ||||
| -rw-r--r-- | include/08.heap/heap18.c (renamed from include/08.heap/deo20) | 0 | ||||
| -rw-r--r-- | include/08.heap/heap19.c (renamed from include/08.heap/deo21) | 0 | ||||
| -rw-r--r-- | include/08.heap/heap2.c | 4 | ||||
| -rw-r--r-- | include/08.heap/heap20.c (renamed from include/08.heap/deo22) | 0 | ||||
| -rw-r--r-- | include/08.heap/heap21.c (renamed from include/08.heap/deo23) | 3 | ||||
| -rw-r--r-- | include/08.heap/heap22.c (renamed from include/08.heap/deo24) | 3 | ||||
| -rw-r--r-- | include/08.heap/heap23.c (renamed from include/08.heap/deo25) | 13 | ||||
| -rw-r--r-- | include/08.heap/heap24.c (renamed from include/08.heap/deo26) | 0 | ||||
| -rw-r--r-- | include/08.heap/heap25.c (renamed from include/08.heap/deo27) | 0 | ||||
| -rw-r--r-- | include/08.heap/heap26.c (renamed from include/08.heap/deo28) | 0 | ||||
| -rw-r--r-- | include/08.heap/heap27.c (renamed from include/08.heap/deo29) | 0 | ||||
| -rw-r--r-- | include/08.heap/heap28.c (renamed from include/08.heap/deo30) | 0 | ||||
| -rw-r--r-- | include/08.heap/heap29.c (renamed from include/08.heap/deo31) | 0 | ||||
| -rw-r--r-- | include/08.heap/heap3.c (renamed from include/08.heap/deo5) | 4 | ||||
| -rw-r--r-- | include/08.heap/heap4.c (renamed from include/08.heap/deo6) | 0 | ||||
| -rw-r--r-- | include/08.heap/heap5.c (renamed from include/08.heap/deo7) | 0 | ||||
| -rw-r--r-- | include/08.heap/heap6.c (renamed from include/08.heap/deo8) | 0 | ||||
| -rw-r--r-- | include/08.heap/heap7.c (renamed from include/08.heap/deo9) | 0 | ||||
| -rw-r--r-- | include/08.heap/heap8.c (renamed from include/08.heap/deo10) | 0 | ||||
| -rw-r--r-- | include/08.heap/heap9.c (renamed from include/08.heap/deo11) | 0 |
37 files changed, 87 insertions, 83 deletions
diff --git a/include/08.heap/deo1 b/include/08.heap/deo1 deleted file mode 100644 index c45e28b..0000000 --- a/include/08.heap/deo1 +++ /dev/null @@ -1 +0,0 @@ -#include<types.h> diff --git a/include/08.heap/deo12 b/include/08.heap/deo12 deleted file mode 100644 index 84c5721..0000000 --- a/include/08.heap/deo12 +++ /dev/null @@ -1,5 +0,0 @@ -static uint8_t k_heapBMGetNID(uint8_t a, uint8_t b) { - uint8_t c; - for (c = a + 1; c == b || c == 0; ++c); - return c; -} diff --git a/include/08.heap/deo14 b/include/08.heap/deo14 deleted file mode 100644 index 8bfb6f0..0000000 --- a/include/08.heap/deo14 +++ /dev/null @@ -1,4 +0,0 @@ - /* iterate blocks */ - for (b = heap->fblock; b; b = b->next) { - /* check if block has enough room */ - if (b->size - (b->used * b->bsize) >= size) { diff --git a/include/08.heap/deo19 b/include/08.heap/deo19 deleted file mode 100644 index 9b49484..0000000 --- a/include/08.heap/deo19 +++ /dev/null @@ -1,4 +0,0 @@ - /* allocate by setting id */ - for (z = 0; z < y; ++z) { - bm[x + z] = nid; - } diff --git a/include/08.heap/deo2 b/include/08.heap/deo2 deleted file mode 100644 index 2a69314..0000000 --- a/include/08.heap/deo2 +++ /dev/null @@ -1,7 +0,0 @@ -typedef struct _KHEAPBLOCKBM { - struct _KHEAPBLOCKBM *next; - uint32_t size; - uint32_t used; - uint32_t bsize; - uint32_t lfb; -} KHEAPBLOCKBM; diff --git a/include/08.heap/deo3 b/include/08.heap/deo3 deleted file mode 100644 index d07786d..0000000 --- a/include/08.heap/deo3 +++ /dev/null @@ -1,3 +0,0 @@ -typedef struct _KHEAPBM { - KHEAPBLOCKBM *fblock; -} KHEAPBM; diff --git a/include/08.heap/deo4 b/include/08.heap/deo4 deleted file mode 100644 index c2dc3b5..0000000 --- a/include/08.heap/deo4 +++ /dev/null @@ -1,3 +0,0 @@ -void k_heapBMInit(KHEAPBM *heap) { - heap->fblock = 0; -} diff --git a/include/08.heap/heap.c b/include/08.heap/heap.c index 99d0a37..3120ed8 100644 --- a/include/08.heap/heap.c +++ b/include/08.heap/heap.c @@ -1,22 +1,14 @@ +#include<source/heap.h> #include<types.h> -typedef struct _KHEAPBLOCKBM { - struct _KHEAPBLOCKBM *next; - uint32_t size; - uint32_t used; - uint32_t bsize; - uint32_t lfb; -} KHEAPBLOCKBM; - -typedef struct _KHEAPBM { - KHEAPBLOCKBM *fblock; -} KHEAPBM; - -void k_heapBMInit(KHEAPBM *heap) { +void k_heapBMInit(KHEAPBM *heap) +{ heap->fblock = 0; } -int k_heapBMAddBlock(KHEAPBM *heap, uintptr_t addr, uint32_t size, uint32_t bsize) { +int k_heapBMAddBlock(KHEAPBM *heap, uintptr_t addr, uint32_t size, uint32_t + bsize) +{ KHEAPBLOCKBM *b; uint32_t bcnt; uint32_t x; @@ -48,13 +40,16 @@ int k_heapBMAddBlock(KHEAPBM *heap, uintptr_t addr, uint32_t size, uint32_t bsiz return 1; } -static uint8_t k_heapBMGetNID(uint8_t a, uint8_t b) { +static uint8_t k_heapBMGetNID(uint8_t a, uint8_t b); +static uint8_t k_heapBMGetNID(uint8_t a, uint8_t b) +{ uint8_t c; - for (c = a + 1; c == b || c == 0; ++c); + for (c=a+1;c==b||c==0;++c); return c; } -void *k_heapBMAlloc(KHEAPBM *heap, uint32_t size) { +void *k_heapBMAlloc(KHEAPBM *heap, uint32_t size) +{ KHEAPBLOCKBM *b; uint8_t *bm; uint32_t bcnt; @@ -63,33 +58,36 @@ void *k_heapBMAlloc(KHEAPBM *heap, uint32_t size) { uint8_t nid; /* iterate blocks */ - for (b = heap->fblock; b; b = b->next) { + for (b = heap->fblock; b; b = b->next) + { /* check if block has enough room */ - if (b->size - (b->used * b->bsize) >= size) { + 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; + bneed = (size / b->bsize) * b->bsize < size ? size / b->bsize + 1 : + size / b->bsize; bm = (uint8_t*)&b[1]; - for (x = (b->lfb + 1 >= bcnt ? 0 : b->lfb + 1); x != b->lfb; ++x) { + for (x = (b->lfb + 1 >= bcnt ? 0 : b->lfb + 1); x != b->lfb; ++x) + { /* just wrap around */ - if (x >= bcnt) { - x = 0; - } + if (x >= bcnt) x = 0; - if (bm[x] == 0) { + if (bm[x] == 0) + { /* count free blocks */ - for (y = 0; bm[x + y] == 0 && y < bneed && (x + y) < bcnt; ++y); + for (y = 0; bm[x + y] == 0 && y < bneed && (x + y) < bcnt; + ++y); /* we have enough, now allocate them */ - if (y == bneed) { + 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; - } + for (z = 0; z < y; ++z) bm[x + z] = nid; /* optimization */ b->lfb = (x + bneed) - 2; @@ -100,7 +98,8 @@ void *k_heapBMAlloc(KHEAPBM *heap, uint32_t size) { return (void*)(x * b->bsize + (uintptr_t)&b[1]); } - /* x will be incremented by one ONCE more in our FOR loop */ + /* x will be incremented by one ONCE more in our FOR loop + * */ x += (y - 1); continue; } @@ -110,7 +109,8 @@ void *k_heapBMAlloc(KHEAPBM *heap, uint32_t size) { return 0; } -void k_heapBMFree(KHEAPBM *heap, void *ptr) { +void k_heapBMFree(KHEAPBM *heap, void *ptr) +{ KHEAPBLOCKBM *b; uintptr_t ptroff; uint32_t bi, x; @@ -118,10 +118,13 @@ void k_heapBMFree(KHEAPBM *heap, void *ptr) { uint8_t id; uint32_t max; - for (b = heap->fblock; b; b = b->next) { - if ((uintptr_t)ptr > (uintptr_t)b && (uintptr_t)ptr < (uintptr_t)b + sizeof(KHEAPBLOCKBM) + b->size) { + for (b = heap->fblock; b; b = b->next) + { + if ((uintptr_t)ptr > (uintptr_t)b && (uintptr_t)ptr < (uintptr_t)b + + sizeof(KHEAPBLOCKBM) + b->size) { /* found block */ - ptroff = (uintptr_t)ptr - (uintptr_t)&b[1]; /* get offset to get block */ + ptroff = (uintptr_t)ptr - (uintptr_t)&b[1]; /* get offset to get + block */ /* block offset in BM */ bi = ptroff / b->bsize; /* .. */ @@ -130,9 +133,7 @@ void k_heapBMFree(KHEAPBM *heap, void *ptr) { id = bm[bi]; /* oddly.. GCC did not optimize this */ max = b->size / b->bsize; - for (x = bi; bm[x] == id && x < max; ++x) { - bm[x] = 0; - } + for (x = bi; bm[x] == id && x < max; ++x) bm[x] = 0; /* update free block count */ b->used -= x - bi; return; diff --git a/include/08.heap/heap1.c b/include/08.heap/heap1.c new file mode 100644 index 0000000..24aa321 --- /dev/null +++ b/include/08.heap/heap1.c @@ -0,0 +1,2 @@ +#include<source/heap.h> +#include<types.h> diff --git a/include/08.heap/heap10.c b/include/08.heap/heap10.c new file mode 100644 index 0000000..15505de --- /dev/null +++ b/include/08.heap/heap10.c @@ -0,0 +1,7 @@ +static uint8_t k_heapBMGetNID(uint8_t a, uint8_t b); +static uint8_t k_heapBMGetNID(uint8_t a, uint8_t b) +{ + uint8_t c; + for (c=a+1;c==b||c==0;++c); + return c; +} diff --git a/include/08.heap/deo13 b/include/08.heap/heap11.c index 369c3f3..90d72d6 100644 --- a/include/08.heap/deo13 +++ b/include/08.heap/heap11.c @@ -1,4 +1,5 @@ -void *k_heapBMAlloc(KHEAPBM *heap, uint32_t size) { +void *k_heapBMAlloc(KHEAPBM *heap, uint32_t size) +{ KHEAPBLOCKBM *b; uint8_t *bm; uint32_t bcnt; diff --git a/include/08.heap/heap12.c b/include/08.heap/heap12.c new file mode 100644 index 0000000..78285eb --- /dev/null +++ b/include/08.heap/heap12.c @@ -0,0 +1,6 @@ + /* iterate blocks */ + for (b = heap->fblock; b; b = b->next) + { + /* check if block has enough room */ + if (b->size - (b->used * b->bsize) >= size) + { diff --git a/include/08.heap/deo15 b/include/08.heap/heap13.c index a867821..ab7463c 100644 --- a/include/08.heap/deo15 +++ b/include/08.heap/heap13.c @@ -1,3 +1,4 @@ bcnt = b->size / b->bsize; - bneed = (size / b->bsize) * b->bsize < size ? size / b->bsize + 1 : size / b->bsize; + bneed = (size / b->bsize) * b->bsize < size ? size / b->bsize + 1 : + size / b->bsize; bm = (uint8_t*)&b[1]; diff --git a/include/08.heap/deo16 b/include/08.heap/heap14.c index 78f4675..6edcf6f 100644 --- a/include/08.heap/deo16 +++ b/include/08.heap/heap14.c @@ -1,5 +1,4 @@ - for (x = (b->lfb + 1 >= bcnt ? 0 : b->lfb + 1); x != b->lfb; ++x) { + for (x = (b->lfb + 1 >= bcnt ? 0 : b->lfb + 1); x != b->lfb; ++x) + { /* just wrap around */ - if (x >= bcnt) { - x = 0; - } + if (x >= bcnt) x = 0; diff --git a/include/08.heap/deo17 b/include/08.heap/heap15.c index d20be4c..5a39a40 100644 --- a/include/08.heap/deo17 +++ b/include/08.heap/heap15.c @@ -1,3 +1,5 @@ - if (bm[x] == 0) { + if (bm[x] == 0) + { /* count free blocks */ - for (y = 0; bm[x + y] == 0 && y < bneed && (x + y) < bcnt; ++y); + for (y = 0; bm[x + y] == 0 && y < bneed && (x + y) < bcnt; + ++y); diff --git a/include/08.heap/deo18 b/include/08.heap/heap16.c index fed8463..206a5e0 100644 --- a/include/08.heap/deo18 +++ b/include/08.heap/heap16.c @@ -1,4 +1,5 @@ /* we have enough, now allocate them */ - if (y == bneed) { + if (y == bneed) + { /* find ID that does not match left or right */ nid = k_heapBMGetNID(bm[x - 1], bm[x + y]); diff --git a/include/08.heap/heap17.c b/include/08.heap/heap17.c new file mode 100644 index 0000000..ff94380 --- /dev/null +++ b/include/08.heap/heap17.c @@ -0,0 +1,2 @@ + /* allocate by setting id */ + for (z = 0; z < y; ++z) bm[x + z] = nid; diff --git a/include/08.heap/deo20 b/include/08.heap/heap18.c index f141368..f141368 100644 --- a/include/08.heap/deo20 +++ b/include/08.heap/heap18.c diff --git a/include/08.heap/deo21 b/include/08.heap/heap19.c index 8fd73e2..8fd73e2 100644 --- a/include/08.heap/deo21 +++ b/include/08.heap/heap19.c diff --git a/include/08.heap/heap2.c b/include/08.heap/heap2.c new file mode 100644 index 0000000..8757694 --- /dev/null +++ b/include/08.heap/heap2.c @@ -0,0 +1,4 @@ +void k_heapBMInit(KHEAPBM *heap) +{ + heap->fblock = 0; +} diff --git a/include/08.heap/deo22 b/include/08.heap/heap20.c index 98c1157..98c1157 100644 --- a/include/08.heap/deo22 +++ b/include/08.heap/heap20.c diff --git a/include/08.heap/deo23 b/include/08.heap/heap21.c index 21aa8c8..2ed3f8c 100644 --- a/include/08.heap/deo23 +++ b/include/08.heap/heap21.c @@ -1,4 +1,5 @@ - /* x will be incremented by one ONCE more in our FOR loop */ + /* x will be incremented by one ONCE more in our FOR loop + * */ x += (y - 1); continue; } diff --git a/include/08.heap/deo24 b/include/08.heap/heap22.c index e9c116b..88cd773 100644 --- a/include/08.heap/deo24 +++ b/include/08.heap/heap22.c @@ -1,4 +1,5 @@ -void k_heapBMFree(KHEAPBM *heap, void *ptr) { +void k_heapBMFree(KHEAPBM *heap, void *ptr) +{ KHEAPBLOCKBM *b; uintptr_t ptroff; uint32_t bi, x; diff --git a/include/08.heap/deo25 b/include/08.heap/heap23.c index 1a22151..70305b7 100644 --- a/include/08.heap/deo25 +++ b/include/08.heap/heap23.c @@ -1,7 +1,10 @@ - for (b = heap->fblock; b; b = b->next) { - if ((uintptr_t)ptr > (uintptr_t)b && (uintptr_t)ptr < (uintptr_t)b + sizeof(KHEAPBLOCKBM) + b->size) { + for (b = heap->fblock; b; b = b->next) + { + if ((uintptr_t)ptr > (uintptr_t)b && (uintptr_t)ptr < (uintptr_t)b + + sizeof(KHEAPBLOCKBM) + b->size) { /* found block */ - ptroff = (uintptr_t)ptr - (uintptr_t)&b[1]; /* get offset to get block */ + ptroff = (uintptr_t)ptr - (uintptr_t)&b[1]; /* get offset to get + block */ /* block offset in BM */ bi = ptroff / b->bsize; /* .. */ @@ -10,9 +13,7 @@ id = bm[bi]; /* oddly.. GCC did not optimize this */ max = b->size / b->bsize; - for (x = bi; bm[x] == id && x < max; ++x) { - bm[x] = 0; - } + for (x = bi; bm[x] == id && x < max; ++x) bm[x] = 0; /* update free block count */ b->used -= x - bi; return; diff --git a/include/08.heap/deo26 b/include/08.heap/heap24.c index 11a4697..11a4697 100644 --- a/include/08.heap/deo26 +++ b/include/08.heap/heap24.c diff --git a/include/08.heap/deo27 b/include/08.heap/heap25.c index 6ac75a9..6ac75a9 100644 --- a/include/08.heap/deo27 +++ b/include/08.heap/heap25.c diff --git a/include/08.heap/deo28 b/include/08.heap/heap26.c index 6d7223d..6d7223d 100644 --- a/include/08.heap/deo28 +++ b/include/08.heap/heap26.c diff --git a/include/08.heap/deo29 b/include/08.heap/heap27.c index de767a2..de767a2 100644 --- a/include/08.heap/deo29 +++ b/include/08.heap/heap27.c diff --git a/include/08.heap/deo30 b/include/08.heap/heap28.c index 22972f4..22972f4 100644 --- a/include/08.heap/deo30 +++ b/include/08.heap/heap28.c diff --git a/include/08.heap/deo31 b/include/08.heap/heap29.c index d4d5941..d4d5941 100644 --- a/include/08.heap/deo31 +++ b/include/08.heap/heap29.c diff --git a/include/08.heap/deo5 b/include/08.heap/heap3.c index 8eda156..dceb8a3 100644 --- a/include/08.heap/deo5 +++ b/include/08.heap/heap3.c @@ -1,4 +1,6 @@ -int k_heapBMAddBlock(KHEAPBM *heap, uintptr_t addr, uint32_t size, uint32_t bsize) { +int k_heapBMAddBlock(KHEAPBM *heap, uintptr_t addr, uint32_t size, uint32_t + bsize) +{ KHEAPBLOCKBM *b; uint32_t bcnt; uint32_t x; diff --git a/include/08.heap/deo6 b/include/08.heap/heap4.c index 024a09a..024a09a 100644 --- a/include/08.heap/deo6 +++ b/include/08.heap/heap4.c diff --git a/include/08.heap/deo7 b/include/08.heap/heap5.c index c816b2c..c816b2c 100644 --- a/include/08.heap/deo7 +++ b/include/08.heap/heap5.c diff --git a/include/08.heap/deo8 b/include/08.heap/heap6.c index 3a9ea03..3a9ea03 100644 --- a/include/08.heap/deo8 +++ b/include/08.heap/heap6.c diff --git a/include/08.heap/deo9 b/include/08.heap/heap7.c index 7183948..7183948 100644 --- a/include/08.heap/deo9 +++ b/include/08.heap/heap7.c diff --git a/include/08.heap/deo10 b/include/08.heap/heap8.c index fd64ff9..fd64ff9 100644 --- a/include/08.heap/deo10 +++ b/include/08.heap/heap8.c diff --git a/include/08.heap/deo11 b/include/08.heap/heap9.c index a61193b..a61193b 100644 --- a/include/08.heap/deo11 +++ b/include/08.heap/heap9.c |
