@@ -431,3 +431,34 @@ ZTEST(k_heap_api, test_k_heap_realloc_fail)
431
431
432
432
k_heap_free (& k_heap_test , p );
433
433
}
434
+
435
+ /**
436
+ * @brief Test k_heap_aligned_alloc() API usage and edge cases
437
+ *
438
+ * @ingroup k_heap_api_tests
439
+ *
440
+ * @details Allocates a block with a specific alignment from the heap
441
+ * and checks alignment, then tries oversize and invalid alignment.
442
+ *
443
+ * @see k_heap_aligned_alloc()
444
+ */
445
+ ZTEST (k_heap_api , test_k_heap_aligned_alloc )
446
+ {
447
+ void * p ;
448
+
449
+ /* Allocate 128 bytes aligned to 16 bytes */
450
+ p = k_heap_aligned_alloc (& k_heap_test , 16 , 128 , K_NO_WAIT );
451
+ zassert_not_null (p , "k_heap_aligned_alloc failed" );
452
+ zassert_true (((uintptr_t )p % 16 ) == 0 , "Pointer not 16-byte aligned" );
453
+ k_heap_free (& k_heap_test , p );
454
+
455
+ /* Oversize allocation returns NULL */
456
+ p = k_heap_aligned_alloc (& k_heap_test , 8 , HEAP_SIZE * 2 , K_NO_WAIT );
457
+ zassert_is_null (p , "k_heap_aligned_alloc with oversize should fail" );
458
+
459
+ /* With non-power-of-two alignment, Zephyr internally rewrites the alignment
460
+ * and proceeds, so this should not assert or necessarily fail.
461
+ */
462
+ p = k_heap_aligned_alloc (& k_heap_test , 3 , 64 , K_NO_WAIT );
463
+ (void )p ;
464
+ }
0 commit comments