Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void *my_malloc(size_t size)
- {
- memblock_t result;
- if ((result = find_first_block(size)) == NULL)
- return (NULL);
- result->free = 0;
- result->size = size;
- return (result + sizeof(struct memblock_s));
- }
- memblock_t heap_memory = NULL;
- void *find_first_block(size_t size)
- {
- memblock_t tmp;
- if (heap_memory == NULL)
- init_heap_memory();
- tmp = heap_memory;
- while (tmp->next != NULL && (tmp->size < size || !tmp->free))
- tmp = tmp->next;
- if (tmp->next == NULL) {
- tmp->next = sbrk(0);
- if (sbrk(MEMBLOCK_SIZE + size) == (void*) -1)
- return (NULL);
- tmp->next->next = NULL;
- tmp->next->prev = tmp;
- brk(tmp->next + size + MEMBLOCK_SIZE);
- }
- return (tmp->next);
- }
- void init_heap_memory()
- {
- if ((heap_memory = sbrk(MEMBLOCK_SIZE)) == (void*) -1)
- return;
- heap_memory->next = NULL;
- heap_memory->size = 0;
- heap_memory->prev = NULL;
- heap_memory->free = 0;
- }
- ==32021== Invalid write of size 1
- ==32021== at 0x4005E4: main (gdb-compliant-main.c:16)
- ==32021== Address 0x4228422 is 902 bytes after the brk data segment limit 0x422809c
Add Comment
Please, Sign In to add comment