int main(int argc, char *argv[]) { unsigned char *buffer; int i; if (!mlockall(MCL_CURRENT | MCL_FUTURE)) mallopt(M_TRIM_THRESHOLD, -1UL); mallopt(M_MMAP_MAX, 0); buffer = malloc(SOMESIZE); if (!buffer) exit(-1); /* * Touch each page in this piece of memory to get it * mapped into RAM */ for (i = 0; i < SOMESIZE; i += 4 * 1024) buffer[i] = 0; free(buffer); /* <do your RT-thing> */ while(1); return 0; }
int lock_all(int stk, int heap) { extern void dump_malloc_stats(void); int err, n, i; unsigned long *pt; char stack[stk ? stk : GROW_STACK]; stack[0] = ' '; /* glibc (2.1.3) limits and default values: dynamically tunable * mmap threshold 128k * mmap threshold max 512k mmap max 1024k * trim threshold 128k * top pad 0k * heap min 32k heap max 1024k */ n = heap / 65536 + 1; if ( n > (sizeof(stack) / sizeof(int))) { printf("heap too large\n"); exit(-1); } err = mallopt(M_MMAP_THRESHOLD, 512*1024); if (!err) { printf("mallopt(M_MMAP_THRESHOLD, heap) failed\n"); exit(-1); } err = mallopt(M_TOP_PAD, heap ? heap : GROW_HEAP); if (!err) { printf("mallopt(M_TOP_PAD, heap) failed\n"); exit(-1); } if(mlockall(MCL_CURRENT|MCL_FUTURE)) { perror("mlockall"); exit(-1); } touch_all(); pt = (unsigned long *) stack; for(i=0; i<n ; i++, pt++) *pt = (unsigned long) malloc(65536); pt = (unsigned long *) stack; for(i=0; i<n ; i++, pt++) free((void *) *pt); return 0; }
static void init_mallopt_disable_mmap(void) { char *env = getenv("HFI_DISABLE_MMAP_MALLOC"); if (env && *env) { if (mallopt(M_MMAP_MAX, 0) && mallopt(M_TRIM_THRESHOLD, -1)) { __hfi_malloc_no_mmap = 1; } } return; }
static void pscom_openib_init_con(pscom_con_t *con) { con->pub.type = PSCOM_CON_TYPE_OPENIB; // Only Polling: con->write_start = pscom_poll_write_start; con->write_stop = pscom_poll_write_stop; con->read_start = pscom_poll_read_start; con->read_stop = pscom_poll_read_stop; con->poll_reader.do_read = pscom_openib_do_read; con->do_write = pscom_openib_do_write; con->close = pscom_openib_con_close; #ifdef IB_USE_RNDV con->rma_mem_register = pscom_openib_rma_mem_register; con->rma_mem_deregister = pscom_openib_rma_mem_deregister; #ifdef IB_RNDV_RDMA_WRITE con->rma_write = pscom_openib_rma_write; #else con->rma_read = pscom_openib_rma_read; #endif con->rendezvous_size = pscom.env.rendezvous_size_openib; #ifdef IB_RNDV_DISABLE_FREE_TO_OS /* We have to prevent free() from returning memory back to the OS: */ #ifndef IB_RNDV_USE_MALLOC_HOOKS if (con->rendezvous_size != ~0U) { /* See 'man mallopt(3) / M_MMAP_MAX': Setting this parameter to 0 disables the use of mmap(2) for servicing large allocation requests. */ mallopt(M_MMAP_MAX, 0); /* See 'man mallopt(3) / M_TRIM_THRESHOLD': Setting M_TRIM_THRESHOLD to -1 disables trimming completely. */ mallopt(M_TRIM_THRESHOLD, -1); } #else if(__morecore == __default_morecore) { /* Switch to our own function pscom_openib_morecore() that does not trim: */ __morecore = pscom_openib_morecore_hook; } __free_hook = pscom_openib_free_hook; #endif #endif #endif pscom_con_setup_ok(con); }
static void configure_malloc_behavior(void) { /* Now lock all current and future pages from preventing of being paged */ if (mlockall(MCL_CURRENT | MCL_FUTURE)) perror("mlockall failed:"); /* Turn off malloc trimming.*/ mallopt(M_TRIM_THRESHOLD, -1); /* Turn off mmap usage. */ mallopt(M_MMAP_MAX, 0); }
void machdep() { long seed ; static int first=1 ; if( !first ) return ; else first = 0 ; /*-- force use of mcw_malloc.c functions - 05 Nov 2001 --*/ #ifdef USING_MCW_MALLOC if( AFNI_yesenv("AFNI_FORCE_MCW_MALLOC") ) enable_mcw_malloc(); #endif /*-- disable mmap() in malloc() [21 Aug 2002: mostly] --*/ #if defined(LINUX) && defined(M_MMAP_MAX) mallopt( M_MMAP_MAX , 1 ) ; #endif seed = (long)AFNI_numenv("AFNI_RANDOM_SEEDVAL") ; init_rand_seed(seed) ; be_quiet = AFNI_yesenv("AFNI_QUIET_STARTUP") ; /* 08 Dec 2010 */ if( AFNI_yesenv("AFNI_USE_FGETS") ) afni_fgets_setskip(1) ; /* 21 Dec 2011 */ AFNI_do_nothing() ; /* 02 Oct 2012 */ return ; }
static void gimp_init_malloc (void) { #ifdef GIMP_GLIB_MEM_PROFILER g_mem_set_vtable (glib_mem_profiler_table); g_atexit (g_mem_profile); #endif #ifdef __GLIBC__ /* Tweak memory allocation so that memory allocated in chunks >= 4k * (64x64 pixel 1bpp tile) gets returned to the system when free()'d. * * The default value for M_MMAP_THRESHOLD in glibc-2.3 is 128k. * This is said to be an empirically derived value that works well * in most systems. Lowering it to 4k is thus probably not the ideal * solution. * * An alternative to tuning this parameter would be to use * malloc_trim(), for example after releasing a large tile-manager. */ #if 0 mallopt (M_MMAP_THRESHOLD, TILE_WIDTH * TILE_HEIGHT); #endif #endif }
int main(void) { void *ptr; int rc; int pagesize = getpagesize(); rc = mallopt(M_MMAP_THRESHOLD, DEFAULT_MMAP_THRESHOLD_MAX); if (rc != 1) { fprintf(stderr, "mallopt failed: %d\n", rc); exit(1); } rc = posix_memalign(&ptr, pagesize, pagesize); if (rc) { fprintf(stderr, "posix_memalign failed: %d\n", rc); exit(1); } rc = mprotect(ptr, pagesize, PROT_READ | PROT_EXEC); if (rc < 0) { perror("mprotect"); exit(1); } exit(0); }
void SessionRep::init( const char* name, int& argc, char** argv, const OptionDesc* opts, const PropertyData* initprops, Display* display ) { argc_ = argc; argv_ = new char*[argc + 1]; for (int i = 0; i < argc; i++) { argv_[i] = argv[i]; } argv_[argc_] = nil; init_style(name, initprops); if (opts != nil) { parse_args(argc, argv, opts); } parse_args(argc, argv, defoptions); init_display(display); Cursor::init(); #ifdef sgi if (style_->value_is_on("malloc_debug")) { mallopt(M_DEBUG, 1); } #endif }
void SetupEnvironment() { #ifdef HAVE_MALLOPT_ARENA_MAX // glibc-specific: On 32-bit systems set the number of arenas to 1. // By default, since glibc 2.10, the C library will create up to two heap // arenas per core. This is known to cause excessive virtual address space // usage in our usage. Work around it by setting the maximum number of // arenas to 1. if (sizeof(void*) == 4) { mallopt(M_ARENA_MAX, 1); } #endif // On most POSIX systems (e.g. Linux, but not BSD) the environment's locale // may be invalid, in which case the "C" locale is used as fallback. #if !defined(WIN32) && !defined(MAC_OSX) && !defined(__FreeBSD__) && !defined(__OpenBSD__) try { std::locale(""); // Raises a runtime error if current locale is invalid } catch (const std::runtime_error&) { setenv("LC_ALL", "C", 1); } #endif // The path locale is lazy initialized and to avoid deinitialization errors // in multithreading environments, it is set explicitly by the main thread. // A dummy locale is used to extract the internal default locale, used by // fs::path, which is then used to explicitly imbue the path. std::locale loc = fs::path::imbue(std::locale::classic()); fs::path::imbue(loc); }
int main(int argc, char **argv){ #if __WORDSIZE == 32 exit(1); #endif char** new_argv = argv; int i; char* temp_ptr; int return_value; #if defined(__linux__) mallopt(M_MMAP_MAX, 0); #endif for(i = 0; i < argc; i++) { __softboundmpx_metadata_store(&new_argv[i], new_argv[i], new_argv[i] + strlen(new_argv[i]) + 1, new_argv[i]); } softboundmpx_init_ctype(); /* Santosh: Real Nasty hack because C programmers assume argv[argc] * to be NULL. Also this NUll is a pointer, doing + 1 will make the * size_of_type to fail */ temp_ptr = ((char*) &new_argv[argc]) + 8; __softboundmpx_allocate_shadow_stack_space(2); __softboundmpx_store_base_shadow_stack(&new_argv[0], 1); __softboundmpx_store_bound_shadow_stack(temp_ptr, 1); return_value = softboundmpx_pseudo_main(argc, new_argv); __softboundmpx_deallocate_shadow_stack_space(); return return_value; }
static void init_memory() { void *dummy; /* Make sure we always have some memory at hand. */ mallopt(MALLOC_MEMORY_HOLD, 1); dummy = calloc(32 * 1024, 1); /* 32K for RAM */ free (dummy); }
int main (int argc, char *argv[]) { gint retval; CajaApplication *application; #if defined (HAVE_MALLOPT) && defined(M_MMAP_THRESHOLD) /* Caja uses lots and lots of small and medium size allocations, * and then a few large ones for the desktop background. By default * glibc uses a dynamic treshold for how large allocations should * be mmaped. Unfortunately this triggers quickly for caja when * it does the desktop background allocations, raising the limit * such that a lot of temporary large allocations end up on the * heap and are thus not returned to the OS. To fix this we set * a hardcoded limit. I don't know what a good value is, but 128K * was the old glibc static limit, lets use that. */ mallopt (M_MMAP_THRESHOLD, 128 *1024); #endif if (g_getenv ("CAJA_DEBUG") != NULL) { eel_make_warnings_and_criticals_stop_in_debugger (); } /* Initialize gettext support */ bindtextdomain (GETTEXT_PACKAGE, MATELOCALEDIR); bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); textdomain (GETTEXT_PACKAGE); g_set_prgname ("caja"); gdk_set_allowed_backends ("x11"); if (g_file_test (DATADIR "/applications/caja.desktop", G_FILE_TEST_EXISTS)) { egg_set_desktop_file (DATADIR "/applications/caja.desktop"); } #ifdef HAVE_EXEMPI xmp_init(); #endif setup_debug_log (); /* Initialize the services that we use. */ LIBXML_TEST_VERSION /* Run the caja application. */ application = caja_application_new(); retval = g_application_run (G_APPLICATION (application), argc, argv); g_object_unref (application); eel_debug_shut_down (); return retval; }
int main(int argc, char* argv[]) { // Now lock all current and future pages from preventing of being paged if (mlockall(MCL_CURRENT | MCL_FUTURE )) { perror("mlockall failed:"); } // Turn off malloc trimming. mallopt (M_TRIM_THRESHOLD, -1); // Turn off mmap usage. mallopt (M_MMAP_MAX, 0); int page_size = sysconf(_SC_PAGESIZE); // Allocate some memory char* buffer = malloc(SOMESIZE); // Touch each page in this piece of memory to get it mapped into RAM int i; for (i=0; i < SOMESIZE; i+=page_size) { // Each write to this buffer will generate a pagefault. // Once the pagefault is handled a page will be locked in memory and never // given back to the system. buffer[i] = 0; // print the number of major and minor pagefaults this application has triggered struct rusage usage; getrusage(RUSAGE_SELF, &usage); (int) printf("Major-pagefaults:%d, Minor Pagefaults:%d\n", (int)usage.ru_majflt, (int)usage.ru_minflt); } free(buffer); // buffer is now released. As glibc is configured such that it never gives back memory to // the kernel, the memory allocated above is locked for this process. All malloc() and new() // calls come from the memory pool reserved and locked above. Issuing free() and delete() // does NOT make this locking undone. So, with this locking mechanism we can build C++ applications // that will never run into a major/minor pagefault, even with swapping enabled. //<do your RT-thing> return 0; }
END_TEST START_TEST(test_mallopt) { int ret; ret = mallopt(NULL, NULL); fail_if(ret != -1); fail_if(errno != ENOSYS); }
static void contest_alloc_init() { inside_init = 1; /* Tell malloc() not to use mmap() */ mallopt(M_MMAP_MAX, 0); sbrk_start = sbrk_largest = sbrk(0); libc_calloc = dlsym(RTLD_NEXT, "calloc"); libc_malloc = dlsym(RTLD_NEXT, "malloc"); libc_free = dlsym(RTLD_NEXT, "free"); libc_realloc = dlsym(RTLD_NEXT, "realloc"); inside_init = 2; alloc_handle = dlopen("./alloc.so", RTLD_NOW | RTLD_GLOBAL); if (!alloc_handle) { char *err = dlerror(); if (!err) fprintf(stderr, "A dynamic linking error occurred: (%s)\n", err); else fprintf(stderr, "An unknown dynamic linking error occurred.\n"); exit(65); } alloc_calloc = dlsym(alloc_handle, "calloc"); alloc_malloc = dlsym(alloc_handle, "malloc"); alloc_free = dlsym(alloc_handle, "free"); alloc_realloc = dlsym(alloc_handle, "realloc"); if (!alloc_calloc || !alloc_malloc || !alloc_free || !alloc_realloc) { fprintf(stderr, "Unable to dynamicly load a required memory allocation call.\n"); exit(66); } char *file_name = getenv("ALLOC_CONTEST_MMAP"); int fd = open(file_name, O_RDWR); stats = mmap(NULL, sizeof(alloc_stats_t), PROT_WRITE, MAP_SHARED, fd, 0); if (fd <= 0 || stats == (void *)-1) { fprintf(stderr, "fd/mmap"); exit(67); } stats->max_heap_used = 0; stats->memory_heap_sum = 0; stats->memory_uses = 0; sbrk_init_done = sbrk(0); inside_init = 0; }
int main(int argc, char** argv) { // Set M_DECAY_TIME so that our allocations aren't immediately purged on free. mallopt(M_DECAY_TIME, 1); android::base::SetMinimumLogSeverity(android::base::WARNING); adb_trace_init(argv); ::benchmark::Initialize(&argc, argv); if (::benchmark::ReportUnrecognizedArguments(argc, argv)) return 1; ::benchmark::RunSpecifiedBenchmarks(); }
void malloc_init() { # if TARGET_OS_VERSION == LINUX_VERSION mallopt(M_MMAP_MAX, 0); // if Linux malloc mmaps, we get bit 31 on, which conflicts with memOop marking # endif mallocReserve= (caddr_t)malloc(mallocReserveAmount); if (mallocReserve == NULL) warning("Couldn't reserve enough memory: system is unlikely to run.\n" "You should increase the amount of swap space available"); std::set_new_handler(MallocFailed); }
int __gl_memInit( size_t maxFast ) { #ifndef NO_MALLOPT /* mallopt( M_MXFAST, maxFast );*/ #ifdef MEMORY_DEBUG mallopt( M_DEBUG, 1 ); #endif #endif return 1; }
int main (int argc, char *argv[]) { gint retval; NautilusApplication *application; #if defined (HAVE_MALLOPT) && defined(M_MMAP_THRESHOLD) /* Nautilus uses lots and lots of small and medium size allocations, * and then a few large ones for the desktop background. By default * glibc uses a dynamic treshold for how large allocations should * be mmaped. Unfortunately this triggers quickly for nautilus when * it does the desktop background allocations, raising the limit * such that a lot of temporary large allocations end up on the * heap and are thus not returned to the OS. To fix this we set * a hardcoded limit. I don't know what a good value is, but 128K * was the old glibc static limit, lets use that. */ mallopt (M_MMAP_THRESHOLD, 128 *1024); #endif /* This will be done by gtk+ later, but for now, force it to GNOME */ g_desktop_app_info_set_desktop_env ("GNOME"); if (g_getenv ("NAUTILUS_DEBUG") != NULL) { eel_make_warnings_and_criticals_stop_in_debugger (); } /* Initialize gettext support */ bindtextdomain (GETTEXT_PACKAGE, GNOMELOCALEDIR); bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8"); textdomain (GETTEXT_PACKAGE); g_set_prgname ("nautilus"); #ifdef HAVE_EXEMPI xmp_init(); #endif /* Run the nautilus application. */ application = nautilus_application_new (); /* hold indefinitely if we're asked to persist */ if (g_getenv ("NAUTILUS_PERSIST") != NULL) { g_application_hold (G_APPLICATION (application)); } retval = g_application_run (G_APPLICATION (application), argc, argv); g_object_unref (application); eel_debug_shut_down (); return retval; }
int main(int argc, char* argv[]) { /* execution begins here 8-) */ int retval; #ifdef M_MMAP_THRESHOLD mallopt(M_MMAP_THRESHOLD, 4096); #endif tls_init(); /* initialize thread-local storage */ retval=main_unix(argc, argv); main_cleanup(); return retval; }
int main (int argc, char *argv[]) { int thresh, rc; if (argc > 1) { thresh = atoi (argv[1]); rc = mallopt (M_MMAP_THRESHOLD, thresh); fprintf (stderr, "rc=%d from requesting mmap" " for more than thresh=%d bytes\n", rc, thresh); } doit (13); doit (13 * 1024); doit (13 * 1024 * 1024); exit (0); }
void TestMallocPrivate::init() { /* When using glibc malloc, this function will be called before any heap allocation. When using other malloc and when running under valgrind, we might get called after some heap allocation. */ struct mallinfo info = mallinfo(); static TestMallocPrivate testmalloc; testmalloc.now_usable.store(info.uordblks); testmalloc.now_overhead.store(0); /* cannot get this figure, but should be close to 0. */ TestMalloc::resetPeak(); testmalloc.selftest(); /* Turn off mmap so that all blocks have a fixed overhead. */ mallopt(M_MMAP_MAX, 0); #ifdef TEST_TESTMALLOC __after_morecore_hook = &TestMallocPrivate::afterMorecore; mallopt(M_TRIM_THRESHOLD, 0); mallopt(M_TOP_PAD, 0); #endif }
/*-------------------------------------------------------------------------* * PL_INIT_MACHINE * * * *-------------------------------------------------------------------------*/ void Pl_Init_Machine(void) { tzset(); start_user_time = Pl_M_User_Time(); start_system_time = Pl_M_System_Time(); start_real_time = Pl_M_Real_Time(); #if defined(HAVE_MALLOPT) && defined(M_MMAP_MAX) mallopt(M_MMAP_MAX, 0); #endif Pl_Init_Machine1(); }
int main() { int i; setvbuf(stdout, NULL, _IONBF, 0); printf("address of main(): %p\n", main); printf("address of stack : %p\n", &i); #if defined(__GLIBC__) mallopt(M_TRIM_THRESHOLD, -1); dl_iterate_phdr(dl_printer, NULL); #endif printf("\nProgram a.out output:\n"); printf("==========================\n"); try_mono(); printf("==========================\n\n"); return 0; }
int main(int argc, char* argv[]) { /* execution begins here 8-) */ int retval; #ifdef M_MMAP_THRESHOLD mallopt(M_MMAP_THRESHOLD, 4096); #endif str_init(); /* initialize per-thread string management */ retval=main_unix(argc, argv); unbind_ports(); s_poll_free(fds); fds=NULL; str_stats(); log_flush(LOG_MODE_ERROR); return retval; }
/// Depending on whether we are building the compiler for the browser or /// standalone, we will end up creating a Ice::BrowserCompileServer or /// Ice::CLCompileServer object. Method /// Ice::CompileServer::runAndReturnErrorCode is used for the invocation. /// There are no real commandline arguments in the browser case. They are /// supplied via IPC so argc, and argv are not used in that case. /// We can only compile the Ice::BrowserCompileServer object with the PNaCl /// compiler toolchain, when building Subzero as a sandboxed translator. int main(int argc, char **argv) { #ifdef __pnacl__ #define M_GRANULARITY (-2) // PNaCl's default malloc implementation grabs small chunks of memory with // mmap at a time, hence causing significant slowdowns. This call ensures that // mmap is used to allocate 16MB at a time, to amortize the system call cost. mallopt(M_GRANULARITY, 16 * 1024 * 1024); #undef M_GRANULARITY #endif // __pnacl__ if (Ice::BuildDefs::browser()) { assert(argc == 1); return Ice::BrowserCompileServer().runAndReturnErrorCode(); } return Ice::CLCompileServer(argc, argv).runAndReturnErrorCode(); }
int main() { int i; for(i=0;i<10;i++) { test_malloc(10+i,0); } mallopt(1002,0); test_malloc(3,1); test_malloc(3,2); test_malloc(2,0); return 0; }
void Application::setupMalloc() { #ifdef M_TRIM_THRESHOLD // Prevent fragmentation of the heap by malloc (glibc). // // The default threshold is 128*1024, which can result in a large memory usage // due to fragmentation especially if we use the raster graphicssystem. On the // otherside if the threshold is too low, free() starts to permanently ask the kernel // about shrinking the heap. #ifdef HAVE_UNISTD_H const int pagesize = sysconf(_SC_PAGESIZE); #else const int pagesize = 4*1024; #endif // HAVE_UNISTD_H mallopt(M_TRIM_THRESHOLD, 5*pagesize); #endif // M_TRIM_THRESHOLD }
CAMLprim value bigstring_init_stub(value __unused v_unit) { bigstring_exc_IOError = caml_named_value("Bigstring.IOError"); bigstring_exc_End_of_file = caml_named_value("Bigstring.End_of_file"); unix_error_exn = caml_named_value("Unix.Unix_error"); #ifdef __GLIBC__ /* GLIBC uses a threshold internally as a cutoff between brk and mmap. Sadly, it nowadays employs a heuristic that may change this value dynamically. The call to mallopt suppresses this behavior, which made it hard to prevent C-heap fragmentation (e.g. in the writer). */ mallopt(M_MMAP_THRESHOLD, 131072); #endif if (unix_error_exn == NULL) caml_invalid_argument( "Exception Unix.Unix_error not initialized, please link unix.cma"); return Val_unit; }