source: trunk/src/gcc/libiberty/dyn-string.c@ 2042

Last change on this file since 2042 was 2, checked in by bird, 23 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 11.7 KB
Line 
1/* An abstract string datatype.
2 Copyright (C) 1998, 1999, 2000, 2002 Free Software Foundation, Inc.
3 Contributed by Mark Mitchell ([email protected]).
4
5This file is part of GNU CC.
6
7GNU CC is free software; you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published by
9the Free Software Foundation; either version 2, or (at your option)
10any later version.
11
12In addition to the permissions in the GNU General Public License, the
13Free Software Foundation gives you unlimited permission to link the
14compiled version of this file into combinations with other programs,
15and to distribute those combinations without any restriction coming
16from the use of this file. (The General Public License restrictions
17do apply in other respects; for example, they cover modification of
18the file, and distribution when not linked into a combined
19executable.)
20
21GNU CC is distributed in the hope that it will be useful,
22but WITHOUT ANY WARRANTY; without even the implied warranty of
23MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24GNU General Public License for more details.
25
26You should have received a copy of the GNU General Public License
27along with GNU CC; see the file COPYING. If not, write to
28the Free Software Foundation, 59 Temple Place - Suite 330,
29Boston, MA 02111-1307, USA. */
30
31#ifdef HAVE_CONFIG_H
32#include "config.h"
33#endif
34
35#include <stdio.h>
36
37#ifdef HAVE_STRING_H
38#include <string.h>
39#endif
40
41#ifdef HAVE_STDLIB_H
42#include <stdlib.h>
43#endif
44
45#include "libiberty.h"
46#include "dyn-string.h"
47
48/* If this file is being compiled for inclusion in the C++ runtime
49 library, as part of the demangler implementation, we don't want to
50 abort if an allocation fails. Instead, percolate an error code up
51 through the call chain. */
52
53#if defined(IN_LIBGCC2) || defined(IN_GLIBCPP_V3)
54#define RETURN_ON_ALLOCATION_FAILURE
55#endif
56
57/* Performs in-place initialization of a dyn_string struct. This
58 function can be used with a dyn_string struct on the stack or
59 embedded in another object. The contents of of the string itself
60 are still dynamically allocated. The string initially is capable
61 of holding at least SPACE characeters, including the terminating
62 NUL. If SPACE is 0, it will silently be increated to 1.
63
64 If RETURN_ON_ALLOCATION_FAILURE is defined and memory allocation
65 fails, returns 0. Otherwise returns 1. */
66
67int
68dyn_string_init (ds_struct_ptr, space)
69 struct dyn_string *ds_struct_ptr;
70 int space;
71{
72 /* We need at least one byte in which to store the terminating NUL. */
73 if (space == 0)
74 space = 1;
75
76#ifdef RETURN_ON_ALLOCATION_FAILURE
77 ds_struct_ptr->s = (char *) malloc (space);
78 if (ds_struct_ptr->s == NULL)
79 return 0;
80#else
81 ds_struct_ptr->s = (char *) xmalloc (space);
82#endif
83 ds_struct_ptr->allocated = space;
84 ds_struct_ptr->length = 0;
85 ds_struct_ptr->s[0] = '\0';
86
87 return 1;
88}
89
90/* Create a new dynamic string capable of holding at least SPACE
91 characters, including the terminating NUL. If SPACE is 0, it will
92 be silently increased to 1. If RETURN_ON_ALLOCATION_FAILURE is
93 defined and memory allocation fails, returns NULL. Otherwise
94 returns the newly allocated string. */
95
96dyn_string_t
97dyn_string_new (space)
98 int space;
99{
100 dyn_string_t result;
101#ifdef RETURN_ON_ALLOCATION_FAILURE
102 result = (dyn_string_t) malloc (sizeof (struct dyn_string));
103 if (result == NULL)
104 return NULL;
105 if (!dyn_string_init (result, space))
106 {
107 free (result);
108 return NULL;
109 }
110#else
111 result = (dyn_string_t) xmalloc (sizeof (struct dyn_string));
112 dyn_string_init (result, space);
113#endif
114 return result;
115}
116
117/* Free the memory used by DS. */
118
119void
120dyn_string_delete (ds)
121 dyn_string_t ds;
122{
123 free (ds->s);
124 free (ds);
125}
126
127/* Returns the contents of DS in a buffer allocated with malloc. It
128 is the caller's responsibility to deallocate the buffer using free.
129 DS is then set to the empty string. Deletes DS itself. */
130
131char*
132dyn_string_release (ds)
133 dyn_string_t ds;
134{
135 /* Store the old buffer. */
136 char* result = ds->s;
137 /* The buffer is no longer owned by DS. */
138 ds->s = NULL;
139 /* Delete DS. */
140 free (ds);
141 /* Return the old buffer. */
142 return result;
143}
144
145/* Increase the capacity of DS so it can hold at least SPACE
146 characters, plus the terminating NUL. This function will not (at
147 present) reduce the capacity of DS. Returns DS on success.
148
149 If RETURN_ON_ALLOCATION_FAILURE is defined and a memory allocation
150 operation fails, deletes DS and returns NULL. */
151
152dyn_string_t
153dyn_string_resize (ds, space)
154 dyn_string_t ds;
155 int space;
156{
157 int new_allocated = ds->allocated;
158
159 /* Increase SPACE to hold the NUL termination. */
160 ++space;
161
162 /* Increase allocation by factors of two. */
163 while (space > new_allocated)
164 new_allocated *= 2;
165
166 if (new_allocated != ds->allocated)
167 {
168 ds->allocated = new_allocated;
169 /* We actually need more space. */
170#ifdef RETURN_ON_ALLOCATION_FAILURE
171 ds->s = (char *) realloc (ds->s, ds->allocated);
172 if (ds->s == NULL)
173 {
174 free (ds);
175 return NULL;
176 }
177#else
178 ds->s = (char *) xrealloc (ds->s, ds->allocated);
179#endif