Annotation of gcc/obstack.c, revision 1.1.1.1

1.1       root        1: /* obstack.c - subroutines used implicitly by object stack macros
                      2:    Copyright (C) 1988 Free Software Foundation, Inc.
                      3: 
                      4: This program is free software; you can redistribute it and/or modify it
                      5: under the terms of the GNU General Public License as published by the
                      6: Free Software Foundation; either version 2, or (at your option) any
                      7: later version.
                      8: 
                      9: This program is distributed in the hope that it will be useful,
                     10: but WITHOUT ANY WARRANTY; without even the implied warranty of
                     11: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
                     12: GNU General Public License for more details.
                     13: 
                     14: You should have received a copy of the GNU General Public License
                     15: along with this program; if not, write to the Free Software
                     16: Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
                     17: 
                     18: #include "obstack.h"
                     19: 
                     20: #ifdef __STDC__
                     21: #define POINTER void *
                     22: #else
                     23: #define POINTER char *
                     24: #endif
                     25: 
                     26: /* Determine default alignment.  */
                     27: struct fooalign {char x; double d;};
                     28: #define DEFAULT_ALIGNMENT ((char *)&((struct fooalign *) 0)->d - (char *)0)
                     29: /* If malloc were really smart, it would round addresses to DEFAULT_ALIGNMENT.
                     30:    But in fact it might be less smart and round addresses to as much as
                     31:    DEFAULT_ROUNDING.  So we prepare for it to do that.  */
                     32: union fooround {long x; double d;};
                     33: #define DEFAULT_ROUNDING (sizeof (union fooround))
                     34: 
                     35: /* When we copy a long block of data, this is the unit to do it with.
                     36:    On some machines, copying successive ints does not work;
                     37:    in such a case, redefine COPYING_UNIT to `long' (if that works)
                     38:    or `char' as a last resort.  */
                     39: #ifndef COPYING_UNIT
                     40: #define COPYING_UNIT int
                     41: #endif
                     42: 
                     43: /* The non-GNU-C macros copy the obstack into this global variable
                     44:    to avoid multiple evaluation.  */
                     45: 
                     46: struct obstack *_obstack;
                     47: 
                     48: /* Initialize an obstack H for use.  Specify chunk size SIZE (0 means default).
                     49:    Objects start on multiples of ALIGNMENT (0 means use default).
                     50:    CHUNKFUN is the function to use to allocate chunks,
                     51:    and FREEFUN the function to free them.  */
                     52: 
                     53: void
                     54: _obstack_begin (h, size, alignment, chunkfun, freefun)
                     55:      struct obstack *h;
                     56:      int size;
                     57:      int alignment;
                     58:      POINTER (*chunkfun) ();
                     59:      void (*freefun) ();
                     60: {
                     61:   register struct _obstack_chunk* chunk; /* points to new chunk */
                     62: 
                     63:   if (alignment == 0)
                     64:     alignment = DEFAULT_ALIGNMENT;
                     65:   if (size == 0)
                     66:     /* Default size is what GNU malloc can fit in a 4096-byte block.  */
                     67:     {
                     68:       /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
                     69:         Use the values for range checking, because if range checking is off,
                     70:         the extra bytes won't be missed terribly, but if range checking is on
                     71:         and we used a larger request, a whole extra 4096 bytes would be
                     72:         allocated.
                     73: 
                     74:         These number are irrelevant to the new GNU malloc.  I suspect it is
                     75:         less sensitive to the size of the request.  */
                     76:       int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1))
                     77:                    + 4 + DEFAULT_ROUNDING - 1)
                     78:                   & ~(DEFAULT_ROUNDING - 1));
                     79:       size = 4096 - extra;
                     80:     }
                     81: 
                     82:   h->chunkfun = (struct _obstack_chunk * (*)()) chunkfun;
                     83:   h->freefun = freefun;
                     84:   h->chunk_size = size;
                     85:   h->alignment_mask = alignment - 1;
                     86: 
                     87:   chunk        = h->chunk = (*h->chunkfun) (h->chunk_size);
                     88:   h->next_free = h->object_base = chunk->contents;
                     89:   h->chunk_limit = chunk->limit
                     90:     = (char *) chunk + h->chunk_size;
                     91:   chunk->prev = 0;
                     92:   /* The initial chunk now contains no empty object.  */
                     93:   h->maybe_empty_object = 0;
                     94: }
                     95: 
                     96: /* Allocate a new current chunk for the obstack *H
                     97:    on the assumption that LENGTH bytes need to be added
                     98:    to the current object, or a new object of length LENGTH allocated.
                     99:    Copies any partial object from the end of the old chunk
                    100:    to the beginning of the new one.  */
                    101: 
                    102: void
                    103: _obstack_newchunk (h, length)
                    104:      struct obstack *h;
                    105:      int length;
                    106: {
                    107:   register struct _obstack_chunk*      old_chunk = h->chunk;
                    108:   register struct _obstack_chunk*      new_chunk;
                    109:   register long        new_size;
                    110:   register int obj_size = h->next_free - h->object_base;
                    111:   register int i;
                    112:   int already;
                    113: 
                    114:   /* Compute size for new chunk.  */
                    115:   new_size = (obj_size + length) + (obj_size >> 3) + 100;
                    116:   if (new_size < h->chunk_size)
                    117:     new_size = h->chunk_size;
                    118: 
                    119:   /* Allocate and initialize the new chunk.  */
                    120:   new_chunk = h->chunk = (*h->chunkfun) (new_size);
                    121:   new_chunk->prev = old_chunk;
                    122:   new_chunk->limit = h->chunk_limit = (char *) new_chunk + new_size;
                    123: 
                    124:   /* Move the existing object to the new chunk.
                    125:      Word at a time is fast and is safe if the object
                    126:      is sufficiently aligned.  */
                    127:   if (h->alignment_mask + 1 >= DEFAULT_ALIGNMENT)
                    128:     {
                    129:       for (i = obj_size / sizeof (COPYING_UNIT) - 1;
                    130:           i >= 0; i--)
                    131:        ((COPYING_UNIT *)new_chunk->contents)[i]
                    132:          = ((COPYING_UNIT *)h->object_base)[i];
                    133:       /* We used to copy the odd few remaining bytes as one extra COPYING_UNIT,
                    134:         but that can cross a page boundary on a machine
                    135:         which does not do strict alignment for COPYING_UNITS.  */
                    136:       already = obj_size / sizeof (COPYING_UNIT) * sizeof (COPYING_UNIT);
                    137:     }
                    138:   else
                    139:     already = 0;
                    140:   /* Copy remaining bytes one by one.  */
                    141:   for (i = already; i < obj_size; i++)
                    142:     new_chunk->contents[i] = h->object_base[i];
                    143: 
                    144:   /* If the object just copied was the only data in OLD_CHUNK,
                    145:      free that chunk and remove it from the chain.
                    146:      But not if that chunk might contain an empty object.  */
                    147:   if (h->object_base == old_chunk->contents && ! h->maybe_empty_object)
                    148:     {
                    149:       new_chunk->prev = old_chunk->prev;
                    150:       (*h->freefun) (old_chunk);
                    151:     }
                    152: 
                    153:   h->object_base = new_chunk->contents;
                    154:   h->next_free = h->object_base + obj_size;
                    155:   /* The new chunk certainly contains no empty object yet.  */
                    156:   h->maybe_empty_object = 0;
                    157: }
                    158: 
                    159: /* Return nonzero if object OBJ has been allocated from obstack H.
                    160:    This is here for debugging.
                    161:    If you use it in a program, you are probably losing.  */
                    162: 
                    163: int
                    164: _obstack_allocated_p (h, obj)
                    165:      struct obstack *h;
                    166:      POINTER obj;
                    167: {
                    168:   register struct _obstack_chunk*  lp; /* below addr of any objects in this chunk */
                    169:   register struct _obstack_chunk*  plp;        /* point to previous chunk if any */
                    170: 
                    171:   lp = (h)->chunk;
                    172:   /* We use >= rather than > since the object cannot be exactly at
                    173:      the beginning of the chunk but might be an empty object exactly
                    174:      at the end of an adjacent chunk. */
                    175:   while (lp != 0 && ((POINTER)lp >= obj || (POINTER)(lp)->limit < obj))
                    176:     {
                    177:       plp = lp->prev;
                    178:       lp = plp;
                    179:     }
                    180:   return lp != 0;
                    181: }
                    182: 
                    183: /* Free objects in obstack H, including OBJ and everything allocate
                    184:    more recently than OBJ.  If OBJ is zero, free everything in H.  */
                    185: 
                    186: #undef obstack_free
                    187: 
                    188: /* This function has two names with identical definitions.
                    189:    This is the first one, called from non-ANSI code.  */
                    190: 
                    191: void
                    192: _obstack_free (h, obj)
                    193:      struct obstack *h;
                    194:      POINTER obj;
                    195: {
                    196:   register struct _obstack_chunk*  lp; /* below addr of any objects in this chunk */
                    197:   register struct _obstack_chunk*  plp;        /* point to previous chunk if any */
                    198: 
                    199:   lp = h->chunk;
                    200:   /* We use >= because there cannot be an object at the beginning of a chunk.
                    201:      But there can be an empty object at that address
                    202:      at the end of another chunk.  */
                    203:   while (lp != 0 && ((POINTER)lp >= obj || (POINTER)(lp)->limit < obj))
                    204:     {
                    205:       plp = lp->prev;
                    206:       (*h->freefun) (lp);
                    207:       lp = plp;
                    208:       /* If we switch chunks, we can't tell whether the new current
                    209:         chunk contains an empty object, so assume that it may.  */
                    210:       h->maybe_empty_object = 1;
                    211:     }
                    212:   if (lp)
                    213:     {
                    214:       h->object_base = h->next_free = (char *)(obj);
                    215:       h->chunk_limit = lp->limit;
                    216:       h->chunk = lp;
                    217:     }
                    218:   else if (obj != 0)
                    219:     /* obj is not in any of the chunks! */
                    220:     abort ();
                    221: }
                    222: 
                    223: /* This function is used from ANSI code.  */
                    224: 
                    225: void
                    226: obstack_free (h, obj)
                    227:      struct obstack *h;
                    228:      POINTER obj;
                    229: {
                    230:   register struct _obstack_chunk*  lp; /* below addr of any objects in this chunk */
                    231:   register struct _obstack_chunk*  plp;        /* point to previous chunk if any */
                    232: 
                    233:   lp = h->chunk;
                    234:   /* We use >= because there cannot be an object at the beginning of a chunk.
                    235:      But there can be an empty object at that address
                    236:      at the end of another chunk.  */
                    237:   while (lp != 0 && ((POINTER)lp >= obj || (POINTER)(lp)->limit < obj))
                    238:     {
                    239:       plp = lp->prev;
                    240:       (*h->freefun) (lp);
                    241:       lp = plp;
                    242:       /* If we switch chunks, we can't tell whether the new current
                    243:         chunk contains an empty object, so assume that it may.  */
                    244:       h->maybe_empty_object = 1;
                    245:     }
                    246:   if (lp)
                    247:     {
                    248:       h->object_base = h->next_free = (char *)(obj);
                    249:       h->chunk_limit = lp->limit;
                    250:       h->chunk = lp;
                    251:     }
                    252:   else if (obj != 0)
                    253:     /* obj is not in any of the chunks! */
                    254:     abort ();
                    255: }
                    256: 
                    257: #if 0
                    258: /* These are now turned off because the applications do not use it
                    259:    and it uses bcopy via obstack_grow, which causes trouble on sysV.  */
                    260: 
                    261: /* Now define the functional versions of the obstack macros.
                    262:    Define them to simply use the corresponding macros to do the job.  */
                    263: 
                    264: #ifdef __STDC__
                    265: /* These function definitions do not work with non-ANSI preprocessors;
                    266:    they won't pass through the macro names in parentheses.  */
                    267: 
                    268: /* The function names appear in parentheses in order to prevent
                    269:    the macro-definitions of the names from being expanded there.  */
                    270: 
                    271: POINTER (obstack_base) (obstack)
                    272:      struct obstack *obstack;
                    273: {
                    274:   return obstack_base (obstack);
                    275: }
                    276: 
                    277: POINTER (obstack_next_free) (obstack)
                    278:      struct obstack *obstack;
                    279: {
                    280:   return obstack_next_free (obstack);
                    281: }
                    282: 
                    283: int (obstack_object_size) (obstack)
                    284:      struct obstack *obstack;
                    285: {
                    286:   return obstack_object_size (obstack);
                    287: }
                    288: 
                    289: int (obstack_room) (obstack)
                    290:      struct obstack *obstack;
                    291: {
                    292:   return obstack_room (obstack);
                    293: }
                    294: 
                    295: void (obstack_grow) (obstack, pointer, length)
                    296:      struct obstack *obstack;
                    297:      POINTER pointer;
                    298:      int length;
                    299: {
                    300:   obstack_grow (obstack, pointer, length);
                    301: }
                    302: 
                    303: void (obstack_grow0) (obstack, pointer, length)
                    304:      struct obstack *obstack;
                    305:      POINTER pointer;
                    306:      int length;
                    307: {
                    308:   obstack_grow0 (obstack, pointer, length);
                    309: }
                    310: 
                    311: void (obstack_1grow) (obstack, character)
                    312:      struct obstack *obstack;
                    313:      int character;
                    314: {
                    315:   obstack_1grow (obstack, character);
                    316: }
                    317: 
                    318: void (obstack_blank) (obstack, length)
                    319:      struct obstack *obstack;
                    320:      int length;
                    321: {
                    322:   obstack_blank (obstack, length);
                    323: }
                    324: 
                    325: void (obstack_1grow_fast) (obstack, character)
                    326:      struct obstack *obstack;
                    327:      int character;
                    328: {
                    329:   obstack_1grow_fast (obstack, character);
                    330: }
                    331: 
                    332: void (obstack_blank_fast) (obstack, length)
                    333:      struct obstack *obstack;
                    334:      int length;
                    335: {
                    336:   obstack_blank_fast (obstack, length);
                    337: }
                    338: 
                    339: POINTER (obstack_finish) (obstack)
                    340:      struct obstack *obstack;
                    341: {
                    342:   return obstack_finish (obstack);
                    343: }
                    344: 
                    345: POINTER (obstack_alloc) (obstack, length)
                    346:      struct obstack *obstack;
                    347:      int length;
                    348: {
                    349:   return obstack_alloc (obstack, length);
                    350: }
                    351: 
                    352: POINTER (obstack_copy) (obstack, pointer, length)
                    353:      struct obstack *obstack;
                    354:      POINTER pointer;
                    355:      int length;
                    356: {
                    357:   return obstack_copy (obstack, pointer, length);
                    358: }
                    359: 
                    360: POINTER (obstack_copy0) (obstack, pointer, length)
                    361:      struct obstack *obstack;
                    362:      POINTER pointer;
                    363:      int length;
                    364: {
                    365:   return obstack_copy0 (obstack, pointer, length);
                    366: }
                    367: 
                    368: #endif /* __STDC__ */
                    369: 
                    370: #endif /* 0 */

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.