1 /* mmap.c -- Memory allocation with mmap.
2 Copyright (C) 2012 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
,
87 struct backtrace_freelist_struct
**pp
;
94 /* If we can acquire the lock, then see if there is space on the
95 free list. If we can't acquire the lock, drop straight into
96 using mmap. __sync_lock_test_and_set returns the old state of
97 the lock, so we have acquired it if it returns 0. */
99 if (!__sync_lock_test_and_set (&state
->lock_alloc
, 1))
101 for (pp
= &state
->freelist
; *pp
!= NULL
; pp
= &(*pp
)->next
)
103 if ((*pp
)->size
>= size
)
105 struct backtrace_freelist_struct
*p
;
110 /* Round for alignment; we assume that no type we care about
111 is more than 8 bytes. */
112 size
= (size
+ 7) & ~ (size_t) 7;
114 backtrace_free_locked (state
, (char *) p
+ size
,
123 __sync_lock_release (&state
->lock_alloc
);
128 /* Allocate a new page. */
130 pagesize
= getpagesize ();
131 asksize
= (size
+ pagesize
- 1) & ~ (pagesize
- 1);
132 page
= mmap (NULL
, asksize
, PROT_READ
| PROT_WRITE
,
133 MAP_PRIVATE
| MAP_ANONYMOUS
, -1, 0);
135 error_callback (data
, "mmap", errno
);
138 size
= (size
+ 7) & ~ (size_t) 7;
140 backtrace_free (state
, (char *) page
+ size
, asksize
- size
,
141 error_callback
, data
);
150 /* Free memory allocated by backtrace_alloc. */
153 backtrace_free (struct backtrace_state
*state
, void *addr
, size_t size
,
154 backtrace_error_callback error_callback ATTRIBUTE_UNUSED
,
155 void *data ATTRIBUTE_UNUSED
)
157 /* If we can acquire the lock, add the new space to the free list.
158 If we can't acquire the lock, just leak the memory.
159 __sync_lock_test_and_set returns the old state of the lock, so we
160 have acquired it if it returns 0. */
161 if (!__sync_lock_test_and_set (&state
->lock_alloc
, 1))
163 backtrace_free_locked (state
, addr
, size
);
165 __sync_lock_release (&state
->lock_alloc
);
169 /* Grow VEC by SIZE bytes. */
172 backtrace_vector_grow (struct backtrace_state
*state
,size_t size
,
173 backtrace_error_callback error_callback
,
174 void *data
, struct backtrace_vector
*vec
)
184 pagesize
= getpagesize ();
185 alc
= vec
->size
+ size
;
188 else if (alc
< pagesize
)
195 alc
= (alc
+ pagesize
- 1) & ~ (pagesize
- 1);
196 base
= backtrace_alloc (state
, alc
, error_callback
, data
);
199 if (vec
->base
!= NULL
)
201 memcpy (base
, vec
->base
, vec
->size
);
202 backtrace_free (state
, vec
->base
, vec
->alc
, error_callback
, data
);
205 vec
->alc
= alc
- vec
->size
;
208 ret
= (char *) vec
->base
+ vec
->size
;
214 /* Finish the current allocation on VEC. */
217 backtrace_vector_finish (struct backtrace_state
*state ATTRIBUTE_UNUSED
,
218 struct backtrace_vector
*vec
)
220 vec
->base
= (char *) vec
->base
+ vec
->size
;
224 /* Release any extra space allocated for VEC. */
227 backtrace_vector_release (struct backtrace_state
*state
,
228 struct backtrace_vector
*vec
,
229 backtrace_error_callback error_callback
,
236 /* Make sure that the block that we free is aligned on an 8-byte
240 aligned
= (size
+ 7) & ~ (size_t) 7;
241 alc
-= aligned
- size
;
243 backtrace_free (state
, (char *) vec
->base
+ aligned
, alc
,
244 error_callback
, data
);