annotate libbacktrace/alloc.c @ 158:494b0b89df80 default tip

...
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Mon, 25 May 2020 18:13:55 +0900
parents 1830386684a0
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
111
kono
parents:
diff changeset
1 /* alloc.c -- Memory allocation without mmap.
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
2 Copyright (C) 2012-2020 Free Software Foundation, Inc.
111
kono
parents:
diff changeset
3 Written by Ian Lance Taylor, Google.
kono
parents:
diff changeset
4
kono
parents:
diff changeset
5 Redistribution and use in source and binary forms, with or without
kono
parents:
diff changeset
6 modification, are permitted provided that the following conditions are
kono
parents:
diff changeset
7 met:
kono
parents:
diff changeset
8
kono
parents:
diff changeset
9 (1) Redistributions of source code must retain the above copyright
kono
parents:
diff changeset
10 notice, this list of conditions and the following disclaimer.
kono
parents:
diff changeset
11
kono
parents:
diff changeset
12 (2) Redistributions in binary form must reproduce the above copyright
kono
parents:
diff changeset
13 notice, this list of conditions and the following disclaimer in
kono
parents:
diff changeset
14 the documentation and/or other materials provided with the
kono
parents:
diff changeset
15 distribution.
kono
parents:
diff changeset
16
kono
parents:
diff changeset
17 (3) The name of the author may not be used to
kono
parents:
diff changeset
18 endorse or promote products derived from this software without
kono
parents:
diff changeset
19 specific prior written permission.
kono
parents:
diff changeset
20
kono
parents:
diff changeset
21 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
kono
parents:
diff changeset
22 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
kono
parents:
diff changeset
23 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
kono
parents:
diff changeset
24 DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
kono
parents:
diff changeset
25 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
kono
parents:
diff changeset
26 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
kono
parents:
diff changeset
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
kono
parents:
diff changeset
28 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
kono
parents:
diff changeset
29 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
kono
parents:
diff changeset
30 IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
kono
parents:
diff changeset
31 POSSIBILITY OF SUCH DAMAGE. */
kono
parents:
diff changeset
32
kono
parents:
diff changeset
33 #include "config.h"
kono
parents:
diff changeset
34
kono
parents:
diff changeset
35 #include <errno.h>
kono
parents:
diff changeset
36 #include <stdlib.h>
kono
parents:
diff changeset
37 #include <sys/types.h>
kono
parents:
diff changeset
38
kono
parents:
diff changeset
39 #include "backtrace.h"
kono
parents:
diff changeset
40 #include "internal.h"
kono
parents:
diff changeset
41
kono
parents:
diff changeset
42 /* Allocation routines to use on systems that do not support anonymous
kono
parents:
diff changeset
43 mmap. This implementation just uses malloc, which means that the
kono
parents:
diff changeset
44 backtrace functions may not be safely invoked from a signal
kono
parents:
diff changeset
45 handler. */
kono
parents:
diff changeset
46
kono
parents:
diff changeset
47 /* Allocate memory like malloc. If ERROR_CALLBACK is NULL, don't
kono
parents:
diff changeset
48 report an error. */
kono
parents:
diff changeset
49
kono
parents:
diff changeset
50 void *
kono
parents:
diff changeset
51 backtrace_alloc (struct backtrace_state *state ATTRIBUTE_UNUSED,
kono
parents:
diff changeset
52 size_t size, backtrace_error_callback error_callback,
kono
parents:
diff changeset
53 void *data)
kono
parents:
diff changeset
54 {
kono
parents:
diff changeset
55 void *ret;
kono
parents:
diff changeset
56
kono
parents:
diff changeset
57 ret = malloc (size);
kono
parents:
diff changeset
58 if (ret == NULL)
kono
parents:
diff changeset
59 {
kono
parents:
diff changeset
60 if (error_callback)
kono
parents:
diff changeset
61 error_callback (data, "malloc", errno);
kono
parents:
diff changeset
62 }
kono
parents:
diff changeset
63 return ret;
kono
parents:
diff changeset
64 }
kono
parents:
diff changeset
65
kono
parents:
diff changeset
66 /* Free memory. */
kono
parents:
diff changeset
67
kono
parents:
diff changeset
68 void
kono
parents:
diff changeset
69 backtrace_free (struct backtrace_state *state ATTRIBUTE_UNUSED,
kono
parents:
diff changeset
70 void *p, size_t size ATTRIBUTE_UNUSED,
kono
parents:
diff changeset
71 backtrace_error_callback error_callback ATTRIBUTE_UNUSED,
kono
parents:
diff changeset
72 void *data ATTRIBUTE_UNUSED)
kono
parents:
diff changeset
73 {
kono
parents:
diff changeset
74 free (p);
kono
parents:
diff changeset
75 }
kono
parents:
diff changeset
76
kono
parents:
diff changeset
77 /* Grow VEC by SIZE bytes. */
kono
parents:
diff changeset
78
kono
parents:
diff changeset
79 void *
kono
parents:
diff changeset
80 backtrace_vector_grow (struct backtrace_state *state ATTRIBUTE_UNUSED,
kono
parents:
diff changeset
81 size_t size, backtrace_error_callback error_callback,
kono
parents:
diff changeset
82 void *data, struct backtrace_vector *vec)
kono
parents:
diff changeset
83 {
kono
parents:
diff changeset
84 void *ret;
kono
parents:
diff changeset
85
kono
parents:
diff changeset
86 if (size > vec->alc)
kono
parents:
diff changeset
87 {
kono
parents:
diff changeset
88 size_t alc;
kono
parents:
diff changeset
89 void *base;
kono
parents:
diff changeset
90
kono
parents:
diff changeset
91 if (vec->size == 0)
kono
parents:
diff changeset
92 alc = 32 * size;
kono
parents:
diff changeset
93 else if (vec->size >= 4096)
kono
parents:
diff changeset
94 alc = vec->size + 4096;
kono
parents:
diff changeset
95 else
kono
parents:
diff changeset
96 alc = 2 * vec->size;
kono
parents:
diff changeset
97
kono
parents:
diff changeset
98 if (alc < vec->size + size)
kono
parents:
diff changeset
99 alc = vec->size + size;
kono
parents:
diff changeset
100
kono
parents:
diff changeset
101 base = realloc (vec->base, alc);
kono
parents:
diff changeset
102 if (base == NULL)
kono
parents:
diff changeset
103 {
kono
parents:
diff changeset
104 error_callback (data, "realloc", errno);
kono
parents:
diff changeset
105 return NULL;
kono
parents:
diff changeset
106 }
kono
parents:
diff changeset
107
kono
parents:
diff changeset
108 vec->base = base;
kono
parents:
diff changeset
109 vec->alc = alc - vec->size;
kono
parents:
diff changeset
110 }
kono
parents:
diff changeset
111
kono
parents:
diff changeset
112 ret = (char *) vec->base + vec->size;
kono
parents:
diff changeset
113 vec->size += size;
kono
parents:
diff changeset
114 vec->alc -= size;
kono
parents:
diff changeset
115 return ret;
kono
parents:
diff changeset
116 }
kono
parents:
diff changeset
117
kono
parents:
diff changeset
118 /* Finish the current allocation on VEC. */
kono
parents:
diff changeset
119
kono
parents:
diff changeset
120 void *
kono
parents:
diff changeset
121 backtrace_vector_finish (struct backtrace_state *state,
kono
parents:
diff changeset
122 struct backtrace_vector *vec,
kono
parents:
diff changeset
123 backtrace_error_callback error_callback,
kono
parents:
diff changeset
124 void *data)
kono
parents:
diff changeset
125 {
kono
parents:
diff changeset
126 void *ret;
kono
parents:
diff changeset
127
kono
parents:
diff changeset
128 /* With this allocator we call realloc in backtrace_vector_grow,
kono
parents:
diff changeset
129 which means we can't easily reuse the memory here. So just
kono
parents:
diff changeset
130 release it. */
kono
parents:
diff changeset
131 if (!backtrace_vector_release (state, vec, error_callback, data))
kono
parents:
diff changeset
132 return NULL;
kono
parents:
diff changeset
133 ret = vec->base;
kono
parents:
diff changeset
134 vec->base = NULL;
kono
parents:
diff changeset
135 vec->size = 0;
kono
parents:
diff changeset
136 vec->alc = 0;
kono
parents:
diff changeset
137 return ret;
kono
parents:
diff changeset
138 }
kono
parents:
diff changeset
139
kono
parents:
diff changeset
140 /* Release any extra space allocated for VEC. */
kono
parents:
diff changeset
141
kono
parents:
diff changeset
142 int
kono
parents:
diff changeset
143 backtrace_vector_release (struct backtrace_state *state ATTRIBUTE_UNUSED,
kono
parents:
diff changeset
144 struct backtrace_vector *vec,
kono
parents:
diff changeset
145 backtrace_error_callback error_callback,
kono
parents:
diff changeset
146 void *data)
kono
parents:
diff changeset
147 {
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
148 vec->alc = 0;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
149
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
150 if (vec->size == 0)
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
151 {
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
152 /* As of C17, realloc with size 0 is marked as an obsolescent feature, use
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
153 free instead. */
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
154 free (vec->base);
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
155 vec->base = NULL;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
156 return 1;
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
157 }
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
158
111
kono
parents:
diff changeset
159 vec->base = realloc (vec->base, vec->size);
kono
parents:
diff changeset
160 if (vec->base == NULL)
kono
parents:
diff changeset
161 {
kono
parents:
diff changeset
162 error_callback (data, "realloc", errno);
kono
parents:
diff changeset
163 return 0;
kono
parents:
diff changeset
164 }
145
1830386684a0 gcc-9.2.0
anatofuz
parents: 131
diff changeset
165
111
kono
parents:
diff changeset
166 return 1;
kono
parents:
diff changeset
167 }