Debian Codegraph @

engine/read_only/marpa_obs.h

raw
1 /* Copyright 2022 Jeffrey Kegler */
2
3 /* This file is a modification of one of the versions of the GNU obstack.h
4 * which was LGPL 2.1. Here is the copyright notice from that file:
5 *
6 * obstack.c - subroutines used implicitly by object stack macros
7 * Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1996, 1997, 1998,
8 * 1999, 2000, 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
9 * This file is part of the GNU C Library.
10 *
11 * The GNU C Library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
15 *
16 * The GNU C Library is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with the GNU C Library; if not, write to the Free
23 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 * Boston, MA 02110-1301, USA. */
25
26 #ifndef _MARPA_OBS_H__
27 #define _MARPA_OBS_H__ 1
28
29 /* Suppress 'unnamed type definition in parentheses' warning
30 in #define ALIGNOF(type) below
31 under MS C compiler older than .NET 2003 */
32 #if defined(_MSC_VER) && _MSC_VER >= 1310 && !defined(__cplusplus)
33 #pragma warning(disable:4116)
34 #endif
35
36 #define ALIGNOF(type) offsetof (struct { char c; type element; }, element)
37
38 /* If B is the base of an object addressed by P, return the result of
39 aligning P to the next multiple of A + 1. B and P must be of type
40 char *. A + 1 must be a power of 2. */
41
42 #define ALIGN_UP(x, align) (((x) + (align) - 1) & ~((align) - 1))
43 #define ALIGN_DOWN(x, align) ((x) & ~((align) - 1))
44 #define ALIGN_POINTER(base, p, align) \
45 ((base) + (ptrdiff_t)ALIGN_UP((size_t)((p)-(base)), (align)))
46
47 /*
48 The original GNU obstack implementation used __PTR_ALIGN,
49 where pointers were converted to integers, aligned as integers,
50 and converted back again.
51 This is unsafe according to C89 and we are purists,
52 so we don't use it.
53 */
54
55 /* |object_base| is the base of the object currently being built.
56 |next_free| is the potential base of the next object, and therefore
57 a limit (we hope!) on the size of the one currently being built.
58 |object_base| == |next_free| if we are "idle" --
59 not currently building an object. An obstack is "idle" when
60 it is initialized.
61
62 Objects are "started" by moving |next_free| forward so that
63 |next_free| > |object_base|. Objects are finished by setting
64 |next_free| == |object_base|, so the obstack is again "idle".
65 */
66
67 struct marpa_obstack /* control current object in current chunk */
68 {
69 struct marpa_obstack_chunk *chunk; /* address of current struct obstack_chunk */
70 char *object_base;
71 char *next_free;
72 size_t minimum_chunk_size; /* preferred size to allocate chunks in */
73 };
74
75 struct marpa_obstack_chunk_header /* Lives at front of each chunk. */
76 {
77 struct marpa_obstack_chunk* prev; /* address of prior chunk or NULL */
78 size_t size;
79 };
80
81 struct marpa_obstack_chunk
82 {
83 struct marpa_obstack_chunk_header header;
84 /* objects begin here in the second and subsequent chunks */
85 char contents[4];
86 };
87
88 extern void* marpa__obs_newchunk (struct marpa_obstack *, size_t, size_t);
89
90 extern struct marpa_obstack* marpa__obs_begin (size_t);
91
92 void marpa__obs_free (struct marpa_obstack *__obstack);
93
94 /* Pointer to beginning of object being allocated or to be allocated next.
95 Note that this might not be the final address of the object
96 because a new chunk might be needed to hold the final size. */
97
98 #define marpa_obs_base(h) ((void *) (h)->object_base)
99
100 #define marpa_obs_init marpa__obs_begin (0)
101
102 # define marpa_obstack_object_size(h) \
103 (unsigned) ((h)->next_free - (h)->object_base)
104
105 # define marpa_obs_free(h) (marpa__obs_free((h)))
106
107 /* Reject any object being built, as if it never existed */
108 # define marpa_obs_reject(h) \
109 ((h)->next_free = (h)->object_base)
110
111 # define marpa_obstack_room(h) \
112 ((h)->chunk->header.size - ((h)->next_free - (char*)((h)->chunk)))
113
114 #define marpa_obs_new(h, type, count) \
115 ((type *)marpa__obs_alloc((h), (sizeof(type)*((size_t)(count))), ALIGNOF(type)))
116
117 /* Start an object */
118 static inline void*
119 marpa_obs_start (struct marpa_obstack *h, size_t length, size_t alignment)
120 {
121 const size_t current_offset = (size_t)(h->next_free - (char *) (h->chunk));
122 const size_t aligned_offset = ALIGN_UP (current_offset, alignment);
123 if (aligned_offset + length > h->chunk->header.size)
124 {
125 return marpa__obs_newchunk (h, length, alignment);
126 }
127 h->object_base = (char *) (h->chunk) + aligned_offset;
128 h->next_free = h->object_base + length;
129 return h->object_base;
130 }
131
132 static inline
133 void *marpa_obs_finish (struct marpa_obstack *h)
134 {
135 void * const finished_object = h->object_base;
136 h->object_base = h->next_free;
137 return finished_object;
138 }
139
140 static inline void *
141 marpa__obs_alloc (struct marpa_obstack *h, size_t length, size_t alignment)
142 {
143 marpa_obs_start (h, length, alignment);
144 return marpa_obs_finish (h);
145 }
146
147 /* "Confirm", which is to set at its final value,
148 * the size of a reserved object, currently being built.
149 * The caller needs to ensure that the
150 * confirmed size is less than or equal to the reserved size.
151 * "Fast" here means there is no check -- it is up to the caller
152 * to ensure that the confirmed size is not too big
153 */
154 static inline
155 void marpa_obs_confirm_fast (struct marpa_obstack* h, int length) {
156 h->next_free = h->object_base + length;
157 }
158
159 #endif /* marpa_obs.h */
160
161 /* vim: set expandtab shiftwidth=4: */