void tls_shutdown() { for (int i = 0; i < tls->entry_count; i++) TlsFree(tls->entries[i]); for (int i = 0; i < TLS_KERNEL_ENTRY_COUNT; i++) TlsFree(tls->kernel_entries[i]); mm_munmap(TLS_DATA_BASE, sizeof(struct tls_data)); }
void kfree(void *mem, int size) { AcquireSRWLockExclusive(&heap->rw_lock); /* Find memory bucket */ void *bucket_addr = (void *)((size_t) mem & (-BLOCK_SIZE)); /* Find pool */ int p = -1; for (int i = 0; i < POOL_COUNT; i++) if (size <= heap->pools[i].objsize) { p = i; break; } if (p == -1) { log_error("kfree(): Invalid size: %x\n", mem); ReleaseSRWLockExclusive(&heap->rw_lock); return; } /* Loop over the chain to find the corresponding bucket */ struct bucket *previous = NULL; struct bucket *current = heap->pools[p].first; while (current) { if (current != bucket_addr) { previous = current; current = current->next_bucket; continue; } *(void **)mem = current->first_free; current->first_free = mem; current->ref_cnt--; if (!current->ref_cnt) { /* Bucket empty, free it */ if (!previous) heap->pools[p].first = current->next_bucket; else previous->next_bucket = current->next_bucket; mm_munmap(current, BLOCK_SIZE); } ReleaseSRWLockExclusive(&heap->rw_lock); return; } log_error("kfree(): Invalid memory pointer or size: (%x, %d)\n", mem, size); ReleaseSRWLockExclusive(&heap->rw_lock); }