boot2

Playing with the boostrap
git clone https://git.ryansepassi.com/git/boot2.git
Log | Files | Refs | README

016-malloc-free.c (1064B)


      1 /* Two-cycle malloc/free smoke. mes's free is a no-op (linux/malloc.c
      2  * is bump-only, by design — tcc-boot2 never reaches a real allocator),
      3  * so this fixture just verifies that calling free between two malloc
      4  * requests doesn't perturb the bump cursor: the second allocation
      5  * must still hand back a usable pointer at strictly higher address.
      6  * 07-malloc-roundtrip covers the single-malloc round-trip; this adds
      7  * the free + second-malloc layer. */
      8 typedef unsigned long size_t;
      9 typedef long ssize_t;
     10 
     11 extern void   *malloc (size_t size);
     12 extern void    free   (void *ptr);
     13 extern ssize_t write  (int fd, void const *buf, size_t n);
     14 
     15 int
     16 main (void)
     17 {
     18   char *p = (char *) malloc (8);
     19   if (!p)
     20     {
     21       write (1, "m1-null\n", 8);
     22       return 1;
     23     }
     24   p[0] = 'A';
     25   free (p);
     26 
     27   char *q = (char *) malloc (16);
     28   if (!q)
     29     {
     30       write (1, "m2-null\n", 8);
     31       return 2;
     32     }
     33   if (q <= p)
     34     {
     35       write (1, "no-advance\n", 11);
     36       return 3;
     37     }
     38   q[0] = 'O';
     39   q[1] = 'K';
     40   q[2] = '\n';
     41   write (1, q, 3);
     42   free (q);
     43   return 0;
     44 }