diff --git a/libc/mem/tinymalloc.inc b/libc/mem/tinymalloc.inc index 450bb3c5e..6dd9b984f 100644 --- a/libc/mem/tinymalloc.inc +++ b/libc/mem/tinymalloc.inc @@ -16,6 +16,7 @@ #include "libc/assert.h" #include "libc/dce.h" #include "libc/errno.h" +#include "libc/intrin/kprintf.h" #include "libc/mem/mem.h" #include "libc/stdalign.h" #include "libc/stdckdint.h" @@ -50,6 +51,10 @@ static void tinymalloc_init(void) { heap.once = 1; } +__attribute__((__destructor__)) static void destroy(void) { + kprintf("used = %'zu\n", heap.used); +} + static inline int isheap(char *mem) { return heap.memory <= mem && mem < heap.memory + heap.used; } diff --git a/tool/build/apelink.c b/tool/build/apelink.c index a9e341c7e..862ed3f13 100644 --- a/tool/build/apelink.c +++ b/tool/build/apelink.c @@ -260,8 +260,6 @@ static Elf64_Xword notesize; static char *r_off32_e_lfanew; -#include "libc/mem/tinymalloc.inc" - static wontreturn void Die(const char *thing, const char *reason) { tinyprint(2, thing, ": ", reason, "\n", NULL); exit(1); diff --git a/tool/build/assimilate.c b/tool/build/assimilate.c index 02babe0b5..eef749455 100644 --- a/tool/build/assimilate.c +++ b/tool/build/assimilate.c @@ -67,8 +67,6 @@ #define FORMAT_MACHO 2 #define FORMAT_PE 3 -#include "libc/mem/tinymalloc.inc" - static int g_arch; static int g_format; static bool g_force; diff --git a/tool/build/compile.c b/tool/build/compile.c index 3f88327dd..4f0f682d9 100644 --- a/tool/build/compile.c +++ b/tool/build/compile.c @@ -229,8 +229,6 @@ const char *const kSafeEnv[] = { "TMPDIR", // needed by compiler }; -#include "libc/mem/tinymalloc.inc" - void OnAlrm(int sig) { ++gotalrm; } diff --git a/tool/build/cp.c b/tool/build/cp.c index afd8e84cc..5aed44442 100644 --- a/tool/build/cp.c +++ b/tool/build/cp.c @@ -69,8 +69,6 @@ char linkbuf[PATH_MAX]; void Cp(char *, char *); -#include "libc/mem/tinymalloc.inc" - bool IsDirectory(const char *path) { int e; bool res; diff --git a/tool/build/elf2pe.c b/tool/build/elf2pe.c index 784525584..c015132d2 100644 --- a/tool/build/elf2pe.c +++ b/tool/build/elf2pe.c @@ -159,9 +159,6 @@ static const char *stubpath; static long FLAG_SizeOfStackCommit = 64 * 1024; static long FLAG_SizeOfStackReserve = 8 * 1024 * 1024; -#define TINYMALLOC_MAX_ALIGN MAX_ALIGN -#include "libc/mem/tinymalloc.inc" - static wontreturn void Die(const char *thing, const char *reason) { tinyprint(2, thing, ": ", reason, "\n", NULL); exit(1); diff --git a/tool/build/fixupobj.c b/tool/build/fixupobj.c index f2adb73ca..dfda7e877 100644 --- a/tool/build/fixupobj.c +++ b/tool/build/fixupobj.c @@ -67,8 +67,6 @@ static Elf64_Ehdr *elf; static const char *epath; static Elf64_Xword symcount; -#include "libc/mem/tinymalloc.inc" - static wontreturn void Die(const char *reason) { tinyprint(2, epath, ": ", reason, "\n", NULL); exit(1); diff --git a/tool/build/gzip.c b/tool/build/gzip.c index cb1e1ff13..c2c436cac 100644 --- a/tool/build/gzip.c +++ b/tool/build/gzip.c @@ -71,8 +71,6 @@ const char *prog; char databuf[32768]; char pathbuf[PATH_MAX]; -#include "libc/mem/tinymalloc.inc" - wontreturn void PrintUsage(int rc, FILE *f) { fputs("usage: ", f); fputs(prog, f); diff --git a/tool/build/killall.c b/tool/build/killall.c index 5ebb61397..c37102a48 100644 --- a/tool/build/killall.c +++ b/tool/build/killall.c @@ -51,8 +51,6 @@ static const char *prog; static char16_t **filters; static uint32_t pids[10000]; -#include "libc/mem/tinymalloc.inc" - static wontreturn void PrintUsage(int rc, FILE *f) { fprintf(f, "Usage: %s [-nshv] NAME...\n" diff --git a/tool/build/mkdeps.c b/tool/build/mkdeps.c index 1a17eb9df..982615f05 100644 --- a/tool/build/mkdeps.c +++ b/tool/build/mkdeps.c @@ -22,10 +22,10 @@ #include "libc/fmt/itoa.h" #include "libc/fmt/libgen.h" #include "libc/fmt/magnumstrs.internal.h" -#include "libc/intrin/kprintf.h" #include "libc/limits.h" #include "libc/macros.h" #include "libc/mem/alg.h" +#include "libc/mem/leaks.h" #include "libc/mem/mem.h" #include "libc/nexgen32e/crc32.h" #include "libc/runtime/runtime.h" @@ -146,8 +146,6 @@ static const char *buildroot; static const char *genroot; static const char *outpath; -#include "libc/mem/tinymalloc.inc" - static inline bool IsBlank(int c) { return c == ' ' || c == '\t'; } @@ -345,9 +343,8 @@ static const char *FindIncludePath(const char *map, size_t mapsize, // scan backwards for hash character for (;;) { - if (q == map) { + if (q == map) return 0; - } if (IsBlank(q[-1])) { --q; continue; @@ -414,17 +411,15 @@ static void LoadRelationships(int argc, char *argv[]) { static char srcdirbuf[PATH_MAX]; const char *p, *pe, *src, *path, *pathend, *srcdir, *final; getargs_init(&ga, argv + optind); - while ((src = getargs_next(&ga))) { + while ((src = getargs_next(&ga))) CreateSourceId(src); - } getargs_destroy(&ga); getargs_init(&ga, argv + optind); while ((src = getargs_next(&ga))) { is_assembly = endswith(src, ".s"); srcid = GetSourceId(src); - if (strlcpy(srcdirbuf, src, PATH_MAX) >= PATH_MAX) { + if (strlcpy(srcdirbuf, src, PATH_MAX) >= PATH_MAX) DiePathTooLong(src); - } srcdir = dirname(srcdirbuf); if ((fd = open(src, O_RDONLY)) == -1) { if (errno == ENOENT && ga.path) { @@ -438,17 +433,14 @@ static void LoadRelationships(int argc, char *argv[]) { } DieSys(src); } - if ((rc = lseek(fd, 0, SEEK_END)) == -1) { + if ((rc = lseek(fd, 0, SEEK_END)) == -1) DieSys(src); - } if ((size = rc)) { // repeatedly map to same fixed address so in order to weasel out // of incurring the additional overhead of all these munmap calls - map = mmap((void *)0x311987030000, size, PROT_READ, - MAP_SHARED | MAP_FIXED, fd, 0); - if (map == MAP_FAILED) { + map = mmap(0, size, PROT_READ, MAP_SHARED, fd, 0); + if (map == MAP_FAILED) DieSys(src); - } for (p = map, pe = map + size; p < pe; ++p) { if (!(p = memmem(p, pe - p, "include ", 8))) break; @@ -477,12 +469,10 @@ static void LoadRelationships(int argc, char *argv[]) { dependency = -1; for (long i = 0; i < systempaths.n; ++i) { if (!(final = - __join_paths(juf, PATH_MAX, systempaths.p[i], incpath))) { + __join_paths(juf, PATH_MAX, systempaths.p[i], incpath))) DiePathTooLong(incpath); - } - if ((dependency = GetSourceId(final)) != -1) { + if ((dependency = GetSourceId(final)) != -1) break; - } } if (dependency != -1) { AppendEdge(&edges, dependency, srcid); @@ -506,9 +496,8 @@ static void LoadRelationships(int argc, char *argv[]) { dependency = GetSourceId((final = incpath)); // let foo/bar.c say `#include "hdr.h"` if (dependency == -1 && !strchr(final, '/')) { - if (!(final = __join_paths(juf, PATH_MAX, srcdir, final))) { + if (!(final = __join_paths(juf, PATH_MAX, srcdir, final))) DiePathTooLong(incpath); - } dependency = GetSourceId(final); } if (dependency == -1) { @@ -526,10 +515,11 @@ static void LoadRelationships(int argc, char *argv[]) { p = pathend + 1; } } + if (munmap(map, size)) + DieSys(src); } - if (close(fd)) { + if (close(fd)) DieSys(src); - } } getargs_destroy(&ga); } @@ -540,9 +530,8 @@ static wontreturn void ShowUsage(int rc, int fd) { } static void AddPath(struct Paths *paths, const char *path) { - if (paths->n == ARRAYLEN(paths->p)) { + if (paths->n == ARRAYLEN(paths->p)) Die("too many path arguments"); - } paths->p[paths->n++] = path; } @@ -557,21 +546,18 @@ static void GetOpts(int argc, char *argv[]) { AddPath(&systempaths, optarg); break; case 'o': - if (outpath) { + if (outpath) Die("multiple output paths specified"); - } outpath = optarg; break; case 'r': - if (buildroot) { + if (buildroot) Die("multiple build roots specified"); - } buildroot = optarg; break; case 'g': - if (genroot) { + if (genroot) Die("multiple generated roots specified"); - } genroot = optarg; break; case 'n': @@ -582,31 +568,24 @@ static void GetOpts(int argc, char *argv[]) { ShowUsage(1, 2); } } - if (optind == argc) { + if (optind == argc) Die("missing input argument"); - } - if (!genroot) { + if (!genroot) genroot = "o/"; - } - if (!endswith(genroot, "/")) { + if (!endswith(genroot, "/")) Die("generated output path must end with slash"); - } - if (!buildroot) { + if (!buildroot) Die("need build output path"); - } - if (!endswith(buildroot, "/")) { + if (!endswith(buildroot, "/")) Die("build output path must end with slash"); - } - if (!startswith(buildroot, genroot)) { + if (!startswith(buildroot, genroot)) Die("build output path must start with generated output path"); - } if (!systempaths.n && hermetic) { AddPath(&systempaths, "third_party/libcxx/include/"); AddPath(&systempaths, "libc/isystem/"); } - if (systempaths.n && !hermetic) { + if (systempaths.n && !hermetic) Die("system path can only be specified in hermetic mode"); - } long j = 0; for (long i = 0; i < systempaths.n; ++i) { size_t n; @@ -619,21 +598,18 @@ static void GetOpts(int argc, char *argv[]) { DieSys(path); } } - if ((n = strlen(path)) >= PATH_MAX) { + if ((n = strlen(path)) >= PATH_MAX) DiePathTooLong(path); - } - if (!n || path[n - 1] != '/') { + if (!n || path[n - 1] != '/') Die("system path must end with slash"); - } } systempaths.n = j; } static const char *StripExt(char pathbuf[hasatleast PATH_MAX], const char *s) { static char *dot; - if (strlcpy(pathbuf, s, PATH_MAX) >= PATH_MAX) { + if (strlcpy(pathbuf, s, PATH_MAX) >= PATH_MAX) DiePathTooLong(s); - } dot = strrchr(pathbuf, '.'); if (dot) *dot = '\0'; @@ -661,13 +637,10 @@ static uint32_t GetFileExtension(const char *s) { static bool IsObjectSource(const char *name) { int i; uint32_t ext; - if ((ext = GetFileExtension(name))) { - for (i = 0; i < ARRAYLEN(kSourceExts); ++i) { - if (ext == kSourceExts[i]) { + if ((ext = GetFileExtension(name))) + for (i = 0; i < ARRAYLEN(kSourceExts); ++i) + if (ext == kSourceExts[i]) return true; - } - } - } return false; } @@ -736,22 +709,18 @@ int main(int argc, char *argv[]) { LoadRelationships(argc, argv); Crunch(); makefile = Explore(); - if (outpath && - (fd = open(outpath, O_WRONLY | O_CREAT | O_TRUNC, 0644)) == -1) { + if (outpath && (fd = open(outpath, O_WRONLY | O_CREAT | O_TRUNC, 0644)) == -1) DieSys(outpath); - } n = appendz(makefile).i; - for (i = 0; i < n; i += (size_t)rc) { - if ((rc = write(fd, makefile + i, n - i)) == -1) { + for (i = 0; i < n; i += (size_t)rc) + if ((rc = write(fd, makefile + i, n - i)) == -1) DieSys(outpath); - } - } - if (outpath && close(fd)) { + if (outpath && close(fd)) DieSys(outpath); - } free(makefile); free(edges.p); free(sauces); free(names); + CheckForMemoryLeaks(); return 0; } diff --git a/tool/build/mv.c b/tool/build/mv.c index 503a2855f..d1cc31b13 100644 --- a/tool/build/mv.c +++ b/tool/build/mv.c @@ -62,8 +62,6 @@ char linkbuf[PATH_MAX]; void Mv(char *, char *); -#include "libc/mem/tinymalloc.inc" - wontreturn void Die(const char *path, const char *reason) { tinyprint(2, path, ": ", reason, "\n", NULL); exit(1); diff --git a/tool/build/package.c b/tool/build/package.c index 0c88b4c2d..9c4c20519 100644 --- a/tool/build/package.c +++ b/tool/build/package.c @@ -151,8 +151,6 @@ struct Relas { } *p; } prtu; -#include "libc/mem/tinymalloc.inc" - static wontreturn void Die(const char *path, const char *reason) { tinyprint(2, path, ": ", reason, "\n", NULL); exit(1); diff --git a/tool/build/resymbol.c b/tool/build/resymbol.c index f525324a2..4bf094ce6 100644 --- a/tool/build/resymbol.c +++ b/tool/build/resymbol.c @@ -33,8 +33,6 @@ const char *FLAG_prefix; const char *FLAG_suffix; const char *path; -#include "libc/mem/tinymalloc.inc" - wontreturn void PrintUsage(int fd, int exitcode) { tinyprint(fd, "\n\ NAME\n\ diff --git a/tool/build/rm.c b/tool/build/rm.c index e4a3a5077..c5a041dbc 100644 --- a/tool/build/rm.c +++ b/tool/build/rm.c @@ -48,8 +48,6 @@ static bool recursive; static bool doemptydirs; static const char *prog; -#include "libc/mem/tinymalloc.inc" - static wontreturn void PrintUsage(int rc, int fd) { tinyprint(fd, "USAGE\n\n ", prog, USAGE, NULL); exit(rc); diff --git a/tool/build/symtab.c b/tool/build/symtab.c index b372d6f4d..3c3584be4 100644 --- a/tool/build/symtab.c +++ b/tool/build/symtab.c @@ -30,8 +30,6 @@ * @fileoverview elf to symbol table file dump tool */ -#include "libc/mem/tinymalloc.inc" - void PrintUsage(FILE *f) { fprintf(f, "%s%s%s\n", "usage: ", program_invocation_name, " [-?h] -o PATH COMDBG");