re PR target/57744 (Power8 support has problems with quad word atomic instructions)
[official-gcc.git] / libbacktrace / alloc.c
blobd0796737786185a1484a802e6f9d973f2edcb6b6
1 /* alloc.c -- Memory allocation without mmap.
2 Copyright (C) 2012-2013 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
7 met:
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
15 distribution.
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. */
33 #include "config.h"
35 #include <errno.h>
36 #include <stdlib.h>
37 #include <sys/types.h>
39 #include "backtrace.h"
40 #include "internal.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
45 handler. */
47 /* Allocate memory like malloc. */
49 void *
50 backtrace_alloc (struct backtrace_state *state ATTRIBUTE_UNUSED,
51 size_t size, backtrace_error_callback error_callback,
52 void *data)
54 void *ret;
56 ret = malloc (size);
57 if (ret == NULL)
58 error_callback (data, "malloc", errno);
59 return ret;
62 /* Free memory. */
64 void
65 backtrace_free (struct backtrace_state *state ATTRIBUTE_UNUSED,
66 void *p, size_t size ATTRIBUTE_UNUSED,
67 backtrace_error_callback error_callback ATTRIBUTE_UNUSED,
68 void *data ATTRIBUTE_UNUSED)
70 free (p);
73 /* Grow VEC by SIZE bytes. */
75 void *
76 backtrace_vector_grow (struct backtrace_state *state ATTRIBUTE_UNUSED,
77 size_t size, backtrace_error_callback error_callback,
78 void *data, struct backtrace_vector *vec)
80 void *ret;
82 if (size > vec->alc)
84 size_t alc;
85 void *base;
87 if (vec->size == 0)
88 alc = 32 * size;
89 else if (vec->size >= 4096)
90 alc = vec->size + 4096;
91 else
92 alc = 2 * vec->size;
94 if (alc < vec->size + size)
95 alc = vec->size + size;
97 base = realloc (vec->base, alc);
98 if (base == NULL)
100 error_callback (data, "realloc", errno);
101 return NULL;
104 vec->base = base;
105 vec->alc = alc - vec->size;
108 ret = (char *) vec->base + vec->size;
109 vec->size += size;
110 vec->alc -= size;
111 return ret;
114 /* Finish the current allocation on VEC. */
116 void
117 backtrace_vector_finish (struct backtrace_state *state ATTRIBUTE_UNUSED,
118 struct backtrace_vector *vec)
120 vec->base = (char *) vec->base + vec->size;
121 vec->size = 0;
124 /* Release any extra space allocated for VEC. */
127 backtrace_vector_release (struct backtrace_state *state ATTRIBUTE_UNUSED,
128 struct backtrace_vector *vec,
129 backtrace_error_callback error_callback,
130 void *data)
132 vec->base = realloc (vec->base, vec->size);
133 if (vec->base == NULL)
135 error_callback (data, "realloc", errno);
136 return 0;
138 vec->alc = 0;
139 return 1;