75 lines
1.8 KiB
C
75 lines
1.8 KiB
C
|
|
#define _POSIX_C_SOURCE 200112L
|
||
|
|
#include <stdlib.h>
|
||
|
|
#include "slab.h"
|
||
|
|
#include "test.h"
|
||
|
|
#include "address.h"
|
||
|
|
#include "ram.h"
|
||
|
|
#include "string.h"
|
||
|
|
|
||
|
|
extern uint64_t identity_mapping_start;
|
||
|
|
|
||
|
|
bool ram_alloc_frame(struct ppn *ppn_out, enum frame_size size) {
|
||
|
|
TEST_ASSERT(size == RAM_PAGE_NORMAL);
|
||
|
|
void *addr;
|
||
|
|
TEST_ASSERT(posix_memalign(&addr, 4096, 4096) == 0);
|
||
|
|
*ppn_out = ppn_from_aligned_pa(pa_from_value((uint64_t)addr));
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
void ram_free(struct ppn ppn) {
|
||
|
|
free(ppn_to_pointer(ppn));
|
||
|
|
}
|
||
|
|
|
||
|
|
size_t num_ctor_calls = 0;
|
||
|
|
size_t num_dtor_calls = 0;
|
||
|
|
|
||
|
|
void constructor(void *obj) {
|
||
|
|
TEST_ASSERT(((uint64_t)obj & 0x3f) == 0);
|
||
|
|
uint8_t *p = obj;
|
||
|
|
for (int i = 0; i < 100; i++) {
|
||
|
|
p[i] = i;
|
||
|
|
}
|
||
|
|
num_ctor_calls++;
|
||
|
|
}
|
||
|
|
|
||
|
|
void destructor(void *obj) {
|
||
|
|
TEST_ASSERT(((uint64_t)obj & 0x3f) == 0);
|
||
|
|
num_dtor_calls++;
|
||
|
|
}
|
||
|
|
|
||
|
|
int main() {
|
||
|
|
identity_mapping_start = 0;
|
||
|
|
struct slab_cache cache;
|
||
|
|
TEST_SHOULD_PANIC(slab_cache_alloc(&cache));
|
||
|
|
slab_cache_init_with_align(&cache, 100, 64, constructor, destructor);
|
||
|
|
|
||
|
|
uint8_t *ptr0 = slab_cache_alloc(&cache);
|
||
|
|
TEST_ASSERT(ptr0 != NULL);
|
||
|
|
TEST_ASSERT(((uint64_t)ptr0 & 0x3f) == 0);
|
||
|
|
|
||
|
|
uint8_t *ptr1 = slab_cache_alloc(&cache);
|
||
|
|
TEST_ASSERT(ptr1 != NULL);
|
||
|
|
TEST_ASSERT(((uint64_t)ptr1 & 0x3f) == 0);
|
||
|
|
|
||
|
|
for (int i = 0; i < 100; i++) {
|
||
|
|
TEST_ASSERT(ptr0[i] == i);
|
||
|
|
TEST_ASSERT(ptr1[i] == i);
|
||
|
|
}
|
||
|
|
|
||
|
|
memset(ptr0, 42, 100);
|
||
|
|
memset(ptr1, 43, 100);
|
||
|
|
|
||
|
|
for (int i = 0; i < 100; i++) {
|
||
|
|
TEST_ASSERT(ptr0[i] == 42);
|
||
|
|
TEST_ASSERT(ptr1[i] == 43);
|
||
|
|
}
|
||
|
|
|
||
|
|
slab_cache_free(ptr0);
|
||
|
|
slab_cache_free(ptr1);
|
||
|
|
slab_cache_destroy(&cache);
|
||
|
|
|
||
|
|
TEST_ASSERT(num_ctor_calls > 0 && num_ctor_calls == num_dtor_calls);
|
||
|
|
}
|
||
|
|
|
||
|
|
// TODO test where we write past object and then expect panic on free
|