boot2

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

malloc.c (1375B)


      1 /* -*-comment-start: "//";comment-end:""-*-
      2  * GNU Mes --- Maxwell Equations of Software
      3  * Copyright © 2016,2017,2018,2019,2021,2022 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
      4  * Copyright © 2021 Danny Milosavljevic <dannym@scratchpost.org>
      5  *
      6  * This file is part of GNU Mes.
      7  *
      8  * GNU Mes is free software; you can redistribute it and/or modify it
      9  * under the terms of the GNU General Public License as published by
     10  * the Free Software Foundation; either version 3 of the License, or (at
     11  * your option) any later version.
     12  *
     13  * GNU Mes is distributed in the hope that it will be useful, but
     14  * WITHOUT ANY WARRANTY; without even the implied warranty of
     15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     16  * GNU General Public License for more details.
     17  *
     18  * You should have received a copy of the GNU General Public License
     19  * along with GNU Mes.  If not, see <http://www.gnu.org/licenses/>.
     20  */
     21 
     22 #include <mes/lib.h>
     23 #include <string.h>
     24 #include <stddef.h>
     25 #include <stdint.h>
     26 
     27 char *__brk = 0;
     28 
     29 void *
     30 malloc (size_t size)
     31 {
     32   if (!__brk)
     33     __brk = cast_long_to_charp (brk (0));
     34 #if !__M2__
     35   /* align what we give back. */
     36   __brk = (char*) (((uintptr_t) __brk
     37                     + sizeof (max_align_t) - 1) & -sizeof (max_align_t));
     38 #endif
     39   if (brk (__brk + size) == -1)
     40     return 0;
     41   char *p = __brk;
     42   __brk = __brk + size;
     43   return p;
     44 }