Debian Codegraph @

engine/read_only/marpa_obs.c

raw
1 /* Copyright 2022 Jeffrey Kegler */
2
3 /* This file is a modification of one of the versions of the GNU obstack.c
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 # include "config.h"
27
28 # include "marpa.h"
29 # include "marpa_ami.h"
30 # include "marpa_obs.h"
31
32 /* This estimate of malloc's overhead is the one used in Linus Torvald's slab
33 * allocator in git. I assume that it assumes a 64-bit architecture.
34 */
35 #define MALLOC_OVERHEAD 32
36 #define DEFAULT_CHUNK_SIZE (4096 - MALLOC_OVERHEAD)
37
38 struct marpa_obstack *
39 marpa__obs_begin (size_t size)
40 {
41 struct marpa_obstack_chunk *chunk; /* points to new chunk */
42 struct marpa_obstack *h; /* points to new obstack */
43 char *object_base;
44 char *chunk_base;
45
46 /* We ignore |size| if it specifies less than the default */
47 size = MAX ((int)DEFAULT_CHUNK_SIZE, size);
48 chunk_base = my_malloc (size);
49
50 /* The chunk header goes at the beginning */
51 chunk = (struct marpa_obstack_chunk*)chunk_base;
52 chunk->header.size = size;
53 chunk->header.prev = 0;
54
55 /* Put the header of the obstack itself after the header of its first
56 chunk. */
57 object_base = chunk_base + sizeof(chunk->header);
58 object_base = ALIGN_POINTER (chunk_base, object_base, ALIGNOF (struct marpa_obstack));
59 h = (struct marpa_obstack *)object_base;
60 h->chunk = chunk;
61 h->minimum_chunk_size = size;
62
63 /* Set the obstack to "idle" with the pointer just after the
64 obstack header */
65 object_base += sizeof(*h);
66 h->next_free = h->object_base = object_base;
67 return h;
68 }
69
70 /* Allocate a new current chunk for the obstack *H
71 on the assumption that LENGTH bytes need to be added
72 to the current object, or a new object of length LENGTH allocated.
73 Unlike original GNU obstacks, does *NOT*
74 copy any partial object from the end of the old chunk
75 to the beginning of the new one.
76
77 In this implementation, we know the next object we will
78 need, and |length| and |alignment| represent that object.
79 We start the new object, and return its base addess.
80 */
81
82 void*
83 marpa__obs_newchunk (struct marpa_obstack *h, size_t length, size_t alignment)
84 {
85 struct marpa_obstack_chunk *old_chunk = h->chunk;
86 struct marpa_obstack_chunk *new_chunk;
87 size_t new_size;
88 const size_t contents_offset = offsetof(struct marpa_obstack_chunk, contents);
89 const size_t aligned_contents_offset = ALIGN_UP(contents_offset, alignment);
90 const size_t space_needed_for_alignment = aligned_contents_offset - contents_offset;
91
92 /* Compute size for new chunk.
93 * Make sure there is enough room for |length|
94 * after adjusting alignment.
95 */
96 new_size = contents_offset + space_needed_for_alignment + length;
97 new_size = MAX(new_size, h->minimum_chunk_size);
98
99 /* Allocate and initialize the new chunk. */
100 new_chunk = my_malloc( new_size);
101 h->chunk = new_chunk;
102 new_chunk->header.prev = old_chunk;
103 new_chunk->header.size = new_size;
104
105 h->object_base = (char *)new_chunk + contents_offset + space_needed_for_alignment;
106 h->next_free = h->object_base + length;
107 return h->object_base;
108 }
109
110 /* Free everything in H. */
111 void
112 marpa__obs_free (struct marpa_obstack *h)
113 {
114 struct marpa_obstack_chunk *lp; /* below addr of any objects in this chunk */
115 struct marpa_obstack_chunk *plp; /* point to previous chunk if any */
116
117 if (!h)
118 return; /* Return safely if never initialized */
119 lp = h->chunk;
120 while (lp != 0)
121 {
122 plp = lp->header.prev;
123 my_free (lp);
124 lp = plp;
125 }
126 }
127
128 /* vim: set expandtab shiftwidth=4: */