Skip to content

Commit 780f402

Browse files
committed
lib, kernel: use single evaluation min/max/clamp
Replace all in-function instances of MIN/MAX/CLAMP with the single evaluation version min/max/clamp. There's probably no race conditions in these files, but the single evaluation ones save a couple of instructions each so they should save few code bytes and potentially perform better, so they should be preferred in general. Signed-off-by: Fabio Baltieri <[email protected]>
1 parent 43084fe commit 780f402

File tree

21 files changed

+39
-39
lines changed

21 files changed

+39
-39
lines changed

kernel/init.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@ void __weak z_early_rand_get(uint8_t *buf, size_t length)
515515
state = state + k_cycle_get_32();
516516
state = state * 2862933555777941757ULL + 3037000493ULL;
517517
val = (uint32_t)(state >> 32);
518-
rc = MIN(length, sizeof(val));
518+
rc = min(length, sizeof(val));
519519
arch_early_memcpy((void *)buf, &val, rc);
520520

521521
length -= rc;

kernel/mem_slab.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ int k_mem_slab_alloc(struct k_mem_slab *slab, void **mem, k_timeout_t timeout)
237237
"slab corruption detected");
238238

239239
#ifdef CONFIG_MEM_SLAB_TRACE_MAX_UTILIZATION
240-
slab->info.max_used = MAX(slab->info.num_used,
240+
slab->info.max_used = max(slab->info.num_used,
241241
slab->info.max_used);
242242
#endif /* CONFIG_MEM_SLAB_TRACE_MAX_UTILIZATION */
243243

kernel/mmu.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -274,8 +274,8 @@ static void virt_region_free(void *vaddr, size_t size)
274274
(vaddr_u8 < Z_VIRT_REGION_END_ADDR)) ||
275275
(((vaddr_u8 + size - 1) >= Z_VIRT_REGION_START_ADDR) &&
276276
((vaddr_u8 + size - 1) < Z_VIRT_REGION_END_ADDR))) {
277-
uint8_t *adjusted_start = MAX(vaddr_u8, Z_VIRT_REGION_START_ADDR);
278-
uint8_t *adjusted_end = MIN(vaddr_u8 + size,
277+
uint8_t *adjusted_start = max(vaddr_u8, Z_VIRT_REGION_START_ADDR);
278+
uint8_t *adjusted_end = min(vaddr_u8 + size,
279279
Z_VIRT_REGION_END_ADDR);
280280
size_t adjusted_sz = adjusted_end - adjusted_start;
281281

@@ -930,8 +930,8 @@ void k_mem_map_phys_bare(uint8_t **virt_ptr, uintptr_t phys, size_t size, uint32
930930
IN_RANGE(aligned_phys + aligned_size - 1,
931931
(uintptr_t)K_MEM_VIRT_RAM_START,
932932
(uintptr_t)(K_MEM_VIRT_RAM_END - 1))) {
933-
uint8_t *adjusted_start = MAX(dest_addr, K_MEM_VIRT_RAM_START);
934-
uint8_t *adjusted_end = MIN(dest_addr + aligned_size,
933+
uint8_t *adjusted_start = max(dest_addr, K_MEM_VIRT_RAM_START);
934+
uint8_t *adjusted_end = min(dest_addr + aligned_size,
935935
K_MEM_VIRT_RAM_END);
936936
size_t adjusted_sz = adjusted_end - adjusted_start;
937937

kernel/pipe.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ static size_t copy_to_pending_readers(struct k_pipe *pipe, bool *need_resched,
113113
}
114114

115115
reader_buf = reader->base.swap_data;
116-
copy_size = MIN(len - written,
116+
copy_size = min(len - written,
117117
reader_buf->len - reader_buf->used);
118118
memcpy(&reader_buf->data[reader_buf->used],
119119
&data[written], copy_size);

kernel/sched.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1005,7 +1005,7 @@ void z_impl_k_thread_absolute_deadline_set(k_tid_t tid, int deadline)
10051005
void z_impl_k_thread_deadline_set(k_tid_t tid, int deadline)
10061006
{
10071007

1008-
deadline = CLAMP(deadline, 0, INT_MAX);
1008+
deadline = clamp(deadline, 0, INT_MAX);
10091009

10101010
int32_t newdl = k_cycle_get_32() + deadline;
10111011

@@ -1139,7 +1139,7 @@ int32_t z_impl_k_sleep(k_timeout_t timeout)
11391139

11401140
/* k_sleep() still returns 32 bit milliseconds for compatibility */
11411141
int64_t ms = K_TIMEOUT_EQ(timeout, K_FOREVER) ? K_TICKS_FOREVER :
1142-
CLAMP(k_ticks_to_ms_ceil64(ticks), 0, INT_MAX);
1142+
clamp(k_ticks_to_ms_ceil64(ticks), 0, INT_MAX);
11431143

11441144
SYS_PORT_TRACING_FUNC_EXIT(k_thread, sleep, timeout, ms);
11451145
return (int32_t) ms;

kernel/thread.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ static size_t copy_bytes(char *dest, size_t dest_size, const char *src, size_t s
217217
{
218218
size_t bytes_to_copy;
219219

220-
bytes_to_copy = MIN(dest_size, src_size);
220+
bytes_to_copy = min(dest_size, src_size);
221221
memcpy(dest, src, bytes_to_copy);
222222

223223
return bytes_to_copy;

kernel/timeout.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ static int32_t next_timeout(int32_t ticks_elapsed)
9090
((int64_t)(to->dticks - ticks_elapsed) > (int64_t)INT_MAX)) {
9191
ret = SYS_CLOCK_MAX_WAIT;
9292
} else {
93-
ret = MAX(0, to->dticks - ticks_elapsed);
93+
ret = max(0, to->dticks - ticks_elapsed);
9494
}
9595

9696
return ret;
@@ -124,7 +124,7 @@ k_ticks_t z_add_timeout(struct _timeout *to, _timeout_func_t fn, k_timeout_t tim
124124
} else {
125125
k_ticks_t dticks = Z_TICK_ABS(timeout.ticks) - curr_tick;
126126

127-
to->dticks = MAX(1, dticks);
127+
to->dticks = max(1, dticks);
128128
ticks = timeout.ticks;
129129
}
130130

@@ -322,7 +322,7 @@ k_timepoint_t sys_timepoint_calc(k_timeout_t timeout)
322322
k_ticks_t dt = timeout.ticks;
323323

324324
if (Z_IS_TIMEOUT_RELATIVE(timeout)) {
325-
timepoint.tick = sys_clock_tick_get() + MAX(1, dt);
325+
timepoint.tick = sys_clock_tick_get() + max(1, dt);
326326
} else {
327327
timepoint.tick = Z_TICK_ABS(dt);
328328
}

kernel/timer.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ void z_timer_expiration_handler(struct _timeout *t)
5757
k_timeout_t next = timer->period;
5858

5959
/* see note about z_add_timeout() in z_impl_k_timer_start() */
60-
next.ticks = MAX(next.ticks - 1, 0);
60+
next.ticks = max(next.ticks - 1, 0);
6161

6262
#ifdef CONFIG_TIMEOUT_64BIT
6363
/* Exploit the fact that uptime during a kernel
@@ -171,7 +171,7 @@ void z_impl_k_timer_start(struct k_timer *timer, k_timeout_t duration,
171171
* is consistent for both 32-bit k_ticks_t which are unsigned
172172
* and 64-bit k_ticks_t which are signed.
173173
*/
174-
duration.ticks = MAX(1, duration.ticks);
174+
duration.ticks = max(1, duration.ticks);
175175
duration.ticks = duration.ticks - 1;
176176
}
177177

lib/heap/heap.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
static inline void increase_allocated_bytes(struct z_heap *h, size_t num_bytes)
1818
{
1919
h->allocated_bytes += num_bytes;
20-
h->max_allocated_bytes = MAX(h->max_allocated_bytes, h->allocated_bytes);
20+
h->max_allocated_bytes = max(h->max_allocated_bytes, h->allocated_bytes);
2121
}
2222
#endif
2323

@@ -321,7 +321,7 @@ void *sys_heap_aligned_alloc(struct sys_heap *heap, size_t align, size_t bytes)
321321
rew = align & -align;
322322
if (align != rew) {
323323
align -= rew;
324-
gap = MIN(rew, chunk_header_bytes(h));
324+
gap = min(rew, chunk_header_bytes(h));
325325
} else {
326326
if (align <= chunk_header_bytes(h)) {
327327
return sys_heap_alloc(heap, bytes);
@@ -482,7 +482,7 @@ void *sys_heap_realloc(struct sys_heap *heap, void *ptr, size_t bytes)
482482
if (ptr2 != NULL) {
483483
size_t prev_size = sys_heap_usable_size(heap, ptr);
484484

485-
memcpy(ptr2, ptr, MIN(prev_size, bytes));
485+
memcpy(ptr2, ptr, min(prev_size, bytes));
486486
sys_heap_free(heap, ptr);
487487
}
488488
return ptr2;
@@ -516,7 +516,7 @@ void *sys_heap_aligned_realloc(struct sys_heap *heap, void *ptr,
516516
if (ptr2 != NULL) {
517517
size_t prev_size = sys_heap_usable_size(heap, ptr);
518518

519-
memcpy(ptr2, ptr, MIN(prev_size, bytes));
519+
memcpy(ptr2, ptr, min(prev_size, bytes));
520520
sys_heap_free(heap, ptr);
521521
}
522522
return ptr2;

lib/heap/heap.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ static ALWAYS_INLINE chunksz_t bytes_to_chunksz(struct z_heap *h, size_t bytes,
245245
size_t oddments = ((bytes % CHUNK_UNIT) + (extra % CHUNK_UNIT) +
246246
chunk_header_bytes(h) + CHUNK_UNIT - 1U) / CHUNK_UNIT;
247247

248-
return (chunksz_t)MIN(chunks + oddments, h->end_chunk);
248+
return (chunksz_t)min(chunks + oddments, h->end_chunk);
249249
}
250250

251251
static inline chunksz_t min_chunk_size(struct z_heap *h)

0 commit comments

Comments
 (0)