Advertisement
Guest User

my demise

a guest
Mar 6th, 2015
331
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.25 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <string.h>
  5. #include <assert.h>
  6.  
  7.  
  8. #define STACK_MAX 512
  9. // instead of global variables will declare a struct
  10.  
  11.  
  12. struct memory_region{
  13. size_t *start;
  14. size_t *end;
  15. };
  16.  
  17. struct memory_region global_mem; // used with x
  18. struct memory_region heap_mem; //
  19. struct memory_region stack_mem;
  20.  
  21.  
  22. //grabbing the address and size of the global memory region from proc
  23. void init_global_range(){
  24. char file[100];
  25. char * line=NULL;
  26. size_t n=0;
  27. size_t read_bytes=0;
  28. size_t start, end;
  29.  
  30. sprintf(file, "/proc/%d/maps", getpid());
  31. FILE * mapfile = fopen(file, "r");
  32. if (mapfile==NULL){
  33. perror("opening maps file failed\n");
  34. exit(-1);
  35. }
  36.  
  37. int counter=0;
  38. while ((read_bytes = getline(&line, &n, mapfile)) != -1) {
  39. if (strstr(line, "hw3")!=NULL){
  40. ++counter;
  41. if (counter==3){
  42. sscanf(line, "%lx-%lx", &start, &end);
  43. global_mem.start=(size_t*)start;
  44. global_mem.end=(size_t*)end;
  45. }
  46. }
  47. else if (read_bytes > 0 && counter==3){
  48. //if the globals are larger than a page, it seems to create a separate mapping
  49. sscanf(line, "%lx-%lx", &start, &end);
  50. if (start==global_mem.end){
  51. global_mem.end=(size_t*)end;
  52. }
  53. break;
  54. }
  55. }
  56. fclose(mapfile);
  57. }
  58.  
  59.  
  60.  
  61. void init_gc() {
  62. init_global_range();
  63. fprintf(stderr,"Intializing garbage collector\n");
  64. void *p = malloc(STACK_MAX);
  65. heap_mem.start = p;
  66. stack_mem.start = &p;
  67.  
  68. // free(p) should be sweep
  69.  
  70. }
  71.  
  72. void gc() {
  73. char stack_var;
  74. int x;
  75. stack_mem.end = &x;
  76. heap_mem.end = sbrk(0);
  77.  
  78.  
  79. long int heap_size = heap_mem.end - heap_mem.start;
  80. long int stack_size = -1*(stack_mem.end - stack_mem.start);
  81.  
  82. void *current_chunk = heap_mem.start;
  83.  
  84. while(current_chunk < heap_mem.end) {
  85. size_t size = *(size_t*)(current_chunk - sizeof(size_t)) & ~1;
  86.  
  87. void *next_chunk = current_chunk + size;
  88. int inuse = 0;
  89. if(next_chunk < heap_mem.end)
  90. inuse = *(size_t*)(next_chunk - sizeof(size_t)) & 1;
  91. }
  92.  
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement