1 /* libunwind - a platform-independent unwind library
2 Copyright (C) 2002-2003, 2005 Hewlett-Packard Co
3 Contributed by David Mosberger-Tang <davidm@hpl.hp.com>
4 Copyright (C) 2012 Tommi Rantala <tt.rantala@gmail.com>
5
6 This file is part of libunwind.
7
8 Permission is hereby granted, free of charge, to any person obtaining
9 a copy of this software and associated documentation files (the
10 "Software"), to deal in the Software without restriction, including
11 without limitation the rights to use, copy, modify, merge, publish,
12 distribute, sublicense, and/or sell copies of the Software, and to
13 permit persons to whom the Software is furnished to do so, subject to
14 the following conditions:
15
16 The above copyright notice and this permission notice shall be
17 included in all copies or substantial portions of the Software.
18
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
26
27 #include "libunwind_i.h"
28 #include <stdalign.h>
29 #include <stdatomic.h>
30
31 /* From GCC docs: ``Gcc also provides a target specific macro
32 * __BIGGEST_ALIGNMENT__, which is the largest alignment ever used for any data
33 * type on the target machine you are compiling for.'' */
34 #ifdef __BIGGEST_ALIGNMENT__
35 # define MAX_ALIGN __BIGGEST_ALIGNMENT__
36 #else
37 /* Crude hack to check that MAX_ALIGN is power-of-two.
38 * sizeof(long double) = 12 on i386. */
39 # define MAX_ALIGN_(n) (n < 8 ? 8 : \
40 n < 16 ? 16 : n)
41 # define MAX_ALIGN MAX_ALIGN_(sizeof (long double))
42 #endif
43
44 static alignas(MAX_ALIGN) char sos_memory[SOS_MEMORY_SIZE];
45 static _Atomic size_t sos_memory_freepos = 0;
46 static size_t pg_size;
47
48 HIDDEN void *
sos_alloc(size_t size)49 sos_alloc (size_t size)
50 {
51 size_t pos;
52
53 size = UNW_ALIGN(size, MAX_ALIGN);
54
55 /* Assume `sos_memory' is suitably aligned. */
56 assert(((uintptr_t) &sos_memory[0] & (MAX_ALIGN-1)) == 0);
57
58 pos = atomic_fetch_add (&sos_memory_freepos, size);
59
60 assert (((uintptr_t) &sos_memory[pos] & (MAX_ALIGN-1)) == 0);
61 assert ((pos+size) <= SOS_MEMORY_SIZE);
62
63 return &sos_memory[pos];
64 }
65
66 /* Must be called while holding the mempool lock. */
67
68 static void
free_object(struct mempool * pool,void * object)69 free_object (struct mempool *pool, void *object)
70 {
71 struct object *obj = object;
72
73 obj->next = pool->free_list;
74 pool->free_list = obj;
75 ++pool->num_free;
76 }
77
78 static void
add_memory(struct mempool * pool,char * mem,size_t size,size_t obj_size)79 add_memory (struct mempool *pool, char *mem, size_t size, size_t obj_size)
80 {
81 char *obj;
82
83 for (obj = mem; obj <= mem + size - obj_size; obj += obj_size)
84 free_object (pool, obj);
85 }
86
87 static void
expand(struct mempool * pool)88 expand (struct mempool *pool)
89 {
90 size_t size;
91 char *mem;
92
93 size = pool->chunk_size;
94 GET_MEMORY (mem, size);
95 if (!mem)
96 {
97 size = UNW_ALIGN(pool->obj_size, pg_size);
98 GET_MEMORY (mem, size);
99 if (!mem)
100 {
101 /* last chance: try to allocate one object from the SOS memory */
102 size = pool->obj_size;
103 mem = sos_alloc (size);
104 }
105 }
106 add_memory (pool, mem, size, pool->obj_size);
107 }
108
109 HIDDEN void
mempool_init(struct mempool * pool,size_t obj_size,size_t reserve)110 mempool_init (struct mempool *pool, size_t obj_size, size_t reserve)
111 {
112 if (pg_size == 0)
113 pg_size = getpagesize ();
114
115 memset (pool, 0, sizeof (*pool));
116
117 lock_init (&pool->lock);
118
119 /* round object-size up to integer multiple of MAX_ALIGN */
120 obj_size = UNW_ALIGN(obj_size, MAX_ALIGN);
121
122 if (!reserve)
123 {
124 reserve = pg_size / obj_size / 4;
125 if (!reserve)
126 reserve = 16;
127 }
128
129 pool->obj_size = obj_size;
130 pool->reserve = reserve;
131 pool->chunk_size = UNW_ALIGN(2*reserve*obj_size, pg_size);
132
133 expand (pool);
134 }
135
136 HIDDEN void *
mempool_alloc(struct mempool * pool)137 mempool_alloc (struct mempool *pool)
138 {
139 intrmask_t saved_mask;
140 struct object *obj;
141
142 lock_acquire (&pool->lock, saved_mask);
143 {
144 if (pool->num_free <= pool->reserve)
145 expand (pool);
146
147 assert (pool->num_free > 0);
148
149 --pool->num_free;
150 obj = pool->free_list;
151 pool->free_list = obj->next;
152 }
153 lock_release (&pool->lock, saved_mask);
154 return obj;
155 }
156
157 HIDDEN void
mempool_free(struct mempool * pool,void * object)158 mempool_free (struct mempool *pool, void *object)
159 {
160 intrmask_t saved_mask;
161
162 lock_acquire (&pool->lock, saved_mask);
163 {
164 free_object (pool, object);
165 }
166 lock_release (&pool->lock, saved_mask);
167 }
168