I have this problem in C where I have to implement a garbage collector. I'm stuck on the fact that I was given 4 functions to complete and not sure how they connect to one another. I'm not sure what to do. This is what I have so far:
void mygc() {
//int **max = (int **) 0xbfffffffUL; // the address of the top of the stack
unsigned long stack_bottom;
int **max = (int **) GC_init(); // get the address of the bottom of the stack
int* q;
int **p = &q; // the address of the bottom of the stack
while (p < max) {
//printf("0. p: %u, *p: %u max: %u\n",p,*p,max);
mark(*p);
p++;
}
//utilize sweep and coalesce (coalesce function already written)
}
void mark(int *p) {
int i;
int *ptr;
//code here
}
void sweep(int *ptr) {
// code here
}
int *isPtr(int *p) {
//return the pointer or NULL
int *ptr = start;
//code here
}
If you don't even understand the question perhaps it's best to speak to your teaching staff. To get you started here's the general idea.
mygc
is obviously the top level function that does the GC. mark
is called to mark memory location/object as in use. It also needs to mark all memory referenced by that location/object as in use (recursive). sweep
is called to unmark all the previously marked memory and to claim back (garbage collect) those locations that are not marked. isPtr
is called to determine whether a memory location is a pointer (as opposed to being any other data). This is used by mark
to know whether a memory location needs to be marked or not. So putting that all together the general pseudo code is:
mygc()
{
loc_list = get stack extents and global variables
foreach (p in loc_list) {
if (isPtr(p)) {
mark(p)
}
}
foreach (p in heap) {
sweep(p)
}
}
There are obviously lots of details not dealt with in that psuedo code. But it should hopefully be enough to answer your original question which is how the four functions fit together.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.