void* realloc(void* ptr, size_t size) { if( !chpl_mem_inited() ) { void* ret = __libc_realloc(ptr, size); if( DEBUG_REPLACE_MALLOC ) printf("in early realloc %p = system realloc(%p,%#x)\n", ret, ptr, (int) size); track_system_allocated(ret, size, __libc_malloc); return ret; } else { void* ret = NULL; size_t allocated_len = 0; size_t copy_size; if( DEBUG_REPLACE_MALLOC ) printf("in realloc(%p,%#x)\n", ptr, (int) size); // check to see if we're realloc'ing a pointer that was allocated // before the our allocator came up. if( is_system_allocated(ptr, &allocated_len) ) { if( DEBUG_REPLACE_MALLOC ) { printf("in realloc, ptr %p was system allocated to size %#x\n", ptr, (int) allocated_len); } // allocate some new memory on the Chapel heap ret = chpl_malloc(size); // copy the minimum of allocated_len and size // to handle realloc expanding or shrinking the allocation copy_size = allocated_len; if( size < copy_size ) copy_size = size; memcpy(ret, ptr, copy_size); // free the old pointer from the system heap. __libc_free(ptr); return ret; } else { return chpl_realloc(ptr, size); } } }
void* malloc(size_t size) { void* ret; if( !chpl_mem_inited() ) { ret = __libc_malloc(size); if( DEBUG_REPLACE_MALLOC ) printf("in early malloc %p = system malloc(%#x)\n", ret, (int) size); track_system_allocated(ret, size, __libc_malloc); return ret; } if( DEBUG_REPLACE_MALLOC ) printf("in malloc\n"); ret = chpl_malloc(size); if( DEBUG_REPLACE_MALLOC ) printf("%p = chpl_malloc(%#x)\n", ret, (int) size); return ret; }
// // Chapel runtime initialization // // Factored out of "int main(int argc, char* argv[])" for the purpose // re-using it when in "library-mode". // // Called from main.c:main(...) and chpl-init.c:chpl_library_init(...) // void chpl_rt_init(int argc, char* argv[]) { int32_t execNumLocales; int runInGDB; // Check that we can get the page size. assert( sys_page_size() > 0 ); // Declare that we are 'locale aware' so that // UTF-8 functions (e.g. wcrtomb) work as // indicated by the locale environment variables. setlocale(LC_CTYPE,""); // So that use of localtime_r is portable. tzset(); // // Handle options that set the environment before doing any other // runtime initialization. // parseArgs(false, parse_dash_E, &argc, argv); chpl_error_init(); // This does local-only initialization chpl_topo_init(); chpl_comm_init(&argc, &argv); chpl_mem_init(); chpl_comm_post_mem_init(); chpl_comm_barrier("about to leave comm init code"); CreateConfigVarTable(); // get ready to start tracking config vars chpl_gen_main_arg.argv = chpl_malloc(argc * sizeof(char*)); chpl_gen_main_arg.argv[0] = argv[0]; chpl_gen_main_arg.argc = 1; chpl_gen_main_arg.return_value = 0; parseArgs(false, parse_normally, &argc, argv); recordExecutionCommand(argc, argv); // // If the user specified a number of locales, have the comm layer // verify that it is reasonable. // execNumLocales = getArgNumLocales(); if (execNumLocales != 0) { chpl_comm_verify_num_locales(execNumLocales); } runInGDB = _runInGDB(); if (runInGDB) { int status; if (chpl_comm_run_in_gdb(argc, argv, runInGDB, &status)) { chpl_exit_all(status); } } // // Initialize the task management layer. // chpl_task_init(); // Initialize privatization, needs to happen before hitting module init chpl_privatization_init(); // // Some comm layer initialization has to wait until after the // tasking layer is initialized. // chpl_comm_post_task_init(); chpl_comm_rollcall(); // // Make sure the runtime is fully set up on all locales before we start // running Chapel code. // chpl_comm_barrier("barrier before main"); }