Allocate sufficient space for string buffer
[deliverable/binutils-gdb.git] / gas / sb.c
1 /* sb.c - string buffer manipulation routines
2 Copyright 1994, 1995, 2000, 2003, 2005, 2006, 2007, 2009, 2012
3 Free Software Foundation, Inc.
4
5 Written by Steve and Judy Chamberlain of Cygnus Support,
6 sac@cygnus.com
7
8 This file is part of GAS, the GNU Assembler.
9
10 GAS is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 3, or (at your option)
13 any later version.
14
15 GAS is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with GAS; see the file COPYING. If not, write to the Free
22 Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
23 02110-1301, USA. */
24
25 #include "as.h"
26 #include "sb.h"
27
28 #ifdef HAVE_LIMITS_H
29 #include <limits.h>
30 #endif
31 #ifndef CHAR_BIT
32 #define CHAR_BIT 8
33 #endif
34
35 /* These routines are about manipulating strings.
36
37 They are managed in things called `sb's which is an abbreviation
38 for string buffers. An sb has to be created, things can be glued
39 on to it, and at the end of it's life it should be freed. The
40 contents should never be pointed at whilst it is still growing,
41 since it could be moved at any time
42
43 eg:
44 sb_new (&foo);
45 sb_grow... (&foo,...);
46 use foo->ptr[*];
47 sb_kill (&foo); */
48
49 /* Buffers start at INIT_ALLOC size, and roughly double each time we
50 go over the current allocation. MALLOC_OVERHEAD is a guess at the
51 system malloc overhead. We aim to not waste any memory in the
52 underlying page/chunk allocated by the system malloc. */
53 #define MALLOC_OVERHEAD (2 * sizeof (size_t))
54 #define INIT_ALLOC (64 - MALLOC_OVERHEAD - 1)
55
56 static void sb_check (sb *, size_t);
57
58 /* Initializes an sb. */
59
60 void
61 sb_build (sb *ptr, size_t size)
62 {
63 ptr->ptr = xmalloc (size + 1);
64 ptr->max = size;
65 ptr->len = 0;
66 }
67
68 void
69 sb_new (sb *ptr)
70 {
71 sb_build (ptr, INIT_ALLOC);
72 }
73
74 /* Deallocate the sb at ptr. */
75
76 void
77 sb_kill (sb *ptr)
78 {
79 free (ptr->ptr);
80 }
81
82 /* Add the sb at s to the end of the sb at ptr. */
83
84 void
85 sb_add_sb (sb *ptr, sb *s)
86 {
87 sb_check (ptr, s->len);
88 memcpy (ptr->ptr + ptr->len, s->ptr, s->len);
89 ptr->len += s->len;
90 }
91
92 /* Helper for sb_scrub_and_add_sb. */
93
94 static sb *sb_to_scrub;
95 static char *scrub_position;
96 static size_t
97 scrub_from_sb (char *buf, size_t buflen)
98 {
99 size_t copy;
100 copy = sb_to_scrub->len - (scrub_position - sb_to_scrub->ptr);
101 if (copy > buflen)
102 copy = buflen;
103 memcpy (buf, scrub_position, copy);
104 scrub_position += copy;
105 return copy;
106 }
107
108 /* Run the sb at s through do_scrub_chars and add the result to the sb
109 at ptr. */
110
111 void
112 sb_scrub_and_add_sb (sb *ptr, sb *s)
113 {
114 sb_to_scrub = s;
115 scrub_position = s->ptr;
116
117 sb_check (ptr, s->len);
118 ptr->len += do_scrub_chars (scrub_from_sb, ptr->ptr + ptr->len, s->len);
119
120 sb_to_scrub = 0;
121 scrub_position = 0;
122 }
123
124 /* Make sure that the sb at ptr has room for another len characters,
125 and grow it if it doesn't. */
126
127 static void
128 sb_check (sb *ptr, size_t len)
129 {
130 size_t want = ptr->len + len;
131
132 if (want > ptr->max)
133 {
134 size_t max;
135
136 want += MALLOC_OVERHEAD + 1;
137 if ((ssize_t) want < 0)
138 as_fatal ("string buffer overflow");
139 #if GCC_VERSION >= 3004
140 max = (size_t) 1 << (CHAR_BIT * sizeof (want) - __builtin_clzl (want));
141 #else
142 max = 128;
143 while (want > max)
144 max <<= 1;
145 #endif
146 max -= MALLOC_OVERHEAD + 1;
147 ptr->max = max;
148 ptr->ptr = xrealloc (ptr->ptr, max + 1);
149 }
150 }
151
152 /* Make the sb at ptr point back to the beginning. */
153
154 void
155 sb_reset (sb *ptr)
156 {
157 ptr->len = 0;
158 }
159
160 /* Add character c to the end of the sb at ptr. */
161
162 void
163 sb_add_char (sb *ptr, size_t c)
164 {
165 sb_check (ptr, 1);
166 ptr->ptr[ptr->len++] = c;
167 }
168
169 /* Add null terminated string s to the end of sb at ptr. */
170
171 void
172 sb_add_string (sb *ptr, const char *s)
173 {
174 size_t len = strlen (s);
175 sb_check (ptr, len);
176 memcpy (ptr->ptr + ptr->len, s, len);
177 ptr->len += len;
178 }
179
180 /* Add string at s of length len to sb at ptr */
181
182 void
183 sb_add_buffer (sb *ptr, const char *s, size_t len)
184 {
185 sb_check (ptr, len);
186 memcpy (ptr->ptr + ptr->len, s, len);
187 ptr->len += len;
188 }
189
190 /* Write terminating NUL and return string. */
191
192 char *
193 sb_terminate (sb *in)
194 {
195 in->ptr[in->len] = 0;
196 return in->ptr;
197 }
198
199 /* Start at the index idx into the string in sb at ptr and skip
200 whitespace. return the index of the first non whitespace character. */
201
202 size_t
203 sb_skip_white (size_t idx, sb *ptr)
204 {
205 while (idx < ptr->len
206 && (ptr->ptr[idx] == ' '
207 || ptr->ptr[idx] == '\t'))
208 idx++;
209 return idx;
210 }
211
212 /* Start at the index idx into the sb at ptr. skips whitespace,
213 a comma and any following whitespace. returns the index of the
214 next character. */
215
216 size_t
217 sb_skip_comma (size_t idx, sb *ptr)
218 {
219 while (idx < ptr->len
220 && (ptr->ptr[idx] == ' '
221 || ptr->ptr[idx] == '\t'))
222 idx++;
223
224 if (idx < ptr->len
225 && ptr->ptr[idx] == ',')
226 idx++;
227
228 while (idx < ptr->len
229 && (ptr->ptr[idx] == ' '
230 || ptr->ptr[idx] == '\t'))
231 idx++;
232
233 return idx;
234 }
This page took 0.049737 seconds and 4 git commands to generate.