Skip to content

Commit

Permalink
Merge branch 'elementary-computer-science'
Browse files Browse the repository at this point in the history
  • Loading branch information
ckirsch committed Oct 20, 2023
2 parents 7df35b0 + b868758 commit 28a043d
Show file tree
Hide file tree
Showing 4 changed files with 308 additions and 309 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ selfie.h: selfie.c
selfie-gc.h: selfie.c
sed 's/gc_init(uint64_t\* context) {/gc_init_deleted(uint64_t\* context) {/' selfie.c > selfie-gc-intermediate.h
sed 's/allocate_memory(uint64_t\* context, uint64_t size) {/allocate_memory_deleted(uint64_t\* context, uint64_t size) {/' selfie-gc-intermediate.h > selfie-gc.h
sed 's/mark_object(uint64_t\* context, uint64_t address) {/mark_object_deleted(uint64_t\* context, uint64_t address) {/' selfie-gc.h > selfie-gc-intermediate.h
sed 's/mark_block(uint64_t\* context, uint64_t address) {/mark_block_deleted(uint64_t\* context, uint64_t address) {/' selfie-gc.h > selfie-gc-intermediate.h
sed 's/sweep(uint64_t\* context) {/sweep_deleted(uint64_t\* context) {/' selfie-gc-intermediate.h > selfie-gc.h
sed 's/allocate_context() {/allocate_context_deleted() {/' selfie-gc.h > selfie-gc-intermediate.h
mv selfie-gc-intermediate.h selfie-gc.h
Expand Down
64 changes: 32 additions & 32 deletions examples/gc/gc-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,25 @@ uint64_t validation_address_3 = 0;
uint64_t validation_address_4 = 0;
uint64_t validation_address_5 = 0;

// global variables (pointers to gc created objects)
// global variables (pointers to gc created memory blocks)
uint64_t* x = (uint64_t*) 0;
uint64_t* y = (uint64_t*) 0;

// simple function allocating memory to demonstrate stack collection
void do_stuff() {
uint64_t* z;

z = gc_malloc(8); // object 5
z = gc_malloc(8); // memory block 5
if (((uint64_t) z) != validation_address_4 - check_offset) {
printf("test 4 failed (local variable inside function)!\n");
exit(1);
}

// return -> free(object 5)
// return -> free(memory block 5)
}

int main(int argc, char** argv) {
// local variables (pointers to gc created objects)
// local variables (pointers to gc created memory blocks)
uint64_t* z;
uint64_t* w;

Expand All @@ -53,13 +53,13 @@ int main(int argc, char** argv) {
// assert: gc_malloc(0) fetches the current program break
heap_start = (uint64_t) gc_malloc(0) + check_offset;

// note: the gc library stores metadata and object in the same heap (order: metadata -> object)
// therefore, we need to consider both object and metadata size when calculating the expected addresses
// note: the gc library stores block and metadata in the same heap (order: metadata -> block)
// therefore, we need to consider both block and metadata size when calculating the expected addresses

// test case 1: allocate object and assign ptr to global variable (-> data segment)
// test case 1: allocate block and assign ptr to global variable (-> data segment)
validation_address_1 = heap_start + GC_METADATA_SIZE;

// test case 2: allocate object and assign ptr to the first object (-> heap)
// test case 2: allocate block and assign ptr to the first block (-> heap)
validation_address_2 = validation_address_1 + 8 + GC_METADATA_SIZE;

// test case 3: reuse (i.e. alloc -> unassign -> alloc)
Expand All @@ -78,79 +78,79 @@ int main(int argc, char** argv) {
// validation addresses of test case 1 and 2 are used

// heap layout (not considering metadata and check offsets):
// +-----------+
// | |
// | object 7 |
// +-----------+ +-----------+ = validation_address_5
// | object 5 | -> | object 6 |
// +-----------+ +-----------+ = validation_address_4
// | object 3 | -> | object 4 |
// +-----------+ +-----------+ = validation_address_3
// | object 2 | -> | object 9 |
// +-----------+ +-----------+ = validation_address_2
// | object 1 | -> | object 8 |
// +-----------+ +-----------+ = validation_address_1
// +---------+
// | |
// | block 7 |
// +---------+ +---------+ = validation_address_5
// | block 5 | -> | block 6 |
// +---------+ +---------+ = validation_address_4
// | block 3 | -> | block 4 |
// +---------+ +---------+ = validation_address_3
// | block 2 | -> | block 9 |
// +---------+ +---------+ = validation_address_2
// | block 1 | -> | block 8 |
// +---------+ +---------+ = validation_address_1

// test cases

// --- test 1 ---
x = gc_malloc(8); // object 1
x = gc_malloc(8); // block 1
if (((uint64_t) x) != validation_address_1 - check_offset) {
printf("0x%08lX - 0x%lX\n", (uint64_t) x, validation_address_1 - check_offset);
printf("test 1 failed!\n");
exit(1);
}

// --- test 2 ---
*x = (uint64_t) gc_malloc(8); // object 2
*x = (uint64_t) gc_malloc(8); // block 2
if (*x != validation_address_2 - check_offset) {
printf("test 2 failed!\n");
exit(1);
}

// --- test 3 ---
y = gc_malloc(8); // object 3
y = gc_malloc(8); // block 3
if (((uint64_t) y) != validation_address_3 - check_offset) {
printf("test 3 failed (first allocation)!\n");
exit(1);
}

y = (uint64_t*) 0; // = free(object 3)
y = (uint64_t*) 0; // = free(block 3)

y = gc_malloc(8); // object 4
y = gc_malloc(8); // block 4
if (((uint64_t) y) != validation_address_3 - check_offset) {
printf("test 3 failed (reuse)! make sure gc period is set to 0!\n");
exit(1);
}

// --- test 4 ---
do_stuff(); // object 5 (inside function)
do_stuff(); // block 5 (inside function)

z = gc_malloc(8); // object 6
z = gc_malloc(8); // block 6
if (((uint64_t) z) != validation_address_4 - check_offset) {
printf("test 4 failed (local variable)! make sure gc period is set to 0!\n");
exit(1);
}

// --- test 5 ---
z = (uint64_t*) 0; // = free(object 6)
z = (uint64_t*) 0; // = free(block 6)

z = gc_malloc(16); // object 7
z = gc_malloc(16); // block 7
if (((uint64_t) z) != validation_address_5 - check_offset) {
printf("test 5 failed!\n");
exit(1);
}

// --- test 6 ---
x = (uint64_t*) 0; // = free(object 1), free(object 2)
x = (uint64_t*) 0; // = free(block 1), free(block 2)

w = gc_malloc(8); // object 8
w = gc_malloc(8); // block 8
if (((uint64_t) w) != validation_address_1 - check_offset) {
printf("test 6 failed! make sure gc period is set to 0!\n");
exit(1);
}

x = gc_malloc(8); // object 9
x = gc_malloc(8); // block 9
if (((uint64_t) x) != validation_address_2 - check_offset) {
printf("test 6 failed! make sure gc period is set to 0!\n");
exit(1);
Expand Down
Loading

0 comments on commit 28a043d

Please sign in to comment.