forked from embeddedartistry/libmemory
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmalloc_freelist.c
More file actions
90 lines (73 loc) · 2.02 KB
/
malloc_freelist.c
File metadata and controls
90 lines (73 loc) · 2.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
* Copyright © 2017 Embedded Artistry LLC.
* License: MIT. See LICENSE file for details.
*/
#include <malloc.h>
#include <stdint.h>
#include <support/memory.h>
#include <tests.h>
// CMocka needs these
// clang-format off
#include <setjmp.h>
#include <stdarg.h>
#include <stddef.h>
#include <cmocka.h>
// clang-format on
#define ALLOCATION_TEST_COUNT 768
static void* pointer_array[ALLOCATION_TEST_COUNT];
static void malloc_test(void** __attribute__((unused)) state)
{
// Make sure memory was previously allocated
if(!memory_allocated())
{
allocate_memory();
}
assert_true(memory_allocated());
uintptr_t mem_block_addr = block_start_addr();
uintptr_t mem_block_end_addr = block_end_addr();
size_t mem_block_size = block_size();
void* ptr = malloc(1024);
assert_non_null(ptr);
assert_in_range((uintptr_t)ptr, mem_block_addr, mem_block_end_addr);
free(ptr);
// Check request too large
ptr = malloc(2 * mem_block_size);
assert_null(ptr);
for(size_t i = 0; i < ALLOCATION_TEST_COUNT; i++)
{
pointer_array[i] = malloc(1024);
assert_non_null(pointer_array[i]);
assert_in_range((uintptr_t)pointer_array[i], mem_block_addr, mem_block_end_addr);
// Test for unique addresses
if(i > 0)
{
assert_not_in_set((uintptr_t)pointer_array[i], (uintptr_t*)pointer_array, i - 1);
}
}
// Cleanup
for(size_t i = 0; i < ALLOCATION_TEST_COUNT; i++)
{
free(pointer_array[i]);
}
// Run test again, will not fail if our memory has been returned!
for(size_t i = 0; i < ALLOCATION_TEST_COUNT; i++)
{
pointer_array[i] = malloc(1024);
assert_non_null(pointer_array[i]);
assert_in_range((uintptr_t)pointer_array[i], mem_block_addr, mem_block_end_addr);
if(i > 0)
{
assert_not_in_set((uintptr_t)pointer_array[i], (uintptr_t*)pointer_array, i - 1);
}
}
// Cleanup
for(size_t i = 0; i < ALLOCATION_TEST_COUNT; i++)
{
free(pointer_array[i]);
}
}
int malloc_tests(void)
{
const struct CMUnitTest malloc_test_suite[] = {cmocka_unit_test(malloc_test)};
return cmocka_run_group_tests(malloc_test_suite, NULL, NULL);
}