The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/netiso/xebec/malloc.c

Version: -  FREEBSD  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-2  -  FREEBSD-11-1  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-4  -  FREEBSD-10-3  -  FREEBSD-10-2  -  FREEBSD-10-1  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-3  -  FREEBSD-9-2  -  FREEBSD-9-1  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-4  -  FREEBSD-8-3  -  FREEBSD-8-2  -  FREEBSD-8-1  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-4  -  FREEBSD-7-3  -  FREEBSD-7-2  -  FREEBSD-7-1  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-4  -  FREEBSD-6-3  -  FREEBSD-6-2  -  FREEBSD-6-1  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-5  -  FREEBSD-5-4  -  FREEBSD-5-3  -  FREEBSD-5-2  -  FREEBSD-5-1  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  linux-2.6  -  linux-2.4.22  -  MK83  -  MK84  -  PLAN9  -  DFBSD  -  NETBSD  -  NETBSD5  -  NETBSD4  -  NETBSD3  -  NETBSD20  -  OPENBSD  -  xnu-517  -  xnu-792  -  xnu-792.6.70  -  xnu-1228  -  xnu-1456.1.26  -  xnu-1699.24.8  -  xnu-2050.18.24  -  OPENSOLARIS  -  minix-3-1-1 
SearchContext: -  none  -  3  -  10 

    1 /*      $NetBSD: malloc.c,v 1.12 2009/03/18 10:22:44 cegger Exp $       */
    2 
    3 /*
    4  * This code is such a kludge that I don't want to put my name on it.
    5  * It was a ridiculously fast hack and needs rewriting.
    6  * However it does work...
    7  */
    8 
    9 /*
   10  * a simple malloc
   11  * it might be brain-damaged but for the purposes of xebec
   12  * it's a whole lot faster than the c library malloc
   13  */
   14 
   15 #include <sys/cdefs.h>
   16 __KERNEL_RCSID(0, "$NetBSD: malloc.c,v 1.12 2009/03/18 10:22:44 cegger Exp $");
   17 
   18 #include <stdio.h>
   19 #include "malloc.h"
   20 #include "debug.h"
   21 #include "main.h"
   22 #define CHUNKSIZE 4096*2
   23 
   24 static char *hiwat, *highend;
   25 int bytesmalloced=0;
   26 int byteswasted = 0;
   27 
   28 void
   29 init_alloc(void)
   30 {
   31 #ifdef LINT
   32         hiwat = 0;
   33         highend = 0;
   34 #else /* !LINT */
   35         extern char *sbrk();
   36 
   37         hiwat = (char *) sbrk(0);
   38         hiwat = (char *)((unsigned)(hiwat + 3) & ~0x3);
   39         highend = hiwat;
   40 #endif /* LINT */
   41 }
   42 
   43 void
   44 HIWAT(char *s)
   45 {
   46         IFDEBUG(M)
   47                 fprintf(stdout, "HIWAT %p  %s\n", hiwat,s);
   48                 fflush(stdout);
   49         ENDDEBUG
   50 }
   51 
   52 #define MIN(x,y) ((x<y)?x:y)
   53 
   54 char *Malloc(x)
   55 int x;
   56 {
   57         char *c;
   58         extern char *sbrk();
   59         static int firsttime=1;
   60         int total = x;
   61         int first_iter = 1;
   62         char *returnvalue;
   63 
   64         IFDEBUG(N)
   65                 fprintf(stdout, "Malloc 0x%x, %d, bytesmalloced %d\n",
   66                         total,total, bytesmalloced);
   67                 fflush(stdout);
   68         ENDDEBUG
   69         IFDEBUG(M)
   70                 fprintf(stdout, "Malloc 0x%x, %d, hiwat %p\n",
   71                         total,total, hiwat);
   72                 fflush(stdout);
   73         ENDDEBUG
   74         if(firsttime) {
   75                 hiwat = sbrk(0);
   76                 if(((unsigned)(hiwat) & 0x3)) {
   77                         bytesmalloced = 4 - (int) ((unsigned)(hiwat) & 0x3);
   78                         hiwat = sbrk( bytesmalloced );
   79                 } else
   80                         bytesmalloced = 0;
   81                 firsttime = 0;
   82                 highend = hiwat;
   83         }
   84         while( total ) {
   85                 x = MIN(CHUNKSIZE, total);
   86                 if(total != x)  {
   87                         IFDEBUG(N)
   88                                 fprintf(stdout, "BIG Malloc tot %d, x %d, left %d net %d\n",
   89                                         total,x, total-x, bytesmalloced);
   90                                 fflush(stdout);
   91                         ENDDEBUG
   92                 }
   93                 if ( (hiwat + x) > highend) {
   94                         c = sbrk(CHUNKSIZE);
   95                         IFDEBUG(M)
   96                                 fprintf(stdout, "hiwat %p, x 0x%x, highend %p, c %p\n",
   97                                                 hiwat, x, highend, c);
   98                                 fflush(stdout);
   99                         ENDDEBUG
  100                         if( c == (char *) -1 ) {
  101                                 fprintf(stderr, "Ran out of memory!\n");
  102                                 Exit(-1);
  103                         }
  104                         if(first_iter) {
  105                                 returnvalue = c;
  106                                 first_iter = 0;
  107                         }
  108                         bytesmalloced +=  CHUNKSIZE;
  109                         IFDEBUG(m)
  110                                 if (highend != c) {
  111                                         fprintf(OUT, "warning: %d wasted bytes!\n", highend - hiwat);
  112                                 fprintf(OUT, " chunksize 0x%x,  x 0x%x \n", CHUNKSIZE, x);
  113                                 }
  114                         ENDDEBUG
  115                         highend = c + CHUNKSIZE;
  116                         hiwat = c;
  117                 }
  118                 c = hiwat;
  119                 if(first_iter) {
  120                         returnvalue = c;
  121                         first_iter = 0;
  122                 }
  123                 hiwat += x;
  124                 total -= x;
  125         }
  126         if((unsigned)hiwat & 0x3) {
  127                 byteswasted += (int)((unsigned)(hiwat) & 0x3);
  128                 hiwat = (char *)((unsigned)(hiwat + 3) & ~0x3);
  129         }
  130         IFDEBUG(M)
  131                 fprintf(stdout, "Malloc = %p, bytesm 0x%x, wasted 0x%x, hiwat %p\n",
  132                         returnvalue, bytesmalloced, byteswasted, hiwat);
  133         ENDDEBUG
  134         IFDEBUG(N)
  135                 fprintf(stdout, "Malloc returns %p, sbrk(0) %p\n", returnvalue, sbrk(0));
  136                 fflush(stdout);
  137         ENDDEBUG
  138         return(returnvalue);
  139 }
  140 

Cache object: 68344d0654baf195ca9bf890a006c289


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.