Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.08 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "part1.h"
  4. #include <stdbool.h>
  5.  
  6.  
  7. Node * STARTOFHEAP;
  8. char * endofheap;
  9.  
  10. void initialise(void *memory, size_t size){
  11.  
  12. STARTOFHEAP = (Node *) memory;
  13.  
  14. STARTOFHEAP->free = true;
  15. STARTOFHEAP->blockSize = size - sizeof(Node);
  16. STARTOFHEAP->previous = NULL;
  17. STARTOFHEAP->next = NULL;
  18. }
  19.  
  20. void *allocate(size_t bytes){
  21. Node * allocatedNode = (Node *) STARTOFHEAP;
  22. Node * freeNode;
  23.  
  24. while(allocatedNode != NULL) {
  25. if ((allocatedNode->free == true) && (allocatedNode->blockSize >= bytes)) {
  26. char *tempHead = (char *) allocatedNode;
  27. int sizeLeft = allocatedNode->blockSize - (sizeof(Node) + bytes);
  28.  
  29. freeNode = (Node *) (tempHead + sizeof(Node) + bytes); //create a new node at the address location that is =
  30. freeNode->free = true; //the start of the head + size of a node + requested bytes
  31.  
  32. if(sizeLeft <= sizeof(Node) + 1){
  33. freeNode->blockSize = allocatedNode->blockSize;
  34. } else {
  35. freeNode->blockSize = allocatedNode->blockSize - bytes;
  36. }
  37.  
  38. if(allocatedNode->next != NULL) {
  39.  
  40. allocatedNode->next->previous = freeNode; //go into allocated node next and set the next nodes previous to the free node
  41. freeNode->next = allocatedNode->next; //set the freenodes next to the allocated nodes next
  42. allocatedNode->next = freeNode; //set the allocatednodes next to the free node
  43. freeNode->previous = allocatedNode;
  44. } else{
  45. allocatedNode->next = freeNode;
  46. freeNode->previous = allocatedNode;
  47. freeNode->next = NULL;
  48. }
  49. // freeNode->next = NULL;
  50. // freeNode->previous = allocatedNode;
  51.  
  52. allocatedNode->next = freeNode;
  53. allocatedNode->free = false;
  54. printf("next Free Node address: %d\n", freeNode);
  55.  
  56. return freeNode->previous + 1;
  57. }
  58. allocatedNode = allocatedNode->next;
  59. }
  60. return (NULL);
  61. }
  62.  
  63.  
  64. int main(int argc, const char* argv[]) {
  65.  
  66. size_t heapSize = 200;
  67. STARTOFHEAP = (Node *) malloc(heapSize);
  68.  
  69. printf("Size of requested heap: %d\n", heapSize);
  70. printf("Size of node: %d\n", sizeof(Node));
  71.  
  72. initialise(STARTOFHEAP, heapSize);
  73.  
  74. endofheap = (char *) STARTOFHEAP;
  75.  
  76. printf("Start of heap address: %d\n", STARTOFHEAP);
  77. printf("End of heap address: %d\n", endofheap = (endofheap + heapSize));
  78. printf("Size of struct Node: %d\n\n", sizeof(Node));
  79.  
  80. printf("Node 1: \n");
  81. Node *nodeOne = (Node *) allocate(18);
  82. printf("Memory Address location returned to no#include <stdio.h>
  83. #include <stdlib.h>
  84. #include "part1.h"
  85. #include <stdbool.h>
  86.  
  87.  
  88. Node * STARTOFHEAP;
  89. char * endofheap;
  90.  
  91. void initialise(void *memory, size_t size){
  92.  
  93. STARTOFHEAP = (Node *) memory;
  94.  
  95. STARTOFHEAP->free = true;
  96. STARTOFHEAP->blockSize = size - sizeof(Node);
  97. STARTOFHEAP->previous = NULL;
  98. STARTOFHEAP->next = NULL;
  99. }
  100.  
  101. void *allocate(size_t bytes){
  102. Node * allocatedNode = (Node *) STARTOFHEAP;
  103. Node * freeNode;
  104.  
  105. while(allocatedNode != NULL) {
  106. if ((allocatedNode->free == true) && (allocatedNode->blockSize >= bytes)) {
  107. char *tempHead = (char *) allocatedNode;
  108. int sizeLeft = allocatedNode->blockSize - (sizeof(Node) + bytes);
  109.  
  110. freeNode = (Node *) (tempHead + sizeof(Node) + bytes); //create a new node at the address location that is =
  111. freeNode->free = true; //the start of the head + size of a node + requested bytes
  112.  
  113. if(sizeLeft <= sizeof(Node) + 1){
  114. freeNode->blockSize = allocatedNode->blockSize;
  115. } else {
  116. freeNode->blockSize = allocatedNode->blockSize - bytes;
  117. }
  118.  
  119. if(allocatedNode->next != NULL) {
  120.  
  121. allocatedNode->next->previous = freeNode; //go into allocated node next and set the next nodes previous to the free node
  122. freeNode->next = allocatedNode->next; //set the freenodes next to the allocated nodes next
  123. allocatedNode->next = freeNode; //set the allocatednodes next to the free node
  124. freeNode->previous = allocatedNode;
  125. } else{
  126. allocatedNode->next = freeNode;
  127. freeNode->previous = allocatedNode;
  128. freeNode->next = NULL;
  129. }
  130. // freeNode->next = NULL;
  131. // freeNode->previous = allocatedNode;
  132.  
  133. allocatedNode->next = freeNode;
  134. allocatedNode->free = false;
  135. printf("next Free Node address: %d\n", freeNode);
  136.  
  137. return freeNode->previous + 1;
  138. }
  139. allocatedNode = allocatedNode->next;
  140. }
  141. return (NULL);
  142. }
  143.  
  144.  
  145. int main(int argc, const char* argv[]) {
  146.  
  147. size_t heapSize = 200;
  148. STARTOFHEAP = (Node *) malloc(heapSize);
  149.  
  150. printf("Size of requested heap: %d\n", heapSize);
  151. printf("Size of node: %d\n", sizeof(Node));
  152.  
  153. initialise(STARTOFHEAP, heapSize);
  154.  
  155. endofheap = (char *) STARTOFHEAP;
  156.  
  157. printf("Start of heap address: %d\n", STARTOFHEAP);
  158. printf("End of heap address: %d\n", endofheap = (endofheap + heapSize));
  159. printf("Size of struct Node: %d\n\n", sizeof(Node));
  160.  
  161. printf("Node 1: \n");
  162. Node *nodeOne = (Node *) allocate(18);
  163. printf("Memory Address location returned to node 1: %d\n", nodeOne);
  164. printf("Node Address location returned to node 1: %d\n\n", nodeOne - 1);
  165.  
  166. printf("Node 2: \n");
  167. Node *nodeTwo = (Node *) allocate(18);
  168. printf("Address returned to node 2: %d\n", nodeTwo);
  169. printf("Node Address location returned to node 2: %d\n\n", nodeTwo - 1);
  170.  
  171. printf("Node 3: \n");
  172. Node *nodeThree = (Node *) allocate(68);
  173. printf("Address returned to node 3: %d\n", nodeThree);
  174. printf("Node Address location returned to node 3: %d\n\n", nodeThree - 1);
  175.  
  176. printf("Node 4: \n");
  177. Node *nodeFour = (Node *) allocate(18
  178. );
  179. printf("Address returned to node 4: %d\n", nodeFour);
  180. printf("Node Address location returned to node 4: %d\n\n", nodeFour - 1);
  181.  
  182.  
  183. return 0;
  184. }
  185.  
  186.  
  187. de 1: %d\n", nodeOne);
  188. printf("Node Address location returned to node 1: %d\n\n", nodeOne - 1);
  189.  
  190. printf("Node 2: \n");
  191. Node *nodeTwo = (Node *) allocate(18);
  192. printf("Address returned to node 2: %d\n", nodeTwo);
  193. printf("Node Address location returned to node 2: %d\n\n", nodeTwo - 1);
  194.  
  195. printf("Node 3: \n");
  196. Node *nodeThree = (Node *) allocate(68);
  197. printf("Address returned to node 3: %d\n", nodeThree);
  198. printf("Node Address location returned to node 3: %d\n\n", nodeThree - 1);
  199.  
  200. printf("Node 4: \n");
  201. Node *nodeFour = (Node *) allocate(18
  202. );
  203. printf("Address returned to node 4: %d\n", nodeFour);
  204. printf("Node Address location returned to node 4: %d\n\n", nodeFour - 1);
  205.  
  206.  
  207. return 0;
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement