/* Iterates through a page and coalesces free buffers. Called only after freeing allocated space on the page. Checks a buffer with its buddy and coalesces the two if both are free. */ void coalesce(pageheaderT* page) { freelistL* buffer = get_required_buffer(1, page); allocheaderT* alloc; allocheaderT* prev = NULL; while (buffer != NULL) { alloc = buffer->first_block; while (alloc != NULL) { if (alloc->next != NULL && (get_buddy(alloc) == alloc->next)) { if (prev == NULL) buffer->first_block = alloc->next->next; else prev->next = alloc->next->next; page->used += 1; kma_free(alloc, alloc->size * 2); return; } else alloc = alloc->next; } buffer = buffer->bigger_size; } }
void deallocate(mem_t *requests, int req_id) { mem_t *cur = &requests[req_id]; assert(cur->state == USED); assert(cur->size > 0); #ifndef COMPETITION // Only run the memory checks if we're testing for correctness. // check memory check((char *) cur->ptr, (char *) cur->value, cur->size); // free memory free(cur->value); #endif kma_free(cur->ptr, cur->size); currentAllocBytes -= cur->size; cur->state = FREE; }