void* ssmem_alloc(ssmem_allocator_t* a, size_t size) { void* m = NULL; #ifdef TIGHT_ALLOC m = (void*) malloc(size); #else /* 1st try to use from the collected memory */ ssmem_free_set_t* cs = a->collected_set_list; if (cs != NULL) { m = (void*) cs->set[--cs->curr]; PREFETCHW(m); if (cs->curr <= 0) { a->collected_set_list = cs->set_next; a->collected_set_num--; ssmem_free_set_make_avail(a, cs); } } else { if ((a->mem_curr + size) >= a->mem_size) { /* printf("[ALLOC] out of mem, need to allocate\n"); */ a->mem = (void*) memalign(CACHE_LINE_SIZE, a->mem_size); assert(a->mem != NULL); a->mem_curr = 0; a->tot_size += a->mem_size; a->mem_chunks = ssmem_list_node_new(a->mem, a->mem_chunks); } m = a->mem + a->mem_curr; a->mem_curr += size; } #endif #if SSMEM_TS_INCR_ON == SSMEM_TS_INCR_ON_ALLOC || SSMEM_TS_INCR_ON == SSMEM_TS_INCR_ON_BOTH ssmem_ts_next(); #endif return m; }
void ssmem_free(ssmem_allocator_t* a, void* obj) { ssmem_free_set_t* fs = a->free_set_list; if (fs->curr == fs->size) { fs->ts_set = ssmem_ts_set_collect(fs->ts_set); ssmem_mem_reclaim(a); /* printf("[ALLOC] free_set is full, doing GC / size of garbage pointers: %10zu = %zu KB\n", garbagep, garbagep / 1024); */ ssmem_free_set_t* fs_new = ssmem_free_set_get_avail(a, a->fs_size, a->free_set_list); a->free_set_list = fs_new; a->free_set_num++; fs = fs_new; } fs->set[fs->curr++] = (uintptr_t) obj; #if SSMEM_TS_INCR_ON == SSMEM_TS_INCR_ON_FREE || SSMEM_TS_INCR_ON == SSMEM_TS_INCR_ON_BOTH ssmem_ts_next(); #endif }
void* ssmem_alloc(ssmem_allocator_t* a, size_t size) { void* m = NULL; /* 1st try to use from the collected memory */ ssmem_free_set_t* cs = a->collected_set_list; if (cs != NULL) { m = (void*) cs->set[--cs->curr]; PREFETCHW(m); if (cs->curr <= 0) { a->collected_set_list = cs->set_next; a->collected_set_num--; ssmem_free_set_make_avail(a, cs); } } else { if ((a->mem_curr + size) >= a->mem_size) { #if SSMEM_MEM_SIZE_DOUBLE == 1 a->mem_size <<= 1; if (a->mem_size > SSMEM_MEM_SIZE_MAX) { a->mem_size = SSMEM_MEM_SIZE_MAX; } #endif /* printf("[ALLOC] out of mem, need to allocate (chunk = %llu MB)\n", */ /* a->mem_size / (1LL<<20)); */ if (size > a->mem_size) { /* printf("[ALLOC] asking for large mem. chunk\n"); */ while (a->mem_size < size) { if (a->mem_size > SSMEM_MEM_SIZE_MAX) { fprintf(stderr, "[ALLOC] asking for memory chunk larger than max (%llu MB) \n", SSMEM_MEM_SIZE_MAX / (1024 * 1024LL)); assert(a->mem_size <= SSMEM_MEM_SIZE_MAX); } a->mem_size <<= 1; } /* printf("[ALLOC] new mem size chunk is %llu MB\n", a->mem_size / (1024 * 1024LL)); */ } #if SSMEM_TRANSPARENT_HUGE_PAGES int ret = posix_memalign(&a->mem, CACHE_LINE_SIZE, a->mem_size); assert(ret == 0); #else a->mem = (void*) memalign(CACHE_LINE_SIZE, a->mem_size); #endif assert(a->mem != NULL); #if SSMEM_ZERO_MEMORY == 1 memset(a->mem, 0, a->mem_size); #endif a->mem_curr = 0; a->tot_size += a->mem_size; a->mem_chunks = ssmem_list_node_new(a->mem, a->mem_chunks); } m = a->mem + a->mem_curr; a->mem_curr += size; } #if SSMEM_TS_INCR_ON == SSMEM_TS_INCR_ON_ALLOC || SSMEM_TS_INCR_ON == SSMEM_TS_INCR_ON_BOTH ssmem_ts_next(); #endif return m; }