static void* memalign_hook_ini(size_t alignment, size_t sz, const __malloc_ptr_t caller) { __memalign_hook = NULL; ptmalloc_init(); return __libc_memalign(alignment, sz); }
int posix_memalign(void **memptr, size_t alignment, size_t size) { int ret; if( !chpl_mem_inited() ) { *memptr = NULL; ret = chpl_posix_memalign_check_valid(alignment); if( ret ) return ret; *memptr = __libc_memalign(alignment, size); if( ! *memptr ) return ENOMEM; if( DEBUG_REPLACE_MALLOC ) printf("in early posix_memalign %p = system posix_memalign(%#x)\n", *memptr, (int) size); track_system_allocated(*memptr, size, __libc_malloc); return 0; } if( DEBUG_REPLACE_MALLOC ) printf("in posix_memalign\n"); ret = chpl_posix_memalign(memptr, alignment, size); if( DEBUG_REPLACE_MALLOC ) printf("%p = chpl_posix_memalign(%#x, %#x) returned %i\n", *memptr, (int) alignment, (int) size, ret); return ret; }
static void * mem_get_memory(size_t align, size_t *alloc_size, int *ispool, mem_slot_queue_t **ppslot) { mem_slot_queue_t *pslot = NULL; size_t new_alloc_size; size_t sn; void *data; new_alloc_size = *alloc_size; if (!nkn_pool_enable || align || (*alloc_size > SIZE_MAX_SLOT)) goto get_poolmiss; SIZE_TO_SLOT(*alloc_size, sn); new_alloc_size = SLOT_TO_SIZE(sn); *alloc_size = new_alloc_size; // Check thread local pool if available if ((int)mempool_key) { pslot = pthread_getspecific(mempool_key); if (pslot) { *ppslot = &pslot[sn]; if ((data = mem_slot_get(&pslot[sn], &pslot))) { *ispool = MEM_SLOT_NLIB; return data; } } } if (!pslot) { // Check global pool pslot = &mem_slot_q[0]; *ppslot = &pslot[sn]; data = mem_slot_get(&mem_slot_q[sn], &pslot); if (data) { *ispool = MEM_SLOT_NLIB; return data; } } // Fall thru to regular allocation with a size that can be // pooled during the free. get_poolmiss: if (align) { data = __libc_memalign(align, new_alloc_size); } else { data = __libc_malloc(new_alloc_size); } return data; }
extern "C" void* memalign(size_t alignment, size_t size) { REF; void* out = __libc_memalign(alignment, size); DEREF; if (out && D) { D->now_usable.fetchAndAddOrdered(malloc_usable_size(out)); D->now_overhead.fetchAndAddOrdered(CHUNK_OVERHEAD); D->updatePeak(); } return out; }
void* memalign(size_t alignment, size_t size) { if( !chpl_mem_inited() ) { void* ret = __libc_memalign(alignment, size); if( DEBUG_REPLACE_MALLOC ) printf("in early memalign %p = system memalign(%#x)\n", ret, (int) size); track_system_allocated(ret, size, __libc_malloc); return ret; } if( DEBUG_REPLACE_MALLOC ) printf("in memalign\n"); return chpl_memalign(alignment, size); }
/* ******************************************************************************* * Internal functions ******************************************************************************* */ static void * int_nkn_memalloc_malloc(size_t size, size_t align, nkn_obj_type_t type) { size_t alloc_size; void *ptr; char *data; mem_alloc_hdr_t *mh; mem_slot_queue_t *pslot = NULL; int align_log2; int n; int ispool = MEM_SLOT_GLIB; if (size >= MH_MAX_MEMORY_SIZE || type >= MH_MAX_TYPE_VAL) { assert(0); } if (align) { // Note: align is assumed to be a pwr2 and multiple of sizeof(void*) for (n = 1; (n * align) < sizeof(mem_alloc_hdr_t); n++) ; } else { n = 0; } alloc_size = ALLOC_SIZE(size, (n * align)); if (align) { // compute log2(align) for (align_log2 = CHUNK_SIZE_LOG2; align > (size_t)(1 << align_log2); align_log2++) ; ptr = __libc_memalign(align, alloc_size); } else { align_log2 = 0; ptr = mem_get_memory(0, &alloc_size, &ispool, &pslot); // alloc_size may be // modified } if (ptr) { data = (char *)ptr + (align ? (n * align) : sizeof(mem_alloc_hdr_t)); mh = (mem_alloc_hdr_t *)(data - sizeof(mem_alloc_hdr_t)); mh->magicno = MH_MAGIC; mh->type = type; mh->size_chunk_units = (size_t)(alloc_size >> CHUNK_SIZE_LOG2); if (align_log2 >= MH_MAX_ALIGN_VAL) assert(0); mh->align_log2 = align_log2; mh->align_units = n; mh->flags = ispool; mh->pslot = pslot; // Update stats if (type < nkn_malloc_max_mem_types) { AO_fetch_and_add(obj_type_data[type].cnt, alloc_size); *obj_type_data[type].obj_size = alloc_size; if (ispool) AO_fetch_and_add1(obj_type_data[type].nlib); else { AO_fetch_and_add1(obj_type_data[type].glib); } } return MHDR_TO_MEM(mh); } else { return 0;
extern "C" void *valloc(size_t size) { return __libc_memalign(sysconf(_SC_PAGESIZE), size); }