22
22
#include "libc/fmt/itoa.h"
23
23
#include "libc/fmt/libgen.h"
24
24
#include "libc/fmt/magnumstrs.internal.h"
25
- #include "libc/intrin/kprintf.h"
26
25
#include "libc/limits.h"
27
26
#include "libc/macros.h"
28
27
#include "libc/mem/alg.h"
28
+ #include "libc/mem/leaks.h"
29
29
#include "libc/mem/mem.h"
30
30
#include "libc/nexgen32e/crc32.h"
31
31
#include "libc/runtime/runtime.h"
@@ -146,8 +146,6 @@ static const char *buildroot;
146
146
static const char * genroot ;
147
147
static const char * outpath ;
148
148
149
- #include "libc/mem/tinymalloc.inc"
150
-
151
149
static inline bool IsBlank (int c ) {
152
150
return c == ' ' || c == '\t' ;
153
151
}
@@ -345,9 +343,8 @@ static const char *FindIncludePath(const char *map, size_t mapsize,
345
343
346
344
// scan backwards for hash character
347
345
for (;;) {
348
- if (q == map ) {
346
+ if (q == map )
349
347
return 0 ;
350
- }
351
348
if (IsBlank (q [-1 ])) {
352
349
-- q ;
353
350
continue ;
@@ -414,17 +411,15 @@ static void LoadRelationships(int argc, char *argv[]) {
414
411
static char srcdirbuf [PATH_MAX ];
415
412
const char * p , * pe , * src , * path , * pathend , * srcdir , * final ;
416
413
getargs_init (& ga , argv + optind );
417
- while ((src = getargs_next (& ga ))) {
414
+ while ((src = getargs_next (& ga )))
418
415
CreateSourceId (src );
419
- }
420
416
getargs_destroy (& ga );
421
417
getargs_init (& ga , argv + optind );
422
418
while ((src = getargs_next (& ga ))) {
423
419
is_assembly = endswith (src , ".s" );
424
420
srcid = GetSourceId (src );
425
- if (strlcpy (srcdirbuf , src , PATH_MAX ) >= PATH_MAX ) {
421
+ if (strlcpy (srcdirbuf , src , PATH_MAX ) >= PATH_MAX )
426
422
DiePathTooLong (src );
427
- }
428
423
srcdir = dirname (srcdirbuf );
429
424
if ((fd = open (src , O_RDONLY )) == -1 ) {
430
425
if (errno == ENOENT && ga .path ) {
@@ -438,17 +433,14 @@ static void LoadRelationships(int argc, char *argv[]) {
438
433
}
439
434
DieSys (src );
440
435
}
441
- if ((rc = lseek (fd , 0 , SEEK_END )) == -1 ) {
436
+ if ((rc = lseek (fd , 0 , SEEK_END )) == -1 )
442
437
DieSys (src );
443
- }
444
438
if ((size = rc )) {
445
439
// repeatedly map to same fixed address so in order to weasel out
446
440
// of incurring the additional overhead of all these munmap calls
447
- map = mmap ((void * )0x311987030000 , size , PROT_READ ,
448
- MAP_SHARED | MAP_FIXED , fd , 0 );
449
- if (map == MAP_FAILED ) {
441
+ map = mmap (0 , size , PROT_READ , MAP_SHARED , fd , 0 );
442
+ if (map == MAP_FAILED )
450
443
DieSys (src );
451
- }
452
444
for (p = map , pe = map + size ; p < pe ; ++ p ) {
453
445
if (!(p = memmem (p , pe - p , "include " , 8 )))
454
446
break ;
@@ -477,12 +469,10 @@ static void LoadRelationships(int argc, char *argv[]) {
477
469
dependency = -1 ;
478
470
for (long i = 0 ; i < systempaths .n ; ++ i ) {
479
471
if (!(final =
480
- __join_paths (juf , PATH_MAX , systempaths .p [i ], incpath ))) {
472
+ __join_paths (juf , PATH_MAX , systempaths .p [i ], incpath )))
481
473
DiePathTooLong (incpath );
482
- }
483
- if ((dependency = GetSourceId (final )) != -1 ) {
474
+ if ((dependency = GetSourceId (final )) != -1 )
484
475
break ;
485
- }
486
476
}
487
477
if (dependency != -1 ) {
488
478
AppendEdge (& edges , dependency , srcid );
@@ -506,9 +496,8 @@ static void LoadRelationships(int argc, char *argv[]) {
506
496
dependency = GetSourceId ((final = incpath ));
507
497
// let foo/bar.c say `#include "hdr.h"`
508
498
if (dependency == -1 && !strchr (final , '/' )) {
509
- if (!(final = __join_paths (juf , PATH_MAX , srcdir , final ))) {
499
+ if (!(final = __join_paths (juf , PATH_MAX , srcdir , final )))
510
500
DiePathTooLong (incpath );
511
- }
512
501
dependency = GetSourceId (final );
513
502
}
514
503
if (dependency == -1 ) {
@@ -526,10 +515,11 @@ static void LoadRelationships(int argc, char *argv[]) {
526
515
p = pathend + 1 ;
527
516
}
528
517
}
518
+ if (munmap (map , size ))
519
+ DieSys (src );
529
520
}
530
- if (close (fd )) {
521
+ if (close (fd ))
531
522
DieSys (src );
532
- }
533
523
}
534
524
getargs_destroy (& ga );
535
525
}
@@ -540,9 +530,8 @@ static wontreturn void ShowUsage(int rc, int fd) {
540
530
}
541
531
542
532
static void AddPath (struct Paths * paths , const char * path ) {
543
- if (paths -> n == ARRAYLEN (paths -> p )) {
533
+ if (paths -> n == ARRAYLEN (paths -> p ))
544
534
Die ("too many path arguments" );
545
- }
546
535
paths -> p [paths -> n ++ ] = path ;
547
536
}
548
537
@@ -557,21 +546,18 @@ static void GetOpts(int argc, char *argv[]) {
557
546
AddPath (& systempaths , optarg );
558
547
break ;
559
548
case 'o' :
560
- if (outpath ) {
549
+ if (outpath )
561
550
Die ("multiple output paths specified" );
562
- }
563
551
outpath = optarg ;
564
552
break ;
565
553
case 'r' :
566
- if (buildroot ) {
554
+ if (buildroot )
567
555
Die ("multiple build roots specified" );
568
- }
569
556
buildroot = optarg ;
570
557
break ;
571
558
case 'g' :
572
- if (genroot ) {
559
+ if (genroot )
573
560
Die ("multiple generated roots specified" );
574
- }
575
561
genroot = optarg ;
576
562
break ;
577
563
case 'n' :
@@ -582,31 +568,24 @@ static void GetOpts(int argc, char *argv[]) {
582
568
ShowUsage (1 , 2 );
583
569
}
584
570
}
585
- if (optind == argc ) {
571
+ if (optind == argc )
586
572
Die ("missing input argument" );
587
- }
588
- if (!genroot ) {
573
+ if (!genroot )
589
574
genroot = "o/" ;
590
- }
591
- if (!endswith (genroot , "/" )) {
575
+ if (!endswith (genroot , "/" ))
592
576
Die ("generated output path must end with slash" );
593
- }
594
- if (!buildroot ) {
577
+ if (!buildroot )
595
578
Die ("need build output path" );
596
- }
597
- if (!endswith (buildroot , "/" )) {
579
+ if (!endswith (buildroot , "/" ))
598
580
Die ("build output path must end with slash" );
599
- }
600
- if (!startswith (buildroot , genroot )) {
581
+ if (!startswith (buildroot , genroot ))
601
582
Die ("build output path must start with generated output path" );
602
- }
603
583
if (!systempaths .n && hermetic ) {
604
584
AddPath (& systempaths , "third_party/libcxx/include/" );
605
585
AddPath (& systempaths , "libc/isystem/" );
606
586
}
607
- if (systempaths .n && !hermetic ) {
587
+ if (systempaths .n && !hermetic )
608
588
Die ("system path can only be specified in hermetic mode" );
609
- }
610
589
long j = 0 ;
611
590
for (long i = 0 ; i < systempaths .n ; ++ i ) {
612
591
size_t n ;
@@ -619,21 +598,18 @@ static void GetOpts(int argc, char *argv[]) {
619
598
DieSys (path );
620
599
}
621
600
}
622
- if ((n = strlen (path )) >= PATH_MAX ) {
601
+ if ((n = strlen (path )) >= PATH_MAX )
623
602
DiePathTooLong (path );
624
- }
625
- if (!n || path [n - 1 ] != '/' ) {
603
+ if (!n || path [n - 1 ] != '/' )
626
604
Die ("system path must end with slash" );
627
- }
628
605
}
629
606
systempaths .n = j ;
630
607
}
631
608
632
609
static const char * StripExt (char pathbuf [hasatleast PATH_MAX ], const char * s ) {
633
610
static char * dot ;
634
- if (strlcpy (pathbuf , s , PATH_MAX ) >= PATH_MAX ) {
611
+ if (strlcpy (pathbuf , s , PATH_MAX ) >= PATH_MAX )
635
612
DiePathTooLong (s );
636
- }
637
613
dot = strrchr (pathbuf , '.' );
638
614
if (dot )
639
615
* dot = '\0' ;
@@ -661,13 +637,10 @@ static uint32_t GetFileExtension(const char *s) {
661
637
static bool IsObjectSource (const char * name ) {
662
638
int i ;
663
639
uint32_t ext ;
664
- if ((ext = GetFileExtension (name ))) {
665
- for (i = 0 ; i < ARRAYLEN (kSourceExts ); ++ i ) {
666
- if (ext == kSourceExts [i ]) {
640
+ if ((ext = GetFileExtension (name )))
641
+ for (i = 0 ; i < ARRAYLEN (kSourceExts ); ++ i )
642
+ if (ext == kSourceExts [i ])
667
643
return true;
668
- }
669
- }
670
- }
671
644
return false;
672
645
}
673
646
@@ -736,22 +709,18 @@ int main(int argc, char *argv[]) {
736
709
LoadRelationships (argc , argv );
737
710
Crunch ();
738
711
makefile = Explore ();
739
- if (outpath &&
740
- (fd = open (outpath , O_WRONLY | O_CREAT | O_TRUNC , 0644 )) == -1 ) {
712
+ if (outpath && (fd = open (outpath , O_WRONLY | O_CREAT | O_TRUNC , 0644 )) == -1 )
741
713
DieSys (outpath );
742
- }
743
714
n = appendz (makefile ).i ;
744
- for (i = 0 ; i < n ; i += (size_t )rc ) {
745
- if ((rc = write (fd , makefile + i , n - i )) == -1 ) {
715
+ for (i = 0 ; i < n ; i += (size_t )rc )
716
+ if ((rc = write (fd , makefile + i , n - i )) == -1 )
746
717
DieSys (outpath );
747
- }
748
- }
749
- if (outpath && close (fd )) {
718
+ if (outpath && close (fd ))
750
719
DieSys (outpath );
751
- }
752
720
free (makefile );
753
721
free (edges .p );
754
722
free (sauces );
755
723
free (names );
724
+ CheckForMemoryLeaks ();
756
725
return 0 ;
757
726
}
0 commit comments