1 /* alloc.c -- Memory allocation without mmap.
2 Copyright (C) 2012-2021 Free Software Foundation, Inc.
3 Written by Ian Lance Taylor, Google.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are
9 (1) Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
12 (2) Redistributions in binary form must reproduce the above copyright
13 notice, this list of conditions and the following disclaimer in
14 the documentation and/or other materials provided with the
17 (3) The name of the author may not be used to
18 endorse or promote products derived from this software without
19 specific prior written permission.
21 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
23 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24 DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
25 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
30 IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 POSSIBILITY OF SUCH DAMAGE. */
37 #include <sys/types.h>
39 #include "backtrace.h"
42 /* Allocation routines to use on systems that do not support anonymous
43 mmap. This implementation just uses malloc, which means that the
44 backtrace functions may not be safely invoked from a signal
47 /* Allocate memory like malloc. If ERROR_CALLBACK is NULL, don't
51 backtrace_alloc (struct backtrace_state
*state ATTRIBUTE_UNUSED
,
52 size_t size
, backtrace_error_callback error_callback
,
61 error_callback (data
, "malloc", errno
);
69 backtrace_free (struct backtrace_state
*state ATTRIBUTE_UNUSED
,
70 void *p
, size_t size ATTRIBUTE_UNUSED
,
71 backtrace_error_callback error_callback ATTRIBUTE_UNUSED
,
72 void *data ATTRIBUTE_UNUSED
)
77 /* Grow VEC by SIZE bytes. */
80 backtrace_vector_grow (struct backtrace_state
*state ATTRIBUTE_UNUSED
,
81 size_t size
, backtrace_error_callback error_callback
,
82 void *data
, struct backtrace_vector
*vec
)
93 else if (vec
->size
>= 4096)
94 alc
= vec
->size
+ 4096;
98 if (alc
< vec
->size
+ size
)
99 alc
= vec
->size
+ size
;
101 base
= realloc (vec
->base
, alc
);
104 error_callback (data
, "realloc", errno
);
109 vec
->alc
= alc
- vec
->size
;
112 ret
= (char *) vec
->base
+ vec
->size
;
118 /* Finish the current allocation on VEC. */
121 backtrace_vector_finish (struct backtrace_state
*state
,
122 struct backtrace_vector
*vec
,
123 backtrace_error_callback error_callback
,
128 /* With this allocator we call realloc in backtrace_vector_grow,
129 which means we can't easily reuse the memory here. So just
131 if (!backtrace_vector_release (state
, vec
, error_callback
, data
))
140 /* Release any extra space allocated for VEC. */
143 backtrace_vector_release (struct backtrace_state
*state ATTRIBUTE_UNUSED
,
144 struct backtrace_vector
*vec
,
145 backtrace_error_callback error_callback
,
152 /* As of C17, realloc with size 0 is marked as an obsolescent feature, use
159 vec
->base
= realloc (vec
->base
, vec
->size
);
160 if (vec
->base
== NULL
)
162 error_callback (data
, "realloc", errno
);