📄 util.c
字号:
#endif } } } } ptr += XMALLOC_LEAK_ALIGN; }}voidxmalloc_find_leaks(void){ int B, I; int leak_sum = 0; extern void _etext; fprintf(stderr, "----- Memory map ----\n"); xmalloc_scan_region(&_etext, (void *) sbrk(0) - (void *) &_etext, 0); for (B = 0; B < DBG_ARRY_BKTS; B++) { for (I = 0; I < DBG_ARRY_SZ; I++) { if (malloc_ptrs[B][I] && malloc_refs[B][I] == 0) { /* Found a leak... */ fprintf(stderr, "Leak found: %p", malloc_ptrs[B][I]); fprintf(stderr, " %s", malloc_file[B][I]); fprintf(stderr, ":%d", malloc_line[B][I]); fprintf(stderr, " size %d", malloc_size[B][I]); fprintf(stderr, " allocation %d\n", malloc_count[B][I]); leak_sum += malloc_size[B][I]; } } } if (leak_sum) { fprintf(stderr, "Total leaked memory: %d\n", leak_sum); } else { fprintf(stderr, "No memory leaks detected\n"); } fprintf(stderr, "----------------------\n");}#endif /* XMALLOC_TRACE *//* * xmalloc() - same as malloc(3). Used for portability. * Never returns NULL; fatal on error. */void *xmalloc(size_t sz){ void *p; if (sz < 1) sz = 1; if ((p = malloc(sz)) == NULL) { if (failure_notify) { snprintf(msg, 128, "xmalloc: Unable to allocate %d bytes!\n", (int) sz); (*failure_notify) (msg); } else { perror("malloc"); } exit(1); }#if XMALLOC_DEBUG check_malloc(p, sz);#endif#if XMALLOC_STATISTICS malloc_stat(sz);#endif#if XMALLOC_TRACE xmalloc_show_trace(p, 1);#endif#if MEM_GEN_TRACE if (tracefp) fprintf(tracefp, "m:%d:%p\n", sz, p);#endif return (p);}/* * xfree() - same as free(3). Will not call free(3) if s == NULL. */voidxfree(void *s){#if XMALLOC_TRACE xmalloc_show_trace(s, -1);#endif#if XMALLOC_DEBUG check_free(s);#endif if (s != NULL) free(s);#if MEM_GEN_TRACE if (tracefp && s) fprintf(tracefp, "f:%p\n", s);#endif}/* xxfree() - like xfree(), but we already know s != NULL */voidxxfree(void *s){#if XMALLOC_TRACE xmalloc_show_trace(s, -1);#endif#if XMALLOC_DEBUG check_free(s);#endif free(s);#if MEM_GEN_TRACE if (tracefp && s) fprintf(tracefp, "f:%p\n", s);#endif}/* * xrealloc() - same as realloc(3). Used for portability. * Never returns NULL; fatal on error. */void *xrealloc(void *s, size_t sz){ void *p;#if XMALLOC_TRACE xmalloc_show_trace(s, -1);#endif if (sz < 1) sz = 1;#if XMALLOC_DEBUG if (s != NULL) check_free(s);#endif if ((p = realloc(s, sz)) == NULL) { if (failure_notify) { snprintf(msg, 128, "xrealloc: Unable to reallocate %d bytes!\n", (int) sz); (*failure_notify) (msg); } else { perror("realloc"); } exit(1); }#if XMALLOC_DEBUG check_malloc(p, sz);#endif#if XMALLOC_STATISTICS malloc_stat(sz);#endif#if XMALLOC_TRACE xmalloc_show_trace(p, 1);#endif#if MEM_GEN_TRACE if (tracefp) /* new ptr, old ptr, new size */ fprintf(tracefp, "r:%p:%p:%d\n", p, s, sz);#endif return (p);}/* * xcalloc() - same as calloc(3). Used for portability. * Never returns NULL; fatal on error. */void *xcalloc(int n, size_t sz){ void *p; if (n < 1) n = 1; if (sz < 1) sz = 1; if ((p = calloc(n, sz)) == NULL) { if (failure_notify) { snprintf(msg, 128, "xcalloc: Unable to allocate %d blocks of %d bytes!\n", (int) n, (int) sz); (*failure_notify) (msg); } else { perror("xcalloc"); } exit(1); }#if XMALLOC_DEBUG check_malloc(p, sz * n);#endif#if XMALLOC_STATISTICS malloc_stat(sz);#endif#if XMALLOC_TRACE xmalloc_show_trace(p, 1);#endif#if MEM_GEN_TRACE if (tracefp) fprintf(tracefp, "c:%d:%d:%p\n", n, sz, p);#endif return (p);}/* * xstrdup() - same as strdup(3). Used for portability. * Never returns NULL; fatal on error. */char *xstrdup(const char *s){ size_t sz; if (s == NULL) { if (failure_notify) { (*failure_notify) ("xstrdup: tried to dup a NULL pointer!\n"); } else { fprintf(stderr, "xstrdup: tried to dup a NULL pointer!\n"); } exit(1); } /* copy string, including terminating character */ sz = strlen(s) + 1; return memcpy(xmalloc(sz), s, sz);}/* * xstrndup() - string dup with length limit. */char *xstrndup(const char *s, size_t n){ size_t sz; assert(s); assert(n); sz = strlen(s) + 1; if (sz > n) sz = n; return xstrncpy(xmalloc(sz), s, sz);}/* * xstrerror() - strerror() wrapper */const char *xstrerror(void){ static char xstrerror_buf[BUFSIZ]; if (errno < 0 || errno >= sys_nerr) return ("Unknown"); snprintf(xstrerror_buf, BUFSIZ, "(%d) %s", errno, strerror(errno)); return xstrerror_buf;}#if NOT_NEEDED/* * xbstrerror with argument for late notification */const char *xbstrerror(int err){ static char xbstrerror_buf[BUFSIZ]; if (err < 0 || err >= sys_nerr) return ("Unknown"); snprintf(xbstrerror_buf, BUFSIZ, "(%d) %s", err, strerror(err)); return xbstrerror_buf;}#endifvoidTolower(char *q){ char *s = q; while (*s) { *s = tolower((unsigned char) *s); s++; }}inttvSubMsec(struct timeval t1, struct timeval t2){ return (t2.tv_sec - t1.tv_sec) * 1000 + (t2.tv_usec - t1.tv_usec) / 1000;}inttvSubUsec(struct timeval t1, struct timeval t2){ return (t2.tv_sec - t1.tv_sec) * 1000000 + (t2.tv_usec - t1.tv_usec);}doubletvSubDsec(struct timeval t1, struct timeval t2){ return (double) (t2.tv_sec - t1.tv_sec) + (double) (t2.tv_usec - t1.tv_usec) / 1000000.0;}/* * xstrncpy() - similar to strncpy(3) but terminates string * always with '\0' if (n != 0 and dst != NULL), * and doesn't do padding */char *xstrncpy(char *dst, const char *src, size_t n){ char *r = dst; if (!n || !dst) return dst; if (src) while (--n != 0 && *src != '\0') *dst++ = *src++; *dst = '\0'; return r;}/* returns the number of leading white spaces in str; handy in skipping ws */size_txcountws(const char *str){ size_t count = 0; if (str) { while (xisspace(*str)) { str++; count++; } } return count;}/* somewhat safer calculation of %s */doublexpercent(double part, double whole){ return xdiv(100 * part, whole);}intxpercentInt(double part, double whole){#if HAVE_RINT return (int) rint(xpercent(part, whole));#else /* SCO 3.2v4.2 doesn't have rint() -- mauri@mbp.ee */ return (int) floor(xpercent(part, whole)+0.5);#endif}/* somewhat safer division */doublexdiv(double nom, double denom){ return (denom != 0.0) ? nom / denom : -1.0;}/* integer to string */const char *xitoa(int num){ static char buf[24]; /* 2^64 = 18446744073709551616 */ snprintf(buf, sizeof(buf), "%d", num); return buf;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -