objalloc.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /* objalloc.h -- routines to allocate memory for objects
  2. Copyright (C) 1997-2022 Free Software Foundation, Inc.
  3. Written by Ian Lance Taylor, Cygnus Solutions.
  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. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, 51 Franklin Street - Fifth Floor,
  15. Boston, MA 02110-1301, USA. */
  16. #ifndef OBJALLOC_H
  17. #define OBJALLOC_H
  18. #include "ansidecl.h"
  19. /* These routines allocate space for an object. The assumption is
  20. that the object will want to allocate space as it goes along, but
  21. will never want to free any particular block. There is a function
  22. to free a block, which also frees all more recently allocated
  23. blocks. There is also a function to free all the allocated space.
  24. This is essentially a specialization of obstacks. The main
  25. difference is that a block may not be allocated a bit at a time.
  26. Another difference is that these routines are always built on top
  27. of malloc, and always pass an malloc failure back to the caller,
  28. unlike more recent versions of obstacks. */
  29. /* This is what an objalloc structure looks like. Callers should not
  30. refer to these fields, nor should they allocate these structure
  31. themselves. Instead, they should only create them via
  32. objalloc_init, and only access them via the functions and macros
  33. listed below. The structure is only defined here so that we can
  34. access it via macros. */
  35. struct objalloc
  36. {
  37. char *current_ptr;
  38. unsigned int current_space;
  39. void *chunks;
  40. };
  41. /* Work out the required alignment. */
  42. struct objalloc_align { char x; double d; };
  43. #if defined (__STDC__) && __STDC__
  44. #ifndef offsetof
  45. #include <stddef.h>
  46. #endif
  47. #endif
  48. #ifndef offsetof
  49. #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
  50. #endif
  51. #define OBJALLOC_ALIGN offsetof (struct objalloc_align, d)
  52. /* Create an objalloc structure. Returns NULL if malloc fails. */
  53. extern struct objalloc *objalloc_create (void);
  54. /* Allocate space from an objalloc structure. Returns NULL if malloc
  55. fails. */
  56. extern void *_objalloc_alloc (struct objalloc *, unsigned long);
  57. /* The macro version of objalloc_alloc. We only define this if using
  58. gcc, because otherwise we would have to evaluate the arguments
  59. multiple times, or use a temporary field as obstack.h does. */
  60. #if defined (__GNUC__) && defined (__STDC__) && __STDC__
  61. /* NextStep 2.0 cc is really gcc 1.93 but it defines __GNUC__ = 2 and
  62. does not implement __extension__. But that compiler doesn't define
  63. __GNUC_MINOR__. */
  64. #if __GNUC__ < 2 || (__NeXT__ && !__GNUC_MINOR__)
  65. #define __extension__
  66. #endif
  67. #define objalloc_alloc(o, l) \
  68. __extension__ \
  69. ({ struct objalloc *__o = (o); \
  70. unsigned long __len = (l); \
  71. if (__len == 0) \
  72. __len = 1; \
  73. __len = (__len + OBJALLOC_ALIGN - 1) &~ (OBJALLOC_ALIGN - 1); \
  74. (__len != 0 && __len <= __o->current_space \
  75. ? (__o->current_ptr += __len, \
  76. __o->current_space -= __len, \
  77. (void *) (__o->current_ptr - __len)) \
  78. : _objalloc_alloc (__o, __len)); })
  79. #else /* ! __GNUC__ */
  80. #define objalloc_alloc(o, l) _objalloc_alloc ((o), (l))
  81. #endif /* ! __GNUC__ */
  82. /* Free an entire objalloc structure. */
  83. extern void objalloc_free (struct objalloc *);
  84. /* Free a block allocated by objalloc_alloc. This also frees all more
  85. recently allocated blocks. */
  86. extern void objalloc_free_block (struct objalloc *, void *);
  87. #endif /* OBJALLOC_H */