Guest User

Untitled

a guest
Jan 16th, 2018
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. /* ************************************************************************** */
  2. /* LE - / */
  3. /* / */
  4. /* gc_like.c .:: .:/ . .:: */
  5. /* +:+:+ +: +: +:+:+ */
  6. /* By: aviscogl <aviscogl@student.le-101.fr> +:+ +: +: +:+ */
  7. /* #+# #+ #+ #+# */
  8. /* Created: 2018/01/08 13:50:10 by aviscogl #+# ## ## #+# */
  9. /* Updated: 2018/01/08 16:37:56 by aviscogl ### #+. /#+ ###.fr */
  10. /* / */
  11. /* / */
  12. /* ************************************************************************** */
  13.  
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16.  
  17. typedef struct s_heap {
  18. void **heap;
  19. size_t size;
  20. } t_heap;
  21.  
  22. void ft_bzero(void *s, size_t n)
  23. {
  24. size_t i;
  25.  
  26. i = 0;
  27. while (i < n)
  28. {
  29. ((char *)s)[i] = 0;
  30. i++;
  31. }
  32. }
  33.  
  34. void *ft_memalloc(size_t size)
  35. {
  36. void *str;
  37.  
  38. str = (void*)malloc(sizeof(void*) * size);
  39. if (str == NULL)
  40. return (NULL);
  41. ft_bzero(str, sizeof(void *) * size);
  42. return (str);
  43. }
  44.  
  45.  
  46. t_heap *get_heap(void)
  47. {
  48. static t_heap *hp = NULL;
  49.  
  50. if (!hp)
  51. {
  52. hp = malloc(sizeof(t_heap));
  53. hp->size = 10;
  54. hp->heap = ft_memalloc(hp->size);
  55. }
  56. return (hp);
  57. }
  58.  
  59. void heap_up()
  60. {
  61. t_heap *hp;
  62. void **tmp;
  63. int i;
  64.  
  65. hp = get_heap();
  66. i = -1;
  67. tmp = ft_memalloc(hp->size * 2);
  68. while (++i < hp->size)
  69. tmp[i] = hp->heap[i];
  70. free(hp->heap);
  71. hp->size *= 2;
  72. hp->heap = tmp;
  73. }
  74.  
  75. void **get_first_empty()
  76. {
  77. const t_heap *hp = get_heap();
  78. int i;
  79.  
  80. i = -1;
  81. while (++i < hp->size)
  82. {
  83. if (hp->heap[i] == NULL)
  84. return (hp->heap + i);
  85. }
  86. heap_up();
  87. return (get_first_empty());
  88. }
  89.  
  90. void *m_malloc(size_t size)
  91. {
  92. void *ptr;
  93. void **pos_tab;
  94.  
  95. pos_tab = get_first_empty();
  96. if ((ptr = malloc(size)) == NULL)
  97. return (NULL);
  98. *pos_tab = ptr;
  99. return (ptr);
  100. }
  101.  
  102. int m_free(void *s)
  103. {
  104. const t_heap *hp = get_heap();
  105. int i;
  106. int contain;
  107.  
  108. i = -1;
  109. contain = 0;
  110. while (++i < hp->size)
  111. {
  112. if (hp->heap[i] == s)
  113. {
  114. contain = 1;
  115. break ;
  116. }
  117. }
  118. if (!contain)
  119. return (0);
  120. free(s);
  121. hp->heap[i] = NULL;
  122. return (1);
  123. }
  124.  
  125. void free_all()
  126. {
  127. t_heap *hp;
  128. int i;
  129.  
  130. hp = get_heap();
  131. i = -1;
  132. while (++i < hp->size)
  133. if (hp->heap[i] != NULL)
  134. free(hp->heap[i]);
  135. free(hp->heap);
  136. free((t_heap *)hp);
  137. hp->heap = NULL;
  138. hp = NULL;
  139. }
  140.  
  141. int main(void)
  142. {
  143.  
  144. int i = -1;
  145. int count = 0;
  146. while (++i < 100)
  147. {
  148. int *a = m_malloc(sizeof(int) * 3);
  149. a[0] = count++;
  150. a[1] = count++;
  151. a[2] = count++;
  152. printf(" - %i %i %i\n", a[0], a[1], a[2]);
  153. }
  154. free_all();
  155. }
Add Comment
Please, Sign In to add comment