Annotation of gcc/obstack.c, revision 1.1.1.2

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;};
1.1.1.2 ! root       28: #define DEFAULT_ALIGNMENT  \
        !            29:   ((PTR_INT_TYPE) ((char *)&((struct fooalign *) 0)->d - (char *)0))
1.1       root       30: /* If malloc were really smart, it would round addresses to DEFAULT_ALIGNMENT.
                     31:    But in fact it might be less smart and round addresses to as much as
                     32:    DEFAULT_ROUNDING.  So we prepare for it to do that.  */
                     33: union fooround {long x; double d;};
                     34: #define DEFAULT_ROUNDING (sizeof (union fooround))
                     35: 
                     36: /* When we copy a long block of data, this is the unit to do it with.
                     37:    On some machines, copying successive ints does not work;
                     38:    in such a case, redefine COPYING_UNIT to `long' (if that works)
                     39:    or `char' as a last resort.  */
                     40: #ifndef COPYING_UNIT
                     41: #define COPYING_UNIT int
                     42: #endif
                     43: 
                     44: /* The non-GNU-C macros copy the obstack into this global variable
                     45:    to avoid multiple evaluation.  */
                     46: 
                     47: struct obstack *_obstack;
1.1.1.2 ! root       48: 
        !            49: /* Define a macro that either calls functions with the traditional malloc/free
        !            50:    calling interface, or calls functions with the mmalloc/mfree interface
        !            51:    (that adds an extra first argument), based on the state of use_extra_arg.
        !            52:    For free, do not use ?:, since some compilers, like the MIPS compilers,
        !            53:    do not allow (expr) ? void : void.  */
        !            54: 
        !            55: #define CALL_CHUNKFUN(h, size) \
        !            56:   (((h) -> use_extra_arg) \
        !            57:    ? (*(h)->chunkfun) ((h)->extra_arg, (size)) \
        !            58:    : (*(h)->chunkfun) ((size)))
        !            59: 
        !            60: #define CALL_FREEFUN(h, old_chunk) \
        !            61:   do { \
        !            62:     if ((h) -> use_extra_arg) \
        !            63:       (*(h)->freefun) ((h)->extra_arg, (old_chunk)); \
        !            64:     else \
        !            65:       (*(h)->freefun) ((old_chunk)); \
        !            66:   } while (0)
        !            67: 
1.1       root       68: 
                     69: /* Initialize an obstack H for use.  Specify chunk size SIZE (0 means default).
                     70:    Objects start on multiples of ALIGNMENT (0 means use default).
                     71:    CHUNKFUN is the function to use to allocate chunks,
                     72:    and FREEFUN the function to free them.  */
                     73: 
                     74: void
                     75: _obstack_begin (h, size, alignment, chunkfun, freefun)
                     76:      struct obstack *h;
                     77:      int size;
                     78:      int alignment;
                     79:      POINTER (*chunkfun) ();
                     80:      void (*freefun) ();
                     81: {
                     82:   register struct _obstack_chunk* chunk; /* points to new chunk */
                     83: 
                     84:   if (alignment == 0)
                     85:     alignment = DEFAULT_ALIGNMENT;
                     86:   if (size == 0)
                     87:     /* Default size is what GNU malloc can fit in a 4096-byte block.  */
                     88:     {
                     89:       /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
                     90:         Use the values for range checking, because if range checking is off,
                     91:         the extra bytes won't be missed terribly, but if range checking is on
                     92:         and we used a larger request, a whole extra 4096 bytes would be
                     93:         allocated.
                     94: 
                     95:         These number are irrelevant to the new GNU malloc.  I suspect it is
                     96:         less sensitive to the size of the request.  */
                     97:       int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1))
                     98:                    + 4 + DEFAULT_ROUNDING - 1)
                     99:                   & ~(DEFAULT_ROUNDING - 1));
                    100:       size = 4096 - extra;
                    101:     }
                    102: 
                    103:   h->chunkfun = (struct _obstack_chunk * (*)()) chunkfun;
                    104:   h->freefun = freefun;
                    105:   h->chunk_size = size;
                    106:   h->alignment_mask = alignment - 1;
