1 /* mmap.c -- Memory allocation with 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
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. */
41 #include "backtrace.h"
44 /* Memory allocation on systems that provide anonymous mmap. This
45 permits the backtrace functions to be invoked from a signal
46 handler, assuming that mmap is async-signal safe. */
49 #define MAP_ANONYMOUS MAP_ANON
52 /* A list of free memory blocks. */
54 struct backtrace_freelist_struct
57 struct backtrace_freelist_struct
*next
;
58 /* Size of this block, including this structure. */
62 /* Free memory allocated by backtrace_alloc. */
65 backtrace_free_locked (struct backtrace_state
*state
, void *addr
, size_t size
)
67 /* Just leak small blocks. We don't have to be perfect. */
68 if (size
>= sizeof (struct backtrace_freelist_struct
))
70 struct backtrace_freelist_struct
*p
;
72 p
= (struct backtrace_freelist_struct
*) addr
;
73 p
->next
= state
->freelist
;
79 /* Allocate memory like malloc. */
82 backtrace_alloc (struct backtrace_state
*state
,
83 size_t size
, backtrace_error_callback error_callback
,
88 struct backtrace_freelist_struct
**pp
;
95 /* If we can acquire the lock, then see if there is space on the
96 free list. If we can't acquire the lock, drop straight into
97 using mmap. __sync_lock_test_and_set returns the old state of
98 the lock, so we have acquired it if it returns 0. */
100 if (!state
->threaded
)
103 locked
= __sync_lock_test_and_set (&state
->lock_alloc
, 1) == 0;
107 for (pp
= &state
->freelist
; *pp
!= NULL
; pp
= &(*pp
)->next
)
109 if ((*pp
)->size
>= size
)
111 struct backtrace_freelist_struct
*p
;
116 /* Round for alignment; we assume that no type we care about
117 is more than 8 bytes. */
118 size
= (size
+ 7) & ~ (size_t) 7;
120 backtrace_free_locked (state
, (char *) p
+ size
,
130 __sync_lock_release (&state
->lock_alloc
);
135 /* Allocate a new page. */
137 pagesize
= getpagesize ();
138 asksize
= (size
+ pagesize
- 1) & ~ (pagesize
- 1);
139 page
= mmap (NULL
, asksize
, PROT_READ
| PROT_WRITE
,
140 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
142 error_callback (data
, "mmap", errno
);
145 size
= (size
+ 7) & ~ (size_t) 7;
147 backtrace_free (state
, (char *) page
+ size
, asksize
- size
,
148 error_callback
, data
);
157 /* Free memory allocated by backtrace_alloc. */
160 backtrace_free (struct backtrace_state
*state
, void *addr
, size_t size
,
161 backtrace_error_callback error_callback ATTRIBUTE_UNUSED
,
162 void *data ATTRIBUTE_UNUSED
)
166 /* If we can acquire the lock, add the new space to the free list.
167 If we can't acquire the lock, just leak the memory.
168 __sync_lock_test_and_set returns the old state of the lock, so we
169 have acquired it if it returns 0. */
171 if (!state
->threaded
)
174 locked
= __sync_lock_test_and_set (&state
->lock_alloc
, 1) == 0;
178 backtrace_free_locked (state
, addr
, size
);
181 __sync_lock_release (&state
->lock_alloc
);
185 /* Grow VEC by SIZE bytes. */
188 backtrace_vector_grow (struct backtrace_state
*state
,size_t size
,
189 backtrace_error_callback error_callback
,
190 void *data
, struct backtrace_vector
*vec
)
200 pagesize
= getpagesize ();
201 alc
= vec
->size
+ size
;
204 else if (alc
< pagesize
)
211 alc
= (alc
+ pagesize
- 1) & ~ (pagesize
- 1);
212 base
= backtrace_alloc (state
, alc
, error_callback
, data
);
215 if (vec
->base
!= NULL
)
217 memcpy (base
, vec
->base
, vec
->size
);
218 backtrace_free (state
, vec
->base
, vec
->alc
, error_callback
, data
);
221 vec
->alc
= alc
- vec
->size
;
224 ret
= (char *) vec
->base
+ vec
->size
;
230 /* Finish the current allocation on VEC. */
233 backtrace_vector_finish (struct backtrace_state
*state ATTRIBUTE_UNUSED
,
234 struct backtrace_vector
*vec
)
236 vec
->base
= (char *) vec
->base
+ vec
->size
;
240 /* Release any extra space allocated for VEC. */
243 backtrace_vector_release (struct backtrace_state
*state
,
244 struct backtrace_vector
*vec
,
245 backtrace_error_callback error_callback
,
252 /* Make sure that the block that we free is aligned on an 8-byte
256 aligned
= (size
+ 7) & ~ (size_t) 7;
257 alc
-= aligned
- size
;
259 backtrace_free (state
, (char *) vec
->base
+ aligned
, alc
,
260 error_callback
, data
);