Skip to content

Commit a54e101

Browse files
Nicolas Pitrenashif
authored andcommitted
lib/os/heap: rename struct z_heap.len to struct z_heap.end_chunk
The end marker chunk was represented by the len field of struct z_heap. It is now renamed to end_chunk to make it more obvious what it is. And while at it... Given that it is used in size_too_big() to cap the allocation size already, we no longer need to test the bucket index against the biggest index possible derived from end_chunk in alloc_chunk(). The corresponding bucket_idx() call is relatively expensive on some architectures so avoiding it (turning it into a CHECK() instead) is a good thing. Signed-off-by: Nicolas Pitre <[email protected]>
1 parent e8910f6 commit a54e101

File tree

3 files changed

+20
-26
lines changed

3 files changed

+20
-26
lines changed

lib/os/heap-validate.c

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
static size_t max_chunkid(struct z_heap *h)
2121
{
22-
return h->len - min_chunk_size(h);
22+
return h->end_chunk - min_chunk_size(h);
2323
}
2424

2525
#define VALIDATE(cond) do { if (!(cond)) { return false; } } while (0)
@@ -28,14 +28,14 @@ static bool in_bounds(struct z_heap *h, chunkid_t c)
2828
{
2929
VALIDATE(c >= right_chunk(h, 0));
3030
VALIDATE(c <= max_chunkid(h));
31-
VALIDATE(chunk_size(h, c) < h->len);
31+
VALIDATE(chunk_size(h, c) < h->end_chunk);
3232
return true;
3333
}
3434