1.1.1.2 ! root      107:   h->use_extra_arg = 0;
        !           108: 
        !           109:   chunk = h->chunk = CALL_CHUNKFUN (h, h -> chunk_size);
        !           110:   h->next_free = h->object_base = chunk->contents;
        !           111:   h->chunk_limit = chunk->limit
        !           112:     = (char *) chunk + h->chunk_size;
        !           113:   chunk->prev = 0;
        !           114:   /* The initial chunk now contains no empty object.  */
        !           115:   h->maybe_empty_object = 0;
        !           116: }
        !           117: 
        !           118: void
        !           119: _obstack_begin_1 (h, size, alignment, chunkfun, freefun, arg)
        !           120:      struct obstack *h;
        !           121:      int size;
        !           122:      int alignment;
        !           123:      POINTER (*chunkfun) ();
        !           124:      void (*freefun) ();
        !           125:      POINTER arg;
        !           126: {
        !           127:   register struct _obstack_chunk* chunk; /* points to new chunk */
        !           128: 
        !           129:   if (alignment == 0)
        !           130:     alignment = DEFAULT_ALIGNMENT;
        !           131:   if (size == 0)
        !           132:     /* Default size is what GNU malloc can fit in a 4096-byte block.  */
        !           133:     {
        !           134:       /* 12 is sizeof (mhead) and 4 is EXTRA from GNU malloc.
        !           135:         Use the values for range checking, because if range checking is off,
        !           136:         the extra bytes won't be missed terribly, but if range checking is on
        !           137:         and we used a larger request, a whole extra 4096 bytes would be
        !           138:         allocated.
        !           139: 
        !           140:         These number are irrelevant to the new GNU malloc.  I suspect it is
        !           141:         less sensitive to the size of the request.  */
        !           142:       int extra = ((((12 + DEFAULT_ROUNDING - 1) & ~(DEFAULT_ROUNDING - 1))
        !           143:                    + 4 + DEFAULT_ROUNDING - 1)
        !           144:                   & ~(DEFAULT_ROUNDING - 1));
        !           145:       size = 4096 - extra;
        !           146:     }
        !           147: 
        !           148:   h->chunkfun = (struct _obstack_chunk * (*)()) chunkfun;
        !           149:   h->freefun = freefun;
        !           150:   h->chunk_size = size;
        !           151:   h->alignment_mask = alignment - 1;
        !           152:   h->extra_arg = arg;
        !           153:   h->use_extra_arg = 1;
1.1       root      154: 
1.1.1.2 ! root      155:   chunk = h->chunk = CALL_CHUNKFUN (h, h -> chunk_size);
1.1       root      156:   h->next_free = h->object_base = chunk->contents;
                    157:   h->chunk_limit = chunk->limit
                    158:     = (char *) chunk + h->chunk_size;
                    159:   chunk->prev = 0;
                    160:   /* The initial chunk now contains no empty object.  */
                    161:   h->maybe_empty_object = 0;
                    162: }
                    163: 
                    164: /* Allocate a new current chunk for the obstack *H
                    165:    on the assumption that LENGTH bytes need to be added
                    166:    to the current object, or a new object of length LENGTH allocated.
                    167:    Copies any partial object from the end of the old chunk
                    168:    to the beginning of the new one.  */
                    169: 
                    170: void
                    171: _obstack_newchunk (h, length)
                    172:      struct obstack *h;
                    173:      int length;
                    174: {
                    175:   register struct _obstack_chunk*      old_chunk = h->chunk;
                    176:   register struct _obstack_chunk*      new_chunk;
                    177:   register long        new_size;
                    178:   register int obj_size = h->next_free - h->object_base;
                    179:   register int i;
                    180:   int already;
                    181: 
                    182:   /* Compute size for new chunk.  */
                    183:   new_size = (obj_size + length) + (obj_size >> 3) + 100;
                    184:   if (new_size < h->chunk_size)
                    185:     new_size = h->chunk_size;
                    186: 
                    187:   /* Allocate and initialize the new chunk.  */
1.1.1.2 ! root      188:   new_chunk = h->chunk = CALL_CHUNKFUN (h, new_size);
1.1       root      189:   new_chunk->prev = old_chunk;
                    190:   new_chunk->limit = h->chunk_limit = (char *) new_chunk + new_size;
                    191: 
                    192:   /* Move the existing object to the new chunk.
                    193:      Word at a time is fast and is safe if the object
                    194:      is sufficiently aligned.  */
                    195:   if (h->alignment_mask + 1 >= DEFAULT_ALIGNMENT)
                    196:     {
                    197:       for (i = obj_size / sizeof (COPYING_UNIT) - 1;
                    198:           i >= 0; i--)
                    199:        ((COPYING_UNIT *)new_chunk->contents)[i]
                    200:          = ((COPYING_UNIT *)h->object_base)[i];
                    201:       /* We used to copy the odd few remaining bytes as one extra COPYING_UNIT,
                    202:         but that can cross a page boundary on a machine
                    203:         which does not do strict alignment for COPYING_UNITS.  */
                    204:       already = obj_size / sizeof (COPYING_UNIT) * sizeof (COPYING_UNIT);
                    205:     }
                    206:   else
                    207:     already = 0;
                    208:   /* Copy remaining bytes one by one.  */
                    209:   for (i = already; i < obj_size; i++)
                    210:     new_chunk->contents[i] = h->object_base[i];
                    211: 
                    212:   /* If the object just copied was the only data in OLD_CHUNK,
                    213:      free that chunk and remove it from the chain.
                    214:      But not if that chunk might contain an empty object.  */
                    215:   if (h->object_base == old_chunk->contents && ! h->maybe_empty_object)
                    216:     {
                    217:       new_chunk->prev = old_chunk->prev;
1.1.1.2 ! root      218:       CALL_FREEFUN (h, old_chunk);
1.1       root      219:     }
                    220: 
                    221:   h->object_base = new_chunk->contents;
                    222:   h->next_free = h->object_base + obj_size;
                    223:   /* The new chunk certainly contains no empty object yet.  */
                    224:   h->maybe_empty_object = 0;
                    225: }
                    226: 
                    227: /* Return nonzero if object OBJ has been allocated from obstack H.
                    228:    This is here for debugging.
                    229:    If you use it in a program, you are probably losing.  */
                    230: 
                    231: int
                    232: _obstack_allocated_p (h, obj)
                    233:      struct obstack *h;
                    234:      POINTER obj;
                    235: {
                    236:   register struct _obstack_chunk*  lp; /* below addr of any objects in this chunk */
                    237:   register struct _obstack_chunk*  plp;        /* point to previous chunk if any */
                    238: 
                    239:   lp = (h)->chunk;
                    240:   /* We use >= rather than > since the object cannot be exactly at
                    241:      the beginning of the chunk but might be an empty object exactly
                    242:      at the end of an adjacent chunk. */
                    243:   while (lp != 0 && ((POINTER)lp >= obj || (POINTER)(lp)->limit < obj))
                    244:     {
                    245:       plp = lp->prev;
                    246:       lp = plp;
                    247:     }
                    248:   return lp != 0;
                    249: }
                    250: 
                    251: /* Free objects in obstack H, including OBJ and everything allocate
                    252:    more recently than OBJ.  If OBJ is zero, free everything in H.  */
                    253: 
                    254: #undef obstack_free
                    255: 
                    256: /* This function has two names with identical definitions.
                    257:    This is the first one, called from non-ANSI code.  */
                    258: 
                    259: void
                    260: _obstack_free (h, obj)
                    261:      struct obstack *h;
                    262:      POINTER obj;
                    263: {
                    264:   register struct _obstack_chunk*  lp; /* below addr of any objects in this chunk */
                    265:   register struct _obstack_chunk*  plp;        /* point to previous chunk if any */
                    266: 
                    267:   lp = h->chunk;
                    268:   /* We use >= because there cannot be an object at the beginning of a chunk.
                    269:      But there can be an empty object at that address
                    270:      at the end of another chunk.  */
                    271:   while (lp != 0 && ((POINTER)lp >= obj || (POINTER)(lp)->limit < obj))
                    272:     {
                    273:       plp = lp->prev;
1.1.1.2 ! root      274:       CALL_FREEFUN (h, lp);
1.1       root      275:       lp = plp;
                    276:       /* If we switch chunks, we can't tell whether the new current
                    277:         chunk contains an empty object, so assume that it may.  */
                    278:       h->maybe_empty_object = 1;
                    279:     }
                    280:   if (lp)
                    281:     {
                    282:       h->object_base = h->next_free = (char *)(obj);
                    283:       h->chunk_limit = lp->limit;
                    284:       h->chunk = lp;
                    285:     }
                    286:   else if (obj != 0)
                    287:     /* obj is not in any of the chunks! */
                    288:     abort ();
                    289: }
                    290: 
                    291: /* This function is used from ANSI code.  */
                    292: 
                    293: void
                    294: obstack_free (h, obj)
                    295:      struct obstack *h;
                    296:      POINTER obj;
                    297: {
                    298:   register struct _obstack_chunk*  lp; /* below addr of any objects in this chunk */
                    299:   register struct _obstack_chunk*  plp;        /* point to previous chunk if any */
                    300: 
                    301:   lp = h->chunk;
                    302:   /* We use >= because there cannot be an object at the beginning of a chunk.
                    303:      But there can be an empty object at that address
                    304:      at the end of another chunk.  */
                    305:   while (lp != 0 && ((POINTER)lp >= obj || (POINTER)(lp)->limit < obj))
                    306:     {
                    307:       plp = lp->prev;
1.1.1.2 ! root      308:       CALL_FREEFUN (h, lp);
1.1       root      309:       lp = plp;
                    310:       /* If we switch chunks, we can't tell whether the new current
                    311:         chunk contains an empty object, so assume that it may.  */
                    312:       h->maybe_empty_object = 1;
                    313:     }
                    314:   if (lp)
                    315:     {
                    316:       h->object_base = h->next_free = (char *)(obj);
                    317:       h->chunk_limit = lp->limit;
                    318:       h->chunk = lp;
                    319:     }
                    320:   else if (obj != 0)
                    321:     /* obj is not in any of the chunks! */
                    322:     abort ();
                    323: }
                    324: 
                    325: #if 0
                    326: /* These are now turned off because the applications do not use it
                    327:    and it uses bcopy via obstack_grow, which causes trouble on sysV.  */
                    328: 
                    329: /* Now define the functional versions of the obstack macros.
                    330:    Define them to simply use the corresponding macros to do the job.  */
                    331: 
                    332: #ifdef __STDC__
                    333: /* These function definitions do not work with non-ANSI preprocessors;
                    334:    they won't pass through the macro names in parentheses.  */
                    335: 
                    336: /* The function names appear in parentheses in order to prevent
                    337:    the macro-definitions of the names from being expanded there.  */
                    338: 
                    339: POINTER (obstack_base) (obstack)
                    340:      struct obstack *obstack;
                    341: {
                    342:   return obstack_base (obstack);
                    343: }
                    344: 
                    345: POINTER (obstack_next_free) (obstack)
                    346:      struct obstack *obstack;
                    347: {
                    348:   return obstack_next_free (obstack);
                    349: }
                    350: 
                    351: int (obstack_object_size) (obstack)
                    352:      struct obstack *obstack;
                    353: {
                    354:   return obstack_object_size (obstack);
                    355: }
                    356: 
                    357: int (obstack_room) (obstack)
                    358:      struct obstack *obstack;
                    359: {
                    360:   return obstack_room (obstack);
                    361: }
                    362: 
                    363: void (obstack_grow) (obstack, pointer, length)
                    364:      struct obstack *obstack;
                    365:      POINTER pointer;
                    366:      int length;
                    367: {
                    368:   obstack_grow (obstack, pointer, length);
                    369: }
                    370: 
                    371: void (obstack_grow0) (obstack, pointer, length)
                    372:      struct obstack *obstack;
                    373:      POINTER pointer;
                    374:      int length;
                    375: {
                    376:   obstack_grow0 (obstack, pointer, length);
                    377: }
                    378: 
                    379: void (obstack_1grow) (obstack, character)
                    380:      struct obstack *obstack;
                    381:      int character;
                    382: {
                    383:   obstack_1grow (obstack, character);
                    384: }
                    385: 
                    386: void (obstack_blank) (obstack, length)
                    387:      struct obstack *obstack;
                    388:      int length;
                    389: {
                    390:   obstack_blank (obstack, length);
                    391: }
                    392: 
                    393: void (obstack_1grow_fast) (obstack, character)
                    394:      struct obstack *obstack;
                    395:      int character;
                    396: {
                    397:   obstack_1grow_fast (obstack, character);
                    398: }
                    399: 
                    400: void (obstack_blank_fast) (obstack, length)
                    401:      struct obstack *obstack;
                    402:      int length;
                    403: {
                    404:   obstack_blank_fast (obstack, length);
                    405: }
                    406: 
                    407: POINTER (obstack_finish) (obstack)
                    408:      struct obstack *obstack;
                    409: {
                    410:   return obstack_finish (obstack);
                    411: }
                    412: 
                    413: POINTER (obstack_alloc) (obstack, length)
                    414:      struct obstack *obstack;
                    415:      int length;
                    416: {
                    417:   return obstack_alloc (obstack, length);
                    418: }
                    419: 
                    420: POINTER (obstack_copy) (obstack, pointer, length)
                    421:      struct obstack *obstack;
                    422:      POINTER pointer;
                    423:      int length;
                    424: {
                    425:   return obstack_copy (obstack, pointer, length);
                    426: }
                    427: 
                    428: POINTER (obstack_copy0) (obstack, pointer, length)
                    429:      struct obstack *obstack;
                    430:      POINTER pointer;
                    431:      int length;
                    432: {
                    433:   return obstack_copy0 (obstack, pointer, length);
                    434: }
                    435: 
                    436: #endif /* __STDC__ */
                    437: 
                    438: #endif /* 0 */

unix.superglobalmegacorp.com

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