| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | #include "test_engine_impl.h" |
| 4 | |
| 5 | #include <mos_stdlib.hpp> |
| 6 | #include <mos_string.hpp> |
| 7 | |
| 8 | MOS_TEST_CASE(kmalloc_single) |
| 9 | { |
| 10 | void *p = kcalloc<char>(n_members: 1024); |
| 11 | MOS_TEST_ASSERT(p != NULL, "kmalloc failed" ); |
| 12 | memset(s: p, c: 0, n: 1024); |
| 13 | kfree(ptr: p); |
| 14 | } |
| 15 | |
| 16 | MOS_TEST_CASE(kmalloc_stress) |
| 17 | { |
| 18 | void *p; |
| 19 | int i; |
| 20 | for (i = 0; i < 100; i++) |
| 21 | { |
| 22 | p = kcalloc<char>(n_members: 1024); |
| 23 | MOS_TEST_ASSERT(p != NULL, "kmalloc failed" ); |
| 24 | memset(s: p, c: 0, n: 1024); |
| 25 | kfree(ptr: p); |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | MOS_TEST_CASE(kmalloc_large) |
| 30 | { |
| 31 | char *p = 0; |
| 32 | p = kcalloc<char>(n_members: 1 MB); |
| 33 | MOS_TEST_ASSERT(p != NULL, "kmalloc failed" ); |
| 34 | memset(s: p, c: 0, n: 1 MB); |
| 35 | kfree(ptr: p); |
| 36 | |
| 37 | p = kcalloc<char>(n_members: 100 MB); |
| 38 | MOS_TEST_ASSERT(p != NULL, "kmalloc failed" ); |
| 39 | memset(s: p, c: 0, n: 100 MB); |
| 40 | kfree(ptr: p); |
| 41 | |
| 42 | // we won't test larger allocations because for (32-bit) x86, the kernel |
| 43 | // heap starts at 0xd0000000, while the initrd is placed at 0xec000000, |
| 44 | // That only leaves 0x1c000000 bytes for the kernel heap i.e. ~460 MB. |
| 45 | } |
| 46 | |
| 47 | MOS_TEST_CASE(kmalloc_a_lot) |
| 48 | { |
| 49 | char *pointers[100]; |
| 50 | for (int t = 0; t < 20; t++) |
| 51 | { |
| 52 | for (int i = 0; i < 50; i++) |
| 53 | { |
| 54 | pointers[i] = kcalloc<char>(n_members: 71); |
| 55 | MOS_TEST_ASSERT(pointers[i] != NULL, "failed to allocate memory" ); |
| 56 | memset(s: pointers[i], c: 0, n: 71); |
| 57 | } |
| 58 | |
| 59 | for (int i = 0; i < 50; i++) |
| 60 | { |
| 61 | kfree(ptr: pointers[i]); |
| 62 | } |
| 63 | } |
| 64 | } |
| 65 | |