Skip to content

Commit 9ecaa16

Browse files
committed
Unify redundant low/high_address computation to save a bit of code size.
1 parent 993a581 commit 9ecaa16

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

supervisor/shared/memory.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,7 @@ supervisor_allocation* allocation_from_ptr(void *ptr) {
134134
}
135135

136136
supervisor_allocation* allocate_remaining_memory(void) {
137-
uint32_t* low_address = low_head ? low_head->data + low_head->length / 4 : port_heap_get_bottom();
138-
uint32_t* high_address = high_head ? (uint32_t*)high_head : port_heap_get_top();
139-
return allocate_memory((high_address - low_address) * 4 - sizeof(supervisor_allocation_node), false, false);
137+
return allocate_memory((uint32_t)-1, false, false);
140138
}
141139

142140
static supervisor_allocation_node* find_hole(supervisor_allocation_node* node, size_t length) {
@@ -152,15 +150,19 @@ static supervisor_allocation_node* allocate_memory_node(uint32_t length, bool hi
152150
// supervisor_move_memory() currently does not support movable allocations on the high side, it
153151
// must be extended first if this is ever needed.
154152
assert(!(high && movable));
153+
uint32_t* low_address = low_head ? low_head->data + low_head->length / 4 : port_heap_get_bottom();
154+
uint32_t* high_address = high_head ? (uint32_t*)high_head : port_heap_get_top();
155+
// Special case for allocate_remaining_memory(), avoids computing low/high_address twice.
156+
if (length == (uint32_t)-1) {
157+
length = (high_address - low_address) * 4 - sizeof(supervisor_allocation_node);
158+
}
155159
if (length == 0 || length % 4 != 0) {
156160
return NULL;
157161
}
158162
// 1. Matching hole on the requested side?
159163
supervisor_allocation_node* node = find_hole(high ? high_head : low_head, length);
160164
if (!node) {
161165
// 2. Enough free space in the middle?
162-
uint32_t* low_address = low_head ? low_head->data + low_head->length / 4 : port_heap_get_bottom();
163-
uint32_t* high_address = high_head ? (uint32_t*)high_head : port_heap_get_top();
164166
if ((high_address - low_address) * 4 >= (int32_t)(sizeof(supervisor_allocation_node) + length)) {
165167
if (high) {
166168
high_address -= (sizeof(supervisor_allocation_node) + length) / 4;

0 commit comments

Comments
 (0)