void *malloc(int size) { t_header *tmp; int total_size; if (size <= 0) return (NULL); lock_thread(); if ((size % sizeof(int)) != 0) size += (sizeof(int) - (size % sizeof(int))); if ((tmp = (t_header *)findFreeBlock(size)) == NULL) tmp = (t_header *)getMoreMem(size + sizeof(*tmp)); else { if (tmp->size - size >= THRESHOLD) { total_size = tmp->size + sizeof(*tmp); tmp->size = size; split_mid(tmp, total_size); } deleteFromFreeList(tmp); } unlock_thread(); if (tmp == NULL) return (NULL); return ((void *)((int)tmp + sizeof(*tmp))); }
int64_t xpl_thread_get_and_reset_calls(xpl_thread_id tid) { assert(ctx); assert(tid); assert(tid < ctx->thread_pool_size); int64_t result = 0L; lock_thread(tid); result = ctx->thread_pool[tid].calls; ctx->thread_pool[tid].calls = 0L; unlock_thread(tid); return result; }
int test_and_set_thread_state(xpl_thread_id tid, int condition, int value) { assert(ctx); assert(tid >= 0); assert(tid < ctx->thread_pool_size); lock_thread(tid); int result = ctx->thread_pool[tid].state; if (result & condition) { ctx->thread_pool[tid].state = value; result = value; } unlock_thread(tid); return result; }