Guest User

Untitled

a guest
Jul 22nd, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.95 KB | None | 0 0
  1. #include <stdio.h> // Any other headers we need here
  2. #include <malloc.h> // We bring in the old malloc function
  3. #include <stdlib.h>
  4. #include <sys/types.h>
  5. #include <unistd.h>
  6. #include <pthread.h>
  7. #include<string.h>
  8.  
  9.  
  10. // The Node in the linked list
  11. struct block
  12. {
  13. size_t size;
  14. int is_free;
  15. struct block *next;
  16. };
  17. struct block *head, *tail;
  18.  
  19. struct block* getFreeBlock(size_t size);
  20.  
  21. void* mymalloc(size_t size)
  22. {
  23. size_t total_size; //total_size = size + header_size
  24. void* block;
  25. struct block *header;
  26. if(!size)
  27. return NULL;
  28. header = getFreeBlock(size);
  29. if(header)
  30. {
  31. header->is_free = 0;
  32. printf("Allocated %zu bytesn",header->size);
  33. return (void*)(header+1); //This is the first byte of the actual memory block.
  34. }
  35. total_size = sizeof(struct block) + size;
  36. block = sbrk(total_size);
  37. if(block == (void*)-1)
  38. {
  39. printf("Malloc Failed!n");
  40. return NULL;
  41. }
  42. header = block;
  43. header->size = size;
  44. header->is_free = 0;
  45. header->next = NULL;
  46. if(!head)
  47. head = header; // the first block is a list of free + allocated blocks
  48. if(tail)
  49. tail->next = header; // if tail is initialized then
  50. tail = header; //if tail is not initialized, this this block must be the first and the last block
  51. printf("Allocated %zu bytesn",header->size);
  52.  
  53. return (void*)(header+1); //header+1 = memory block
  54. }
  55.  
  56. struct block* getFreeBlock(size_t size)
  57. {
  58. struct block *current = head;
  59. while(current)
  60. {
  61. if(current->is_free)
  62. {
  63. if(current->size >= size)
  64. return current;
  65. }
  66. current = current->next;
  67. }
  68. return NULL;
  69. }
  70.  
  71. void myfree(void *block){
  72. struct block *header, *temp;
  73. void *brk;
  74. if(!block)
  75. return;
  76. header = (struct block*)block - 1; // moving to the block that is just behind the block we want to free.
  77. brk = sbrk(0);
  78. // WE check if the block t be freed is at the end of the heap can compare it to the current program break.
  79. if((char*)block + header->size == brk)
  80. {
  81. // If we only have one block, and that block is freed
  82. if(head == tail)
  83. head = tail = NULL;
  84. // Traversing to the end of the linked list.
  85. else
  86. {
  87. temp = head;
  88. while(temp)
  89. {
  90. if(temp->next == tail)
  91. {
  92. temp->next = NULL;
  93. tail = temp;
  94. }
  95. temp = temp->next;
  96. }
  97. }
  98. // Releasing the memory
  99. // total_size = size_of_block + header_size
  100. sbrk(- (sizeof(struct block) + header->size));
  101. printf("Freed some memoryn");
  102. return;
  103. }
  104. // If the block is not the last block, then we simply set the block as free.
  105. // The getFreeBlock function first checks for free blocks, before calling sbrk().
  106. header->is_free = 1;
  107. printf("Freed some memoryn");
  108. }
Add Comment
Please, Sign In to add comment