aboutsummaryrefslogtreecommitdiff
path: root/tests/test_memory.c
diff options
context:
space:
mode:
Diffstat (limited to 'tests/test_memory.c')
-rw-r--r--tests/test_memory.c98
1 files changed, 72 insertions, 26 deletions
diff --git a/tests/test_memory.c b/tests/test_memory.c
index a977a70..c85e228 100644
--- a/tests/test_memory.c
+++ b/tests/test_memory.c
@@ -14,26 +14,25 @@ struct TEST_STRUCT {
};
struct TEST_STRUCT2 {
- uint32_t array[10];
+ uint32_t array[16];
};
/* Copy of the node structure. */
+// The sizes will count the number of WORDS allocated.
+// Since there's a max size of 16k, only 12 bits will be
+// needed for this.
typedef struct KALLOC_NODE {
- union {
- uint32_t header;
- struct {
- /* Is this memory block currently in use (hasn't been kfree'd) */
- bool used : 1;
- /* Number of words allocated. Does not include the header. */
- uint16_t size : 12;
- /* The location of the previous block (in WORDS from offest) */
- uint16_t prev : 12;
- uint8_t canary : 7;
- } PACKED;
- };
+ uint16_t size_words;
+ uint16_t prev;
+ /*
+ * LSB is whether this block is used.
+ *
+ * Rest of the bits must equal (0xdeadbeee >> 1)
+ */
+ uint32_t used_and_canary;
uint8_t mem[]; /* The memory to use. */
-} kalloc_node_t;
+} PACKED kalloc_node_t;
extern kalloc_node_t* kalloc_start;
@@ -65,7 +64,8 @@ static struct TEST_STRUCT2* new_test_struct2()
return ret;
}
-#define ASSERT_CHAIN(t1, t2) ASSERT_EQ(V(t1) + sizeof(*t1) + 4, V(t2))
+#define ASSERT_CHAIN(t1, t2) \
+ ASSERT_EQ(V(t1) + sizeof(*t1) + sizeof(kalloc_node_t), V(t2))
TEST(memory, kalloc)
{
@@ -127,15 +127,17 @@ TEST(memory, uneven_kalloc)
struct UNEVEN_STRUCT* test1 = new_uneven_struct();
struct UNEVEN_STRUCT* test2 = new_uneven_struct();
- ASSERT_EQ(V(test1) + 12, test2);
+ ASSERT_EQ(V(test1) + 8 + sizeof(kalloc_node_t), test2);
wipeout_kalloc();
return 0;
}
-#define ASSERT_KALLOC_EMPTY() \
- ASSERT_EQ((void*)(kalloc_start + kalloc_start->size + 1), (void*)&HEAP_STOP)
+#define ASSERT_KALLOC_EMPTY() \
+ ASSERT_EQ( \
+ (void*)((uint8_t*)kalloc_start + kalloc_start->size_words * sizeof(uint32_t) + sizeof(kalloc_node_t)), \
+ (void*)&HEAP_STOP)
TEST(memory, kalloc_free)
{
@@ -143,6 +145,10 @@ TEST(memory, kalloc_free)
wipeout_kalloc();
}
+ kalloc_init();
+
+ ASSERT_KALLOC_EMPTY();
+
struct TEST_STRUCT* test1 = new_test_struct();
struct TEST_STRUCT2* test2 = new_test_struct2();
struct TEST_STRUCT* test3 = new_test_struct();
@@ -248,6 +254,46 @@ TEST(memory, relink_backref_after_free)
return 0;
}
+TEST(memory, alloc1)
+{
+ char buf[1024];
+
+ kalloc(5);
+ kalloc(1);
+
+ if (debug_kalloc_assert_consistency(buf, 1024)) {
+ fprintf(
+ stderr,
+ "Consistency check failed. (%s:%d)\n",
+ __FILE__,
+ __LINE__);
+ fprintf(stderr, "%s", buf);
+ ASSERT_TRUE(false);
+ }
+
+ return 0;
+}
+
+TEST(memory, alloc0)
+{
+ char buf[1024];
+
+ kalloc(5);
+ kalloc(0);
+
+ if (debug_kalloc_assert_consistency(buf, 1024)) {
+ fprintf(
+ stderr,
+ "Consistency check failed. (%s:%d)\n",
+ __FILE__,
+ __LINE__);
+ fprintf(stderr, "%s", buf);
+ ASSERT_TRUE(false);
+ }
+
+ return 0;
+}
+
TEST(memory, consistency_stress)
{
#define NRUNS 500
@@ -258,6 +304,7 @@ TEST(memory, consistency_stress)
int i;
void* allocd[NRUNS] = {0};
char buf[1024];
+ void* tofree;
for (i = 0; i < NRUNS; ++i) {
size_t nalloc = rand() % 20;
@@ -266,10 +313,11 @@ TEST(memory, consistency_stress)
if (debug_kalloc_assert_consistency(buf, 1024)) {
fprintf(
stderr,
- "Consistency check failed. (At index=%d, %s:%d)\n",
+ "Consistency check failed. (At index=%d, %s:%d, nalloc=%ld)\n",
i,
__FILE__,
- __LINE__);
+ __LINE__,
+ nalloc);
fprintf(stderr, "%s", buf);
ASSERT_TRUE(false);
}
@@ -290,7 +338,8 @@ TEST(memory, consistency_stress)
ASSERT_TRUE(false);
}
- kfree(allocd[idx]);
+ tofree = allocd[idx];
+ kfree(tofree);
allocd[idx] = NULL;
if (debug_kalloc_assert_consistency(buf, 1024)) {
@@ -305,7 +354,8 @@ TEST(memory, consistency_stress)
}
idx = rand() % NRUNS;
- kfree(allocd[idx]);
+ tofree = allocd[idx];
+ kfree(tofree);
allocd[idx] = NULL;
if (debug_kalloc_assert_consistency(buf, 1024)) {
@@ -363,11 +413,7 @@ TEST(memory, kalloc_free_alloc)
kfree(test2);
struct TEST_STRUCT* test7 = new_test_struct();
- struct TEST_STRUCT* test8 = new_test_struct();
- // Test 2 was large enough to accomodate 3 smaller structs.
ASSERT_EQ(V(test7), V(test2));
- ASSERT_EQ(V(test8), V(test2) + sizeof(*test7) + 4);
-
return 0;
}