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); }
static void* malloc_hook_ini(size_t sz, const __malloc_ptr_t caller) { __malloc_hook = NULL; ptmalloc_init(); return __libc_malloc(sz); }
static void* memalign_hook_ini(size_t alignment, size_t sz, const void * caller) { __memalign_hook = NULL; ptmalloc_init(); return public_mEMALIGn(alignment, sz); }
static void* malloc_hook_ini(size_t sz, const void * caller) { __malloc_hook = NULL; ptmalloc_init(); return public_mALLOc(sz); }
static void* realloc_hook_ini(void* ptr, size_t sz, const __malloc_ptr_t caller) { __malloc_hook = NULL; __realloc_hook = NULL; ptmalloc_init(); return __libc_realloc(ptr, sz); }
static void * realloc_hook_ini(void *ptr, size_t sz, const void * caller) { __malloc_hook = NULL; __realloc_hook = NULL; ptmalloc_init(); return public_rEALLOc(ptr, sz); }
/* * This Function is called before main. */ void ManRiX_init(char *user_data) { struct pthread_internal pint; pint.pt_fork.prepare = NULL; pint.pt_fork.parent = NULL; pint.pt_fork.child = NULL; // pint.tid = pthread_self(); /* pthread structure is after argc, argv pointer and envp pointer */ memcpy((user_data + 12), (char *)&pint, sizeof(struct pthread_internal)); /* Environment pointer */ environ = NULL;//(char **)(*((unsigned long *)user_data + 2)); ptmalloc_init(); /* ptmalloc initialization */ }
void public_mSTATs(void) { int i; struct malloc_arena* ar_ptr; /*unsigned long in_use_b, system_b, avail_b;*/ #if THREAD_STATS long stat_lock_direct = 0, stat_lock_loop = 0, stat_lock_wait = 0; #endif if(__malloc_initialized < 0) ptmalloc_init (); for (i=0, ar_ptr = &main_arena;; ++i) { struct malloc_state* msp = arena_to_mspace(ar_ptr); fprintf(stderr, "Arena %d:\n", i); mspace_malloc_stats(msp); #if THREAD_STATS stat_lock_direct += ar_ptr->stat_lock_direct; stat_lock_loop += ar_ptr->stat_lock_loop; stat_lock_wait += ar_ptr->stat_lock_wait; #endif if (MALLOC_DEBUG > 1) { struct malloc_segment* mseg = &msp->seg; while (mseg) { fprintf(stderr, " seg %08lx-%08lx\n", (unsigned long)mseg->base, (unsigned long)(mseg->base + mseg->size)); mseg = mseg->next; } } ar_ptr = ar_ptr->next; if (ar_ptr == &main_arena) break; } #if THREAD_STATS fprintf(stderr, "locked directly = %10ld\n", stat_lock_direct); fprintf(stderr, "locked in loop = %10ld\n", stat_lock_loop); fprintf(stderr, "locked waiting = %10ld\n", stat_lock_wait); fprintf(stderr, "locked total = %10ld\n", stat_lock_direct + stat_lock_loop + stat_lock_wait); if (main_arena.stat_starter > 0) fprintf(stderr, "starter hooks = %10ld\n", main_arena.stat_starter); #endif }
void* public_vALLOc(size_t bytes) { struct malloc_arena* ar_ptr; void *p; if(__malloc_initialized < 0) ptmalloc_init (); arena_get(ar_ptr, bytes + FOOTER_OVERHEAD + MIN_CHUNK_SIZE); if(!ar_ptr) return 0; if (ar_ptr != &main_arena) bytes += FOOTER_OVERHEAD; p = mspace_memalign(arena_to_mspace(ar_ptr), 4096, bytes); if (p && ar_ptr != &main_arena) set_non_main_arena(p, ar_ptr); (void)mutex_unlock(&ar_ptr->mutex); return p; }
int main(int argc, char *argv[]) { int i, bins; int n_thr=N_THREADS; int i_max=I_MAX; unsigned long size=SIZE; struct thread_st *st; #if USE_MALLOC && USE_STARTER==2 ptmalloc_init(); printf("ptmalloc_init\n"); #endif if(argc > 1) n_total_max = atoi(argv[1]); if(n_total_max < 1) n_thr = 1; if(argc > 2) n_thr = atoi(argv[2]); if(n_thr < 1) n_thr = 1; if(n_thr > 100) n_thr = 100; if(argc > 3) i_max = atoi(argv[3]); if(argc > 4) size = atol(argv[4]); if(size < 2) size = 2; bins = MEMORY/(size*n_thr); if(argc > 5) bins = atoi(argv[5]); if(bins < 4) bins = 4; /*protect_stack(n_thr);*/ thread_init(); printf("total=%d threads=%d i_max=%d size=%ld bins=%d\n", n_total_max, n_thr, i_max, size, bins); st = (struct thread_st *)malloc(n_thr*sizeof(*st)); if(!st) exit(-1); #if !defined NO_THREADS && (defined __sun__ || defined sun) /* I know of no other way to achieve proper concurrency with Solaris. */ thr_setconcurrency(n_thr); #endif /* Start all n_thr threads. */ for(i=0; i<n_thr; i++) { st[i].u.bins = bins; st[i].u.max = i_max; st[i].u.size = size; st[i].u.seed = ((long)i_max*size + i) ^ bins; st[i].sp = 0; st[i].func = malloc_test; if(thread_create(&st[i])) { printf("Creating thread #%d failed.\n", i); n_thr = i; break; } printf("Created thread %lx.\n", (long)st[i].id); } /* Start an extra thread so we don't run out of stacks. */ if(0) { struct thread_st lst; lst.u.bins = 10; lst.u.max = 20; lst.u.size = 8000; lst.u.seed = 8999; lst.sp = 0; lst.func = malloc_test; if(thread_create(&lst)) { printf("Creating thread #%d failed.\n", i); } else { wait_for_thread(&lst, 1, NULL); } } for(n_running=n_total=n_thr; n_running>0;) { wait_for_thread(st, n_thr, my_end_thread); } for(i=0; i<n_thr; i++) { free(st[i].sp); } free(st); #if USE_MALLOC malloc_stats(); #endif printf("Done.\n"); return 0; }
int main(int argc, char *argv[]) { int i, j, bins; int n_thr=N_THREADS; int i_max=I_MAX; unsigned long size=SIZE; struct thread_st *st; #if USE_MALLOC && USE_STARTER==2 ptmalloc_init(); printf("ptmalloc_init\n"); #endif if(argc > 1) n_total_max = atoi(argv[1]); if(n_total_max < 1) n_thr = 1; if(argc > 2) n_thr = atoi(argv[2]); if(n_thr < 1) n_thr = 1; if(n_thr > 100) n_thr = 100; if(argc > 3) i_max = atoi(argv[3]); if(argc > 4) size = atol(argv[4]); if(size < 2) size = 2; bins = MEMORY/size; if(argc > 5) bins = atoi(argv[5]); if(bins < BINS_PER_BLOCK) bins = BINS_PER_BLOCK; n_blocks = bins/BINS_PER_BLOCK; blocks = (struct block *)malloc(n_blocks*sizeof(*blocks)); if(!blocks) exit(1); thread_init(); printf("total=%d threads=%d i_max=%d size=%ld bins=%d\n", n_total_max, n_thr, i_max, size, n_blocks*BINS_PER_BLOCK); for(i=0; i<n_blocks; i++) { mutex_init(&blocks[i].mutex); for(j=0; j<BINS_PER_BLOCK; j++) blocks[i].b[j].size = 0; } st = (struct thread_st *)malloc(n_thr*sizeof(*st)); if(!st) exit(-1); #if !defined NO_THREADS && (defined __sun__ || defined sun) /* I know of no other way to achieve proper concurrency with Solaris. */ thr_setconcurrency(n_thr); #endif /* Start all n_thr threads. */ for(i=0; i<n_thr; i++) { st[i].u.max = i_max; st[i].u.size = size; st[i].u.seed = ((long)i_max*size + i) ^ n_blocks; st[i].sp = 0; st[i].func = malloc_test; if(thread_create(&st[i])) { printf("Creating thread #%d failed.\n", i); n_thr = i; break; } printf("Created thread %lx.\n", (long)st[i].id); } for(n_running=n_total=n_thr; n_running>0;) { wait_for_thread(st, n_thr, my_end_thread); } for(i=0; i<n_blocks; i++) { for(j=0; j<BINS_PER_BLOCK; j++) bin_free(&blocks[i].b[j]); } for(i=0; i<n_thr; i++) { free(st[i].sp); } free(st); free(blocks); #if USE_MALLOC malloc_stats(); #endif printf("Done.\n"); return 0; }
int __malloc_set_state(void* msptr) { struct malloc_save_state* ms = (struct malloc_save_state*)msptr; size_t i; mbinptr b; disallow_malloc_check = 1; ptmalloc_init(); if(ms->magic != MALLOC_STATE_MAGIC) return -1; /* Must fail if the major version is too high. */ if((ms->version & ~0xffl) > (MALLOC_STATE_VERSION & ~0xffl)) return -2; (void)mutex_lock(&main_arena.mutex); /* There are no fastchunks. */ clear_fastchunks(&main_arena); if (ms->version >= 4) set_max_fast(ms->max_fast); else set_max_fast(64); /* 64 used to be the value we always used. */ for (i=0; i<NFASTBINS; ++i) fastbin (&main_arena, i) = 0; for (i=0; i<BINMAPSIZE; ++i) main_arena.binmap[i] = 0; top(&main_arena) = ms->av[2]; main_arena.last_remainder = 0; for(i=1; i<NBINS; i++) { b = bin_at(&main_arena, i); if(ms->av[2*i+2] == 0) { assert(ms->av[2*i+3] == 0); first(b) = last(b) = b; } else { if(ms->version >= 3 && (i<NSMALLBINS || (largebin_index(chunksize(ms->av[2*i+2]))==i && largebin_index(chunksize(ms->av[2*i+3]))==i))) { first(b) = ms->av[2*i+2]; last(b) = ms->av[2*i+3]; /* Make sure the links to the bins within the heap are correct. */ first(b)->bk = b; last(b)->fd = b; /* Set bit in binblocks. */ mark_bin(&main_arena, i); } else { /* Oops, index computation from chunksize must have changed. Link the whole list into unsorted_chunks. */ first(b) = last(b) = b; b = unsorted_chunks(&main_arena); ms->av[2*i+2]->bk = b; ms->av[2*i+3]->fd = b->fd; b->fd->bk = ms->av[2*i+3]; b->fd = ms->av[2*i+2]; } } } if (ms->version < 3) { /* Clear fd_nextsize and bk_nextsize fields. */ b = unsorted_chunks(&main_arena)->fd; while (b != unsorted_chunks(&main_arena)) { if (!in_smallbin_range(chunksize(b))) { b->fd_nextsize = NULL; b->bk_nextsize = NULL; } b = b->fd; } } mp_.sbrk_base = ms->sbrk_base; main_arena.system_mem = ms->sbrked_mem_bytes; mp_.trim_threshold = ms->trim_threshold; mp_.top_pad = ms->top_pad; mp_.n_mmaps_max = ms->n_mmaps_max; mp_.mmap_threshold = ms->mmap_threshold; check_action = ms->check_action; main_arena.max_system_mem = ms->max_sbrked_mem; mp_.n_mmaps = ms->n_mmaps; mp_.max_n_mmaps = ms->max_n_mmaps; mp_.mmapped_mem = ms->mmapped_mem; mp_.max_mmapped_mem = ms->max_mmapped_mem; /* add version-dependent code here */ if (ms->version >= 1) { /* Check whether it is safe to enable malloc checking, or whether it is necessary to disable it. */ if (ms->using_malloc_checking && !using_malloc_checking && !disallow_malloc_check) __malloc_check_init (); else if (!ms->using_malloc_checking && using_malloc_checking) { __malloc_hook = NULL; __free_hook = NULL; __realloc_hook = NULL; __memalign_hook = NULL; using_malloc_checking = 0; } } if (ms->version >= 4) { #ifdef PER_THREAD mp_.arena_test = ms->arena_test; mp_.arena_max = ms->arena_max; narenas = ms->narenas; #endif } check_malloc_state(&main_arena); (void)mutex_unlock(&main_arena.mutex); return 0; }
void public_mSTATs() { int i; mstate ar_ptr; struct malloc_global_info mgi; struct malloc_arena_info mai; unsigned long in_use_b, system_b, avail_b; #if defined(THREAD_STATS) && THREAD_STATS long stat_lock_direct = 0, stat_lock_loop = 0, stat_lock_wait = 0; #endif #if 0 if(__malloc_initialized < 0) ptmalloc_init (); #endif _int_get_global_info(&mgi); system_b = in_use_b = mgi.mmapped_mem; #ifdef _LIBC _IO_flockfile (stderr); int old_flags2 = ((_IO_FILE *) stderr)->_flags2; ((_IO_FILE *) stderr)->_flags2 |= _IO_FLAGS2_NOTCANCEL; #endif for (i=0; (ar_ptr = _int_get_arena(i)); i++) { _int_get_arena_info(ar_ptr, &mai); avail_b = mai.fastavail + mai.binavail + mai.top_size; fprintf(stderr, "Arena %d:\n", i); fprintf(stderr, "system bytes = %10lu\n", (unsigned long)mai.system_mem); fprintf(stderr, "in use bytes = %10lu\n", (unsigned long)(mai.system_mem - avail_b)); #if MALLOC_DEBUG > 1 if (i > 0) dump_heap(heap_for_ptr(top(ar_ptr))); #endif system_b += mai.system_mem; in_use_b += mai.system_mem - avail_b; #if defined(THREAD_STATS) && THREAD_STATS stat_lock_direct += mai.stat_lock_direct; stat_lock_loop += mai.stat_lock_loop; stat_lock_wait += mai.stat_lock_wait; #endif } #if HAVE_MMAP fprintf(stderr, "Total (incl. mmap):\n"); #else fprintf(stderr, "Total:\n"); #endif fprintf(stderr, "system bytes = %10lu\n", system_b); fprintf(stderr, "in use bytes = %10lu\n", in_use_b); #ifdef NO_THREADS fprintf(stderr, "max system bytes = %10lu\n", (unsigned long)mgi.max_total_mem); #endif #if HAVE_MMAP fprintf(stderr, "max mmap regions = %10u\n", (unsigned int)mgi.max_n_mmaps); fprintf(stderr, "max mmap bytes = %10lu\n", (unsigned long)mgi.max_mmapped_mem); #endif #if defined(THREAD_STATS) && THREAD_STATS fprintf(stderr, "heaps created = %10d\n", mgi.stat_n_heaps); fprintf(stderr, "locked directly = %10ld\n", stat_lock_direct); fprintf(stderr, "locked in loop = %10ld\n", stat_lock_loop); fprintf(stderr, "locked waiting = %10ld\n", stat_lock_wait); fprintf(stderr, "locked total = %10ld\n", stat_lock_direct + stat_lock_loop + stat_lock_wait); #endif #ifdef _LIBC ((_IO_FILE *) stderr)->_flags2 |= old_flags2; _IO_funlockfile (stderr); #endif }