Back to index

lightning-sunbird  0.9+nobinonly
mallocx.c
Go to the documentation of this file.
00001 /*
00002  * Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
00003  * Copyright (c) 1991-1994 by Xerox Corporation.  All rights reserved.
00004  * Copyright (c) 1996 by Silicon Graphics.  All rights reserved.
00005  *
00006  * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
00007  * OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
00008  *
00009  * Permission is hereby granted to use or copy this program
00010  * for any purpose,  provided the above notices are retained on all copies.
00011  * Permission to modify the code and to distribute modified code is granted,
00012  * provided the above notices are retained, and a notice that the code was
00013  * modified is included with the above copyright notice.
00014  */
00015 
00016 /*
00017  * These are extra allocation routines which are likely to be less
00018  * frequently used than those in malloc.c.  They are separate in the
00019  * hope that the .o file will be excluded from statically linked
00020  * executables.  We should probably break this up further.
00021  */
00022 
00023 #include <stdio.h>
00024 #include "gc_priv.h"
00025 
00026 extern ptr_t GC_clear_stack();  /* in misc.c, behaves like identity */
00027 void GC_extend_size_map();      /* in misc.c. */
00028 GC_bool GC_alloc_reclaim_list();   /* in malloc.c */
00029 
00030 /* Some externally visible but unadvertised variables to allow access to */
00031 /* free lists from inlined allocators without including gc_priv.h      */
00032 /* or introducing dependencies on internal data structure layouts.     */
00033 ptr_t * CONST GC_objfreelist_ptr = GC_objfreelist;
00034 ptr_t * CONST GC_aobjfreelist_ptr = GC_aobjfreelist;
00035 ptr_t * CONST GC_uobjfreelist_ptr = GC_uobjfreelist;
00036 # ifdef ATOMIC_UNCOLLECTABLE
00037     ptr_t * CONST GC_auobjfreelist_ptr = GC_auobjfreelist;
00038 # endif
00039 
00040 /* Allocate a composite object of size n bytes.  The caller guarantees  */
00041 /* that pointers past the first page are not relevant.  Caller holds    */
00042 /* allocation lock.                                                     */
00043 ptr_t GC_generic_malloc_inner_ignore_off_page(lb, k)
00044 register size_t lb;
00045 register int k;
00046 {
00047     register struct hblk * h;
00048     register word n_blocks;
00049     register word lw;
00050     register ptr_t op;
00051 
00052     if (lb <= HBLKSIZE)
00053         return(GC_generic_malloc_inner((word)lb, k));
00054     n_blocks = divHBLKSZ(ADD_SLOP(lb) + HDR_BYTES + HBLKSIZE-1);
00055     if (!GC_is_initialized) GC_init_inner();
00056     /* Do our share of marking work */
00057     if(GC_incremental && !GC_dont_gc)
00058         GC_collect_a_little_inner((int)n_blocks);
00059     lw = ROUNDED_UP_WORDS(lb);
00060     while ((h = GC_allochblk(lw, k, IGNORE_OFF_PAGE)) == 0
00061            && GC_collect_or_expand(n_blocks, TRUE));
00062     if (h == 0) {
00063         op = 0;
00064     } else {
00065         op = (ptr_t) (h -> hb_body);
00066         GC_words_wasted += BYTES_TO_WORDS(n_blocks * HBLKSIZE) - lw;
00067     }
00068     GC_words_allocd += lw;
00069     return((ptr_t)op);
00070 }
00071 
00072 ptr_t GC_generic_malloc_ignore_off_page(lb, k)
00073 register size_t lb;
00074 register int k;
00075 {
00076     register ptr_t result;
00077     DCL_LOCK_STATE;
00078     
00079     GC_INVOKE_FINALIZERS();
00080     DISABLE_SIGNALS();
00081     LOCK();
00082     result = GC_generic_malloc_inner_ignore_off_page(lb,k);
00083     UNLOCK();
00084     ENABLE_SIGNALS();
00085     if (0 == result) {
00086         return((*GC_oom_fn)(lb));
00087     } else {
00088         return(result);
00089     }
00090 }
00091 
00092 # if defined(__STDC__) || defined(__cplusplus)
00093   void * GC_malloc_ignore_off_page(size_t lb)
00094 # else
00095   char * GC_malloc_ignore_off_page(lb)
00096   register size_t lb;
00097 # endif
00098 {
00099     return((GC_PTR)GC_generic_malloc_ignore_off_page(lb, NORMAL));
00100 }
00101 
00102 # if defined(__STDC__) || defined(__cplusplus)
00103   void * GC_malloc_atomic_ignore_off_page(size_t lb)
00104 # else
00105   char * GC_malloc_atomic_ignore_off_page(lb)
00106   register size_t lb;
00107 # endif
00108 {
00109     return((GC_PTR)GC_generic_malloc_ignore_off_page(lb, PTRFREE));
00110 }
00111 
00112 /* Increment GC_words_allocd from code that doesn't have direct access       */
00113 /* to GC_arrays.                                               */
00114 # ifdef __STDC__
00115 void GC_incr_words_allocd(size_t n)
00116 {
00117     GC_words_allocd += n;
00118 }
00119 
00120 /* The same for GC_mem_freed.                           */
00121 void GC_incr_mem_freed(size_t n)
00122 {
00123     GC_mem_freed += n;
00124 }
00125 # endif /* __STDC__ */
00126 
00127 /* Analogous to the above, but assumes a small object size, and       */
00128 /* bypasses MERGE_SIZES mechanism.  Used by gc_inline.h.              */
00129 #ifdef __STDC__
00130      ptr_t GC_generic_malloc_words_small(size_t lw, int k)
00131 #else 
00132      ptr_t GC_generic_malloc_words_small(lw, k)
00133      register word lw;
00134      register int k;
00135 #endif
00136 {
00137 register ptr_t op;
00138 register ptr_t *opp;
00139 register struct obj_kind * kind = GC_obj_kinds + k;
00140 DCL_LOCK_STATE;
00141 
00142     GC_INVOKE_FINALIZERS();
00143     DISABLE_SIGNALS();
00144     LOCK();
00145     opp = &(kind -> ok_freelist[lw]);
00146     if( (op = *opp) == 0 ) {
00147         if (!GC_is_initialized) {
00148             GC_init_inner();
00149         }
00150        if (kind -> ok_reclaim_list != 0 || GC_alloc_reclaim_list(kind)) {
00151            op = GC_clear_stack(GC_allocobj((word)lw, k));
00152        }
00153        if (op == 0) {
00154            UNLOCK();
00155            ENABLE_SIGNALS();
00156            return ((*GC_oom_fn)(WORDS_TO_BYTES(lw)));
00157        }
00158     }
00159     *opp = obj_link(op);
00160     obj_link(op) = 0;
00161     GC_words_allocd += lw;
00162     UNLOCK();
00163     ENABLE_SIGNALS();
00164     return((ptr_t)op);
00165 }
00166 
00167 #if defined(THREADS) && !defined(SRC_M3)
00168 /* Return a list of 1 or more objects of the indicated size, linked   */
00169 /* through the first word in the object.  This has the advantage that */
00170 /* it acquires the allocation lock only once, and may greatly reduce  */
00171 /* time wasted contending for the allocation lock.  Typical usage would */
00172 /* be in a thread that requires many items of the same size.  It would       */
00173 /* keep its own free list in thread-local storage, and call           */
00174 /* GC_malloc_many or friends to replenish it.  (We do not round up    */
00175 /* object sizes, since a call indicates the intention to consume many */
00176 /* objects of exactly this size.)                              */
00177 /* Note that the client should usually clear the link field.          */
00178 ptr_t GC_generic_malloc_many(lb, k)
00179 register word lb;
00180 register int k;
00181 {
00182 ptr_t op;
00183 register ptr_t p;
00184 ptr_t *opp;
00185 word lw;
00186 register word my_words_allocd;
00187 DCL_LOCK_STATE;
00188 
00189     if (!SMALL_OBJ(lb)) {
00190         op = GC_generic_malloc(lb, k);
00191         if(0 != op) obj_link(op) = 0;
00192         return(op);
00193     }
00194     lw = ALIGNED_WORDS(lb);
00195     GC_INVOKE_FINALIZERS();
00196     DISABLE_SIGNALS();
00197     LOCK();
00198     opp = &(GC_obj_kinds[k].ok_freelist[lw]);
00199     if( (op = *opp) == 0 ) {
00200         if (!GC_is_initialized) {
00201             GC_init_inner();
00202         }
00203        op = GC_clear_stack(GC_allocobj(lw, k));
00204        if (op == 0) {
00205            UNLOCK();
00206            ENABLE_SIGNALS();
00207            op = (*GC_oom_fn)(lb);
00208            if(0 != op) obj_link(op) = 0;
00209             return(op);
00210        }
00211     }
00212     *opp = 0;
00213     my_words_allocd = 0;
00214     for (p = op; p != 0; p = obj_link(p)) {
00215         my_words_allocd += lw;
00216         if (my_words_allocd >= BODY_SZ) {
00217             *opp = obj_link(p);
00218             obj_link(p) = 0;
00219             break;
00220         }
00221     }
00222     GC_words_allocd += my_words_allocd;
00223     
00224 out:
00225     UNLOCK();
00226     ENABLE_SIGNALS();
00227     return(op);
00228 
00229 }
00230 
00231 GC_PTR GC_malloc_many(size_t lb)
00232 {
00233     return(GC_generic_malloc_many(lb, NORMAL));
00234 }
00235 
00236 /* Note that the "atomic" version of this would be unsafe, since the  */
00237 /* links would not be seen by the collector.                          */
00238 # endif
00239 
00240 /* Allocate lb bytes of pointerful, traced, but not collectable data */
00241 # ifdef __STDC__
00242     GC_PTR GC_malloc_uncollectable(size_t lb)
00243 # else
00244     GC_PTR GC_malloc_uncollectable(lb)
00245     size_t lb;
00246 # endif
00247 {
00248 register ptr_t op;
00249 register ptr_t *opp;
00250 register word lw;
00251 DCL_LOCK_STATE;
00252 
00253     if( SMALL_OBJ(lb) ) {
00254 #       ifdef MERGE_SIZES
00255 #        ifdef ADD_BYTE_AT_END
00256            if (lb != 0) lb--;
00257                 /* We don't need the extra byte, since this won't be  */
00258                 /* collected anyway.                                  */
00259 #        endif
00260          lw = GC_size_map[lb];
00261 #      else
00262          lw = ALIGNED_WORDS(lb);
00263 #       endif
00264        opp = &(GC_uobjfreelist[lw]);
00265        FASTLOCK();
00266         if( FASTLOCK_SUCCEEDED() && (op = *opp) != 0 ) {
00267             /* See above comment on signals.     */
00268             *opp = obj_link(op);
00269             obj_link(op) = 0;
00270             GC_words_allocd += lw;
00271             /* Mark bit ws already set on free list.  It will be      */
00272            /* cleared only temporarily during a collection, as a      */
00273            /* result of the normal free list mark bit clearing.       */
00274             GC_non_gc_bytes += WORDS_TO_BYTES(lw);
00275             FASTUNLOCK();
00276             return((GC_PTR) op);
00277         }
00278         FASTUNLOCK();
00279         op = (ptr_t)GC_generic_malloc((word)lb, UNCOLLECTABLE);
00280     } else {
00281        op = (ptr_t)GC_generic_malloc((word)lb, UNCOLLECTABLE);
00282     }
00283     if (0 == op) return(0);
00284     /* We don't need the lock here, since we have an undisguised      */
00285     /* pointer.  We do need to hold the lock while we adjust          */
00286     /* mark bits.                                              */
00287     {
00288        register struct hblk * h;
00289        
00290        h = HBLKPTR(op);
00291        lw = HDR(h) -> hb_sz;
00292        
00293        DISABLE_SIGNALS();
00294        LOCK();
00295        GC_set_mark_bit(op);
00296        GC_non_gc_bytes += WORDS_TO_BYTES(lw);
00297        UNLOCK();
00298        ENABLE_SIGNALS();
00299        return((GC_PTR) op);
00300     }
00301 }
00302 
00303 # ifdef ATOMIC_UNCOLLECTABLE
00304 /* Allocate lb bytes of pointerfree, untraced, uncollectable data     */
00305 /* This is normally roughly equivalent to the system malloc.          */
00306 /* But it may be useful if malloc is redefined.                       */
00307 # ifdef __STDC__
00308     GC_PTR GC_malloc_atomic_uncollectable(size_t lb)
00309 # else
00310     GC_PTR GC_malloc_atomic_uncollectable(lb)
00311     size_t lb;
00312 # endif
00313 {
00314 register ptr_t op;
00315 register ptr_t *opp;
00316 register word lw;
00317 DCL_LOCK_STATE;
00318 
00319     if( SMALL_OBJ(lb) ) {
00320 #       ifdef MERGE_SIZES
00321 #        ifdef ADD_BYTE_AT_END
00322            if (lb != 0) lb--;
00323                 /* We don't need the extra byte, since this won't be  */
00324                 /* collected anyway.                                  */
00325 #        endif
00326          lw = GC_size_map[lb];
00327 #      else
00328          lw = ALIGNED_WORDS(lb);
00329 #       endif
00330        opp = &(GC_auobjfreelist[lw]);
00331        FASTLOCK();
00332         if( FASTLOCK_SUCCEEDED() && (op = *opp) != 0 ) {
00333             /* See above comment on signals.     */
00334             *opp = obj_link(op);
00335             obj_link(op) = 0;
00336             GC_words_allocd += lw;
00337            /* Mark bit was already set while object was on free list. */
00338             GC_non_gc_bytes += WORDS_TO_BYTES(lw);
00339             FASTUNLOCK();
00340             return((GC_PTR) op);
00341         }
00342         FASTUNLOCK();
00343         op = (ptr_t)GC_generic_malloc((word)lb, AUNCOLLECTABLE);
00344     } else {
00345        op = (ptr_t)GC_generic_malloc((word)lb, AUNCOLLECTABLE);
00346     }
00347     if (0 == op) return(0);
00348     /* We don't need the lock here, since we have an undisguised      */
00349     /* pointer.  We do need to hold the lock while we adjust          */
00350     /* mark bits.                                              */
00351     {
00352        register struct hblk * h;
00353        
00354        h = HBLKPTR(op);
00355        lw = HDR(h) -> hb_sz;
00356        
00357        DISABLE_SIGNALS();
00358        LOCK();
00359        GC_set_mark_bit(op);
00360        GC_non_gc_bytes += WORDS_TO_BYTES(lw);
00361        UNLOCK();
00362        ENABLE_SIGNALS();
00363        return((GC_PTR) op);
00364     }
00365 }
00366 
00367 #endif /* ATOMIC_UNCOLLECTABLE */