📄 info.c
字号:
__mp_log(h, v); __mp_warn(ET_MAXALN, v->type, v->file, v->line, NULL, a); __mp_diag("\n"); } a = h->alloc.heap.memory.page; } } else if ((v->type == AT_VALLOC) || (v->type == AT_PVALLOC)) { /* Check that the specified size and alignment for valloc() and * pvalloc() are valid. */ if (v->type == AT_PVALLOC) { if (l == 0) l = 1; l = __mp_roundup(l, h->alloc.heap.memory.page); } a = h->alloc.heap.memory.page; } if ((h->recur == 1) && (((h->limit > 0) && (h->alloc.asize + l > h->limit)) || ((h->ffreq > 0) && ((rand() % h->ffreq) == 0)))) errno = ENOMEM; else { if (!(h->flags & FLG_NOPROTECT)) __mp_protectinfo(h, MA_READWRITE); if ((((v->type != AT_ALLOCA) && (v->type != AT_STRDUPA) && (v->type != AT_STRNDUPA)) || (g = getallocanode(h))) && (m = getinfonode(h))) if (n = __mp_getalloc(&h->alloc, l, a, m)) {#if MP_THREADS_SUPPORT t = __mp_threadid();#else /* MP_THREADS_SUPPORT */ t = 0;#endif /* MP_THREADS_SUPPORT */ /* Fill in the details of the allocation information node. */ m->data.type = v->type; m->data.alloc = c; m->data.realloc = 0;#if MP_THREADS_SUPPORT m->data.thread = t;#endif /* MP_THREADS_SUPPORT */ m->data.event = h->event; m->data.func = v->func; m->data.file = v->file; m->data.line = v->line; m->data.stack = __mp_getaddrs(&h->addr, v->stack); m->data.typestr = v->typestr; m->data.typesize = v->typesize; m->data.userdata = NULL; if (h->recur > 1) m->data.flags = FLG_INTERNAL; else m->data.flags = 0; p = n->block; if ((v->type == AT_CALLOC) || (v->type == AT_XCALLOC) || (v->type == AT_RECALLOC)) __mp_memset(p, 0, l); else __mp_memset(p, h->alloc.abyte, l); if (h->recur == 1) { if (h->ltable.tracing) leaktabentry(h, m, l, 0); if (h->prof.profiling && __mp_profilealloc(&h->prof, n->size, m, !(h->flags & FLG_NOPROTECT))) m->data.flags |= FLG_PROFILED; if (h->trace.tracing) { __mp_tracealloc(&h->trace, c, p, l, t, v->func, v->file, v->line); m->data.flags |= FLG_TRACED; } }#if MP_INUSE_SUPPORT _Inuse_malloc(p, l);#endif /* MP_INUSE_SUPPORT */ } else __mp_freeslot(&h->table, m); if (((v->type == AT_ALLOCA) || (v->type == AT_STRDUPA) || (v->type == AT_STRNDUPA)) && (g != NULL)) if (p != NULL) { __mp_addhead(&h->astack, &g->node); g->block = p;#if MP_FULLSTACK /* If we support full stack tracebacks then we can more * accurately determine when we can free up any allocations * made by alloca(), strdupa() or strndupa() that are now out * of scope. */ g->data.frame = (void *) m->data.stack;#else /* MP_FULLSTACK */ /* Otherwise, we take the address of a local variable in the * calling function in order to determine if subsequent calls * are closer to or further away from the program's entry point. * This information can later be used to free up any * allocations made by alloca(), strdupa() or strndupa() that * are now out of scope. */ g->data.frame = (void *) &v->stack->frame;#endif /* MP_FULLSTACK */ } else __mp_freeslot(&h->atable, g); if ((h->recur == 1) && !(h->flags & FLG_NOPROTECT)) __mp_protectinfo(h, MA_READONLY); if (h->cpeak < h->alloc.atree.size) h->cpeak = h->alloc.atree.size; if (h->peak < h->alloc.asize) h->peak = h->alloc.asize; } if ((h->flags & FLG_LOGALLOCS) && (h->recur == 1)) __mp_diag("returns " MP_POINTER "\n\n", p); return p;}/* Resize an existing block of memory to a new size and alignment. */MP_GLOBALvoid *__mp_resizememory(infohead *h, void *p, size_t l, size_t a, loginfo *v){ allocnode *n, *r; infonode *i, *m; size_t d; unsigned long t; v->ltype = LT_REALLOC; v->variant.logrealloc.block = p; v->variant.logrealloc.size = l; v->variant.logrealloc.align = a; if (h->flags & FLG_LOGREALLOCS) __mp_log(h, v); if (p == NULL) { if (h->flags & FLG_CHECKREALLOCS) { __mp_log(h, v); __mp_warn(ET_RSZNUL, v->type, v->file, v->line, NULL); __mp_diag("\n"); } p = __mp_getmemory(h, l, a, v); } else if (n = __mp_findfreed(&h->alloc, p)) { /* This block of memory has already been freed but has not been * returned to the free tree. */ m = (infonode *) n->info; __mp_log(h, v); __mp_error(ET_PRVFRD, v->type, v->file, v->line, NULL, p, __mp_functionnames[m->data.type]); __mp_printalloc(&h->syms, n); __mp_diag("\n"); p = NULL; } else if (((n = __mp_findalloc(&h->alloc, p)) == NULL) || ((m = (infonode *) n->info) == NULL)) { /* We know nothing about this block of memory. */ __mp_log(h, v); __mp_error(ET_NOTALL, v->type, v->file, v->line, NULL, p); __mp_diag("\n"); p = NULL; } else if (p != n->block) { /* The address of the block passed in does not match the start * address of the block we know about. */ __mp_log(h, v); __mp_error(ET_MISMAT, v->type, v->file, v->line, NULL, p, n->block); __mp_printalloc(&h->syms, n); __mp_diag("\n"); p = NULL; } else if ((m->data.type == AT_ALLOCA) || (m->data.type == AT_STRDUPA) || (m->data.type == AT_STRNDUPA) || (m->data.type == AT_NEW) || (m->data.type == AT_NEWVEC)) { /* The function used to allocate the block is incompatible with * alloca(), strdupa(), strndupa(), operator new or operator new[]. */ __mp_log(h, v); __mp_error(ET_INCOMP, v->type, v->file, v->line, NULL, p, __mp_functionnames[m->data.type]); __mp_printalloc(&h->syms, n); __mp_diag("\n"); p = NULL; } else if (l == 0) { if (h->flags & FLG_CHECKREALLOCS) { __mp_log(h, v); __mp_warn(ET_RSZZER, v->type, v->file, v->line, NULL); __mp_diag("\n"); } __mp_freememory(h, p, v); p = NULL; } else { if ((h->flags & FLG_LOGREALLOCS) && (h->recur == 1)) { __mp_printalloc(&h->syms, n); __mp_diag("\n"); } if ((m->data.realloc + 1 == h->rstop) && ((h->astop == 0) || (m->data.alloc == h->astop))) { /* Abort at the specified reallocation index. */ __mp_printsummary(h); __mp_diag("\n"); if (h->astop == 0) __mp_diag("stopping at reallocation %lu\n", h->rstop); else __mp_diag("stopping at reallocation %lu of allocation %lu\n", h->rstop, h->astop); __mp_trap(); } if ((h->recur == 1) && (((h->limit > 0) && (l > n->size) && (h->alloc.asize + l - n->size > h->limit)) || ((h->ffreq > 0) && ((rand() % h->ffreq) == 0)))) { errno = ENOMEM; p = NULL; } else {#if MP_THREADS_SUPPORT t = __mp_threadid();#else /* MP_THREADS_SUPPORT */ t = 0;#endif /* MP_THREADS_SUPPORT */ d = n->size; if (!(h->flags & FLG_NOPROTECT)) __mp_protectinfo(h, MA_READWRITE); m->data.realloc++; if ((v->type != AT_EXPAND) && (h->alloc.flags & FLG_NOFREE)) /* We are not going to even attempt to resize the memory if * we are preserving free blocks, and instead we will just * create a new block all the time and preserve the old block. */ if ((i = getinfonode(h)) && (r = __mp_getalloc(&h->alloc, l, a, m))) { /* Fill in the details of the allocation information node. */ i->data.type = v->type; i->data.alloc = m->data.alloc; i->data.realloc = m->data.realloc - 1;#if MP_THREADS_SUPPORT i->data.thread = t;#endif /* MP_THREADS_SUPPORT */ i->data.event = h->event; i->data.func = v->func; i->data.file = v->file; i->data.line = v->line; i->data.stack = __mp_getaddrs(&h->addr, v->stack); i->data.typestr = m->data.typestr; i->data.typesize = m->data.typesize; i->data.userdata = m->data.userdata; i->data.flags = m->data.flags | FLG_FREED; __mp_memcopy(r->block, n->block, (l > d) ? d : l); if (m->data.flags & FLG_TRACED) __mp_tracerealloc(&h->trace, m->data.alloc, r->block, l, t, v->func, v->file, v->line);#if MP_INUSE_SUPPORT _Inuse_realloc(n->block, r->block, l);#endif /* MP_INUSE_SUPPORT */ __mp_freealloc(&h->alloc, n, i); p = r->block; } else { if (i != NULL) __mp_freeslot(&h->table, i); p = NULL; } else if (l == d) /* The old size is the same as the new size, so we just * return an address to the start of the existing block. */ p = n->block; else if (!__mp_resizealloc(&h->alloc, n, l)) /* If __mp_resizealloc() failed and all allocations are to * be aligned to the end of pages or the size requested is * greater than the existing size then we must allocate a * new block, copy the contents and free the old block. */ if ((v->type != AT_EXPAND) && (((h->alloc.flags & FLG_PAGEALLOC) && (h->alloc.flags & FLG_ALLOCUPPER)) || (l > d)) && (r = __mp_getalloc(&h->alloc, l, a, m))) { __mp_memcopy(r->block, n->block, (l > d) ? d : l); if (m->data.flags & FLG_TRACED) __mp_tracerealloc(&h->trace, m->data.alloc, r->block, l, t, v->func, v->file, v->line);#if MP_INUSE_SUPPORT _Inuse_realloc(n->block, r->block, l);#endif /* MP_INUSE_SUPPORT */ __mp_freealloc(&h->alloc, n, NULL); p = r->block; } else p = NULL; else { /* We have been able to increase or decrease the size of the * block without having to relocate it. */ if (m->data.flags & FLG_TRACED) __mp_tracerealloc(&h->trace, m->data.alloc, n->block, l, t, v->func, v->file, v->line);#if MP_INUSE_SUPPORT _Inuse_realloc(n->block, n->block, l);#endif /* MP_INUSE_SUPPORT */ } if (p != NULL) { if (m->data.flags & FLG_MARKED) { h->mtotal -= d; h->mtotal += l; } if (h->ltable.tracing) leaktabentry(h, m, d, 1); if (m->data.flags & FLG_PROFILED) __mp_profilefree(&h->prof, d, m, !(h->flags & FLG_NOPROTECT)); m->data.type = v->type;#if MP_THREADS_SUPPORT m->data.thread = t;#endif /* MP_THREADS_SUPPORT */ m->data.event = h->event; m->data.func = v->func; m->data.file = v->file; m->data.line = v->line; __mp_freeaddrs(&h->addr, m->data.stack); m->data.stack = __mp_getaddrs(&h->addr, v->stack); m->data.typestr = v->typestr; m->data.typesize = v->typesize; if (h->ltable.tracing) leaktabentry(h, m, l, 0); if (m->data.flags & FLG_PROFILED) __mp_profilealloc(&h->prof, l, m, !(h->flags & FLG_NOPROTECT)); } if ((h->recur == 1) && !(h->flags & FLG_NOPROTECT)) __mp_protectinfo(h, MA_READONLY); if ((p != NULL) && (l > d)) if (v->type == AT_RECALLOC) __mp_memset((char *) p + d, 0, l - d); else __mp_memset((char *) p + d, h->alloc.abyte, l - d); if (h->cpeak < h->alloc.atree.size)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -