Advertisement
Guest User

Untitled

a guest
Nov 16th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.40 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/mman.h>
  4. #include <unistd.h>
  5. #include <fcntl.h>
  6. #include <sys/stat.h>
  7. #include <string.h>
  8. #include <sys/types.h>
  9. #include <memory.h>
  10.  
  11. const int INIT_SIZE = 64 * 1024;
  12.  
  13. #pragma pack(1)
  14. struct bos_hdr {
  15. int magic; // must be set to 0xb055;
  16. union {
  17. char padding[16];
  18. } u;
  19. };
  20.  
  21. struct bos_entry {
  22. int allocated:1; // will be non-zero if allocated
  23. int size; // includes the size of bos_entry
  24. union {
  25. char padding[8];
  26. } u;
  27. };
  28.  
  29. #pragma pack()
  30.  
  31. int main(int argc, char **argv) {
  32. if (argc != 2) {
  33. printf("USAGE: %s bos_filename\n", argv[0]);
  34. exit(2);
  35. }
  36. int fd = open(argv[1], O_CREAT | O_RDWR, 0777);
  37. if (fd == -1) {
  38. perror(argv[1]);
  39. exit(3);
  40. }
  41.  
  42. void *base = mmap(NULL, 1024*1024, PROT_WRITE | PROT_READ, MAP_SHARED, fd, 0);
  43. void *end;
  44. struct bos_hdr *hdr = base;
  45. struct bos_entry *first_entry = (struct bos_entry *)(hdr+1);
  46.  
  47. struct stat stat;
  48. if (fstat(fd, &stat) == -1) {
  49. perror("fstat");
  50. exit(4);
  51. }
  52. if (stat.st_size == 0) {
  53. ftruncate(fd, INIT_SIZE);
  54. memset(hdr, 0, sizeof *hdr);
  55. hdr->magic = 0xb055;
  56. memset(first_entry, 0, sizeof *first_entry);
  57. first_entry->size = INIT_SIZE - sizeof *hdr;
  58. end = (char*)base + INIT_SIZE;
  59. } else {
  60. end = (char*)base + stat.st_size;
  61. }
  62.  
  63. size_t n;
  64. char *line = NULL;
  65. while (getline(&line, &n, stdin) > 0) {
  66. // remove the newline
  67. size_t endi = strlen(line) - 1;
  68. if (line[endi] == '\n') {
  69. line[endi] = '\0';
  70. }
  71. switch(line[0]) {
  72. // need to add 'd'
  73. case 'l': {
  74. struct bos_entry *entry = first_entry;
  75. while ((void*)entry < end) {
  76. if (entry->allocated) {
  77. printf("%s\n", (char*)&entry[1]);
  78. }
  79. entry = (struct bos_entry *) ((char *) entry + entry->size);
  80. }
  81. }
  82. break; // break from the switch statement
  83. case 'a': {
  84. // we should check for duplicates first!
  85. char *str = &line[2];
  86. int smallestLeftover = 0;
  87. struct bos_entry *bestFitEntry = NULL;
  88. struct bos_entry *entry = first_entry;
  89. while ((void*)entry < end) {
  90. if (!entry->allocated) {
  91. // split it (note this should be fixed so that it can grow if needed
  92. int needed_size = sizeof(*entry) + strlen(str) + 1;
  93. int left_over = entry->size - needed_size;
  94. if (left_over < sizeof(*entry) && smallestLeftover < left_over) {
  95. //smallestLeftover = left_over;
  96. //bestFitEntry = entry;
  97. // if we don't have enough left over for an entry struct, we just
  98. // use it here and it will be internal fragmentation
  99. needed_size = entry->size;
  100. left_over = 0;
  101. }
  102. if (entry->size < needed_size) {
  103. printf("hmmm, you need to grow!!!\n");
  104. exit(6);
  105. }
  106. entry->size = needed_size;
  107. entry->allocated = 1;
  108. strcpy((char *) &entry[1], str);
  109. if (left_over > 0) {
  110. entry = (struct bos_entry *) ((char *) entry + entry->size);
  111. memset(entry, 0, sizeof *entry);
  112. entry->size = left_over;
  113. }
  114. break;
  115. }
  116. entry = (struct bos_entry *) ((char *) entry + entry->size);
  117. }
  118. }
  119. break;
  120. case 'd': {
  121. char *str = &line[2];
  122. struct bos_entry *entry = first_entry;
  123. while ((void*)entry < end) {
  124. printf("entry[1] = %s and str = %s\n", (char *)&entry[1], str);
  125. if(entry->allocated && strcmp(str, (char *)&entry[1]) != 0){
  126. entry->allocated = 0;
  127. printf("Entered loop and turned off allocation");
  128. }
  129. entry = (struct bos_entry *) ((char *) entry + entry->size);
  130. }
  131.  
  132. }
  133. break;
  134.  
  135. case 'L': {
  136. struct bos_entry *entry = first_entry;
  137. int i = 1;
  138. while ((void*)entry < end) {
  139. char * allocated = "not allocated";
  140. if(entry->allocated){
  141. allocated = "allocated";
  142. }
  143.  
  144. printf("Entry #%d of size %d and is %s\n", i, entry->size, allocated);
  145. printf("%s\n", (char*)&entry[1]);
  146. entry = (struct bos_entry *) ((char *) entry + entry->size);
  147. i++;
  148. }
  149. }
  150.  
  151. break; // break from the switch statement
  152. }
  153. free(line);
  154. n = 0;
  155. line = 0;
  156. }
  157. return 0;
  158. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement