Advertisement
Guest User

Untitled

a guest
Apr 20th, 2015
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <errno.h>
  4.  
  5. #define HEAP_SIZE 64
  6. #define FREE 0
  7. #define ALLOCATED 1
  8.  
  9. void* my_malloc(size_t size);
  10. void my_free(void *p);
  11. char* header();
  12. char* footer();
  13. char* previous_header();
  14. char* previous_footer();
  15. char* next_header();
  16. char* next_footer();
  17.  
  18. struct header {
  19. unsigned int length :29;
  20. unsigned int NOT_USED :2;
  21. unsigned int allocated :1;
  22. };
  23.  
  24. struct footer {
  25. unsigned int length :29;
  26. unsigned int NOT_USED :2;
  27. unsigned int allocated :1;
  28. };
  29.  
  30. static char* myHeap = FREE;
  31.  
  32. void *my_malloc(size_t size)
  33. {
  34. if(size <=0)
  35. return NULL;
  36.  
  37. if(myHeap){
  38. char* temp = myHeap;
  39. int allocated = 0;
  40. while(!allocated && temp!=(myHeap+(HEAP_SIZE-1))){
  41. if(temp)
  42. {
  43. struct header *hpTemp = temp;
  44. if(hpTemp->allocated==ALLOCATED)
  45. {
  46. int move = hpTemp->length;
  47. int i;
  48. for(i=0;i<move;i++)
  49. temp++;
  50. }
  51. }
  52. else
  53. {
  54. allocated = 1;
  55. }
  56. }
  57. errno=ENOMEM;
  58. printf("Unable to Allocate\n");
  59. return NULL;
  60. }
  61. else{
  62. myHeap = calloc(HEAP_SIZE,1);
  63. struct header *hp;
  64. hp=&myHeap[0];
  65. if(size>HEAP_SIZE){
  66. return NULL;
  67. }
  68. else
  69. {
  70. while(size%8!=0)
  71. {
  72. size++;
  73. }
  74. hp->length=(size+8);
  75. hp->allocated=ALLOCATED;
  76. struct footer *fp;
  77. fp = hp+(size+4);
  78. fp->length=(size+8);
  79. fp->allocated=ALLOCATED;
  80. return hp+4;
  81. }
  82. }
  83.  
  84. return NULL;
  85. }
  86.  
  87. void my_free(void *p)
  88. {
  89. struct header *temp = p-4;
  90. temp->allocated=FREE;
  91. if(temp-4->allocated==FREE&&temp!=myHeap)
  92. {
  93. int tempLength = temp->length;
  94. int leftTempLen = temp-4->length;
  95. temp-(8+leftTempLen)->length=(tempLength+leftTempLen+8);
  96. (footer)temp+tempLength->length=(tempLength+leftTempLen+8);
  97. temp+tempLength->allocated=FREE;
  98. temp-=(8+leftTempLen);
  99. }
  100. if(temp+4->allocated==FREE&&temp!=(myHeap+HEAP_SIZE))
  101. {
  102. int tempLength = temp->length;
  103. int rightTempLen = temp+(tempLength+4)->length;
  104. temp-(8+leftTempLen)->length=(tempLength+leftTempLen+8);
  105. (header)temp->length=(tempLength+leftTempLen+8);
  106. (footer)temp+(tempLength+rightTempLen+8)->allocated=FREE;
  107. }
  108.  
  109. }
  110.  
  111. char *header()
  112. {
  113. return NULL;
  114. }
  115. char *footer()
  116. {
  117. return NULL;
  118. }
  119. char *previous_header()
  120. {
  121. return NULL;
  122. }
  123. char *previous_footer()
  124. {
  125. return NULL;
  126. }
  127. char *next_header()
  128. {
  129. return NULL;
  130. }
  131. char *next_footer()
  132. {
  133. return NULL;
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement