sys_heap: introduce min_chunk_size()

With this we can remove magic constants, especially those used with
big_heap().

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
This commit is contained in:
Nicolas Pitre 2019-09-25 17:15:18 -04:00 committed by Carles Cufí
parent e553161b8e
commit 6d827fa080
3 changed files with 12 additions and 6 deletions

View file

@ -19,7 +19,7 @@
static size_t max_chunkid(struct z_heap *h)
{
return h->len - bytes_to_chunksz(h, 1);
return h->len - min_chunk_size(h);
}
static bool in_bounds(struct z_heap *h, chunkid_t c)

View file

@ -92,7 +92,7 @@ static void *split_alloc(struct z_heap *h, int bidx, size_t sz)
CHECK(rem < h->len);
if (rem >= (big_heap(h) ? 2 : 1)) {
if (rem >= min_chunk_size(h)) {
chunkid_t c2 = c + sz;
chunkid_t c3 = right_chunk(h, c);
@ -224,7 +224,7 @@ void sys_heap_init(struct sys_heap *heap, void *mem, size_t bytes)
size_t chunk0_size = chunksz(sizeof(struct z_heap) +
nb_buckets * sizeof(struct z_heap_bucket));
CHECK(chunk0_size < buf_sz);
CHECK(chunk0_size + min_chunk_size(h) < buf_sz);
for (int i = 0; i < nb_buckets; i++) {
h->buckets[i].list_size = 0;

View file

@ -199,10 +199,16 @@ static inline size_t bytes_to_chunksz(struct z_heap *h, size_t bytes)
return chunksz(chunk_header_bytes(h) + bytes);
}
static int bucket_idx(struct z_heap *h, size_t sz)
static inline int min_chunk_size(struct z_heap *h)
{
/* A chunk of size 2 is the minimum size on big heaps */
return 31 - __builtin_clz(sz) - (big_heap(h) ? 1 : 0);
return bytes_to_chunksz(h, 1);
}
static inline int bucket_idx(struct z_heap *h, size_t sz)
{
size_t usable_sz = sz - min_chunk_size(h) + 1;
return 31 - __builtin_clz(usable_sz);
}
#endif /* ZEPHYR_INCLUDE_LIB_OS_HEAP_H_ */