Back to index

lightning-sunbird  0.9+nobinonly
prmalloc.c
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the Netscape Portable Runtime (NSPR).
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998-2000
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "primpl.h"
00039 
00040 /*
00041 ** We override malloc etc. on any platform which has preemption +
00042 ** nspr20 user level threads.  When we're debugging, we can make our
00043 ** version of malloc fail occasionally.
00044 */
00045 #ifdef _PR_OVERRIDE_MALLOC
00046 
00047 /*
00048 ** Thread safe version of malloc, calloc, realloc, free
00049 */
00050 #include <stdarg.h>
00051 
00052 #ifdef DEBUG
00053 #define SANITY
00054 #define EXTRA_SANITY
00055 #else
00056 #undef SANITY
00057 #undef EXTRA_SANITY
00058 #endif
00059 
00060 /* Forward decls */
00061 void *_PR_UnlockedMalloc(size_t size);
00062 void _PR_UnlockedFree(void *ptr);
00063 void *_PR_UnlockedRealloc(void *ptr, size_t size);
00064 void *_PR_UnlockedCalloc(size_t n, size_t elsize);
00065 
00066 /************************************************************************/
00067 
00068 /*
00069  * ----------------------------------------------------------------------------
00070  * "THE BEER-WARE LICENSE" (Revision 42):
00071  * <phk@FreeBSD.ORG> wrote this file.  As long as you retain this notice you
00072  * can do whatever you want with this stuff. If we meet some day, and you think
00073  * this stuff is worth it, you can buy me a beer in return.   Poul-Henning Kamp
00074  * ----------------------------------------------------------------------------
00075  *
00076  */
00077 
00078 /*
00079  * Defining SANITY will enable some checks which will tell you if the users
00080  * program did botch something
00081  */
00082 
00083 /*
00084  * Defining EXTRA_SANITY will enable some checks which are mostly related
00085  * to internal conditions in malloc.c
00086  */
00087 
00088 /*
00089  * Very verbose progress on stdout...
00090  */
00091 #if 0
00092 #  define TRACE(foo)    printf  foo
00093 static int malloc_event;
00094 #else
00095 #  define TRACE(foo) 
00096 #endif
00097 
00098 /* XXX Pick a number, any number */
00099 #   define malloc_pagesize         4096UL
00100 #   define malloc_pageshift        12UL
00101 
00102 #ifdef XP_UNIX
00103 #include <stdio.h>
00104 #include <stdlib.h>
00105 #include <unistd.h>
00106 #include <string.h>
00107 #include <errno.h>
00108 #include <sys/types.h>
00109 #include <sys/mman.h>
00110 #endif
00111 
00112 /*
00113  * This structure describes a page's worth of chunks.
00114  */
00115 
00116 struct pginfo {
00117     struct pginfo    *next; /* next on the free list */
00118     char             *page; /* Pointer to the page */
00119     u_short          size;  /* size of this page's chunks */
00120     u_short          shift; /* How far to shift for this size chunks */
00121     u_short          free;  /* How many free chunks */
00122     u_short          total; /* How many chunk */
00123     u_long           bits[1]; /* Which chunks are free */
00124 };
00125 
00126 struct pgfree {
00127     struct pgfree    *next; /* next run of free pages */
00128     struct pgfree    *prev; /* prev run of free pages */
00129     char             *page; /* pointer to free pages */
00130     char             *end;  /* pointer to end of free pages */
00131     u_long           size;  /* number of bytes free */
00132 };
00133 
00134 /*
00135  * How many bits per u_long in the bitmap.
00136  * Change only if not 8 bits/byte
00137  */
00138 #define       MALLOC_BITS   (8*sizeof(u_long))
00139 
00140 /*
00141  * Magic values to put in the page_directory
00142  */
00143 #define MALLOC_NOT_MINE     ((struct pginfo*) 0)
00144 #define MALLOC_FREE  ((struct pginfo*) 1)
00145 #define MALLOC_FIRST ((struct pginfo*) 2)
00146 #define MALLOC_FOLLOW       ((struct pginfo*) 3)
00147 #define MALLOC_MAGIC ((struct pginfo*) 4)
00148 
00149 /*
00150  * Set to one when malloc_init has been called
00151  */
00152 static unsigned      initialized;
00153 
00154 /*
00155  * The size of a page.
00156  * Must be a integral multiplum of the granularity of mmap(2).
00157  * Your toes will curl if it isn't a power of two
00158  */
00159 #define malloc_pagemask     ((malloc_pagesize)-1)
00160 
00161 /*
00162  * The size of the largest chunk.
00163  * Half a page.
00164  */
00165 #define malloc_maxsize      ((malloc_pagesize)>>1)
00166 
00167 /*
00168  * malloc_pagesize == 1 << malloc_pageshift
00169  */
00170 #ifndef malloc_pageshift
00171 static unsigned      malloc_pageshift;
00172 #endif /* malloc_pageshift */
00173 
00174 /*
00175  * The smallest allocation we bother about.
00176  * Must be power of two
00177  */
00178 #ifndef malloc_minsize
00179 static unsigned  malloc_minsize;
00180 #endif /* malloc_minsize */
00181 
00182 /*
00183  * The largest chunk we care about.
00184  * Must be smaller than pagesize
00185  * Must be power of two
00186  */
00187 #ifndef malloc_maxsize
00188 static unsigned  malloc_maxsize;
00189 #endif /* malloc_maxsize */
00190 
00191 #ifndef malloc_cache
00192 static unsigned  malloc_cache;
00193 #endif /* malloc_cache */
00194 
00195 /*
00196  * The offset from pagenumber to index into the page directory
00197  */
00198 static u_long  malloc_origo;
00199 
00200 /*
00201  * The last index in the page directory we care about
00202  */
00203 static u_long  last_index;
00204 
00205 /*
00206  * Pointer to page directory.
00207  * Allocated "as if with" malloc
00208  */
00209 static struct pginfo **page_dir;
00210 
00211 /*
00212  * How many slots in the page directory
00213  */
00214 static unsigned      malloc_ninfo;
00215 
00216 /*
00217  * Free pages line up here 
00218  */
00219 static struct pgfree free_list;
00220 
00221 /*
00222  * Abort() if we fail to get VM ?
00223  */
00224 static int malloc_abort;
00225 
00226 #ifdef SANITY
00227 /*
00228  * Are we trying to die ?
00229  */
00230 static int suicide;
00231 #endif
00232 
00233 /*
00234  * dump statistics
00235  */
00236 static int malloc_stats;
00237 
00238 /*
00239  * always realloc ?
00240  */
00241 static int malloc_realloc;
00242 
00243 /*
00244  * my last break.
00245  */
00246 static void *malloc_brk;
00247 
00248 /*
00249  * one location cache for free-list holders
00250  */
00251 static struct pgfree *px;
00252 
00253 static int set_pgdir(void *ptr, struct  pginfo *info);
00254 static int extend_page_directory(u_long index);
00255 
00256 #ifdef SANITY
00257 void
00258 malloc_dump(FILE *fd)
00259 {
00260     struct pginfo **pd;
00261     struct pgfree *pf;
00262     int j;
00263 
00264     pd = page_dir;
00265 
00266     /* print out all the pages */
00267     for(j=0;j<=last_index;j++) {
00268        fprintf(fd,"%08lx %5d ",(j+malloc_origo) << malloc_pageshift,j);
00269        if (pd[j] == MALLOC_NOT_MINE) {
00270            for(j++;j<=last_index && pd[j] == MALLOC_NOT_MINE;j++)
00271               ;
00272            j--;
00273            fprintf(fd,".. %5d not mine\n",       j);
00274        } else if (pd[j] == MALLOC_FREE) {
00275            for(j++;j<=last_index && pd[j] == MALLOC_FREE;j++)
00276               ;
00277            j--;
00278            fprintf(fd,".. %5d free\n", j);
00279        } else if (pd[j] == MALLOC_FIRST) {
00280            for(j++;j<=last_index && pd[j] == MALLOC_FOLLOW;j++)
00281               ;
00282            j--;
00283            fprintf(fd,".. %5d in use\n", j);
00284        } else if (pd[j] < MALLOC_MAGIC) {
00285            fprintf(fd,"(%p)\n", pd[j]);
00286        } else {
00287            fprintf(fd,"%p %d (of %d) x %d @ %p --> %p\n",
00288               pd[j],pd[j]->free, pd[j]->total, 
00289               pd[j]->size, pd[j]->page, pd[j]->next);
00290        }
00291     }
00292 
00293     for(pf=free_list.next; pf; pf=pf->next) {
00294        fprintf(fd,"Free: @%p [%p...%p[ %ld ->%p <-%p\n",
00295               pf,pf->page,pf->end,pf->size,pf->prev,pf->next);
00296        if (pf == pf->next) {
00297               fprintf(fd,"Free_list loops.\n");
00298               break;
00299        }
00300     }
00301 
00302     /* print out various info */
00303     fprintf(fd,"Minsize\t%d\n",malloc_minsize);
00304     fprintf(fd,"Maxsize\t%ld\n",malloc_maxsize);
00305     fprintf(fd,"Pagesize\t%ld\n",malloc_pagesize);
00306     fprintf(fd,"Pageshift\t%ld\n",malloc_pageshift);
00307     fprintf(fd,"FirstPage\t%ld\n",malloc_origo);
00308     fprintf(fd,"LastPage\t%ld %lx\n",last_index+malloc_pageshift,
00309        (last_index + malloc_pageshift) << malloc_pageshift);
00310     fprintf(fd,"Break\t%ld\n",(u_long)sbrk(0) >> malloc_pageshift);
00311 }
00312 
00313 static void wrterror(char *fmt, ...)
00314 {
00315     char *q = "malloc() error: ";
00316     char buf[100];
00317     va_list ap;
00318 
00319     suicide = 1;
00320 
00321     va_start(ap, fmt);
00322     PR_vsnprintf(buf, sizeof(buf), fmt, ap);
00323     va_end(ap);
00324     fputs(q, stderr);
00325     fputs(buf, stderr);
00326 
00327     malloc_dump(stderr);
00328     PR_Abort();
00329 }
00330 
00331 static void wrtwarning(char *fmt, ...)
00332 {
00333     char *q = "malloc() warning: ";
00334     char buf[100];
00335     va_list ap;
00336 
00337     va_start(ap, fmt);
00338     PR_vsnprintf(buf, sizeof(buf), fmt, ap);
00339     va_end(ap);
00340     fputs(q, stderr);
00341     fputs(buf, stderr);
00342 }
00343 #endif /* SANITY */
00344 
00345 
00346 /*
00347  * Allocate a number of pages from the OS
00348  */
00349 static caddr_t
00350 map_pages(int pages, int update)
00351 {
00352     caddr_t result,tail;
00353 
00354     result = ((caddr_t)sbrk(0)) + malloc_pagemask - 1;
00355     result = (caddr_t) ((u_long)result & ~malloc_pagemask);
00356     tail = result + (pages << malloc_pageshift);
00357     if (!brk(tail)) {
00358        last_index = ((u_long)tail >> malloc_pageshift) - malloc_origo -1;
00359        malloc_brk = tail;
00360        TRACE(("%6d S %p .. %p\n",malloc_event++, result, tail));
00361        if (!update || last_index < malloc_ninfo ||
00362          extend_page_directory(last_index))
00363            return result;
00364     }
00365     TRACE(("%6d s %d %p %d\n",malloc_event++,pages,sbrk(0),errno));
00366 #ifdef EXTRA_SANITY
00367     wrterror("map_pages fails\n");
00368 #endif
00369     return 0;
00370 }
00371 
00372 #define set_bit(_pi,_bit) \
00373     (_pi)->bits[(_bit)/MALLOC_BITS] |= 1L<<((_bit)%MALLOC_BITS)
00374 
00375 #define clr_bit(_pi,_bit) \
00376     (_pi)->bits[(_bit)/MALLOC_BITS] &= ~(1L<<((_bit)%MALLOC_BITS));
00377 
00378 #define tst_bit(_pi,_bit) \
00379     ((_pi)->bits[(_bit)/MALLOC_BITS] & (1L<<((_bit)%MALLOC_BITS)))
00380 
00381 /*
00382  * Extend page directory
00383  */
00384 static int
00385 extend_page_directory(u_long index)
00386 {
00387     struct  pginfo **young, **old;
00388     int i;
00389 
00390     TRACE(("%6d E %lu\n",malloc_event++,index));
00391     
00392     /* Make it this many pages */
00393     i = index * sizeof *page_dir;
00394     i /= malloc_pagesize;
00395     i += 2;
00396 
00397     /* Get new pages, if you used this much mem you don't care :-) */
00398     young = (struct pginfo**) map_pages(i,0);
00399     if (!young)
00400        return 0;
00401 
00402     /* Copy the old stuff */
00403     memset(young, 0, i * malloc_pagesize);
00404     memcpy(young, page_dir,
00405            malloc_ninfo * sizeof *page_dir);
00406 
00407     /* register the new size */
00408     malloc_ninfo = i * malloc_pagesize / sizeof *page_dir;
00409 
00410     /* swap the pointers */
00411     old = page_dir;
00412     page_dir = young;
00413 
00414     /* Mark the pages */
00415     index = ((u_long)young >> malloc_pageshift) - malloc_origo;
00416     page_dir[index] = MALLOC_FIRST;
00417     while (--i) {
00418        page_dir[++index] = MALLOC_FOLLOW;
00419     }
00420 
00421     /* Now free the old stuff */
00422     _PR_UnlockedFree(old);
00423     return 1;
00424 }
00425 
00426 /*
00427  * Set entry in page directory.
00428  * Extend page directory if need be.
00429  */
00430 static int
00431 set_pgdir(void *ptr, struct  pginfo *info)
00432 {
00433     u_long index = ((u_long)ptr >> malloc_pageshift) - malloc_origo;
00434 
00435     if (index >= malloc_ninfo && !extend_page_directory(index))
00436        return 0;
00437     page_dir[index] = info;
00438     return 1;
00439 }
00440 
00441 /*
00442  * Initialize the world
00443  */
00444 static void
00445 malloc_init (void)
00446 {
00447     int i;
00448     char *p;
00449 
00450     TRACE(("%6d I\n",malloc_event++));
00451 #ifdef DEBUG
00452     for (p=getenv("MALLOC_OPTIONS"); p && *p; p++) {
00453        switch (*p) {
00454            case 'a': malloc_abort = 0; break;
00455            case 'A': malloc_abort = 1; break;
00456            case 'd': malloc_stats = 0; break;
00457            case 'D': malloc_stats = 1; break;
00458            case 'r': malloc_realloc = 0; break;
00459            case 'R': malloc_realloc = 1; break;
00460            default:
00461               wrtwarning("Unknown chars in MALLOC_OPTIONS\n");
00462               break;
00463        }
00464     }
00465 #endif
00466 
00467 #ifndef malloc_pagesize
00468     /* determine our pagesize */
00469     malloc_pagesize = getpagesize();
00470 #endif /* malloc_pagesize */
00471 
00472 #ifndef malloc_pageshift
00473     /* determine how much we shift by to get there */
00474     for (i = malloc_pagesize; i > 1; i >>= 1)
00475        malloc_pageshift++;
00476 #endif /* malloc_pageshift */
00477 
00478 #ifndef malloc_cache
00479     malloc_cache = 50 << malloc_pageshift;       
00480 #endif /* malloc_cache */
00481 
00482 #ifndef malloc_minsize
00483     /*
00484      * find the smallest size allocation we will bother about.
00485      * this is determined as the smallest allocation that can hold
00486      * it's own pginfo;
00487      */
00488     i = 2;
00489     for(;;) {
00490        int j;
00491 
00492        /* Figure out the size of the bits */
00493        j = malloc_pagesize/i;
00494        j /= 8;
00495        if (j < sizeof(u_long))
00496               j = sizeof (u_long);
00497        if (sizeof(struct pginfo) + j - sizeof (u_long) <= i)
00498               break;
00499        i += i;
00500     }
00501     malloc_minsize = i;
00502 #endif /* malloc_minsize */
00503 
00504 
00505     /* Allocate one page for the page directory */
00506     page_dir = (struct pginfo **) map_pages(1,0);
00507 #ifdef SANITY
00508     if (!page_dir)
00509        wrterror("fatal: my first mmap failed.  (check limits ?)\n");
00510 #endif
00511 
00512     /*
00513      * We need a maximum of malloc_pageshift buckets, steal these from the
00514      * front of the page_directory;
00515      */
00516     malloc_origo = (u_long) page_dir >> malloc_pageshift;
00517     malloc_origo -= malloc_pageshift;
00518 
00519     /* Clear it */
00520     memset(page_dir,0,malloc_pagesize);
00521 
00522     /* Find out how much it tells us */
00523     malloc_ninfo = malloc_pagesize / sizeof *page_dir;
00524 
00525     /* Plug the page directory into itself */
00526     i = set_pgdir(page_dir,MALLOC_FIRST);
00527 #ifdef SANITY
00528     if (!i)
00529        wrterror("fatal: couldn't set myself in the page directory\n");
00530 #endif
00531 
00532     /* Been here, done that */
00533     initialized++;
00534 }
00535 
00536 /*
00537  * Allocate a number of complete pages
00538  */
00539 static void *malloc_pages(size_t size)
00540 {
00541     void *p,*delay_free = 0;
00542     int i;
00543     struct pgfree *pf;
00544     u_long index;
00545 
00546     /* How many pages ? */
00547     size += (malloc_pagesize-1);
00548     size &= ~malloc_pagemask;
00549 
00550     p = 0;
00551     /* Look for free pages before asking for more */
00552     for(pf = free_list.next; pf; pf = pf->next) {
00553 #ifdef EXTRA_SANITY
00554        if (pf->page == pf->end)
00555            wrterror("zero entry on free_list\n");
00556        if (pf->page > pf->end) {
00557            TRACE(("%6d !s %p %p %p <%d>\n",malloc_event++,
00558               pf,pf->page,pf->end,__LINE__));
00559            wrterror("sick entry on free_list\n");
00560        }
00561        if ((void*)pf->page >= (void*)sbrk(0))
00562            wrterror("entry on free_list past brk\n");
00563        if (page_dir[((u_long)pf->page >> malloc_pageshift) - malloc_origo] 
00564          != MALLOC_FREE) {
00565            TRACE(("%6d !f %p %p %p <%d>\n",malloc_event++,
00566               pf,pf->page,pf->end,__LINE__));
00567            wrterror("non-free first page on free-list\n");
00568        }
00569        if (page_dir[((u_long)pf->end >> malloc_pageshift) - 1 - malloc_origo] 
00570          != MALLOC_FREE)
00571            wrterror("non-free last page on free-list\n");
00572 #endif /* EXTRA_SANITY */
00573        if (pf->size < size) 
00574            continue;
00575        else if (pf->size == size) {
00576            p = pf->page;
00577            if (pf->next)
00578                   pf->next->prev = pf->prev;
00579            pf->prev->next = pf->next;
00580            delay_free = pf;
00581            break;
00582        } else {
00583            p = pf->page;
00584            pf->page += size;
00585            pf->size -= size;
00586            break;
00587         }
00588     }
00589 #ifdef EXTRA_SANITY
00590     if (p && page_dir[((u_long)p >> malloc_pageshift) - malloc_origo] 
00591       != MALLOC_FREE) {
00592        wrterror("allocated non-free page on free-list\n");
00593     }
00594 #endif /* EXTRA_SANITY */
00595 
00596     size >>= malloc_pageshift;
00597 
00598     /* Map new pages */
00599     if (!p)
00600        p = map_pages(size,1);
00601 
00602     if (p) {
00603        /* Mark the pages in the directory */
00604        index = ((u_long)p >> malloc_pageshift) - malloc_origo;
00605        page_dir[index] = MALLOC_FIRST;
00606        for (i=1;i<size;i++)
00607            page_dir[index+i] = MALLOC_FOLLOW;
00608     }
00609     if (delay_free) {
00610        if (!px) 
00611            px = (struct pgfree*)delay_free;
00612        else
00613            _PR_UnlockedFree(delay_free);
00614     }
00615     return p;
00616 }
00617 
00618 /*
00619  * Allocate a page of fragments
00620  */
00621 
00622 static int
00623 malloc_make_chunks(int bits)
00624 {
00625     struct  pginfo *bp;
00626     void *pp;
00627     int i,k,l;
00628 
00629     /* Allocate a new bucket */
00630     pp = malloc_pages(malloc_pagesize);
00631     if (!pp)
00632        return 0;
00633     l = sizeof *bp - sizeof(u_long);
00634     l += sizeof(u_long) *
00635        (((malloc_pagesize >> bits)+MALLOC_BITS-1) / MALLOC_BITS);
00636     if ((1<<(bits)) <= l+l) {
00637        bp = (struct  pginfo *)pp;
00638     } else {
00639        bp = (struct  pginfo *)_PR_UnlockedMalloc(l);
00640     }
00641     if (!bp)
00642        return 0;
00643     bp->size = (1<<bits);
00644     bp->shift = bits;
00645     bp->total = bp->free = malloc_pagesize >> bits;
00646     bp->next = page_dir[bits];
00647     bp->page = (char*)pp;
00648     i = set_pgdir(pp,bp);
00649     if (!i)
00650        return 0;
00651 
00652     /* We can safely assume that there is nobody in this chain */
00653     page_dir[bits] = bp;
00654 
00655     /* set all valid bits in the bits */
00656     k = bp->total;
00657     i = 0;
00658 /*
00659     for(;k-i >= MALLOC_BITS; i += MALLOC_BITS)
00660        bp->bits[i / MALLOC_BITS] = ~0;
00661 */
00662     for(; i < k; i++)
00663        set_bit(bp,i);
00664 
00665     if (bp != pp)
00666        return 1;
00667 
00668     /* We may have used the first ones already */
00669     for(i=0;l > 0;i++) {
00670        clr_bit(bp,i);
00671        bp->free--;
00672        bp->total--;
00673        l -= (1 << bits);
00674     }
00675     return 1;
00676 }
00677 
00678 /*
00679  * Allocate a fragment
00680  */
00681 static void *malloc_bytes(size_t size)
00682 {
00683     size_t s;
00684     int j;
00685     struct  pginfo *bp;
00686     int k;
00687     u_long *lp, bf;
00688 
00689     /* Don't bother with anything less than this */
00690     if (size < malloc_minsize) {
00691        size = malloc_minsize;
00692     }
00693 
00694     /* Find the right bucket */
00695     j = 1;
00696     s = size - 1;
00697     while (s >>= 1) {
00698         j++;
00699     }
00700 
00701     /* If it's empty, make a page more of that size chunks */
00702     if (!page_dir[j] && !malloc_make_chunks(j))
00703        return 0;
00704 
00705     /* Find first word of bitmap which isn't empty */
00706     bp = page_dir[j];
00707     for (lp = bp->bits; !*lp; lp++)
00708        ;
00709 
00710     /* Find that bit */
00711     bf = *lp;
00712     k = 0;
00713     while ((bf & 1) == 0) {
00714        bf >>= 1;
00715        k++;
00716     }
00717 
00718     *lp ^= 1L<<k;                       /* clear it */
00719     bp->free--;
00720     if (!bp->free) {
00721        page_dir[j] = bp->next;
00722        bp->next = 0;
00723     }
00724     k += (lp - bp->bits)*MALLOC_BITS;
00725     return bp->page + (k << bp->shift);
00726 }
00727 
00728 void *_PR_UnlockedMalloc(size_t size)
00729 {
00730     void *result;
00731 
00732     /* Round up to a multiple of 8 bytes */
00733     if (size & 7) {
00734        size = size + 8 - (size & 7);
00735     }
00736 
00737     if (!initialized)
00738        malloc_init();
00739 
00740 #ifdef SANITY
00741     if (suicide)
00742        PR_Abort();
00743 #endif
00744 
00745     if (size <= malloc_maxsize)
00746        result =  malloc_bytes(size);
00747     else
00748        result =  malloc_pages(size);
00749 #ifdef SANITY
00750     if (malloc_abort && !result)
00751        wrterror("malloc() returns NULL\n");
00752 #endif
00753     TRACE(("%6d M %p %d\n",malloc_event++,result,size));
00754 
00755     return result;
00756 }
00757 
00758 void *_PR_UnlockedMemalign(size_t alignment, size_t size)
00759 {
00760     void *result;
00761 
00762     /*
00763      * alignment has to be a power of 2
00764      */
00765 
00766     if ((size <= alignment) && (alignment <= malloc_maxsize))
00767               size = alignment;    
00768     else
00769               size += alignment - 1;      
00770 
00771     /* Round up to a multiple of 8 bytes */
00772     if (size & 7) {
00773        size = size + 8 - (size & 7);
00774     }
00775 
00776     if (!initialized)
00777        malloc_init();
00778 
00779 #ifdef SANITY
00780     if (suicide)
00781        abort();
00782 #endif
00783 
00784     if (size <= malloc_maxsize)
00785        result =  malloc_bytes(size);
00786     else
00787        result =  malloc_pages(size);
00788 #ifdef SANITY
00789     if (malloc_abort && !result)
00790        wrterror("malloc() returns NULL\n");
00791 #endif
00792     TRACE(("%6d A %p %d\n",malloc_event++,result,size));
00793 
00794     if ((u_long)result & (alignment - 1))
00795        return ((void *)(((u_long)result + alignment)  & ~(alignment - 1)));
00796     else
00797        return result;
00798 }
00799 
00800 void *_PR_UnlockedCalloc(size_t n, size_t nelem)
00801 {
00802     void *p;
00803 
00804     /* Compute total size and then round up to a double word amount */
00805     n *= nelem;
00806     if (n & 7) {
00807        n = n + 8 - (n & 7);
00808     }
00809 
00810     /* Get the memory */
00811     p = _PR_UnlockedMalloc(n);
00812     if (p) {
00813        /* Zero it */
00814        memset(p, 0, n);
00815     }
00816     return p;
00817 }
00818 
00819 /*
00820  * Change an allocation's size
00821  */
00822 void *_PR_UnlockedRealloc(void *ptr, size_t size)
00823 {
00824     void *p;
00825     u_long osize,page,index,tmp_index;
00826     struct pginfo **mp;
00827 
00828     if (!initialized)
00829        malloc_init();
00830 
00831 #ifdef SANITY
00832     if (suicide)
00833        PR_Abort();
00834 #endif
00835 
00836     /* used as free() */
00837     TRACE(("%6d R %p %d\n",malloc_event++, ptr, size));
00838     if (ptr && !size) {
00839        _PR_UnlockedFree(ptr);
00840        return _PR_UnlockedMalloc (1);
00841     }
00842 
00843     /* used as malloc() */
00844     if (!ptr) {
00845        p = _PR_UnlockedMalloc(size);
00846        return p;
00847     }
00848 
00849     /* Find the page directory entry for the page in question */
00850     page = (u_long)ptr >> malloc_pageshift;
00851     index = page - malloc_origo;
00852 
00853     /*
00854      * check if memory was allocated by memalign
00855      */
00856     tmp_index = index;
00857     while (page_dir[tmp_index] == MALLOC_FOLLOW)
00858        tmp_index--;
00859     if (tmp_index != index) {
00860        /*
00861         * memalign-allocated memory
00862         */
00863        index = tmp_index;
00864        page = index + malloc_origo;                     
00865        ptr = (void *) (page << malloc_pageshift);
00866     }
00867     TRACE(("%6d R2 %p %d\n",malloc_event++, ptr, size));
00868 
00869     /* make sure it makes sense in some fashion */
00870     if (index < malloc_pageshift || index > last_index) {
00871 #ifdef SANITY
00872        wrtwarning("junk pointer passed to realloc()\n");
00873 #endif
00874        return 0;
00875     }
00876 
00877     /* find the size of that allocation, and see if we need to relocate */
00878     mp = &page_dir[index];
00879     if (*mp == MALLOC_FIRST) {
00880        osize = malloc_pagesize;
00881        while (mp[1] == MALLOC_FOLLOW) {
00882            osize += malloc_pagesize;
00883            mp++;
00884        }
00885         if (!malloc_realloc && 
00886               size < osize && 
00887               size > malloc_maxsize &&
00888            size > (osize - malloc_pagesize)) {
00889            return ptr;
00890        }
00891     } else if (*mp >= MALLOC_MAGIC) {
00892        osize = (*mp)->size;
00893        if (!malloc_realloc &&
00894               size < osize && 
00895            (size > (*mp)->size/2 || (*mp)->size == malloc_minsize)) {
00896            return ptr;
00897        }
00898     } else {
00899 #ifdef SANITY
00900        wrterror("realloc() of wrong page.\n");
00901 #endif
00902     }
00903 
00904     /* try to reallocate */
00905     p = _PR_UnlockedMalloc(size);
00906 
00907     if (p) {
00908        /* copy the lesser of the two sizes */
00909        if (osize < size)
00910            memcpy(p,ptr,osize);
00911        else
00912            memcpy(p,ptr,size);
00913        _PR_UnlockedFree(ptr);
00914     }
00915 #ifdef DEBUG
00916     else if (malloc_abort)
00917        wrterror("realloc() returns NULL\n");
00918 #endif
00919 
00920     return p;
00921 }
00922 
00923 /*
00924  * Free a sequence of pages
00925  */
00926 
00927 static void
00928 free_pages(char *ptr, u_long page, int index, struct pginfo *info)
00929 {
00930     int i;
00931     struct pgfree *pf,*pt;
00932     u_long l;
00933     char *tail;
00934 
00935     TRACE(("%6d FP %p %d\n",malloc_event++, ptr, page));
00936     /* Is it free already ? */
00937     if (info == MALLOC_FREE) {
00938 #ifdef SANITY
00939        wrtwarning("freeing free page at %p.\n", ptr);
00940 #endif
00941        return;
00942     }
00943 
00944 #ifdef SANITY
00945     /* Is it not the right place to begin ? */
00946     if (info != MALLOC_FIRST)
00947        wrterror("freeing wrong page.\n");
00948 
00949     /* Is this really a pointer to a page ? */
00950     if ((u_long)ptr & malloc_pagemask)
00951        wrterror("freeing messed up page pointer.\n");
00952 #endif
00953 
00954     /* Count how many pages it is anyway */
00955     page_dir[index] = MALLOC_FREE;
00956     for (i = 1; page_dir[index+i] == MALLOC_FOLLOW; i++)
00957        page_dir[index + i] = MALLOC_FREE;
00958 
00959     l = i << malloc_pageshift;
00960 
00961     tail = ptr+l;
00962 
00963     /* add to free-list */
00964     if (!px)
00965        px = (struct pgfree*)_PR_UnlockedMalloc(sizeof *pt);
00966     /* XXX check success */
00967     px->page = ptr;
00968     px->end =  tail;
00969     px->size = l;
00970     if (!free_list.next) {
00971        px->next = free_list.next;
00972        px->prev = &free_list;
00973        free_list.next = px;
00974        pf = px;
00975        px = 0;
00976     } else {
00977        tail = ptr+l;
00978        for(pf = free_list.next; pf->next && pf->end < ptr; pf = pf->next)
00979            ;
00980        for(; pf; pf = pf->next) {
00981            if (pf->end == ptr ) {
00982               /* append to entry */
00983               pf->end += l;
00984               pf->size += l;
00985               if (pf->next && pf->end == pf->next->page ) {
00986                   pt = pf->next;
00987                   pf->end = pt->end;
00988                   pf->size += pt->size;
00989                   pf->next = pt->next;
00990                   if (pf->next)
00991                      pf->next->prev = pf;
00992                   _PR_UnlockedFree(pt);
00993               }
00994            } else if (pf->page == tail) {
00995               /* prepend to entry */
00996               pf->size += l;
00997               pf->page = ptr;
00998            } else if (pf->page > ptr) {
00999               px->next = pf;
01000               px->prev = pf->prev;
01001               pf->prev = px;
01002               px->prev->next = px;
01003               pf = px;
01004               px = 0;
01005            } else if (!pf->next) {
01006               px->next = 0;
01007               px->prev = pf;
01008               pf->next = px;
01009               pf = px;
01010               px = 0;
01011            } else {
01012               continue;
01013            }
01014            break;
01015        }
01016     }
01017     if (!pf->next &&
01018       pf->size > malloc_cache &&
01019       pf->end == malloc_brk &&
01020       malloc_brk == (void*)sbrk(0)) {
01021        pf->end = pf->page + malloc_cache;
01022        pf->size = malloc_cache;
01023        TRACE(("%6d U %p %d\n",malloc_event++,pf->end,pf->end - pf->page));
01024        brk(pf->end);
01025        malloc_brk = pf->end;
01026        /* Find the page directory entry for the page in question */
01027        page = (u_long)pf->end >> malloc_pageshift;
01028        index = page - malloc_origo;
01029        /* Now update the directory */
01030        for(i=index;i <= last_index;)
01031            page_dir[i++] = MALLOC_NOT_MINE;
01032        last_index = index - 1;
01033     }
01034 }
01035 
01036 /*
01037  * Free a chunk, and possibly the page it's on, if the page becomes empty.
01038  */
01039 
01040 static void
01041 free_bytes(void *ptr, u_long page, int index, struct pginfo *info)
01042 {
01043     int i;
01044     struct pginfo **mp;
01045     void *vp;
01046 
01047     /* Make sure that pointer is multiplum of chunk-size */
01048 #ifdef SANITY
01049     if ((u_long)ptr & (info->size - 1))
01050        wrterror("freeing messed up chunk pointer\n");
01051 #endif
01052 
01053     /* Find the chunk number on the page */
01054     i = ((u_long)ptr & malloc_pagemask) >> info->shift;
01055 
01056     /* See if it's free already */
01057     if (tst_bit(info,i)) {
01058 #ifdef SANITY
01059        wrtwarning("freeing free chunk at %p\n", ptr);
01060 #endif
01061        return;
01062     }
01063 
01064     /* Mark it free */
01065     set_bit(info,i);
01066     info->free++;
01067 
01068     /* If the page was full before, we need to put it on the queue now */
01069     if (info->free == 1) {
01070        mp = page_dir + info->shift;
01071        while (*mp && (*mp)->next && (*mp)->next->page < info->page)
01072            mp = &(*mp)->next;
01073        info->next = *mp;
01074        *mp = info;
01075        return;
01076     }
01077 
01078     /* If this page isn't empty, don't do anything. */
01079     if (info->free != info->total)
01080        return;
01081 
01082     /* We may want to keep at least one page of each size chunks around.  */
01083     mp = page_dir + info->shift;
01084     if (0 && (*mp == info) && !info->next)
01085        return;
01086 
01087     /* Find & remove this page in the queue */
01088     while (*mp != info) {
01089        mp = &((*mp)->next);
01090 #ifdef EXTRA_SANITY
01091        if (!*mp) {
01092               TRACE(("%6d !q %p\n",malloc_event++,info));
01093               wrterror("Not on queue\n");
01094        }
01095 #endif
01096     }
01097     *mp = info->next;
01098 
01099     /* Free the page & the info structure if need be */
01100     set_pgdir(info->page,MALLOC_FIRST);
01101     if((void*)info->page == (void*)info) {
01102        _PR_UnlockedFree(info->page);
01103     } else {
01104        vp = info->page;
01105        _PR_UnlockedFree(info);
01106        _PR_UnlockedFree(vp);
01107     }
01108 }
01109 
01110 void _PR_UnlockedFree(void *ptr)
01111 {
01112     u_long page;
01113     struct pginfo *info;
01114     int index, tmp_index;
01115 
01116     TRACE(("%6d F %p\n",malloc_event++,ptr));
01117     /* This is legal */
01118     if (!ptr)
01119        return;
01120 
01121 #ifdef SANITY
01122     /* There wouldn't be anything to free */
01123     if (!initialized) {
01124        wrtwarning("free() called before malloc() ever got called\n");
01125        return;
01126     }
01127 #endif
01128 
01129 #ifdef SANITY
01130     if (suicide)
01131        PR_Abort();
01132 #endif
01133 
01134     /* Find the page directory entry for the page in question */
01135     page = (u_long)ptr >> malloc_pageshift;
01136     index = page - malloc_origo;
01137 
01138     /*
01139      * check if memory was allocated by memalign
01140      */
01141     tmp_index = index;
01142     while (page_dir[tmp_index] == MALLOC_FOLLOW)
01143        tmp_index--;
01144     if (tmp_index != index) {
01145        /*
01146         * memalign-allocated memory
01147         */
01148        index = tmp_index;
01149        page = index + malloc_origo;                     
01150        ptr = (void *) (page << malloc_pageshift);
01151     }
01152     /* make sure it makes sense in some fashion */
01153     if (index < malloc_pageshift) {
01154 #ifdef SANITY
01155        wrtwarning("junk pointer %p (low) passed to free()\n", ptr);
01156 #endif
01157        return;
01158     }
01159     if (index > last_index) {
01160 #ifdef SANITY
01161        wrtwarning("junk pointer %p (high) passed to free()\n", ptr);
01162 #endif
01163        return;
01164     }
01165 
01166     /* handle as page-allocation or chunk allocation */
01167     info = page_dir[index];
01168     if (info < MALLOC_MAGIC)
01169         free_pages((char*)ptr, page, index, info);
01170     else 
01171        free_bytes(ptr,page,index,info);
01172     return;
01173 }
01174 #endif /* _PR_OVERRIDE_MALLOC */