/*Resolve defragmentation with a next block*/ static void defragmentation(struct block_desc *md) { struct block_desc *next_md; next_md = (void *) (((size_t) md) + md->size); if (!correct_address((char *) md)) { return; } if (get_available(md)) { /*Look at the next block and it is free, paste it*/ if (get_available(next_md)) { md->size = md->size + next_md->size; defragmentation(md); } } defragmentation(next_md); }
/* This procedure makes free busy block * at the specified @address. * @address - block address being freed; */ static void memory_free(void *address) { /* Detect address of memory_block */ struct block_desc *md = address - BLOCK_DESC_SIZE; /* Make block free*/ set_available(md); /* Set the new value of pointer to the free block */ if (current_space > (char *) md) { current_space = (char *) md; } else { md = (void *) current_space; } /*Resolve defragmentation*/ defragmentation(md); printf("NEW current_free_space 0x%x\n", (uint32_t) current_space); }
void* ora_salloc(size_t size) { size_t aligned_size; int repeat_c; void *ptr = NULL; aligned_size = align_size(size); for (repeat_c = 0; repeat_c < 2; repeat_c++) { size_t max_min = max_size; int select = -1; int i; /* find first good free block */ for (i = 0; i < *list_c; i++) { if (list[i].dispossible) { /* If this block is just the right size, return it */ if (list[i].size == aligned_size) { list[i].dispossible = false; ptr = list[i].first_byte_ptr; /* list[i].context = context; */ return ptr; } if (list[i].size > aligned_size && list[i].size < max_min) { max_min = list[i].size; select = i; } } } /* If no suitable free slot found, defragment and try again. */ if (select == -1 || *list_c == LIST_ITEMS) { defragmentation(); continue; } /* * A slot larger than required was found. Divide it to avoid wasting * space, and return the slot of the right size. */ list[*list_c].size = list[select].size - aligned_size; list[*list_c].first_byte_ptr = (char*)list[select].first_byte_ptr + aligned_size; list[*list_c].dispossible = true; list[select].size = aligned_size; list[select].dispossible = false; /* list[select].context = context; */ ptr = list[select].first_byte_ptr; *list_c += 1; break; } return ptr; }