📄 inter.c
字号:
__mp_init(void){ size_t i; savesignals(); if (memhead.fini) /* We currently don't allow the library to be reinitialised after it * has been terminated. To reinitialise the library before it has been * terminated use __mp_reinit(). */ __mp_abort(); if (!memhead.init) { __mp_newinfo(&memhead);#if (TARGET == TARGET_AMIGA && defined(__GNUC__)) /* Initialise the simulated UNIX heap. This gets done anyway, but it * forces a reference to the sbrk module which is necessary since * we need it for libiberty, which gets linked after libmpatrol. */ sbrk(0);#endif /* TARGET && __GNUC__ */#if MP_INUSE_SUPPORT _Inuse_init(0, 0);#endif /* MP_INUSE_SUPPORT */#if MP_USE_ATEXIT /* We will have to terminate the library using atexit() since either * this has been explicitly specified or there is no support for * .init/.fini functions, constructor/destructor functions or C++. * This is usually OK to do but there may be problems if the mpatrol * library is terminated before all memory allocations are freed. */ atexit(__mp_fini);#endif /* MP_USE_ATEXIT */ /* Read any options from the specified environment variable. */ __mp_parseoptions(&memhead); /* Set up the random number generator for the FAILFREQ option. */ if (memhead.fseed == 0) memhead.fseed = (unsigned long) time(NULL); srand((unsigned int) memhead.fseed); /* Attempt to open the log file. */ if (!__mp_openlogfile(memhead.log)) memhead.log = NULL; /* Output the header and version information. */ __mp_printversion(); /* Attempt to read symbols from any shared libraries and possibly * determine the program name if it has not already been set. */ __mp_addextsymbols(&memhead.syms, &memhead.alloc.heap.memory); /* Obtain the program filename and attempt to read any symbols from * that file. */ if (memhead.alloc.heap.memory.prog != NULL) __mp_addsymbols(&memhead.syms, memhead.alloc.heap.memory.prog, NULL, 0); __mp_fixsymbols(&memhead.syms); if (__mp_diagflags & FLG_HTML) __mp_diagtag("<HR>"); __mp_diag("\n"); if (!(memhead.flags & FLG_NOPROTECT)) { __mp_protectstrtab(&memhead.syms.strings, MA_READONLY); __mp_protectsymbols(&memhead.syms, MA_READONLY); __mp_protectinfo(&memhead, MA_READONLY); } /* Start the leak table if necessary. */ if (memhead.flags & FLG_LEAKTABLE) memhead.ltable.tracing = 1; /* Finally, call any initialisation functions in the order in which * they were registered. */ for (i = 0; i < memhead.initcount; i++) { memhead.inits[i](); memhead.inits[i] = NULL; } memhead.initcount = 0; } restoresignals();}/* Reinitialise the mpatrol library. */MP_APIvoid__mp_reinit(void){ char l[256]; allocnode *n; treenode *t; unsigned long i; savesignals(); if (!memhead.init) __mp_init(); else if (((memhead.recur == 1) || (memhead.flags & FLG_CHECKFORK)) && ((i = __mp_processid()) != memhead.pid)) { memhead.pid = i; if (!(memhead.flags & FLG_NOPROTECT)) __mp_protectinfo(&memhead, MA_READWRITE); if (memhead.log == NULL) strcpy(l, "stderr"); else strcpy(l, memhead.log); memhead.log = __mp_logfile(&memhead.alloc.heap.memory, "%n.%p.log"); __mp_diag("Log file split to %s\n\n", memhead.log); __mp_closelogfile(); /* Attempt to open the log file. */ if (!__mp_openlogfile(memhead.log)) memhead.log = NULL; /* Output the header and version information. */ __mp_printversion(); __mp_diag("Log file continued from %s\n\n", l); /* Write out any profiling information to the profiling output file. */ if (memhead.prof.autocount > 0) __mp_writeprofile(&memhead.prof, !(memhead.flags & FLG_NOPROTECT)); memhead.prof.file = __mp_proffile(&memhead.alloc.heap.memory, "%n.%p.out"); /* Remove the traced flag from any existing memory allocations and then * change the tracing output file. */ for (t = __mp_minimum(memhead.alloc.atree.root); t != NULL; t = __mp_successor(t)) { n = (allocnode *) ((char *) t - offsetof(allocnode, tnode)); ((infonode *) n->info)->data.flags &= ~FLG_TRACED; } __mp_changetrace(&memhead.trace, __mp_tracefile(&memhead.alloc.heap.memory, "%n.%p.trace"), 0); if ((memhead.recur == 1) && !(memhead.flags & FLG_NOPROTECT)) __mp_protectinfo(&memhead, MA_READONLY); } restoresignals();}/* Finalise the mpatrol library. */MP_APIvoid__mp_fini(void){ stackinfo i; loginfo v; savesignals(); if (memhead.init) { if (__mp_processid() != memhead.pid) __mp_reinit(); if (!memhead.fini) { /* First, determine the call stack details in case we need to * free any remaining allocations that were made by alloca(). */ __mp_newframe(&i, NULL); if (__mp_getframe(&i)) __mp_getframe(&i); v.ltype = LT_MAX; v.type = AT_MAX; v.func = NULL; v.file = NULL; v.line = 0; v.stack = &i; v.typestr = NULL; v.typesize = 0; v.logged = 0; __mp_checkinfo(&memhead, &v); checkalloca(&v, 1); /* Then call any finalisation functions in the reverse order in * which they were registered. */ while (memhead.finicount > 0) { memhead.finis[--memhead.finicount](); memhead.finis[memhead.finicount] = NULL; } /* Then close any access library handles that might still be open. */ __mp_closesymbols(&memhead.syms); /* Then print a summary of library statistics and settings. */ __mp_printsummary(&memhead); /* Display the leak table if necessary. */ if (memhead.flags & FLG_LEAKTABLE) { if (!(memhead.flags & FLG_NOPROTECT)) __mp_protectinfo(&memhead, MA_READWRITE); __mp_diag("\n"); __mp_printleaktab(&memhead, 0, 2, 0); if (!(memhead.flags & FLG_NOPROTECT)) __mp_protectinfo(&memhead, MA_READONLY); } /* Then deal with any SHOW options that may have been requested. */ if ((memhead.flags & FLG_SHOWMAP) && (memhead.alloc.list.size > 0)) { __mp_diag("\n"); __mp_printmap(&memhead); } if ((memhead.flags & FLG_SHOWSYMBOLS) && (memhead.syms.dtree.size > 0)) __mp_printsymbols(&memhead.syms); if ((memhead.flags & FLG_SHOWFREE) && (memhead.alloc.fsize > 0)) __mp_printfree(&memhead); if ((memhead.flags & FLG_SHOWFREED) && (memhead.alloc.gsize > 0)) __mp_printfreed(&memhead); if ((memhead.alloc.asize - memhead.mtotal) > 0) { if (memhead.flags & FLG_SHOWUNFREED) __mp_printallocs(&memhead, 0); if ((memhead.uabort > 0) && ((memhead.alloc.asize - memhead.mtotal) >= memhead.uabort)) __mp_printallocs(&memhead, 1); } /* Deal with anything that needs to be done with marked allocations. */ finishmarked(); /* Next, close the tracing output file. This will do nothing if * tracing has not been enabled. */ __mp_endtrace(&memhead.trace); /* Now write out any profiling information to the profiling output * file. */ if (memhead.prof.autocount > 0) __mp_writeprofile(&memhead.prof, !(memhead.flags & FLG_NOPROTECT)); /* Finally, attempt to close the log file. */ __mp_closelogfile(); memhead.fini = 1;#if MP_INUSE_SUPPORT _Inuse_close();#endif /* MP_INUSE_SUPPORT */ }#if MP_DELETEHEAP /* We only need to perform this step if the operating system does not * reclaim memory from a terminated process. We must not perform this * step if the operating system needs to deal with dynamically * allocated memory after the library has terminated. */ if (!(memhead.flags & FLG_NOPROTECT)) { __mp_protectinfo(&memhead, MA_READWRITE); __mp_protectsymbols(&memhead.syms, MA_READWRITE); __mp_protectstrtab(&memhead.syms.strings, MA_READWRITE); } __mp_deleteinfo(&memhead);#if MP_THREADS_SUPPORT __mp_finimutexes();#endif /* MP_THREADS_SUPPORT */#endif /* MP_DELETEHEAP */ memhead.init = 0; } restoresignals();}/* Provide a function which can be used as a breakpoint target in a debugger. */MP_APIvoid__mp_trap(void){}/* Register a finalisation function to be called when __mp_fini() is called. */MP_APIint__mp_atexit(void (*f)(void)){ int r; savesignals(); if (!memhead.init) __mp_init(); if (__mp_processid() != memhead.pid) __mp_reinit(); if (memhead.finicount == MP_MAXFINIS) r = 0; else { memhead.finis[memhead.finicount++] = f; r = 1; } restoresignals(); return r;}/* Set an mpatrol option after the library has been initialised. */MP_APIunsigned long__mp_setoption(long o, unsigned long v){ unsigned long r; savesignals(); if (!memhead.init) __mp_init(); if (__mp_processid() != memhead.pid) __mp_reinit(); if (o > 0) r = 1; else { o = -o; if (!(memhead.flags & FLG_NOPROTECT)) __mp_protectinfo(&memhead, MA_READWRITE); if ((r = __mp_set(&memhead, (unsigned long) o, v)) && (o != OPT_SETFLAGS) && (o != OPT_UNSETFLAGS)) r = 1; if ((memhead.recur == 1) && !(memhead.flags & FLG_NOPROTECT)) __mp_protectinfo(&memhead, MA_READONLY); } restoresignals(); return r;}/* Get an mpatrol option after the library has been initialised. */MP_APIint__mp_getoption(long o, unsigned long *v){ int r; savesignals(); if (!memhead.init) __mp_init(); if (__mp_processid() != memhead.pid) __mp_reinit(); if (o > 0) r = 0; else { o = -o; r = __mp_get(&memhead, (unsigned long) o, v); } restoresignals(); return r;}/* Return the memory header structure. */MP_APIinfohead *__mp_memhead(void){ return &memhead;}/* Allocate a new block of memory of a specified size and alignment. */MP_APIvoid *__mp_alloc(size_t l, size_t a, alloctype f, char *s, char *t, unsigned long u, char *g, size_t h, size_t k){ void *p; stackinfo i; loginfo v; int j, z;#if TARGET == TARGET_UNIX || TARGET == TARGET_WINDOWS /* If the C run-time library has not finished initialising then we must * allocate new memory with sbrk(). We don't even attempt to do anything * with calls to memalign(), valloc() and pvalloc() but these shouldn't * be coming through anyway. */ if (!crt_initialised()) { if (l == 0) l = 1; if ((p = sbrk(l)) == (void *) -1) if ((f == AT_ALLOCA) || (f == AT_XMALLOC) || (f == AT_XCALLOC)) abort(); else p = NULL; else if ((f == AT_CALLOC) || (f == AT_XCALLOC)) __mp_memset(p, 0, l); return p; }#endif /* TARGET */ savesignals(); if (!memhead.init) __mp_init(); if (__mp_processid() != memhead.pid) __mp_reinit(); /* Determine the call stack details. */ __mp_newframe(&i, NULL); if (__mp_getframe(&i)) { j = __mp_getframe(&i); while ((k > 0) && (j != 0)) { j = __mp_getframe(&i); k--; } } /* If no filename was passed through then attempt to read any debugging * information to determine the source location of the call. */ if ((memhead.recur == 1) && (t == NULL) && (i.addr != NULL) && __mp_findsource(&memhead.syms, (char *) i.addr - 1, &s, &t, &u)) { if (!(memhead.flags & FLG_NOPROTECT)) __mp_protectstrtab(&memhead.syms.strings, MA_READWRITE); if (s != NULL) s = __mp_addstring(&memhead.syms.strings, s); if (t != NULL) t = __mp_addstring(&memhead.syms.strings, t); if (!(memhead.flags & FLG_NOPROTECT)) __mp_protectstrtab(&memhead.syms.strings, MA_READONLY); } if (memhead.prologue && (memhead.recur == 1)) memhead.prologue((void *) -1, l, __mp_fixalign(&memhead, f, a), s, t, u, i.addr); v.ltype = LT_MAX; v.type = f; v.func = s; v.file = t; v.line = u; v.stack = &i; v.typestr = g; v.typesize = h; v.logged = 0; checkheap(&v, memhead.count + 1); checkalloca(&v, 0); memhead.event++; if (((f == AT_XMALLOC) || (f == AT_XCALLOC)) && (l == 0) && (h != 0)) l = h; z = 0; retry: p = __mp_getmemory(&memhead, l, a, &v);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -