Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <string.h>
- #include <assert.h>
- #define STACK_MAX 512
- // instead of global variables will declare a struct
- struct memory_region{
- size_t *start;
- size_t *end;
- };
- struct memory_region global_mem; // used with x
- struct memory_region heap_mem; //
- struct memory_region stack_mem;
- //grabbing the address and size of the global memory region from proc
- void init_global_range(){
- char file[100];
- char * line=NULL;
- size_t n=0;
- size_t read_bytes=0;
- size_t start, end;
- sprintf(file, "/proc/%d/maps", getpid());
- FILE * mapfile = fopen(file, "r");
- if (mapfile==NULL){
- perror("opening maps file failed\n");
- exit(-1);
- }
- int counter=0;
- while ((read_bytes = getline(&line, &n, mapfile)) != -1) {
- if (strstr(line, "hw3")!=NULL){
- ++counter;
- if (counter==3){
- sscanf(line, "%lx-%lx", &start, &end);
- global_mem.start=(size_t*)start;
- global_mem.end=(size_t*)end;
- }
- }
- else if (read_bytes > 0 && counter==3){
- //if the globals are larger than a page, it seems to create a separate mapping
- sscanf(line, "%lx-%lx", &start, &end);
- if (start==global_mem.end){
- global_mem.end=(size_t*)end;
- }
- break;
- }
- }
- fclose(mapfile);
- }
- void init_gc() {
- init_global_range();
- fprintf(stderr,"Intializing garbage collector\n");
- void *p = malloc(STACK_MAX);
- heap_mem.start = p;
- stack_mem.start = &p;
- // free(p) should be sweep
- }
- void gc() {
- char stack_var;
- int x;
- stack_mem.end = &x;
- heap_mem.end = sbrk(0);
- long int heap_size = heap_mem.end - heap_mem.start;
- long int stack_size = -1*(stack_mem.end - stack_mem.start);
- void *current_chunk = heap_mem.start;
- while(current_chunk < heap_mem.end) {
- size_t size = *(size_t*)(current_chunk - sizeof(size_t)) & ~1;
- void *next_chunk = current_chunk + size;
- int inuse = 0;
- if(next_chunk < heap_mem.end)
- inuse = *(size_t*)(next_chunk - sizeof(size_t)) & 1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement