xref: /netbsd-src/external/gpl3/gcc/dist/include/objalloc.h (revision e9e6e0f6fbc36b8de7586170291cf5fc97cab8b6)
14fee23f9Smrg /* objalloc.h -- routines to allocate memory for objects
2*e9e6e0f6Smrg    Copyright (C) 1997-2022 Free Software Foundation, Inc.
34fee23f9Smrg    Written by Ian Lance Taylor, Cygnus Solutions.
44fee23f9Smrg 
54fee23f9Smrg This program is free software; you can redistribute it and/or modify it
64fee23f9Smrg under the terms of the GNU General Public License as published by the
74fee23f9Smrg Free Software Foundation; either version 2, or (at your option) any
84fee23f9Smrg later version.
94fee23f9Smrg 
104fee23f9Smrg This program is distributed in the hope that it will be useful,
114fee23f9Smrg but WITHOUT ANY WARRANTY; without even the implied warranty of
124fee23f9Smrg MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
134fee23f9Smrg GNU General Public License for more details.
144fee23f9Smrg 
154fee23f9Smrg You should have received a copy of the GNU General Public License
164fee23f9Smrg along with this program; if not, write to the Free Software
174fee23f9Smrg Foundation, 51 Franklin Street - Fifth Floor,
184fee23f9Smrg Boston, MA 02110-1301, USA.  */
194fee23f9Smrg 
204fee23f9Smrg #ifndef OBJALLOC_H
214fee23f9Smrg #define OBJALLOC_H
224fee23f9Smrg 
234fee23f9Smrg #include "ansidecl.h"
244fee23f9Smrg 
254fee23f9Smrg /* These routines allocate space for an object.  The assumption is
264fee23f9Smrg    that the object will want to allocate space as it goes along, but
274fee23f9Smrg    will never want to free any particular block.  There is a function
284fee23f9Smrg    to free a block, which also frees all more recently allocated
294fee23f9Smrg    blocks.  There is also a function to free all the allocated space.
304fee23f9Smrg 
314fee23f9Smrg    This is essentially a specialization of obstacks.  The main
324fee23f9Smrg    difference is that a block may not be allocated a bit at a time.
334fee23f9Smrg    Another difference is that these routines are always built on top
344fee23f9Smrg    of malloc, and always pass an malloc failure back to the caller,
354fee23f9Smrg    unlike more recent versions of obstacks.  */
364fee23f9Smrg 
374fee23f9Smrg /* This is what an objalloc structure looks like.  Callers should not
384fee23f9Smrg    refer to these fields, nor should they allocate these structure
394fee23f9Smrg    themselves.  Instead, they should only create them via
404fee23f9Smrg    objalloc_init, and only access them via the functions and macros
414fee23f9Smrg    listed below.  The structure is only defined here so that we can
424fee23f9Smrg    access it via macros.  */
434fee23f9Smrg 
444fee23f9Smrg struct objalloc
454fee23f9Smrg {
464fee23f9Smrg   char *current_ptr;
474fee23f9Smrg   unsigned int current_space;
484fee23f9Smrg   void *chunks;
494fee23f9Smrg };
504fee23f9Smrg 
514fee23f9Smrg /* Work out the required alignment.  */
524fee23f9Smrg 
534fee23f9Smrg struct objalloc_align { char x; double d; };
544fee23f9Smrg 
554fee23f9Smrg #if defined (__STDC__) && __STDC__
564fee23f9Smrg #ifndef offsetof
574fee23f9Smrg #include <stddef.h>
584fee23f9Smrg #endif
594fee23f9Smrg #endif
604fee23f9Smrg #ifndef offsetof
614fee23f9Smrg #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
624fee23f9Smrg #endif
634fee23f9Smrg #define OBJALLOC_ALIGN offsetof (struct objalloc_align, d)
644fee23f9Smrg 
654fee23f9Smrg /* Create an objalloc structure.  Returns NULL if malloc fails.  */
664fee23f9Smrg 
674fee23f9Smrg extern struct objalloc *objalloc_create (void);
684fee23f9Smrg 
694fee23f9Smrg /* Allocate space from an objalloc structure.  Returns NULL if malloc
704fee23f9Smrg    fails.  */
714fee23f9Smrg 
724fee23f9Smrg extern void *_objalloc_alloc (struct objalloc *, unsigned long);
734fee23f9Smrg 
744fee23f9Smrg /* The macro version of objalloc_alloc.  We only define this if using
754fee23f9Smrg    gcc, because otherwise we would have to evaluate the arguments
764fee23f9Smrg    multiple times, or use a temporary field as obstack.h does.  */
774fee23f9Smrg 
784fee23f9Smrg #if defined (__GNUC__) && defined (__STDC__) && __STDC__
794fee23f9Smrg 
804fee23f9Smrg /* NextStep 2.0 cc is really gcc 1.93 but it defines __GNUC__ = 2 and
814fee23f9Smrg    does not implement __extension__.  But that compiler doesn't define
824fee23f9Smrg    __GNUC_MINOR__.  */
834fee23f9Smrg #if __GNUC__ < 2 || (__NeXT__ && !__GNUC_MINOR__)
844fee23f9Smrg #define __extension__
854fee23f9Smrg #endif
864fee23f9Smrg 
874fee23f9Smrg #define objalloc_alloc(o, l)						\
884fee23f9Smrg   __extension__								\
894fee23f9Smrg   ({ struct objalloc *__o = (o);					\
904fee23f9Smrg      unsigned long __len = (l);						\
914fee23f9Smrg      if (__len == 0)							\
924fee23f9Smrg        __len = 1;							\
934fee23f9Smrg      __len = (__len + OBJALLOC_ALIGN - 1) &~ (OBJALLOC_ALIGN - 1);	\
94d18545deSdrochner      (__len != 0 && __len <= __o->current_space				\
954fee23f9Smrg       ? (__o->current_ptr += __len,					\
964fee23f9Smrg 	 __o->current_space -= __len,					\
974fee23f9Smrg 	 (void *) (__o->current_ptr - __len))				\
984fee23f9Smrg       : _objalloc_alloc (__o, __len)); })
994fee23f9Smrg 
1004fee23f9Smrg #else /* ! __GNUC__ */
1014fee23f9Smrg 
1024fee23f9Smrg #define objalloc_alloc(o, l) _objalloc_alloc ((o), (l))
1034fee23f9Smrg 
1044fee23f9Smrg #endif /* ! __GNUC__ */
1054fee23f9Smrg 
1064fee23f9Smrg /* Free an entire objalloc structure.  */
1074fee23f9Smrg 
1084fee23f9Smrg extern void objalloc_free (struct objalloc *);
1094fee23f9Smrg 
1104fee23f9Smrg /* Free a block allocated by objalloc_alloc.  This also frees all more
1114fee23f9Smrg    recently allocated blocks.  */
1124fee23f9Smrg 
1134fee23f9Smrg extern void objalloc_free_block (struct objalloc *, void *);
1144fee23f9Smrg 
1154fee23f9Smrg #endif /* OBJALLOC_H */
116