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.8 2005/02/26 22:39:49 perry 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.8 2005/02/26 22:39:49 perry 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()
   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(s)
   45         char *s;
   46 {
   47         IFDEBUG(M)
   48                 fprintf(stdout, "HIWAT 0x%p  %s\n", hiwat,s);
   49                 fflush(stdout);
   50         ENDDEBUG
   51 }
   52 
   53 #define MIN(x,y) ((x<y)?x:y)
   54 
   55 char *Malloc(x)
   56 int x;
   57 {
   58         char *c;
   59         extern char *sbrk();
   60         static int firsttime=1;
   61         int total = x;
   62         int first_iter = 1;
   63         char *returnvalue;
   64 
   65         IFDEBUG(N)
   66                 fprintf(stdout, "Malloc 0x%x, %d, bytesmalloced %d\n",
   67                         total,total, bytesmalloced);
   68                 fflush(stdout);
   69         ENDDEBUG
   70         IFDEBUG(M)
   71                 fprintf(stdout, "Malloc 0x%x, %d, hiwat 0x%p\n",
   72                         total,total, hiwat);
   73                 fflush(stdout);
   74         ENDDEBUG
   75         if(firsttime) {
   76                 hiwat = sbrk(0);
   77                 if(((unsigned)(hiwat) & 0x3)) {
   78                         bytesmalloced = 4 - (int) ((unsigned)(hiwat) & 0x3);
   79                         hiwat = sbrk( bytesmalloced );
   80                 } else
   81                         bytesmalloced = 0;
   82                 firsttime = 0;
   83                 highend = hiwat;
   84         }
   85         while( total ) {
   86                 x = MIN(CHUNKSIZE, total);
   87                 if(total != x)  {
   88                         IFDEBUG(N)
   89                                 fprintf(stdout, "BIG Malloc tot %d, x %d, left %d net %d\n",
   90                                         total,x, total-x, bytesmalloced);
   91                                 fflush(stdout);
   92                         ENDDEBUG
   93                 }
   94                 if ( (hiwat + x) > highend) {
   95                         c = sbrk(CHUNKSIZE);
   96                         IFDEBUG(M)
   97                                 fprintf(stdout, "hiwat 0x%p, x 0x%x, highend 0x%p, c 0x%p\n",
   98                                                 hiwat, x, highend, c);
   99                                 fflush(stdout);
  100                         ENDDEBUG
  101                         if( c == (char *) -1 ) {
  102                                 fprintf(stderr, "Ran out of memory!\n");
  103                                 Exit(-1);
  104                         }
  105                         if(first_iter) {
  106                                 returnvalue = c;
  107                                 first_iter = 0;
  108                         }
  109                         bytesmalloced +=  CHUNKSIZE;
  110                         IFDEBUG(m)
  111                                 if (highend != c) {
  112                                         fprintf(OUT, "warning: %d wasted bytes!\n", highend - hiwat);
  113                                 fprintf(OUT, " chunksize 0x%x,  x 0x%x \n", CHUNKSIZE, x);
  114                                 }
  115                         ENDDEBUG
  116                         highend = c + CHUNKSIZE;
  117                         hiwat = c;
  118                 }
  119                 c = hiwat;
  120                 if(first_iter) {
  121                         returnvalue = c;
  122                         first_iter = 0;
  123                 }
  124                 hiwat += x;
  125                 total -= x;
  126         }
  127         if((unsigned)hiwat & 0x3) {
  128                 byteswasted += (int)((unsigned)(hiwat) & 0x3);
  129                 hiwat = (char *)((unsigned)(hiwat + 3) & ~0x3);
  130         }
  131         IFDEBUG(M)
  132                 fprintf(stdout, "Malloc = 0x%p, bytesm 0x%x, wasted 0x%x, hiwat 0x%p\n",
  133                         returnvalue, bytesmalloced, byteswasted, hiwat);
  134         ENDDEBUG
  135         IFDEBUG(N)
  136                 fprintf(stdout, "Malloc returns 0x%p, sbrk(0) 0x%p\n", returnvalue, sbrk(0));
  137                 fflush(stdout);
  138         ENDDEBUG
  139         return(returnvalue);
  140 }
  141 

Cache object: 53e5710b7b62a1236fa37b3fb0bf1ade


[ 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.