3535
static bool valid_chunk(struct z_heap *h, chunkid_t c)
3636
{
3737
VALIDATE(chunk_size(h, c) > 0);
38-
VALIDATE(c + chunk_size(h, c) <= h->len);
38+
VALIDATE(c + chunk_size(h, c) <= h->end_chunk);
3939
VALIDATE(in_bounds(h, c));
4040
VALIDATE(right_chunk(h, left_chunk(h, c)) == c);
4141
VALIDATE(left_chunk(h, right_chunk(h, c)) == c);
@@ -85,15 +85,15 @@ bool sys_heap_validate(struct sys_heap *heap)
8585
return false;
8686
}
8787
}
88-
if (c != h->len) {
88+
if (c != h->end_chunk) {
8989
return false; /* Should have exactly consumed the buffer */
9090
}
9191

9292
/* Check the free lists: entry count should match, empty bit
9393
* should be correct, and all chunk entries should point into
9494
* valid unused chunks. Mark those chunks USED, temporarily.
9595
*/
96-
for (int b = 0; b <= bucket_idx(h, h->len); b++) {
96+
for (int b = 0; b <= bucket_idx(h, h->end_chunk); b++) {
9797
chunkid_t c0 = h->buckets[b].next;
9898
uint32_t n = 0;
9999

@@ -137,15 +137,15 @@ bool sys_heap_validate(struct sys_heap *heap)
137137

138138
set_chunk_used(h, c, solo_free_header(h, c));
139139
}
140-
if (c != h->len) {
140+
if (c != h->end_chunk) {
141141
return false; /* Should have exactly consumed the buffer */
142142
}
143143

144144
/* Go through the free lists again checking that the linear
145145
* pass caught all the blocks and that they now show UNUSED.
146146
* Mark them USED.
147147
*/
148-
for (int b = 0; b <= bucket_idx(h, h->len); b++) {
148+
for (int b = 0; b <= bucket_idx(h, h->end_chunk); b++) {
149149
chunkid_t c0 = h->buckets[b].next;
150150
int n = 0;
151151

@@ -318,11 +318,11 @@ void sys_heap_stress(void *(*alloc)(void *arg, size_t bytes),
318318
*/
319319
void heap_print_info(struct z_heap *h, bool dump_chunks)
320320
{
321-
int i, nb_buckets = bucket_idx(h, h->len) + 1;
321+
int i, nb_buckets = bucket_idx(h, h->end_chunk) + 1;
322322
size_t free_bytes, allocated_bytes, total, overhead;
323323

324324
printk("Heap at %p contains %d units in %d buckets\n\n",
325-
chunk_buf(h), h->len, nb_buckets);
325+
chunk_buf(h), h->end_chunk, nb_buckets);
326326

327327
printk(" bucket# min units total largest largest\n"
328328
" threshold chunks (units) (bytes)\n"
@@ -352,7 +352,7 @@ void heap_print_info(struct z_heap *h, bool dump_chunks)
352352
}
353353
free_bytes = allocated_bytes = 0;
354354
for (chunkid_t c = 0; ; c = right_chunk(h, c)) {
355-
if (c == 0 || c == h->len) {
355+
if (c == 0 || c == h->end_chunk) {
356356
/* those are always allocated for internal purposes */
357357
} else if (chunk_used(h, c)) {
358358
allocated_bytes += chunk_size(h, c) * CHUNK_UNIT
@@ -371,16 +371,13 @@ void heap_print_info(struct z_heap *h, bool dump_chunks)
371371
left_chunk(h, c),
372372
right_chunk(h, c));
373373
}
374-
if (c == h->len) {
374+
if (c == h->end_chunk) {
375375
break;
376376
}
377377
}
378378

379-
/*
380-
* The final chunk at h->len is just a header serving as a end
381-
* marker. It is part of the overhead.
382-
*/
383-
total = h->len * CHUNK_UNIT + chunk_header_bytes(h);
379+
/* The end marker chunk has a header. It is part of the overhead. */
380+
total = h->end_chunk * CHUNK_UNIT + chunk_header_bytes(h);
384381
overhead = total - free_bytes - allocated_bytes;
385382
printk("\n%zd free bytes, %zd allocated bytes, overhead = %zd bytes (%zd.%zd%%)\n",
386383
free_bytes, allocated_bytes, overhead,

lib/os/heap.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,7 @@ static chunkid_t alloc_chunk(struct z_heap *h, size_t sz)
172172
int bi = bucket_idx(h, sz);
173173
struct z_heap_bucket *b = &h->buckets[bi];
174174

175-
if (bi > bucket_idx(h, h->len)) {
176-
return 0;
177-
}
175+
CHECK(bi <= bucket_idx(h, h->end_chunk));
178176

179177
/* First try a bounded count of items from the minimal bucket
180178
* size. These may not fit, trying (e.g.) three means that
@@ -383,7 +381,7 @@ void sys_heap_init(struct sys_heap *heap, void *mem, size_t bytes)
383381
/* Must fit in a 31 bit count of HUNK_UNIT */
384382
__ASSERT(bytes / CHUNK_UNIT <= 0x7fffffffU, "heap size is too big");
385383

386-
/* Reserve the final marker chunk's header */
384+
/* Reserve the end marker chunk's header */
387385
__ASSERT(bytes > heap_footer_bytes(bytes), "heap size is too small");
388386
bytes -= heap_footer_bytes(bytes);
389387

@@ -398,7 +396,7 @@ void sys_heap_init(struct sys_heap *heap, void *mem, size_t bytes)
398396
struct z_heap *h = (struct z_heap *)addr;
399397
heap->heap = h;
400398
h->chunk0_hdr_area = 0;
401-
h->len = buf_sz;
399+
h->end_chunk = buf_sz;
402400
h->avail_buckets = 0;
403401

404402
int nb_buckets = bucket_idx(h, buf_sz) + 1;

lib/os/heap.h

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ struct z_heap_bucket {
6464

6565
struct z_heap {
6666
uint64_t chunk0_hdr_area; /* matches the largest header */
67-
uint32_t len;
67+
chunkid_t end_chunk;
6868
uint32_t avail_buckets;
6969
struct z_heap_bucket buckets[0];
7070
};
@@ -81,7 +81,7 @@ static inline bool big_heap_bytes(size_t bytes)
8181

8282
static inline bool big_heap(struct z_heap *h)
8383
{
84-
return big_heap_chunks(h->len);
84+
return big_heap_chunks(h->end_chunk);
8585
}
8686

8787
static inline chunk_unit_t *chunk_buf(struct z_heap *h)
@@ -106,7 +106,7 @@ static inline size_t chunk_field(struct z_heap *h, chunkid_t c,
106106
static inline void chunk_set(struct z_heap *h, chunkid_t c,
107107
enum chunk_fields f, chunkid_t val)
108108
{
109-
CHECK(c <= h->len);
109+
CHECK(c <= h->end_chunk);
110110

111111
chunk_unit_t *buf = chunk_buf(h);
112112
void *cmem = &buf[c];
@@ -239,9 +239,8 @@ static inline bool size_too_big(struct z_heap *h, size_t bytes)
239239
/*
240240
* Quick check to bail out early if size is too big.
241241
* Also guards against potential arithmetic overflows elsewhere.
242-
* There is a minimum of one chunk always in use by the heap header.
243242
*/
244-
return (bytes / CHUNK_UNIT) >= h->len;
243+
return (bytes / CHUNK_UNIT) >= h->end_chunk;
245244
}
246245

247246
/* For debugging */

0 commit comments

Comments
 (0)