Guest User

Untitled

a guest
Jan 23rd, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. void *my_malloc(size_t size)
  2. {
  3. memblock_t result;
  4. if ((result = find_first_block(size)) == NULL)
  5. return (NULL);
  6. result->free = 0;
  7. result->size = size;
  8. return (result + sizeof(struct memblock_s));
  9. }
  10.  
  11. memblock_t heap_memory = NULL;
  12.  
  13. void *find_first_block(size_t size)
  14. {
  15. memblock_t tmp;
  16.  
  17. if (heap_memory == NULL)
  18. init_heap_memory();
  19. tmp = heap_memory;
  20. while (tmp->next != NULL && (tmp->size < size || !tmp->free))
  21. tmp = tmp->next;
  22. if (tmp->next == NULL) {
  23. tmp->next = sbrk(0);
  24. if (sbrk(MEMBLOCK_SIZE + size) == (void*) -1)
  25. return (NULL);
  26. tmp->next->next = NULL;
  27. tmp->next->prev = tmp;
  28. brk(tmp->next + size + MEMBLOCK_SIZE);
  29. }
  30. return (tmp->next);
  31. }
  32.  
  33.  
  34.  
  35. void init_heap_memory()
  36. {
  37. if ((heap_memory = sbrk(MEMBLOCK_SIZE)) == (void*) -1)
  38. return;
  39. heap_memory->next = NULL;
  40. heap_memory->size = 0;
  41. heap_memory->prev = NULL;
  42. heap_memory->free = 0;
  43. }
  44.  
  45. ==32021== Invalid write of size 1
  46. ==32021== at 0x4005E4: main (gdb-compliant-main.c:16)
  47. ==32021== Address 0x4228422 is 902 bytes after the brk data segment limit 0x422809c
Add Comment
Please, Sign In to add comment