2013-02-13 Richard Biener <rguenther@suse.de>
[official-gcc.git] / libbacktrace / alloc.c
blobcb6f07b8ee9cdf5dd2eb76cda7ccf29e6fb0b233
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>
38 #include "backtrace.h"
39 #include "internal.h"
41 /* Allocation routines to use on systems that do not support anonymous
42 mmap. This implementation just uses malloc, which means that the
43 backtrace functions may not be safely invoked from a signal
44 handler. */
46 /* Allocate memory like malloc. */
48 void *
49 backtrace_alloc (struct backtrace_state *state ATTRIBUTE_UNUSED,
50 size_t size, backtrace_error_callback error_callback,
51 void *data)
53 void *ret;
55 ret = malloc (size);
56 if (ret == NULL)
57 error_callback (data, "malloc", errno);
58 return ret;
61 /* Free memory. */
63 void
64 backtrace_free (struct backtrace_state *state ATTRIBUTE_UNUSED,
65 void *p, size_t size ATTRIBUTE_UNUSED,
66 backtrace_error_callback error_callback ATTRIBUTE_UNUSED,
67 void *data ATTRIBUTE_UNUSED)
69 free (p);
72 /* Grow VEC by SIZE bytes. */
74 void *
75 backtrace_vector_grow (struct backtrace_state *state ATTRIBUTE_UNUSED,
76 size_t size, backtrace_error_callback error_callback,
77 void *data, struct backtrace_vector *vec)
79 void *ret;
81 if (size > vec->alc)
83 size_t alc;
84 void *base;
86 if (vec->size == 0)
87 alc = 32 * size;
88 else if (vec->size >= 4096)
89 alc = vec->size + 4096;
90 else
91 alc = 2 * vec->size;
93 if (alc < vec->size + size)
94 alc = vec->size + size;
96 base = realloc (vec->base, alc);
97 if (base == NULL)
99 error_callback (data, "realloc", errno);
100 return NULL;
103 vec->base = base;
104 vec->alc = alc - vec->size;
107 ret = (char *) vec->base + vec->size;
108 vec->size += size;
109 vec->alc -= size;
110 return ret;
113 /* Finish the current allocation on VEC. */
115 void
116 backtrace_vector_finish (struct backtrace_state *state ATTRIBUTE_UNUSED,
117 struct backtrace_vector *vec)
119 vec->base = (char *) vec->base + vec->size;
120 vec->size = 0;
123 /* Release any extra space allocated for VEC. */
126 backtrace_vector_release (struct backtrace_state *state ATTRIBUTE_UNUSED,
127 struct backtrace_vector *vec,
128 backtrace_error_callback error_callback,
129 void *data)
131 vec->base = realloc (vec->base, vec->size);
132 if (vec->base == NULL)
134 error_callback (data, "realloc", errno);
135 return 0;
137 vec->alc = 0;
138 return 1;