void* chpl_pvalloc(size_t size) { size_t page_size = chpl_getHeapPageSize(); size_t num_pages = (size + page_size - 1) / page_size; size_t rounded_up = num_pages * page_size; return chpl_memalign(chpl_getHeapPageSize(), rounded_up); }
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); }
int chpl_posix_memalign(void** ptr, size_t alignment, size_t size) { void* allocated; int err; *ptr = NULL; err = chpl_posix_memalign_check_valid(alignment); if( err ) return err; // otherwise, allocate the pointer and return 0 or ENOMEM if it failed. allocated = chpl_memalign(alignment, size); if( ! allocated ) return ENOMEM; *ptr = allocated; return 0; }
void* chpl_alloc_pthread_stack(size_t stack_size){ size_t page_size, mem_size; void* stack; void* mem_buffer; int rc; rc = 0; // Gets the system page size since we'll use it for guard pages // and guard pages are only enabled when not using a huge-pages heap. page_size = chpl_getSysPageSize(); mem_size = (chpl_use_guard_page ? stack_size + page_size : stack_size); // uses memalign to align to page_size to simplify logic for the guard page // case but thread stacks could be allocated without such alignment mem_buffer = chpl_memalign(page_size, mem_size); if(mem_buffer == NULL){ return NULL; } stack = mem_buffer; if(chpl_use_guard_page){ /* This is architecture-dependent * * Since the stack grows downward, I have to insert the guard * page at the lowest address. */ stack = (unsigned char*)mem_buffer + page_size; rc = mprotect(mem_buffer, page_size, PROT_NONE); if( rc != 0 ) { chpl_free(mem_buffer); return NULL; } } return stack; }
void* chpl_valloc(size_t size) { return chpl_memalign(chpl_getHeapPageSize(), size); }