1*61046927SAndroid Build Coastguard Worker /*
2*61046927SAndroid Build Coastguard Worker * Copyright © 2010 Intel Corporation
3*61046927SAndroid Build Coastguard Worker *
4*61046927SAndroid Build Coastguard Worker * Permission is hereby granted, free of charge, to any person obtaining a
5*61046927SAndroid Build Coastguard Worker * copy of this software and associated documentation files (the "Software"),
6*61046927SAndroid Build Coastguard Worker * to deal in the Software without restriction, including without limitation
7*61046927SAndroid Build Coastguard Worker * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8*61046927SAndroid Build Coastguard Worker * and/or sell copies of the Software, and to permit persons to whom the
9*61046927SAndroid Build Coastguard Worker * Software is furnished to do so, subject to the following conditions:
10*61046927SAndroid Build Coastguard Worker *
11*61046927SAndroid Build Coastguard Worker * The above copyright notice and this permission notice (including the next
12*61046927SAndroid Build Coastguard Worker * paragraph) shall be included in all copies or substantial portions of the
13*61046927SAndroid Build Coastguard Worker * Software.
14*61046927SAndroid Build Coastguard Worker *
15*61046927SAndroid Build Coastguard Worker * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16*61046927SAndroid Build Coastguard Worker * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17*61046927SAndroid Build Coastguard Worker * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18*61046927SAndroid Build Coastguard Worker * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19*61046927SAndroid Build Coastguard Worker * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20*61046927SAndroid Build Coastguard Worker * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21*61046927SAndroid Build Coastguard Worker * DEALINGS IN THE SOFTWARE.
22*61046927SAndroid Build Coastguard Worker */
23*61046927SAndroid Build Coastguard Worker
24*61046927SAndroid Build Coastguard Worker #include <assert.h>
25*61046927SAndroid Build Coastguard Worker #include <stdarg.h>
26*61046927SAndroid Build Coastguard Worker #include <stdint.h>
27*61046927SAndroid Build Coastguard Worker #include <stdio.h>
28*61046927SAndroid Build Coastguard Worker #include <stdlib.h>
29*61046927SAndroid Build Coastguard Worker #include <string.h>
30*61046927SAndroid Build Coastguard Worker
31*61046927SAndroid Build Coastguard Worker #include "util/list.h"
32*61046927SAndroid Build Coastguard Worker #include "util/macros.h"
33*61046927SAndroid Build Coastguard Worker #include "util/u_math.h"
34*61046927SAndroid Build Coastguard Worker #include "util/u_printf.h"
35*61046927SAndroid Build Coastguard Worker
36*61046927SAndroid Build Coastguard Worker #include "ralloc.h"
37*61046927SAndroid Build Coastguard Worker
38*61046927SAndroid Build Coastguard Worker #define CANARY 0x5A1106
39*61046927SAndroid Build Coastguard Worker
40*61046927SAndroid Build Coastguard Worker #if defined(__LP64__) || defined(_WIN64)
41*61046927SAndroid Build Coastguard Worker #define HEADER_ALIGN 16
42*61046927SAndroid Build Coastguard Worker #else
43*61046927SAndroid Build Coastguard Worker #define HEADER_ALIGN 8
44*61046927SAndroid Build Coastguard Worker #endif
45*61046927SAndroid Build Coastguard Worker
46*61046927SAndroid Build Coastguard Worker /* Align the header's size so that ralloc() allocations will return with the
47*61046927SAndroid Build Coastguard Worker * same alignment as a libc malloc would have (8 on 32-bit GLIBC, 16 on
48*61046927SAndroid Build Coastguard Worker * 64-bit), avoiding performance penalities on x86 and alignment faults on
49*61046927SAndroid Build Coastguard Worker * ARM.
50*61046927SAndroid Build Coastguard Worker */
51*61046927SAndroid Build Coastguard Worker struct ralloc_header
52*61046927SAndroid Build Coastguard Worker {
53*61046927SAndroid Build Coastguard Worker alignas(HEADER_ALIGN)
54*61046927SAndroid Build Coastguard Worker
55*61046927SAndroid Build Coastguard Worker #ifndef NDEBUG
56*61046927SAndroid Build Coastguard Worker /* A canary value used to determine whether a pointer is ralloc'd. */
57*61046927SAndroid Build Coastguard Worker unsigned canary;
58*61046927SAndroid Build Coastguard Worker unsigned size;
59*61046927SAndroid Build Coastguard Worker #endif
60*61046927SAndroid Build Coastguard Worker
61*61046927SAndroid Build Coastguard Worker struct ralloc_header *parent;
62*61046927SAndroid Build Coastguard Worker
63*61046927SAndroid Build Coastguard Worker /* The first child (head of a linked list) */
64*61046927SAndroid Build Coastguard Worker struct ralloc_header *child;
65*61046927SAndroid Build Coastguard Worker
66*61046927SAndroid Build Coastguard Worker /* Linked list of siblings */
67*61046927SAndroid Build Coastguard Worker struct ralloc_header *prev;
68*61046927SAndroid Build Coastguard Worker struct ralloc_header *next;
69*61046927SAndroid Build Coastguard Worker
70*61046927SAndroid Build Coastguard Worker void (*destructor)(void *);
71*61046927SAndroid Build Coastguard Worker };
72*61046927SAndroid Build Coastguard Worker
73*61046927SAndroid Build Coastguard Worker typedef struct ralloc_header ralloc_header;
74*61046927SAndroid Build Coastguard Worker
75*61046927SAndroid Build Coastguard Worker static void unlink_block(ralloc_header *info);
76*61046927SAndroid Build Coastguard Worker static void unsafe_free(ralloc_header *info);
77*61046927SAndroid Build Coastguard Worker
78*61046927SAndroid Build Coastguard Worker static ralloc_header *
get_header(const void * ptr)79*61046927SAndroid Build Coastguard Worker get_header(const void *ptr)
80*61046927SAndroid Build Coastguard Worker {
81*61046927SAndroid Build Coastguard Worker ralloc_header *info = (ralloc_header *) (((char *) ptr) -
82*61046927SAndroid Build Coastguard Worker sizeof(ralloc_header));
83*61046927SAndroid Build Coastguard Worker assert(info->canary == CANARY);
84*61046927SAndroid Build Coastguard Worker return info;
85*61046927SAndroid Build Coastguard Worker }
86*61046927SAndroid Build Coastguard Worker
87*61046927SAndroid Build Coastguard Worker #define PTR_FROM_HEADER(info) (((char *) info) + sizeof(ralloc_header))
88*61046927SAndroid Build Coastguard Worker
89*61046927SAndroid Build Coastguard Worker static void
add_child(ralloc_header * parent,ralloc_header * info)90*61046927SAndroid Build Coastguard Worker add_child(ralloc_header *parent, ralloc_header *info)
91*61046927SAndroid Build Coastguard Worker {
92*61046927SAndroid Build Coastguard Worker if (parent != NULL) {
93*61046927SAndroid Build Coastguard Worker info->parent = parent;
94*61046927SAndroid Build Coastguard Worker info->next = parent->child;
95*61046927SAndroid Build Coastguard Worker parent->child = info;
96*61046927SAndroid Build Coastguard Worker
97*61046927SAndroid Build Coastguard Worker if (info->next != NULL)
98*61046927SAndroid Build Coastguard Worker info->next->prev = info;
99*61046927SAndroid Build Coastguard Worker }
100*61046927SAndroid Build Coastguard Worker }
101*61046927SAndroid Build Coastguard Worker
102*61046927SAndroid Build Coastguard Worker void *
ralloc_context(const void * ctx)103*61046927SAndroid Build Coastguard Worker ralloc_context(const void *ctx)
104*61046927SAndroid Build Coastguard Worker {
105*61046927SAndroid Build Coastguard Worker return ralloc_size(ctx, 0);
106*61046927SAndroid Build Coastguard Worker }
107*61046927SAndroid Build Coastguard Worker
108*61046927SAndroid Build Coastguard Worker void *
ralloc_size(const void * ctx,size_t size)109*61046927SAndroid Build Coastguard Worker ralloc_size(const void *ctx, size_t size)
110*61046927SAndroid Build Coastguard Worker {
111*61046927SAndroid Build Coastguard Worker /* Some malloc allocation doesn't always align to 16 bytes even on 64 bits
112*61046927SAndroid Build Coastguard Worker * system, from Android bionic/tests/malloc_test.cpp:
113*61046927SAndroid Build Coastguard Worker * - Allocations of a size that rounds up to a multiple of 16 bytes
114*61046927SAndroid Build Coastguard Worker * must have at least 16 byte alignment.
115*61046927SAndroid Build Coastguard Worker * - Allocations of a size that rounds up to a multiple of 8 bytes and
116*61046927SAndroid Build Coastguard Worker * not 16 bytes, are only required to have at least 8 byte alignment.
117*61046927SAndroid Build Coastguard Worker */
118*61046927SAndroid Build Coastguard Worker void *block = malloc(align64(size + sizeof(ralloc_header),
119*61046927SAndroid Build Coastguard Worker alignof(ralloc_header)));
120*61046927SAndroid Build Coastguard Worker ralloc_header *info;
121*61046927SAndroid Build Coastguard Worker ralloc_header *parent;
122*61046927SAndroid Build Coastguard Worker
123*61046927SAndroid Build Coastguard Worker if (unlikely(block == NULL))
124*61046927SAndroid Build Coastguard Worker return NULL;
125*61046927SAndroid Build Coastguard Worker
126*61046927SAndroid Build Coastguard Worker info = (ralloc_header *) block;
127*61046927SAndroid Build Coastguard Worker /* measurements have shown that calloc is slower (because of
128*61046927SAndroid Build Coastguard Worker * the multiplication overflow checking?), so clear things
129*61046927SAndroid Build Coastguard Worker * manually
130*61046927SAndroid Build Coastguard Worker */
131*61046927SAndroid Build Coastguard Worker info->parent = NULL;
132*61046927SAndroid Build Coastguard Worker info->child = NULL;
133*61046927SAndroid Build Coastguard Worker info->prev = NULL;
134*61046927SAndroid Build Coastguard Worker info->next = NULL;
135*61046927SAndroid Build Coastguard Worker info->destructor = NULL;
136*61046927SAndroid Build Coastguard Worker
137*61046927SAndroid Build Coastguard Worker parent = ctx != NULL ? get_header(ctx) : NULL;
138*61046927SAndroid Build Coastguard Worker
139*61046927SAndroid Build Coastguard Worker add_child(parent, info);
140*61046927SAndroid Build Coastguard Worker
141*61046927SAndroid Build Coastguard Worker #ifndef NDEBUG
142*61046927SAndroid Build Coastguard Worker info->canary = CANARY;
143*61046927SAndroid Build Coastguard Worker info->size = size;
144*61046927SAndroid Build Coastguard Worker #endif
145*61046927SAndroid Build Coastguard Worker
146*61046927SAndroid Build Coastguard Worker return PTR_FROM_HEADER(info);
147*61046927SAndroid Build Coastguard Worker }
148*61046927SAndroid Build Coastguard Worker
149*61046927SAndroid Build Coastguard Worker void *
rzalloc_size(const void * ctx,size_t size)150*61046927SAndroid Build Coastguard Worker rzalloc_size(const void *ctx, size_t size)
151*61046927SAndroid Build Coastguard Worker {
152*61046927SAndroid Build Coastguard Worker void *ptr = ralloc_size(ctx, size);
153*61046927SAndroid Build Coastguard Worker
154*61046927SAndroid Build Coastguard Worker if (likely(ptr))
155*61046927SAndroid Build Coastguard Worker memset(ptr, 0, size);
156*61046927SAndroid Build Coastguard Worker
157*61046927SAndroid Build Coastguard Worker return ptr;
158*61046927SAndroid Build Coastguard Worker }
159*61046927SAndroid Build Coastguard Worker
160*61046927SAndroid Build Coastguard Worker /* helper function - assumes ptr != NULL */
161*61046927SAndroid Build Coastguard Worker static void *
resize(void * ptr,size_t size)162*61046927SAndroid Build Coastguard Worker resize(void *ptr, size_t size)
163*61046927SAndroid Build Coastguard Worker {
164*61046927SAndroid Build Coastguard Worker ralloc_header *child, *old, *info;
165*61046927SAndroid Build Coastguard Worker
166*61046927SAndroid Build Coastguard Worker old = get_header(ptr);
167*61046927SAndroid Build Coastguard Worker info = realloc(old, align64(size + sizeof(ralloc_header),
168*61046927SAndroid Build Coastguard Worker alignof(ralloc_header)));
169*61046927SAndroid Build Coastguard Worker
170*61046927SAndroid Build Coastguard Worker if (info == NULL)
171*61046927SAndroid Build Coastguard Worker return NULL;
172*61046927SAndroid Build Coastguard Worker
173*61046927SAndroid Build Coastguard Worker /* Update parent and sibling's links to the reallocated node. */
174*61046927SAndroid Build Coastguard Worker if (info != old && info->parent != NULL) {
175*61046927SAndroid Build Coastguard Worker if (info->parent->child == old)
176*61046927SAndroid Build Coastguard Worker info->parent->child = info;
177*61046927SAndroid Build Coastguard Worker
178*61046927SAndroid Build Coastguard Worker if (info->prev != NULL)
179*61046927SAndroid Build Coastguard Worker info->prev->next = info;
180*61046927SAndroid Build Coastguard Worker
181*61046927SAndroid Build Coastguard Worker if (info->next != NULL)
182*61046927SAndroid Build Coastguard Worker info->next->prev = info;
183*61046927SAndroid Build Coastguard Worker }
184*61046927SAndroid Build Coastguard Worker
185*61046927SAndroid Build Coastguard Worker /* Update child->parent links for all children */
186*61046927SAndroid Build Coastguard Worker for (child = info->child; child != NULL; child = child->next)
187*61046927SAndroid Build Coastguard Worker child->parent = info;
188*61046927SAndroid Build Coastguard Worker
189*61046927SAndroid Build Coastguard Worker return PTR_FROM_HEADER(info);
190*61046927SAndroid Build Coastguard Worker }
191*61046927SAndroid Build Coastguard Worker
192*61046927SAndroid Build Coastguard Worker void *
reralloc_size(const void * ctx,void * ptr,size_t size)193*61046927SAndroid Build Coastguard Worker reralloc_size(const void *ctx, void *ptr, size_t size)
194*61046927SAndroid Build Coastguard Worker {
195*61046927SAndroid Build Coastguard Worker if (unlikely(ptr == NULL))
196*61046927SAndroid Build Coastguard Worker return ralloc_size(ctx, size);
197*61046927SAndroid Build Coastguard Worker
198*61046927SAndroid Build Coastguard Worker assert(ralloc_parent(ptr) == ctx);
199*61046927SAndroid Build Coastguard Worker return resize(ptr, size);
200*61046927SAndroid Build Coastguard Worker }
201*61046927SAndroid Build Coastguard Worker
202*61046927SAndroid Build Coastguard Worker void *
rerzalloc_size(const void * ctx,void * ptr,size_t old_size,size_t new_size)203*61046927SAndroid Build Coastguard Worker rerzalloc_size(const void *ctx, void *ptr, size_t old_size, size_t new_size)
204*61046927SAndroid Build Coastguard Worker {
205*61046927SAndroid Build Coastguard Worker if (unlikely(ptr == NULL))
206*61046927SAndroid Build Coastguard Worker return rzalloc_size(ctx, new_size);
207*61046927SAndroid Build Coastguard Worker
208*61046927SAndroid Build Coastguard Worker assert(ralloc_parent(ptr) == ctx);
209*61046927SAndroid Build Coastguard Worker ptr = resize(ptr, new_size);
210*61046927SAndroid Build Coastguard Worker
211*61046927SAndroid Build Coastguard Worker if (new_size > old_size)
212*61046927SAndroid Build Coastguard Worker memset((char *)ptr + old_size, 0, new_size - old_size);
213*61046927SAndroid Build Coastguard Worker
214*61046927SAndroid Build Coastguard Worker return ptr;
215*61046927SAndroid Build Coastguard Worker }
216*61046927SAndroid Build Coastguard Worker
217*61046927SAndroid Build Coastguard Worker void *
ralloc_array_size(const void * ctx,size_t size,unsigned count)218*61046927SAndroid Build Coastguard Worker ralloc_array_size(const void *ctx, size_t size, unsigned count)
219*61046927SAndroid Build Coastguard Worker {
220*61046927SAndroid Build Coastguard Worker if (count > SIZE_MAX/size)
221*61046927SAndroid Build Coastguard Worker return NULL;
222*61046927SAndroid Build Coastguard Worker
223*61046927SAndroid Build Coastguard Worker return ralloc_size(ctx, size * count);
224*61046927SAndroid Build Coastguard Worker }
225*61046927SAndroid Build Coastguard Worker
226*61046927SAndroid Build Coastguard Worker void *
rzalloc_array_size(const void * ctx,size_t size,unsigned count)227*61046927SAndroid Build Coastguard Worker rzalloc_array_size(const void *ctx, size_t size, unsigned count)
228*61046927SAndroid Build Coastguard Worker {
229*61046927SAndroid Build Coastguard Worker if (count > SIZE_MAX/size)
230*61046927SAndroid Build Coastguard Worker return NULL;
231*61046927SAndroid Build Coastguard Worker
232*61046927SAndroid Build Coastguard Worker return rzalloc_size(ctx, size * count);
233*61046927SAndroid Build Coastguard Worker }
234*61046927SAndroid Build Coastguard Worker
235*61046927SAndroid Build Coastguard Worker void *
reralloc_array_size(const void * ctx,void * ptr,size_t size,unsigned count)236*61046927SAndroid Build Coastguard Worker reralloc_array_size(const void *ctx, void *ptr, size_t size, unsigned count)
237*61046927SAndroid Build Coastguard Worker {
238*61046927SAndroid Build Coastguard Worker if (count > SIZE_MAX/size)
239*61046927SAndroid Build Coastguard Worker return NULL;
240*61046927SAndroid Build Coastguard Worker
241*61046927SAndroid Build Coastguard Worker return reralloc_size(ctx, ptr, size * count);
242*61046927SAndroid Build Coastguard Worker }
243*61046927SAndroid Build Coastguard Worker
244*61046927SAndroid Build Coastguard Worker void *
rerzalloc_array_size(const void * ctx,void * ptr,size_t size,unsigned old_count,unsigned new_count)245*61046927SAndroid Build Coastguard Worker rerzalloc_array_size(const void *ctx, void *ptr, size_t size,
246*61046927SAndroid Build Coastguard Worker unsigned old_count, unsigned new_count)
247*61046927SAndroid Build Coastguard Worker {
248*61046927SAndroid Build Coastguard Worker if (new_count > SIZE_MAX/size)
249*61046927SAndroid Build Coastguard Worker return NULL;
250*61046927SAndroid Build Coastguard Worker
251*61046927SAndroid Build Coastguard Worker return rerzalloc_size(ctx, ptr, size * old_count, size * new_count);
252*61046927SAndroid Build Coastguard Worker }
253*61046927SAndroid Build Coastguard Worker
254*61046927SAndroid Build Coastguard Worker void
ralloc_free(void * ptr)255*61046927SAndroid Build Coastguard Worker ralloc_free(void *ptr)
256*61046927SAndroid Build Coastguard Worker {
257*61046927SAndroid Build Coastguard Worker ralloc_header *info;
258*61046927SAndroid Build Coastguard Worker
259*61046927SAndroid Build Coastguard Worker if (ptr == NULL)
260*61046927SAndroid Build Coastguard Worker return;
261*61046927SAndroid Build Coastguard Worker
262*61046927SAndroid Build Coastguard Worker info = get_header(ptr);
263*61046927SAndroid Build Coastguard Worker unlink_block(info);
264*61046927SAndroid Build Coastguard Worker unsafe_free(info);
265*61046927SAndroid Build Coastguard Worker }
266*61046927SAndroid Build Coastguard Worker
267*61046927SAndroid Build Coastguard Worker static void
unlink_block(ralloc_header * info)268*61046927SAndroid Build Coastguard Worker unlink_block(ralloc_header *info)
269*61046927SAndroid Build Coastguard Worker {
270*61046927SAndroid Build Coastguard Worker /* Unlink from parent & siblings */
271*61046927SAndroid Build Coastguard Worker if (info->parent != NULL) {
272*61046927SAndroid Build Coastguard Worker if (info->parent->child == info)
273*61046927SAndroid Build Coastguard Worker info->parent->child = info->next;
274*61046927SAndroid Build Coastguard Worker
275*61046927SAndroid Build Coastguard Worker if (info->prev != NULL)
276*61046927SAndroid Build Coastguard Worker info->prev->next = info->next;
277*61046927SAndroid Build Coastguard Worker
278*61046927SAndroid Build Coastguard Worker if (info->next != NULL)
279*61046927SAndroid Build Coastguard Worker info->next->prev = info->prev;
280*61046927SAndroid Build Coastguard Worker }
281*61046927SAndroid Build Coastguard Worker info->parent = NULL;
282*61046927SAndroid Build Coastguard Worker info->prev = NULL;
283*61046927SAndroid Build Coastguard Worker info->next = NULL;
284*61046927SAndroid Build Coastguard Worker }
285*61046927SAndroid Build Coastguard Worker
286*61046927SAndroid Build Coastguard Worker static void
unsafe_free(ralloc_header * info)287*61046927SAndroid Build Coastguard Worker unsafe_free(ralloc_header *info)
288*61046927SAndroid Build Coastguard Worker {
289*61046927SAndroid Build Coastguard Worker /* Recursively free any children...don't waste time unlinking them. */
290*61046927SAndroid Build Coastguard Worker ralloc_header *temp;
291*61046927SAndroid Build Coastguard Worker while (info->child != NULL) {
292*61046927SAndroid Build Coastguard Worker temp = info->child;
293*61046927SAndroid Build Coastguard Worker info->child = temp->next;
294*61046927SAndroid Build Coastguard Worker unsafe_free(temp);
295*61046927SAndroid Build Coastguard Worker }
296*61046927SAndroid Build Coastguard Worker
297*61046927SAndroid Build Coastguard Worker /* Free the block itself. Call the destructor first, if any. */
298*61046927SAndroid Build Coastguard Worker if (info->destructor != NULL)
299*61046927SAndroid Build Coastguard Worker info->destructor(PTR_FROM_HEADER(info));
300*61046927SAndroid Build Coastguard Worker
301*61046927SAndroid Build Coastguard Worker free(info);
302*61046927SAndroid Build Coastguard Worker }
303*61046927SAndroid Build Coastguard Worker
304*61046927SAndroid Build Coastguard Worker void
ralloc_steal(const void * new_ctx,void * ptr)305*61046927SAndroid Build Coastguard Worker ralloc_steal(const void *new_ctx, void *ptr)
306*61046927SAndroid Build Coastguard Worker {
307*61046927SAndroid Build Coastguard Worker ralloc_header *info, *parent;
308*61046927SAndroid Build Coastguard Worker
309*61046927SAndroid Build Coastguard Worker if (unlikely(ptr == NULL))
310*61046927SAndroid Build Coastguard Worker return;
311*61046927SAndroid Build Coastguard Worker
312*61046927SAndroid Build Coastguard Worker info = get_header(ptr);
313*61046927SAndroid Build Coastguard Worker parent = new_ctx ? get_header(new_ctx) : NULL;
314*61046927SAndroid Build Coastguard Worker
315*61046927SAndroid Build Coastguard Worker unlink_block(info);
316*61046927SAndroid Build Coastguard Worker
317*61046927SAndroid Build Coastguard Worker add_child(parent, info);
318*61046927SAndroid Build Coastguard Worker }
319*61046927SAndroid Build Coastguard Worker
320*61046927SAndroid Build Coastguard Worker void
ralloc_adopt(const void * new_ctx,void * old_ctx)321*61046927SAndroid Build Coastguard Worker ralloc_adopt(const void *new_ctx, void *old_ctx)
322*61046927SAndroid Build Coastguard Worker {
323*61046927SAndroid Build Coastguard Worker ralloc_header *new_info, *old_info, *child;
324*61046927SAndroid Build Coastguard Worker
325*61046927SAndroid Build Coastguard Worker if (unlikely(old_ctx == NULL))
326*61046927SAndroid Build Coastguard Worker return;
327*61046927SAndroid Build Coastguard Worker
328*61046927SAndroid Build Coastguard Worker old_info = get_header(old_ctx);
329*61046927SAndroid Build Coastguard Worker new_info = get_header(new_ctx);
330*61046927SAndroid Build Coastguard Worker
331*61046927SAndroid Build Coastguard Worker /* If there are no children, bail. */
332*61046927SAndroid Build Coastguard Worker if (unlikely(old_info->child == NULL))
333*61046927SAndroid Build Coastguard Worker return;
334*61046927SAndroid Build Coastguard Worker
335*61046927SAndroid Build Coastguard Worker /* Set all the children's parent to new_ctx; get a pointer to the last child. */
336*61046927SAndroid Build Coastguard Worker for (child = old_info->child; child->next != NULL; child = child->next) {
337*61046927SAndroid Build Coastguard Worker child->parent = new_info;
338*61046927SAndroid Build Coastguard Worker }
339*61046927SAndroid Build Coastguard Worker child->parent = new_info;
340*61046927SAndroid Build Coastguard Worker
341*61046927SAndroid Build Coastguard Worker /* Connect the two lists together; parent them to new_ctx; make old_ctx empty. */
342*61046927SAndroid Build Coastguard Worker child->next = new_info->child;
343*61046927SAndroid Build Coastguard Worker if (child->next)
344*61046927SAndroid Build Coastguard Worker child->next->prev = child;
345*61046927SAndroid Build Coastguard Worker new_info->child = old_info->child;
346*61046927SAndroid Build Coastguard Worker old_info->child = NULL;
347*61046927SAndroid Build Coastguard Worker }
348*61046927SAndroid Build Coastguard Worker
349*61046927SAndroid Build Coastguard Worker void *
ralloc_parent(const void * ptr)350*61046927SAndroid Build Coastguard Worker ralloc_parent(const void *ptr)
351*61046927SAndroid Build Coastguard Worker {
352*61046927SAndroid Build Coastguard Worker ralloc_header *info;
353*61046927SAndroid Build Coastguard Worker
354*61046927SAndroid Build Coastguard Worker if (unlikely(ptr == NULL))
355*61046927SAndroid Build Coastguard Worker return NULL;
356*61046927SAndroid Build Coastguard Worker
357*61046927SAndroid Build Coastguard Worker info = get_header(ptr);
358*61046927SAndroid Build Coastguard Worker return info->parent ? PTR_FROM_HEADER(info->parent) : NULL;
359*61046927SAndroid Build Coastguard Worker }
360*61046927SAndroid Build Coastguard Worker
361*61046927SAndroid Build Coastguard Worker void
ralloc_set_destructor(const void * ptr,void (* destructor)(void *))362*61046927SAndroid Build Coastguard Worker ralloc_set_destructor(const void *ptr, void(*destructor)(void *))
363*61046927SAndroid Build Coastguard Worker {
364*61046927SAndroid Build Coastguard Worker ralloc_header *info = get_header(ptr);
365*61046927SAndroid Build Coastguard Worker info->destructor = destructor;
366*61046927SAndroid Build Coastguard Worker }
367*61046927SAndroid Build Coastguard Worker
368*61046927SAndroid Build Coastguard Worker void *
ralloc_memdup(const void * ctx,const void * mem,size_t n)369*61046927SAndroid Build Coastguard Worker ralloc_memdup(const void *ctx, const void *mem, size_t n)
370*61046927SAndroid Build Coastguard Worker {
371*61046927SAndroid Build Coastguard Worker void *ptr = ralloc_size(ctx, n);
372*61046927SAndroid Build Coastguard Worker
373*61046927SAndroid Build Coastguard Worker if (unlikely(ptr == NULL))
374*61046927SAndroid Build Coastguard Worker return NULL;
375*61046927SAndroid Build Coastguard Worker
376*61046927SAndroid Build Coastguard Worker memcpy(ptr, mem, n);
377*61046927SAndroid Build Coastguard Worker return ptr;
378*61046927SAndroid Build Coastguard Worker }
379*61046927SAndroid Build Coastguard Worker
380*61046927SAndroid Build Coastguard Worker char *
ralloc_strdup(const void * ctx,const char * str)381*61046927SAndroid Build Coastguard Worker ralloc_strdup(const void *ctx, const char *str)
382*61046927SAndroid Build Coastguard Worker {
383*61046927SAndroid Build Coastguard Worker size_t n;
384*61046927SAndroid Build Coastguard Worker char *ptr;
385*61046927SAndroid Build Coastguard Worker
386*61046927SAndroid Build Coastguard Worker if (unlikely(str == NULL))
387*61046927SAndroid Build Coastguard Worker return NULL;
388*61046927SAndroid Build Coastguard Worker
389*61046927SAndroid Build Coastguard Worker n = strlen(str);
390*61046927SAndroid Build Coastguard Worker ptr = ralloc_array(ctx, char, n + 1);
391*61046927SAndroid Build Coastguard Worker memcpy(ptr, str, n);
392*61046927SAndroid Build Coastguard Worker ptr[n] = '\0';
393*61046927SAndroid Build Coastguard Worker return ptr;
394*61046927SAndroid Build Coastguard Worker }
395*61046927SAndroid Build Coastguard Worker
396*61046927SAndroid Build Coastguard Worker char *
ralloc_strndup(const void * ctx,const char * str,size_t max)397*61046927SAndroid Build Coastguard Worker ralloc_strndup(const void *ctx, const char *str, size_t max)
398*61046927SAndroid Build Coastguard Worker {
399*61046927SAndroid Build Coastguard Worker size_t n;
400*61046927SAndroid Build Coastguard Worker char *ptr;
401*61046927SAndroid Build Coastguard Worker
402*61046927SAndroid Build Coastguard Worker if (unlikely(str == NULL))
403*61046927SAndroid Build Coastguard Worker return NULL;
404*61046927SAndroid Build Coastguard Worker
405*61046927SAndroid Build Coastguard Worker n = strnlen(str, max);
406*61046927SAndroid Build Coastguard Worker ptr = ralloc_array(ctx, char, n + 1);
407*61046927SAndroid Build Coastguard Worker memcpy(ptr, str, n);
408*61046927SAndroid Build Coastguard Worker ptr[n] = '\0';
409*61046927SAndroid Build Coastguard Worker return ptr;
410*61046927SAndroid Build Coastguard Worker }
411*61046927SAndroid Build Coastguard Worker
412*61046927SAndroid Build Coastguard Worker /* helper routine for strcat/strncat - n is the exact amount to copy */
413*61046927SAndroid Build Coastguard Worker static bool
cat(char ** dest,const char * str,size_t n)414*61046927SAndroid Build Coastguard Worker cat(char **dest, const char *str, size_t n)
415*61046927SAndroid Build Coastguard Worker {
416*61046927SAndroid Build Coastguard Worker char *both;
417*61046927SAndroid Build Coastguard Worker size_t existing_length;
418*61046927SAndroid Build Coastguard Worker assert(dest != NULL && *dest != NULL);
419*61046927SAndroid Build Coastguard Worker
420*61046927SAndroid Build Coastguard Worker existing_length = strlen(*dest);
421*61046927SAndroid Build Coastguard Worker both = resize(*dest, existing_length + n + 1);
422*61046927SAndroid Build Coastguard Worker if (unlikely(both == NULL))
423*61046927SAndroid Build Coastguard Worker return false;
424*61046927SAndroid Build Coastguard Worker
425*61046927SAndroid Build Coastguard Worker memcpy(both + existing_length, str, n);
426*61046927SAndroid Build Coastguard Worker both[existing_length + n] = '\0';
427*61046927SAndroid Build Coastguard Worker
428*61046927SAndroid Build Coastguard Worker *dest = both;
429*61046927SAndroid Build Coastguard Worker return true;
430*61046927SAndroid Build Coastguard Worker }
431*61046927SAndroid Build Coastguard Worker
432*61046927SAndroid Build Coastguard Worker
433*61046927SAndroid Build Coastguard Worker bool
ralloc_strcat(char ** dest,const char * str)434*61046927SAndroid Build Coastguard Worker ralloc_strcat(char **dest, const char *str)
435*61046927SAndroid Build Coastguard Worker {
436*61046927SAndroid Build Coastguard Worker return cat(dest, str, strlen(str));
437*61046927SAndroid Build Coastguard Worker }
438*61046927SAndroid Build Coastguard Worker
439*61046927SAndroid Build Coastguard Worker bool
ralloc_strncat(char ** dest,const char * str,size_t n)440*61046927SAndroid Build Coastguard Worker ralloc_strncat(char **dest, const char *str, size_t n)
441*61046927SAndroid Build Coastguard Worker {
442*61046927SAndroid Build Coastguard Worker return cat(dest, str, strnlen(str, n));
443*61046927SAndroid Build Coastguard Worker }
444*61046927SAndroid Build Coastguard Worker
445*61046927SAndroid Build Coastguard Worker bool
ralloc_str_append(char ** dest,const char * str,size_t existing_length,size_t str_size)446*61046927SAndroid Build Coastguard Worker ralloc_str_append(char **dest, const char *str,
447*61046927SAndroid Build Coastguard Worker size_t existing_length, size_t str_size)
448*61046927SAndroid Build Coastguard Worker {
449*61046927SAndroid Build Coastguard Worker char *both;
450*61046927SAndroid Build Coastguard Worker assert(dest != NULL && *dest != NULL);
451*61046927SAndroid Build Coastguard Worker
452*61046927SAndroid Build Coastguard Worker both = resize(*dest, existing_length + str_size + 1);
453*61046927SAndroid Build Coastguard Worker if (unlikely(both == NULL))
454*61046927SAndroid Build Coastguard Worker return false;
455*61046927SAndroid Build Coastguard Worker
456*61046927SAndroid Build Coastguard Worker memcpy(both + existing_length, str, str_size);
457*61046927SAndroid Build Coastguard Worker both[existing_length + str_size] = '\0';
458*61046927SAndroid Build Coastguard Worker
459*61046927SAndroid Build Coastguard Worker *dest = both;
460*61046927SAndroid Build Coastguard Worker
461*61046927SAndroid Build Coastguard Worker return true;
462*61046927SAndroid Build Coastguard Worker }
463*61046927SAndroid Build Coastguard Worker
464*61046927SAndroid Build Coastguard Worker char *
ralloc_asprintf(const void * ctx,const char * fmt,...)465*61046927SAndroid Build Coastguard Worker ralloc_asprintf(const void *ctx, const char *fmt, ...)
466*61046927SAndroid Build Coastguard Worker {
467*61046927SAndroid Build Coastguard Worker char *ptr;
468*61046927SAndroid Build Coastguard Worker va_list args;
469*61046927SAndroid Build Coastguard Worker va_start(args, fmt);
470*61046927SAndroid Build Coastguard Worker ptr = ralloc_vasprintf(ctx, fmt, args);
471*61046927SAndroid Build Coastguard Worker va_end(args);
472*61046927SAndroid Build Coastguard Worker return ptr;
473*61046927SAndroid Build Coastguard Worker }
474*61046927SAndroid Build Coastguard Worker
475*61046927SAndroid Build Coastguard Worker char *
ralloc_vasprintf(const void * ctx,const char * fmt,va_list args)476*61046927SAndroid Build Coastguard Worker ralloc_vasprintf(const void *ctx, const char *fmt, va_list args)
477*61046927SAndroid Build Coastguard Worker {
478*61046927SAndroid Build Coastguard Worker size_t size = u_printf_length(fmt, args) + 1;
479*61046927SAndroid Build Coastguard Worker
480*61046927SAndroid Build Coastguard Worker char *ptr = ralloc_size(ctx, size);
481*61046927SAndroid Build Coastguard Worker if (ptr != NULL)
482*61046927SAndroid Build Coastguard Worker vsnprintf(ptr, size, fmt, args);
483*61046927SAndroid Build Coastguard Worker
484*61046927SAndroid Build Coastguard Worker return ptr;
485*61046927SAndroid Build Coastguard Worker }
486*61046927SAndroid Build Coastguard Worker
487*61046927SAndroid Build Coastguard Worker bool
ralloc_asprintf_append(char ** str,const char * fmt,...)488*61046927SAndroid Build Coastguard Worker ralloc_asprintf_append(char **str, const char *fmt, ...)
489*61046927SAndroid Build Coastguard Worker {
490*61046927SAndroid Build Coastguard Worker bool success;
491*61046927SAndroid Build Coastguard Worker va_list args;
492*61046927SAndroid Build Coastguard Worker va_start(args, fmt);
493*61046927SAndroid Build Coastguard Worker success = ralloc_vasprintf_append(str, fmt, args);
494*61046927SAndroid Build Coastguard Worker va_end(args);
495*61046927SAndroid Build Coastguard Worker return success;
496*61046927SAndroid Build Coastguard Worker }
497*61046927SAndroid Build Coastguard Worker
498*61046927SAndroid Build Coastguard Worker bool
ralloc_vasprintf_append(char ** str,const char * fmt,va_list args)499*61046927SAndroid Build Coastguard Worker ralloc_vasprintf_append(char **str, const char *fmt, va_list args)
500*61046927SAndroid Build Coastguard Worker {
501*61046927SAndroid Build Coastguard Worker size_t existing_length;
502*61046927SAndroid Build Coastguard Worker assert(str != NULL);
503*61046927SAndroid Build Coastguard Worker existing_length = *str ? strlen(*str) : 0;
504*61046927SAndroid Build Coastguard Worker return ralloc_vasprintf_rewrite_tail(str, &existing_length, fmt, args);
505*61046927SAndroid Build Coastguard Worker }
506*61046927SAndroid Build Coastguard Worker
507*61046927SAndroid Build Coastguard Worker bool
ralloc_asprintf_rewrite_tail(char ** str,size_t * start,const char * fmt,...)508*61046927SAndroid Build Coastguard Worker ralloc_asprintf_rewrite_tail(char **str, size_t *start, const char *fmt, ...)
509*61046927SAndroid Build Coastguard Worker {
510*61046927SAndroid Build Coastguard Worker bool success;
511*61046927SAndroid Build Coastguard Worker va_list args;
512*61046927SAndroid Build Coastguard Worker va_start(args, fmt);
513*61046927SAndroid Build Coastguard Worker success = ralloc_vasprintf_rewrite_tail(str, start, fmt, args);
514*61046927SAndroid Build Coastguard Worker va_end(args);
515*61046927SAndroid Build Coastguard Worker return success;
516*61046927SAndroid Build Coastguard Worker }
517*61046927SAndroid Build Coastguard Worker
518*61046927SAndroid Build Coastguard Worker bool
ralloc_vasprintf_rewrite_tail(char ** str,size_t * start,const char * fmt,va_list args)519*61046927SAndroid Build Coastguard Worker ralloc_vasprintf_rewrite_tail(char **str, size_t *start, const char *fmt,
520*61046927SAndroid Build Coastguard Worker va_list args)
521*61046927SAndroid Build Coastguard Worker {
522*61046927SAndroid Build Coastguard Worker size_t new_length;
523*61046927SAndroid Build Coastguard Worker char *ptr;
524*61046927SAndroid Build Coastguard Worker
525*61046927SAndroid Build Coastguard Worker assert(str != NULL);
526*61046927SAndroid Build Coastguard Worker
527*61046927SAndroid Build Coastguard Worker if (unlikely(*str == NULL)) {
528*61046927SAndroid Build Coastguard Worker // Assuming a NULL context is probably bad, but it's expected behavior.
529*61046927SAndroid Build Coastguard Worker *str = ralloc_vasprintf(NULL, fmt, args);
530*61046927SAndroid Build Coastguard Worker *start = strlen(*str);
531*61046927SAndroid Build Coastguard Worker return true;
532*61046927SAndroid Build Coastguard Worker }
533*61046927SAndroid Build Coastguard Worker
534*61046927SAndroid Build Coastguard Worker new_length = u_printf_length(fmt, args);
535*61046927SAndroid Build Coastguard Worker
536*61046927SAndroid Build Coastguard Worker ptr = resize(*str, *start + new_length + 1);
537*61046927SAndroid Build Coastguard Worker if (unlikely(ptr == NULL))
538*61046927SAndroid Build Coastguard Worker return false;
539*61046927SAndroid Build Coastguard Worker
540*61046927SAndroid Build Coastguard Worker vsnprintf(ptr + *start, new_length + 1, fmt, args);
541*61046927SAndroid Build Coastguard Worker *str = ptr;
542*61046927SAndroid Build Coastguard Worker *start += new_length;
543*61046927SAndroid Build Coastguard Worker return true;
544*61046927SAndroid Build Coastguard Worker }
545*61046927SAndroid Build Coastguard Worker
546*61046927SAndroid Build Coastguard Worker /***************************************************************************
547*61046927SAndroid Build Coastguard Worker * GC context.
548*61046927SAndroid Build Coastguard Worker ***************************************************************************
549*61046927SAndroid Build Coastguard Worker */
550*61046927SAndroid Build Coastguard Worker
551*61046927SAndroid Build Coastguard Worker /* The maximum size of an object that will be allocated specially.
552*61046927SAndroid Build Coastguard Worker */
553*61046927SAndroid Build Coastguard Worker #define MAX_FREELIST_SIZE 512
554*61046927SAndroid Build Coastguard Worker
555*61046927SAndroid Build Coastguard Worker /* Allocations small enough to be allocated from a freelist will be aligned up
556*61046927SAndroid Build Coastguard Worker * to this size.
557*61046927SAndroid Build Coastguard Worker */
558*61046927SAndroid Build Coastguard Worker #define FREELIST_ALIGNMENT 32
559*61046927SAndroid Build Coastguard Worker
560*61046927SAndroid Build Coastguard Worker #define NUM_FREELIST_BUCKETS (MAX_FREELIST_SIZE / FREELIST_ALIGNMENT)
561*61046927SAndroid Build Coastguard Worker
562*61046927SAndroid Build Coastguard Worker /* The size of a slab. */
563*61046927SAndroid Build Coastguard Worker #define SLAB_SIZE (32 * 1024)
564*61046927SAndroid Build Coastguard Worker
565*61046927SAndroid Build Coastguard Worker #define GC_CONTEXT_CANARY 0xAF6B6C83
566*61046927SAndroid Build Coastguard Worker #define GC_CANARY 0xAF6B5B72
567*61046927SAndroid Build Coastguard Worker
568*61046927SAndroid Build Coastguard Worker enum gc_flags {
569*61046927SAndroid Build Coastguard Worker IS_USED = (1 << 0),
570*61046927SAndroid Build Coastguard Worker CURRENT_GENERATION = (1 << 1),
571*61046927SAndroid Build Coastguard Worker IS_PADDING = (1 << 7),
572*61046927SAndroid Build Coastguard Worker };
573*61046927SAndroid Build Coastguard Worker
574*61046927SAndroid Build Coastguard Worker typedef struct
575*61046927SAndroid Build Coastguard Worker {
576*61046927SAndroid Build Coastguard Worker #ifndef NDEBUG
577*61046927SAndroid Build Coastguard Worker /* A canary value used to determine whether a pointer is allocated using gc_alloc. */
578*61046927SAndroid Build Coastguard Worker unsigned canary;
579*61046927SAndroid Build Coastguard Worker #endif
580*61046927SAndroid Build Coastguard Worker
581*61046927SAndroid Build Coastguard Worker uint16_t slab_offset;
582*61046927SAndroid Build Coastguard Worker uint8_t bucket;
583*61046927SAndroid Build Coastguard Worker uint8_t flags;
584*61046927SAndroid Build Coastguard Worker
585*61046927SAndroid Build Coastguard Worker /* The last padding byte must have IS_PADDING set and is used to store the amount of padding. If
586*61046927SAndroid Build Coastguard Worker * there is no padding, the IS_PADDING bit of "flags" is unset and "flags" is checked instead.
587*61046927SAndroid Build Coastguard Worker * Because of this, "flags" must be the last member of this struct.
588*61046927SAndroid Build Coastguard Worker */
589*61046927SAndroid Build Coastguard Worker uint8_t padding[];
590*61046927SAndroid Build Coastguard Worker } gc_block_header;
591*61046927SAndroid Build Coastguard Worker
592*61046927SAndroid Build Coastguard Worker /* This structure is at the start of the slab. Objects inside a slab are
593*61046927SAndroid Build Coastguard Worker * allocated using a freelist backed by a simple linear allocator.
594*61046927SAndroid Build Coastguard Worker */
595*61046927SAndroid Build Coastguard Worker typedef struct gc_slab {
596*61046927SAndroid Build Coastguard Worker alignas(HEADER_ALIGN)
597*61046927SAndroid Build Coastguard Worker
598*61046927SAndroid Build Coastguard Worker gc_ctx *ctx;
599*61046927SAndroid Build Coastguard Worker
600*61046927SAndroid Build Coastguard Worker /* Objects are allocated using either linear or freelist allocation. "next_available" is the
601*61046927SAndroid Build Coastguard Worker * pointer used for linear allocation, while "freelist" is the next free object for freelist
602*61046927SAndroid Build Coastguard Worker * allocation.
603*61046927SAndroid Build Coastguard Worker */
604*61046927SAndroid Build Coastguard Worker char *next_available;
605*61046927SAndroid Build Coastguard Worker gc_block_header *freelist;
606*61046927SAndroid Build Coastguard Worker
607*61046927SAndroid Build Coastguard Worker /* Slabs that handle the same-sized objects. */
608*61046927SAndroid Build Coastguard Worker struct list_head link;
609*61046927SAndroid Build Coastguard Worker
610*61046927SAndroid Build Coastguard Worker /* Free slabs that handle the same-sized objects. */
611*61046927SAndroid Build Coastguard Worker struct list_head free_link;
612*61046927SAndroid Build Coastguard Worker
613*61046927SAndroid Build Coastguard Worker /* Number of allocated and free objects, recorded so that we can free the slab if it
614*61046927SAndroid Build Coastguard Worker * becomes empty or add one to the freelist if it's no longer full.
615*61046927SAndroid Build Coastguard Worker */
616*61046927SAndroid Build Coastguard Worker unsigned num_allocated;
617*61046927SAndroid Build Coastguard Worker unsigned num_free;
618*61046927SAndroid Build Coastguard Worker } gc_slab;
619*61046927SAndroid Build Coastguard Worker
620*61046927SAndroid Build Coastguard Worker struct gc_ctx {
621*61046927SAndroid Build Coastguard Worker #ifndef NDEBUG
622*61046927SAndroid Build Coastguard Worker unsigned canary;
623*61046927SAndroid Build Coastguard Worker #endif
624*61046927SAndroid Build Coastguard Worker
625*61046927SAndroid Build Coastguard Worker /* Array of slabs for fixed-size allocations. Each slab tracks allocations
626*61046927SAndroid Build Coastguard Worker * of specific sized blocks. User allocations are rounded up to the nearest
627*61046927SAndroid Build Coastguard Worker * fixed size. slabs[N] contains allocations of size
628*61046927SAndroid Build Coastguard Worker * FREELIST_ALIGNMENT * (N + 1).
629*61046927SAndroid Build Coastguard Worker */
630*61046927SAndroid Build Coastguard Worker struct {
631*61046927SAndroid Build Coastguard Worker /* List of slabs in this bucket. */
632*61046927SAndroid Build Coastguard Worker struct list_head slabs;
633*61046927SAndroid Build Coastguard Worker
634*61046927SAndroid Build Coastguard Worker /* List of slabs with free space in this bucket, so we can quickly choose one when
635*61046927SAndroid Build Coastguard Worker * allocating.
636*61046927SAndroid Build Coastguard Worker */
637*61046927SAndroid Build Coastguard Worker struct list_head free_slabs;
638*61046927SAndroid Build Coastguard Worker } slabs[NUM_FREELIST_BUCKETS];
639*61046927SAndroid Build Coastguard Worker
640*61046927SAndroid Build Coastguard Worker uint8_t current_gen;
641*61046927SAndroid Build Coastguard Worker void *rubbish;
642*61046927SAndroid Build Coastguard Worker };
643*61046927SAndroid Build Coastguard Worker
644*61046927SAndroid Build Coastguard Worker static gc_block_header *
get_gc_header(const void * ptr)645*61046927SAndroid Build Coastguard Worker get_gc_header(const void *ptr)
646*61046927SAndroid Build Coastguard Worker {
647*61046927SAndroid Build Coastguard Worker uint8_t *c_ptr = (uint8_t *)ptr;
648*61046927SAndroid Build Coastguard Worker
649*61046927SAndroid Build Coastguard Worker /* Adjust for padding added to ensure alignment of the allocation. There might also be padding
650*61046927SAndroid Build Coastguard Worker * added by the compiler into gc_block_header, but that isn't counted in the IS_PADDING byte.
651*61046927SAndroid Build Coastguard Worker */
652*61046927SAndroid Build Coastguard Worker if (c_ptr[-1] & IS_PADDING)
653*61046927SAndroid Build Coastguard Worker c_ptr -= c_ptr[-1] & ~IS_PADDING;
654*61046927SAndroid Build Coastguard Worker
655*61046927SAndroid Build Coastguard Worker c_ptr -= sizeof(gc_block_header);
656*61046927SAndroid Build Coastguard Worker
657*61046927SAndroid Build Coastguard Worker gc_block_header *info = (gc_block_header *)c_ptr;
658*61046927SAndroid Build Coastguard Worker assert(info->canary == GC_CANARY);
659*61046927SAndroid Build Coastguard Worker return info;
660*61046927SAndroid Build Coastguard Worker }
661*61046927SAndroid Build Coastguard Worker
662*61046927SAndroid Build Coastguard Worker static gc_block_header *
get_gc_freelist_next(gc_block_header * ptr)663*61046927SAndroid Build Coastguard Worker get_gc_freelist_next(gc_block_header *ptr)
664*61046927SAndroid Build Coastguard Worker {
665*61046927SAndroid Build Coastguard Worker gc_block_header *next;
666*61046927SAndroid Build Coastguard Worker /* work around possible strict aliasing bug using memcpy */
667*61046927SAndroid Build Coastguard Worker memcpy(&next, (void*)(ptr + 1), sizeof(next));
668*61046927SAndroid Build Coastguard Worker return next;
669*61046927SAndroid Build Coastguard Worker }
670*61046927SAndroid Build Coastguard Worker
671*61046927SAndroid Build Coastguard Worker static void
set_gc_freelist_next(gc_block_header * ptr,gc_block_header * next)672*61046927SAndroid Build Coastguard Worker set_gc_freelist_next(gc_block_header *ptr, gc_block_header *next)
673*61046927SAndroid Build Coastguard Worker {
674*61046927SAndroid Build Coastguard Worker memcpy((void*)(ptr + 1), &next, sizeof(next));
675*61046927SAndroid Build Coastguard Worker }
676*61046927SAndroid Build Coastguard Worker
677*61046927SAndroid Build Coastguard Worker static gc_slab *
get_gc_slab(gc_block_header * header)678*61046927SAndroid Build Coastguard Worker get_gc_slab(gc_block_header *header)
679*61046927SAndroid Build Coastguard Worker {
680*61046927SAndroid Build Coastguard Worker return (gc_slab *)((char *)header - header->slab_offset);
681*61046927SAndroid Build Coastguard Worker }
682*61046927SAndroid Build Coastguard Worker
683*61046927SAndroid Build Coastguard Worker gc_ctx *
gc_context(const void * parent)684*61046927SAndroid Build Coastguard Worker gc_context(const void *parent)
685*61046927SAndroid Build Coastguard Worker {
686*61046927SAndroid Build Coastguard Worker gc_ctx *ctx = rzalloc(parent, gc_ctx);
687*61046927SAndroid Build Coastguard Worker for (unsigned i = 0; i < NUM_FREELIST_BUCKETS; i++) {
688*61046927SAndroid Build Coastguard Worker list_inithead(&ctx->slabs[i].slabs);
689*61046927SAndroid Build Coastguard Worker list_inithead(&ctx->slabs[i].free_slabs);
690*61046927SAndroid Build Coastguard Worker }
691*61046927SAndroid Build Coastguard Worker #ifndef NDEBUG
692*61046927SAndroid Build Coastguard Worker ctx->canary = GC_CONTEXT_CANARY;
693*61046927SAndroid Build Coastguard Worker #endif
694*61046927SAndroid Build Coastguard Worker return ctx;
695*61046927SAndroid Build Coastguard Worker }
696*61046927SAndroid Build Coastguard Worker
697*61046927SAndroid Build Coastguard Worker static_assert(UINT32_MAX >= MAX_FREELIST_SIZE, "Freelist sizes use uint32_t");
698*61046927SAndroid Build Coastguard Worker
699*61046927SAndroid Build Coastguard Worker static uint32_t
gc_bucket_obj_size(uint32_t bucket)700*61046927SAndroid Build Coastguard Worker gc_bucket_obj_size(uint32_t bucket)
701*61046927SAndroid Build Coastguard Worker {
702*61046927SAndroid Build Coastguard Worker return (bucket + 1) * FREELIST_ALIGNMENT;
703*61046927SAndroid Build Coastguard Worker }
704*61046927SAndroid Build Coastguard Worker
705*61046927SAndroid Build Coastguard Worker static uint32_t
gc_bucket_for_size(uint32_t size)706*61046927SAndroid Build Coastguard Worker gc_bucket_for_size(uint32_t size)
707*61046927SAndroid Build Coastguard Worker {
708*61046927SAndroid Build Coastguard Worker return (size - 1) / FREELIST_ALIGNMENT;
709*61046927SAndroid Build Coastguard Worker }
710*61046927SAndroid Build Coastguard Worker
711*61046927SAndroid Build Coastguard Worker static_assert(UINT32_MAX >= SLAB_SIZE, "SLAB_SIZE use uint32_t");
712*61046927SAndroid Build Coastguard Worker
713*61046927SAndroid Build Coastguard Worker static uint32_t
gc_bucket_num_objs(uint32_t bucket)714*61046927SAndroid Build Coastguard Worker gc_bucket_num_objs(uint32_t bucket)
715*61046927SAndroid Build Coastguard Worker {
716*61046927SAndroid Build Coastguard Worker return (SLAB_SIZE - sizeof(gc_slab)) / gc_bucket_obj_size(bucket);
717*61046927SAndroid Build Coastguard Worker }
718*61046927SAndroid Build Coastguard Worker
719*61046927SAndroid Build Coastguard Worker static gc_block_header *
alloc_from_slab(gc_slab * slab,uint32_t bucket)720*61046927SAndroid Build Coastguard Worker alloc_from_slab(gc_slab *slab, uint32_t bucket)
721*61046927SAndroid Build Coastguard Worker {
722*61046927SAndroid Build Coastguard Worker uint32_t size = gc_bucket_obj_size(bucket);
723*61046927SAndroid Build Coastguard Worker gc_block_header *header;
724*61046927SAndroid Build Coastguard Worker if (slab->freelist) {
725*61046927SAndroid Build Coastguard Worker /* Prioritize already-allocated chunks, since they probably have a page
726*61046927SAndroid Build Coastguard Worker * backing them.
727*61046927SAndroid Build Coastguard Worker */
728*61046927SAndroid Build Coastguard Worker header = slab->freelist;
729*61046927SAndroid Build Coastguard Worker slab->freelist = get_gc_freelist_next(slab->freelist);
730*61046927SAndroid Build Coastguard Worker } else if (slab->next_available + size <= ((char *) slab) + SLAB_SIZE) {
731*61046927SAndroid Build Coastguard Worker header = (gc_block_header *) slab->next_available;
732*61046927SAndroid Build Coastguard Worker header->slab_offset = (char *) header - (char *) slab;
733*61046927SAndroid Build Coastguard Worker header->bucket = bucket;
734*61046927SAndroid Build Coastguard Worker slab->next_available += size;
735*61046927SAndroid Build Coastguard Worker } else {
736*61046927SAndroid Build Coastguard Worker return NULL;
737*61046927SAndroid Build Coastguard Worker }
738*61046927SAndroid Build Coastguard Worker
739*61046927SAndroid Build Coastguard Worker slab->num_allocated++;
740*61046927SAndroid Build Coastguard Worker slab->num_free--;
741*61046927SAndroid Build Coastguard Worker if (!slab->num_free)
742*61046927SAndroid Build Coastguard Worker list_del(&slab->free_link);
743*61046927SAndroid Build Coastguard Worker return header;
744*61046927SAndroid Build Coastguard Worker }
745*61046927SAndroid Build Coastguard Worker
746*61046927SAndroid Build Coastguard Worker static void
free_slab(gc_slab * slab)747*61046927SAndroid Build Coastguard Worker free_slab(gc_slab *slab)
748*61046927SAndroid Build Coastguard Worker {
749*61046927SAndroid Build Coastguard Worker if (list_is_linked(&slab->free_link))
750*61046927SAndroid Build Coastguard Worker list_del(&slab->free_link);
751*61046927SAndroid Build Coastguard Worker list_del(&slab->link);
752*61046927SAndroid Build Coastguard Worker ralloc_free(slab);
753*61046927SAndroid Build Coastguard Worker }
754*61046927SAndroid Build Coastguard Worker
755*61046927SAndroid Build Coastguard Worker static void
free_from_slab(gc_block_header * header,bool keep_empty_slabs)756*61046927SAndroid Build Coastguard Worker free_from_slab(gc_block_header *header, bool keep_empty_slabs)
757*61046927SAndroid Build Coastguard Worker {
758*61046927SAndroid Build Coastguard Worker gc_slab *slab = get_gc_slab(header);
759*61046927SAndroid Build Coastguard Worker
760*61046927SAndroid Build Coastguard Worker if (slab->num_allocated == 1 && !(keep_empty_slabs && list_is_singular(&slab->free_link))) {
761*61046927SAndroid Build Coastguard Worker /* Free the slab if this is the last object. */
762*61046927SAndroid Build Coastguard Worker free_slab(slab);
763*61046927SAndroid Build Coastguard Worker return;
764*61046927SAndroid Build Coastguard Worker } else if (slab->num_free == 0) {
765*61046927SAndroid Build Coastguard Worker list_add(&slab->free_link, &slab->ctx->slabs[header->bucket].free_slabs);
766*61046927SAndroid Build Coastguard Worker } else {
767*61046927SAndroid Build Coastguard Worker /* Keep the free list sorted by the number of free objects in ascending order. By prefering to
768*61046927SAndroid Build Coastguard Worker * allocate from the slab with the fewest free objects, we help free the slabs with many free
769*61046927SAndroid Build Coastguard Worker * objects.
770*61046927SAndroid Build Coastguard Worker */
771*61046927SAndroid Build Coastguard Worker while (slab->free_link.next != &slab->ctx->slabs[header->bucket].free_slabs &&
772*61046927SAndroid Build Coastguard Worker slab->num_free > list_entry(slab->free_link.next, gc_slab, free_link)->num_free) {
773*61046927SAndroid Build Coastguard Worker gc_slab *next = list_entry(slab->free_link.next, gc_slab, free_link);
774*61046927SAndroid Build Coastguard Worker
775*61046927SAndroid Build Coastguard Worker /* Move "slab" to after "next". */
776*61046927SAndroid Build Coastguard Worker list_move_to(&slab->free_link, &next->free_link);
777*61046927SAndroid Build Coastguard Worker }
778*61046927SAndroid Build Coastguard Worker }
779*61046927SAndroid Build Coastguard Worker
780*61046927SAndroid Build Coastguard Worker set_gc_freelist_next(header, slab->freelist);
781*61046927SAndroid Build Coastguard Worker slab->freelist = header;
782*61046927SAndroid Build Coastguard Worker
783*61046927SAndroid Build Coastguard Worker slab->num_allocated--;
784*61046927SAndroid Build Coastguard Worker slab->num_free++;
785*61046927SAndroid Build Coastguard Worker }
786*61046927SAndroid Build Coastguard Worker
787*61046927SAndroid Build Coastguard Worker static uint32_t
get_slab_size(uint32_t bucket)788*61046927SAndroid Build Coastguard Worker get_slab_size(uint32_t bucket)
789*61046927SAndroid Build Coastguard Worker {
790*61046927SAndroid Build Coastguard Worker /* SLAB_SIZE rounded down to a multiple of the object size so that it's not larger than what can
791*61046927SAndroid Build Coastguard Worker * be used.
792*61046927SAndroid Build Coastguard Worker */
793*61046927SAndroid Build Coastguard Worker uint32_t obj_size = gc_bucket_obj_size(bucket);
794*61046927SAndroid Build Coastguard Worker uint32_t num_objs = gc_bucket_num_objs(bucket);
795*61046927SAndroid Build Coastguard Worker return align((uint32_t)sizeof(gc_slab) + num_objs * obj_size, alignof(gc_slab));
796*61046927SAndroid Build Coastguard Worker }
797*61046927SAndroid Build Coastguard Worker
798*61046927SAndroid Build Coastguard Worker static gc_slab *
create_slab(gc_ctx * ctx,unsigned bucket)799*61046927SAndroid Build Coastguard Worker create_slab(gc_ctx *ctx, unsigned bucket)
800*61046927SAndroid Build Coastguard Worker {
801*61046927SAndroid Build Coastguard Worker gc_slab *slab = ralloc_size(ctx, get_slab_size(bucket));
802*61046927SAndroid Build Coastguard Worker if (unlikely(!slab))
803*61046927SAndroid Build Coastguard Worker return NULL;
804*61046927SAndroid Build Coastguard Worker
805*61046927SAndroid Build Coastguard Worker slab->ctx = ctx;
806*61046927SAndroid Build Coastguard Worker slab->freelist = NULL;
807*61046927SAndroid Build Coastguard Worker slab->next_available = (char*)(slab + 1);
808*61046927SAndroid Build Coastguard Worker slab->num_allocated = 0;
809*61046927SAndroid Build Coastguard Worker slab->num_free = gc_bucket_num_objs(bucket);
810*61046927SAndroid Build Coastguard Worker
811*61046927SAndroid Build Coastguard Worker list_addtail(&slab->link, &ctx->slabs[bucket].slabs);
812*61046927SAndroid Build Coastguard Worker list_addtail(&slab->free_link, &ctx->slabs[bucket].free_slabs);
813*61046927SAndroid Build Coastguard Worker
814*61046927SAndroid Build Coastguard Worker return slab;
815*61046927SAndroid Build Coastguard Worker }
816*61046927SAndroid Build Coastguard Worker
817*61046927SAndroid Build Coastguard Worker void *
gc_alloc_size(gc_ctx * ctx,size_t size,size_t alignment)818*61046927SAndroid Build Coastguard Worker gc_alloc_size(gc_ctx *ctx, size_t size, size_t alignment)
819*61046927SAndroid Build Coastguard Worker {
820*61046927SAndroid Build Coastguard Worker assert(ctx);
821*61046927SAndroid Build Coastguard Worker assert(util_is_power_of_two_nonzero_uintptr(alignment));
822*61046927SAndroid Build Coastguard Worker
823*61046927SAndroid Build Coastguard Worker alignment = MAX2(alignment, alignof(gc_block_header));
824*61046927SAndroid Build Coastguard Worker
825*61046927SAndroid Build Coastguard Worker /* Alignment will add at most align-alignof(gc_block_header) bytes of padding to the header, and
826*61046927SAndroid Build Coastguard Worker * the IS_PADDING byte can only encode up to 127.
827*61046927SAndroid Build Coastguard Worker */
828*61046927SAndroid Build Coastguard Worker assert((alignment - alignof(gc_block_header)) <= 127);
829*61046927SAndroid Build Coastguard Worker
830*61046927SAndroid Build Coastguard Worker /* We can only align as high as the slab is. */
831*61046927SAndroid Build Coastguard Worker assert(alignment <= HEADER_ALIGN);
832*61046927SAndroid Build Coastguard Worker
833*61046927SAndroid Build Coastguard Worker size_t header_size = align64(sizeof(gc_block_header), alignment);
834*61046927SAndroid Build Coastguard Worker size = align64(size, alignment);
835*61046927SAndroid Build Coastguard Worker size += header_size;
836*61046927SAndroid Build Coastguard Worker
837*61046927SAndroid Build Coastguard Worker gc_block_header *header = NULL;
838*61046927SAndroid Build Coastguard Worker if (size <= MAX_FREELIST_SIZE) {
839*61046927SAndroid Build Coastguard Worker uint32_t bucket = gc_bucket_for_size((uint32_t)size);
840*61046927SAndroid Build Coastguard Worker if (list_is_empty(&ctx->slabs[bucket].free_slabs) && !create_slab(ctx, bucket))
841*61046927SAndroid Build Coastguard Worker return NULL;
842*61046927SAndroid Build Coastguard Worker gc_slab *slab = list_first_entry(&ctx->slabs[bucket].free_slabs, gc_slab, free_link);
843*61046927SAndroid Build Coastguard Worker header = alloc_from_slab(slab, bucket);
844*61046927SAndroid Build Coastguard Worker } else {
845*61046927SAndroid Build Coastguard Worker header = ralloc_size(ctx, size);
846*61046927SAndroid Build Coastguard Worker if (unlikely(!header))
847*61046927SAndroid Build Coastguard Worker return NULL;
848*61046927SAndroid Build Coastguard Worker /* Mark the header as allocated directly, so we know to actually free it. */
849*61046927SAndroid Build Coastguard Worker header->bucket = NUM_FREELIST_BUCKETS;
850*61046927SAndroid Build Coastguard Worker }
851*61046927SAndroid Build Coastguard Worker
852*61046927SAndroid Build Coastguard Worker header->flags = ctx->current_gen | IS_USED;
853*61046927SAndroid Build Coastguard Worker #ifndef NDEBUG
854*61046927SAndroid Build Coastguard Worker header->canary = GC_CANARY;
855*61046927SAndroid Build Coastguard Worker #endif
856*61046927SAndroid Build Coastguard Worker
857*61046927SAndroid Build Coastguard Worker uint8_t *ptr = (uint8_t *)header + header_size;
858*61046927SAndroid Build Coastguard Worker if ((header_size - 1) != offsetof(gc_block_header, flags))
859*61046927SAndroid Build Coastguard Worker ptr[-1] = IS_PADDING | (header_size - sizeof(gc_block_header));
860*61046927SAndroid Build Coastguard Worker
861*61046927SAndroid Build Coastguard Worker assert(((uintptr_t)ptr & (alignment - 1)) == 0);
862*61046927SAndroid Build Coastguard Worker return ptr;
863*61046927SAndroid Build Coastguard Worker }
864*61046927SAndroid Build Coastguard Worker
865*61046927SAndroid Build Coastguard Worker void *
gc_zalloc_size(gc_ctx * ctx,size_t size,size_t alignment)866*61046927SAndroid Build Coastguard Worker gc_zalloc_size(gc_ctx *ctx, size_t size, size_t alignment)
867*61046927SAndroid Build Coastguard Worker {
868*61046927SAndroid Build Coastguard Worker void *ptr = gc_alloc_size(ctx, size, alignment);
869*61046927SAndroid Build Coastguard Worker
870*61046927SAndroid Build Coastguard Worker if (likely(ptr))
871*61046927SAndroid Build Coastguard Worker memset(ptr, 0, size);
872*61046927SAndroid Build Coastguard Worker
873*61046927SAndroid Build Coastguard Worker return ptr;
874*61046927SAndroid Build Coastguard Worker }
875*61046927SAndroid Build Coastguard Worker
876*61046927SAndroid Build Coastguard Worker void
gc_free(void * ptr)877*61046927SAndroid Build Coastguard Worker gc_free(void *ptr)
878*61046927SAndroid Build Coastguard Worker {
879*61046927SAndroid Build Coastguard Worker if (!ptr)
880*61046927SAndroid Build Coastguard Worker return;
881*61046927SAndroid Build Coastguard Worker
882*61046927SAndroid Build Coastguard Worker gc_block_header *header = get_gc_header(ptr);
883*61046927SAndroid Build Coastguard Worker header->flags &= ~IS_USED;
884*61046927SAndroid Build Coastguard Worker
885*61046927SAndroid Build Coastguard Worker if (header->bucket < NUM_FREELIST_BUCKETS)
886*61046927SAndroid Build Coastguard Worker free_from_slab(header, true);
887*61046927SAndroid Build Coastguard Worker else
888*61046927SAndroid Build Coastguard Worker ralloc_free(header);
889*61046927SAndroid Build Coastguard Worker }
890*61046927SAndroid Build Coastguard Worker
gc_get_context(void * ptr)891*61046927SAndroid Build Coastguard Worker gc_ctx *gc_get_context(void *ptr)
892*61046927SAndroid Build Coastguard Worker {
893*61046927SAndroid Build Coastguard Worker gc_block_header *header = get_gc_header(ptr);
894*61046927SAndroid Build Coastguard Worker
895*61046927SAndroid Build Coastguard Worker if (header->bucket < NUM_FREELIST_BUCKETS)
896*61046927SAndroid Build Coastguard Worker return get_gc_slab(header)->ctx;
897*61046927SAndroid Build Coastguard Worker else
898*61046927SAndroid Build Coastguard Worker return ralloc_parent(header);
899*61046927SAndroid Build Coastguard Worker }
900*61046927SAndroid Build Coastguard Worker
901*61046927SAndroid Build Coastguard Worker void
gc_sweep_start(gc_ctx * ctx)902*61046927SAndroid Build Coastguard Worker gc_sweep_start(gc_ctx *ctx)
903*61046927SAndroid Build Coastguard Worker {
904*61046927SAndroid Build Coastguard Worker ctx->current_gen ^= CURRENT_GENERATION;
905*61046927SAndroid Build Coastguard Worker
906*61046927SAndroid Build Coastguard Worker ctx->rubbish = ralloc_context(NULL);
907*61046927SAndroid Build Coastguard Worker ralloc_adopt(ctx->rubbish, ctx);
908*61046927SAndroid Build Coastguard Worker }
909*61046927SAndroid Build Coastguard Worker
910*61046927SAndroid Build Coastguard Worker void
gc_mark_live(gc_ctx * ctx,const void * mem)911*61046927SAndroid Build Coastguard Worker gc_mark_live(gc_ctx *ctx, const void *mem)
912*61046927SAndroid Build Coastguard Worker {
913*61046927SAndroid Build Coastguard Worker gc_block_header *header = get_gc_header(mem);
914*61046927SAndroid Build Coastguard Worker if (header->bucket < NUM_FREELIST_BUCKETS)
915*61046927SAndroid Build Coastguard Worker header->flags ^= CURRENT_GENERATION;
916*61046927SAndroid Build Coastguard Worker else
917*61046927SAndroid Build Coastguard Worker ralloc_steal(ctx, header);
918*61046927SAndroid Build Coastguard Worker }
919*61046927SAndroid Build Coastguard Worker
920*61046927SAndroid Build Coastguard Worker void
gc_sweep_end(gc_ctx * ctx)921*61046927SAndroid Build Coastguard Worker gc_sweep_end(gc_ctx *ctx)
922*61046927SAndroid Build Coastguard Worker {
923*61046927SAndroid Build Coastguard Worker assert(ctx->rubbish);
924*61046927SAndroid Build Coastguard Worker
925*61046927SAndroid Build Coastguard Worker for (unsigned i = 0; i < NUM_FREELIST_BUCKETS; i++) {
926*61046927SAndroid Build Coastguard Worker unsigned obj_size = gc_bucket_obj_size(i);
927*61046927SAndroid Build Coastguard Worker list_for_each_entry_safe(gc_slab, slab, &ctx->slabs[i].slabs, link) {
928*61046927SAndroid Build Coastguard Worker if (!slab->num_allocated) {
929*61046927SAndroid Build Coastguard Worker free_slab(slab);
930*61046927SAndroid Build Coastguard Worker continue;
931*61046927SAndroid Build Coastguard Worker }
932*61046927SAndroid Build Coastguard Worker
933*61046927SAndroid Build Coastguard Worker for (char *ptr = (char*)(slab + 1); ptr != slab->next_available; ptr += obj_size) {
934*61046927SAndroid Build Coastguard Worker gc_block_header *header = (gc_block_header *)ptr;
935*61046927SAndroid Build Coastguard Worker if (!(header->flags & IS_USED))
936*61046927SAndroid Build Coastguard Worker continue;
937*61046927SAndroid Build Coastguard Worker if ((header->flags & CURRENT_GENERATION) == ctx->current_gen)
938*61046927SAndroid Build Coastguard Worker continue;
939*61046927SAndroid Build Coastguard Worker
940*61046927SAndroid Build Coastguard Worker bool last = slab->num_allocated == 1;
941*61046927SAndroid Build Coastguard Worker
942*61046927SAndroid Build Coastguard Worker header->flags &= ~IS_USED;
943*61046927SAndroid Build Coastguard Worker free_from_slab(header, false);
944*61046927SAndroid Build Coastguard Worker
945*61046927SAndroid Build Coastguard Worker if (last)
946*61046927SAndroid Build Coastguard Worker break;
947*61046927SAndroid Build Coastguard Worker }
948*61046927SAndroid Build Coastguard Worker }
949*61046927SAndroid Build Coastguard Worker }
950*61046927SAndroid Build Coastguard Worker
951*61046927SAndroid Build Coastguard Worker for (unsigned i = 0; i < NUM_FREELIST_BUCKETS; i++) {
952*61046927SAndroid Build Coastguard Worker list_for_each_entry(gc_slab, slab, &ctx->slabs[i].slabs, link) {
953*61046927SAndroid Build Coastguard Worker assert(slab->num_allocated > 0); /* free_from_slab() should free it otherwise */
954*61046927SAndroid Build Coastguard Worker ralloc_steal(ctx, slab);
955*61046927SAndroid Build Coastguard Worker }
956*61046927SAndroid Build Coastguard Worker }
957*61046927SAndroid Build Coastguard Worker
958*61046927SAndroid Build Coastguard Worker ralloc_free(ctx->rubbish);
959*61046927SAndroid Build Coastguard Worker ctx->rubbish = NULL;
960*61046927SAndroid Build Coastguard Worker }
961*61046927SAndroid Build Coastguard Worker
962*61046927SAndroid Build Coastguard Worker /***************************************************************************
963*61046927SAndroid Build Coastguard Worker * Linear allocator for short-lived allocations.
964*61046927SAndroid Build Coastguard Worker ***************************************************************************
965*61046927SAndroid Build Coastguard Worker *
966*61046927SAndroid Build Coastguard Worker * The allocator consists of a parent node (2K buffer), which requires
967*61046927SAndroid Build Coastguard Worker * a ralloc parent, and child nodes (allocations). Child nodes can't be freed
968*61046927SAndroid Build Coastguard Worker * directly, because the parent doesn't track them. You have to release
969*61046927SAndroid Build Coastguard Worker * the parent node in order to release all its children.
970*61046927SAndroid Build Coastguard Worker *
971*61046927SAndroid Build Coastguard Worker * The allocator uses a fixed-sized buffer with a monotonically increasing
972*61046927SAndroid Build Coastguard Worker * offset after each allocation. If the buffer is all used, another buffer
973*61046927SAndroid Build Coastguard Worker * is allocated, using the linear parent node as ralloc parent.
974*61046927SAndroid Build Coastguard Worker *
975*61046927SAndroid Build Coastguard Worker * The linear parent node is always the first buffer and keeps track of all
976*61046927SAndroid Build Coastguard Worker * other buffers.
977*61046927SAndroid Build Coastguard Worker */
978*61046927SAndroid Build Coastguard Worker
979*61046927SAndroid Build Coastguard Worker #define SUBALLOC_ALIGNMENT 8
980*61046927SAndroid Build Coastguard Worker #define LMAGIC_CONTEXT 0x87b9c7d3
981*61046927SAndroid Build Coastguard Worker #define LMAGIC_NODE 0x87b910d3
982*61046927SAndroid Build Coastguard Worker
983*61046927SAndroid Build Coastguard Worker struct linear_ctx {
984*61046927SAndroid Build Coastguard Worker
985*61046927SAndroid Build Coastguard Worker alignas(HEADER_ALIGN)
986*61046927SAndroid Build Coastguard Worker
987*61046927SAndroid Build Coastguard Worker #ifndef NDEBUG
988*61046927SAndroid Build Coastguard Worker unsigned magic; /* for debugging */
989*61046927SAndroid Build Coastguard Worker #endif
990*61046927SAndroid Build Coastguard Worker unsigned min_buffer_size;
991*61046927SAndroid Build Coastguard Worker
992*61046927SAndroid Build Coastguard Worker unsigned offset; /* points to the first unused byte in the latest buffer */
993*61046927SAndroid Build Coastguard Worker unsigned size; /* size of the latest buffer */
994*61046927SAndroid Build Coastguard Worker void *latest; /* the only buffer that has free space */
995*61046927SAndroid Build Coastguard Worker };
996*61046927SAndroid Build Coastguard Worker
997*61046927SAndroid Build Coastguard Worker typedef struct linear_ctx linear_ctx;
998*61046927SAndroid Build Coastguard Worker
999*61046927SAndroid Build Coastguard Worker #ifndef NDEBUG
1000*61046927SAndroid Build Coastguard Worker struct linear_node_canary {
1001*61046927SAndroid Build Coastguard Worker alignas(HEADER_ALIGN)
1002*61046927SAndroid Build Coastguard Worker unsigned magic;
1003*61046927SAndroid Build Coastguard Worker unsigned offset; /* points to the first unused byte in *this* buffer */
1004*61046927SAndroid Build Coastguard Worker };
1005*61046927SAndroid Build Coastguard Worker
1006*61046927SAndroid Build Coastguard Worker typedef struct linear_node_canary linear_node_canary;
1007*61046927SAndroid Build Coastguard Worker
1008*61046927SAndroid Build Coastguard Worker static linear_node_canary *
get_node_canary(void * ptr)1009*61046927SAndroid Build Coastguard Worker get_node_canary(void *ptr)
1010*61046927SAndroid Build Coastguard Worker {
1011*61046927SAndroid Build Coastguard Worker return (void *)((char *)ptr - sizeof(linear_node_canary));
1012*61046927SAndroid Build Coastguard Worker }
1013*61046927SAndroid Build Coastguard Worker #endif
1014*61046927SAndroid Build Coastguard Worker
1015*61046927SAndroid Build Coastguard Worker static unsigned
get_node_canary_size()1016*61046927SAndroid Build Coastguard Worker get_node_canary_size()
1017*61046927SAndroid Build Coastguard Worker {
1018*61046927SAndroid Build Coastguard Worker #ifndef NDEBUG
1019*61046927SAndroid Build Coastguard Worker return sizeof(linear_node_canary);
1020*61046927SAndroid Build Coastguard Worker #else
1021*61046927SAndroid Build Coastguard Worker return 0;
1022*61046927SAndroid Build Coastguard Worker #endif
1023*61046927SAndroid Build Coastguard Worker }
1024*61046927SAndroid Build Coastguard Worker
1025*61046927SAndroid Build Coastguard Worker void *
linear_alloc_child(linear_ctx * ctx,unsigned size)1026*61046927SAndroid Build Coastguard Worker linear_alloc_child(linear_ctx *ctx, unsigned size)
1027*61046927SAndroid Build Coastguard Worker {
1028*61046927SAndroid Build Coastguard Worker assert(ctx->magic == LMAGIC_CONTEXT);
1029*61046927SAndroid Build Coastguard Worker assert(get_node_canary(ctx->latest)->magic == LMAGIC_NODE);
1030*61046927SAndroid Build Coastguard Worker assert(get_node_canary(ctx->latest)->offset == ctx->offset);
1031*61046927SAndroid Build Coastguard Worker
1032*61046927SAndroid Build Coastguard Worker size = ALIGN_POT(size, SUBALLOC_ALIGNMENT);
1033*61046927SAndroid Build Coastguard Worker
1034*61046927SAndroid Build Coastguard Worker if (unlikely(ctx->offset + size > ctx->size)) {
1035*61046927SAndroid Build Coastguard Worker /* allocate a new node */
1036*61046927SAndroid Build Coastguard Worker unsigned node_size = size;
1037*61046927SAndroid Build Coastguard Worker if (likely(node_size < ctx->min_buffer_size))
1038*61046927SAndroid Build Coastguard Worker node_size = ctx->min_buffer_size;
1039*61046927SAndroid Build Coastguard Worker
1040*61046927SAndroid Build Coastguard Worker const unsigned canary_size = get_node_canary_size();
1041*61046927SAndroid Build Coastguard Worker const unsigned full_size = canary_size + node_size;
1042*61046927SAndroid Build Coastguard Worker
1043*61046927SAndroid Build Coastguard Worker /* linear context is also a ralloc context */
1044*61046927SAndroid Build Coastguard Worker char *ptr = ralloc_size(ctx, full_size);
1045*61046927SAndroid Build Coastguard Worker if (unlikely(!ptr))
1046*61046927SAndroid Build Coastguard Worker return NULL;
1047*61046927SAndroid Build Coastguard Worker
1048*61046927SAndroid Build Coastguard Worker #ifndef NDEBUG
1049*61046927SAndroid Build Coastguard Worker linear_node_canary *canary = (void *) ptr;
1050*61046927SAndroid Build Coastguard Worker canary->magic = LMAGIC_NODE;
1051*61046927SAndroid Build Coastguard Worker canary->offset = 0;
1052*61046927SAndroid Build Coastguard Worker #endif
1053*61046927SAndroid Build Coastguard Worker
1054*61046927SAndroid Build Coastguard Worker /* If the new buffer is going to be full, don't update `latest`
1055*61046927SAndroid Build Coastguard Worker * pointer. Either the current one is also full, so doesn't
1056*61046927SAndroid Build Coastguard Worker * matter, or the current one is not full, so there's still chance
1057*61046927SAndroid Build Coastguard Worker * to use that space.
1058*61046927SAndroid Build Coastguard Worker */
1059*61046927SAndroid Build Coastguard Worker if (unlikely(size == node_size)) {
1060*61046927SAndroid Build Coastguard Worker #ifndef NDEBUG
1061*61046927SAndroid Build Coastguard Worker canary->offset = size;
1062*61046927SAndroid Build Coastguard Worker #endif
1063*61046927SAndroid Build Coastguard Worker assert((uintptr_t)(ptr + canary_size) % SUBALLOC_ALIGNMENT == 0);
1064*61046927SAndroid Build Coastguard Worker return ptr + canary_size;
1065*61046927SAndroid Build Coastguard Worker }
1066*61046927SAndroid Build Coastguard Worker
1067*61046927SAndroid Build Coastguard Worker ctx->offset = 0;
1068*61046927SAndroid Build Coastguard Worker ctx->size = node_size;
1069*61046927SAndroid Build Coastguard Worker ctx->latest = ptr + canary_size;
1070*61046927SAndroid Build Coastguard Worker }
1071*61046927SAndroid Build Coastguard Worker
1072*61046927SAndroid Build Coastguard Worker void *ptr = (char *)ctx->latest + ctx->offset;
1073*61046927SAndroid Build Coastguard Worker ctx->offset += size;
1074*61046927SAndroid Build Coastguard Worker
1075*61046927SAndroid Build Coastguard Worker #ifndef NDEBUG
1076*61046927SAndroid Build Coastguard Worker linear_node_canary *canary = get_node_canary(ctx->latest);
1077*61046927SAndroid Build Coastguard Worker canary->offset += size;
1078*61046927SAndroid Build Coastguard Worker #endif
1079*61046927SAndroid Build Coastguard Worker
1080*61046927SAndroid Build Coastguard Worker assert((uintptr_t)ptr % SUBALLOC_ALIGNMENT == 0);
1081*61046927SAndroid Build Coastguard Worker return ptr;
1082*61046927SAndroid Build Coastguard Worker }
1083*61046927SAndroid Build Coastguard Worker
1084*61046927SAndroid Build Coastguard Worker linear_ctx *
linear_context(void * ralloc_ctx)1085*61046927SAndroid Build Coastguard Worker linear_context(void *ralloc_ctx)
1086*61046927SAndroid Build Coastguard Worker {
1087*61046927SAndroid Build Coastguard Worker const linear_opts opts = {0};
1088*61046927SAndroid Build Coastguard Worker return linear_context_with_opts(ralloc_ctx, &opts);
1089*61046927SAndroid Build Coastguard Worker }
1090*61046927SAndroid Build Coastguard Worker
1091*61046927SAndroid Build Coastguard Worker linear_ctx *
linear_context_with_opts(void * ralloc_ctx,const linear_opts * opts)1092*61046927SAndroid Build Coastguard Worker linear_context_with_opts(void *ralloc_ctx, const linear_opts *opts)
1093*61046927SAndroid Build Coastguard Worker {
1094*61046927SAndroid Build Coastguard Worker linear_ctx *ctx;
1095*61046927SAndroid Build Coastguard Worker
1096*61046927SAndroid Build Coastguard Worker if (unlikely(!ralloc_ctx))
1097*61046927SAndroid Build Coastguard Worker return NULL;
1098*61046927SAndroid Build Coastguard Worker
1099*61046927SAndroid Build Coastguard Worker const unsigned default_min_buffer_size = 2048;
1100*61046927SAndroid Build Coastguard Worker const unsigned min_buffer_size =
1101*61046927SAndroid Build Coastguard Worker MAX2(ALIGN_POT(opts->min_buffer_size, default_min_buffer_size),
1102*61046927SAndroid Build Coastguard Worker default_min_buffer_size);
1103*61046927SAndroid Build Coastguard Worker
1104*61046927SAndroid Build Coastguard Worker const unsigned size = min_buffer_size;
1105*61046927SAndroid Build Coastguard Worker const unsigned canary_size = get_node_canary_size();
1106*61046927SAndroid Build Coastguard Worker const unsigned full_size =
1107*61046927SAndroid Build Coastguard Worker sizeof(linear_ctx) + canary_size + size;
1108*61046927SAndroid Build Coastguard Worker
1109*61046927SAndroid Build Coastguard Worker ctx = ralloc_size(ralloc_ctx, full_size);
1110*61046927SAndroid Build Coastguard Worker if (unlikely(!ctx))
1111*61046927SAndroid Build Coastguard Worker return NULL;
1112*61046927SAndroid Build Coastguard Worker
1113*61046927SAndroid Build Coastguard Worker ctx->min_buffer_size = min_buffer_size;
1114*61046927SAndroid Build Coastguard Worker
1115*61046927SAndroid Build Coastguard Worker ctx->offset = 0;
1116*61046927SAndroid Build Coastguard Worker ctx->size = size;
1117*61046927SAndroid Build Coastguard Worker ctx->latest = (char *)&ctx[1] + canary_size;
1118*61046927SAndroid Build Coastguard Worker #ifndef NDEBUG
1119*61046927SAndroid Build Coastguard Worker ctx->magic = LMAGIC_CONTEXT;
1120*61046927SAndroid Build Coastguard Worker linear_node_canary *canary = get_node_canary(ctx->latest);
1121*61046927SAndroid Build Coastguard Worker canary->magic = LMAGIC_NODE;
1122*61046927SAndroid Build Coastguard Worker canary->offset = 0;
1123*61046927SAndroid Build Coastguard Worker #endif
1124*61046927SAndroid Build Coastguard Worker
1125*61046927SAndroid Build Coastguard Worker return ctx;
1126*61046927SAndroid Build Coastguard Worker }
1127*61046927SAndroid Build Coastguard Worker
1128*61046927SAndroid Build Coastguard Worker void *
linear_zalloc_child(linear_ctx * ctx,unsigned size)1129*61046927SAndroid Build Coastguard Worker linear_zalloc_child(linear_ctx *ctx, unsigned size)
1130*61046927SAndroid Build Coastguard Worker {
1131*61046927SAndroid Build Coastguard Worker void *ptr = linear_alloc_child(ctx, size);
1132*61046927SAndroid Build Coastguard Worker
1133*61046927SAndroid Build Coastguard Worker if (likely(ptr))
1134*61046927SAndroid Build Coastguard Worker memset(ptr, 0, size);
1135*61046927SAndroid Build Coastguard Worker return ptr;
1136*61046927SAndroid Build Coastguard Worker }
1137*61046927SAndroid Build Coastguard Worker
1138*61046927SAndroid Build Coastguard Worker void
linear_free_context(linear_ctx * ctx)1139*61046927SAndroid Build Coastguard Worker linear_free_context(linear_ctx *ctx)
1140*61046927SAndroid Build Coastguard Worker {
1141*61046927SAndroid Build Coastguard Worker if (unlikely(!ctx))
1142*61046927SAndroid Build Coastguard Worker return;
1143*61046927SAndroid Build Coastguard Worker
1144*61046927SAndroid Build Coastguard Worker assert(ctx->magic == LMAGIC_CONTEXT);
1145*61046927SAndroid Build Coastguard Worker
1146*61046927SAndroid Build Coastguard Worker /* Linear context is also the ralloc parent of extra nodes. */
1147*61046927SAndroid Build Coastguard Worker ralloc_free(ctx);
1148*61046927SAndroid Build Coastguard Worker }
1149*61046927SAndroid Build Coastguard Worker
1150*61046927SAndroid Build Coastguard Worker void
ralloc_steal_linear_context(void * new_ralloc_ctx,linear_ctx * ctx)1151*61046927SAndroid Build Coastguard Worker ralloc_steal_linear_context(void *new_ralloc_ctx, linear_ctx *ctx)
1152*61046927SAndroid Build Coastguard Worker {
1153*61046927SAndroid Build Coastguard Worker if (unlikely(!ctx))
1154*61046927SAndroid Build Coastguard Worker return;
1155*61046927SAndroid Build Coastguard Worker
1156*61046927SAndroid Build Coastguard Worker assert(ctx->magic == LMAGIC_CONTEXT);
1157*61046927SAndroid Build Coastguard Worker
1158*61046927SAndroid Build Coastguard Worker /* Linear context is also the ralloc parent of extra nodes. */
1159*61046927SAndroid Build Coastguard Worker ralloc_steal(new_ralloc_ctx, ctx);
1160*61046927SAndroid Build Coastguard Worker }
1161*61046927SAndroid Build Coastguard Worker
1162*61046927SAndroid Build Coastguard Worker void *
ralloc_parent_of_linear_context(linear_ctx * ctx)1163*61046927SAndroid Build Coastguard Worker ralloc_parent_of_linear_context(linear_ctx *ctx)
1164*61046927SAndroid Build Coastguard Worker {
1165*61046927SAndroid Build Coastguard Worker assert(ctx->magic == LMAGIC_CONTEXT);
1166*61046927SAndroid Build Coastguard Worker return PTR_FROM_HEADER(get_header(ctx)->parent);
1167*61046927SAndroid Build Coastguard Worker }
1168*61046927SAndroid Build Coastguard Worker
1169*61046927SAndroid Build Coastguard Worker /* All code below is pretty much copied from ralloc and only the alloc
1170*61046927SAndroid Build Coastguard Worker * calls are different.
1171*61046927SAndroid Build Coastguard Worker */
1172*61046927SAndroid Build Coastguard Worker
1173*61046927SAndroid Build Coastguard Worker char *
linear_strdup(linear_ctx * ctx,const char * str)1174*61046927SAndroid Build Coastguard Worker linear_strdup(linear_ctx *ctx, const char *str)
1175*61046927SAndroid Build Coastguard Worker {
1176*61046927SAndroid Build Coastguard Worker unsigned n;
1177*61046927SAndroid Build Coastguard Worker char *ptr;
1178*61046927SAndroid Build Coastguard Worker
1179*61046927SAndroid Build Coastguard Worker if (unlikely(!str))
1180*61046927SAndroid Build Coastguard Worker return NULL;
1181*61046927SAndroid Build Coastguard Worker
1182*61046927SAndroid Build Coastguard Worker n = strlen(str);
1183*61046927SAndroid Build Coastguard Worker ptr = linear_alloc_child(ctx, n + 1);
1184*61046927SAndroid Build Coastguard Worker if (unlikely(!ptr))
1185*61046927SAndroid Build Coastguard Worker return NULL;
1186*61046927SAndroid Build Coastguard Worker
1187*61046927SAndroid Build Coastguard Worker memcpy(ptr, str, n);
1188*61046927SAndroid Build Coastguard Worker ptr[n] = '\0';
1189*61046927SAndroid Build Coastguard Worker return ptr;
1190*61046927SAndroid Build Coastguard Worker }
1191*61046927SAndroid Build Coastguard Worker
1192*61046927SAndroid Build Coastguard Worker char *
linear_asprintf(linear_ctx * ctx,const char * fmt,...)1193*61046927SAndroid Build Coastguard Worker linear_asprintf(linear_ctx *ctx, const char *fmt, ...)
1194*61046927SAndroid Build Coastguard Worker {
1195*61046927SAndroid Build Coastguard Worker char *ptr;
1196*61046927SAndroid Build Coastguard Worker va_list args;
1197*61046927SAndroid Build Coastguard Worker va_start(args, fmt);
1198*61046927SAndroid Build Coastguard Worker ptr = linear_vasprintf(ctx, fmt, args);
1199*61046927SAndroid Build Coastguard Worker va_end(args);
1200*61046927SAndroid Build Coastguard Worker return ptr;
1201*61046927SAndroid Build Coastguard Worker }
1202*61046927SAndroid Build Coastguard Worker
1203*61046927SAndroid Build Coastguard Worker char *
linear_vasprintf(linear_ctx * ctx,const char * fmt,va_list args)1204*61046927SAndroid Build Coastguard Worker linear_vasprintf(linear_ctx *ctx, const char *fmt, va_list args)
1205*61046927SAndroid Build Coastguard Worker {
1206*61046927SAndroid Build Coastguard Worker unsigned size = u_printf_length(fmt, args) + 1;
1207*61046927SAndroid Build Coastguard Worker
1208*61046927SAndroid Build Coastguard Worker char *ptr = linear_alloc_child(ctx, size);
1209*61046927SAndroid Build Coastguard Worker if (ptr != NULL)
1210*61046927SAndroid Build Coastguard Worker vsnprintf(ptr, size, fmt, args);
1211*61046927SAndroid Build Coastguard Worker
1212*61046927SAndroid Build Coastguard Worker return ptr;
1213*61046927SAndroid Build Coastguard Worker }
1214*61046927SAndroid Build Coastguard Worker
1215*61046927SAndroid Build Coastguard Worker bool
linear_asprintf_append(linear_ctx * ctx,char ** str,const char * fmt,...)1216*61046927SAndroid Build Coastguard Worker linear_asprintf_append(linear_ctx *ctx, char **str, const char *fmt, ...)
1217*61046927SAndroid Build Coastguard Worker {
1218*61046927SAndroid Build Coastguard Worker bool success;
1219*61046927SAndroid Build Coastguard Worker va_list args;
1220*61046927SAndroid Build Coastguard Worker va_start(args, fmt);
1221*61046927SAndroid Build Coastguard Worker success = linear_vasprintf_append(ctx, str, fmt, args);
1222*61046927SAndroid Build Coastguard Worker va_end(args);
1223*61046927SAndroid Build Coastguard Worker return success;
1224*61046927SAndroid Build Coastguard Worker }
1225*61046927SAndroid Build Coastguard Worker
1226*61046927SAndroid Build Coastguard Worker bool
linear_vasprintf_append(linear_ctx * ctx,char ** str,const char * fmt,va_list args)1227*61046927SAndroid Build Coastguard Worker linear_vasprintf_append(linear_ctx *ctx, char **str, const char *fmt, va_list args)
1228*61046927SAndroid Build Coastguard Worker {
1229*61046927SAndroid Build Coastguard Worker size_t existing_length;
1230*61046927SAndroid Build Coastguard Worker assert(str != NULL);
1231*61046927SAndroid Build Coastguard Worker existing_length = *str ? strlen(*str) : 0;
1232*61046927SAndroid Build Coastguard Worker return linear_vasprintf_rewrite_tail(ctx, str, &existing_length, fmt, args);
1233*61046927SAndroid Build Coastguard Worker }
1234*61046927SAndroid Build Coastguard Worker
1235*61046927SAndroid Build Coastguard Worker bool
linear_asprintf_rewrite_tail(linear_ctx * ctx,char ** str,size_t * start,const char * fmt,...)1236*61046927SAndroid Build Coastguard Worker linear_asprintf_rewrite_tail(linear_ctx *ctx, char **str, size_t *start,
1237*61046927SAndroid Build Coastguard Worker const char *fmt, ...)
1238*61046927SAndroid Build Coastguard Worker {
1239*61046927SAndroid Build Coastguard Worker bool success;
1240*61046927SAndroid Build Coastguard Worker va_list args;
1241*61046927SAndroid Build Coastguard Worker va_start(args, fmt);
1242*61046927SAndroid Build Coastguard Worker success = linear_vasprintf_rewrite_tail(ctx, str, start, fmt, args);
1243*61046927SAndroid Build Coastguard Worker va_end(args);
1244*61046927SAndroid Build Coastguard Worker return success;
1245*61046927SAndroid Build Coastguard Worker }
1246*61046927SAndroid Build Coastguard Worker
1247*61046927SAndroid Build Coastguard Worker bool
linear_vasprintf_rewrite_tail(linear_ctx * ctx,char ** str,size_t * start,const char * fmt,va_list args)1248*61046927SAndroid Build Coastguard Worker linear_vasprintf_rewrite_tail(linear_ctx *ctx, char **str, size_t *start,
1249*61046927SAndroid Build Coastguard Worker const char *fmt, va_list args)
1250*61046927SAndroid Build Coastguard Worker {
1251*61046927SAndroid Build Coastguard Worker size_t new_length;
1252*61046927SAndroid Build Coastguard Worker char *ptr;
1253*61046927SAndroid Build Coastguard Worker
1254*61046927SAndroid Build Coastguard Worker assert(str != NULL);
1255*61046927SAndroid Build Coastguard Worker
1256*61046927SAndroid Build Coastguard Worker if (unlikely(*str == NULL)) {
1257*61046927SAndroid Build Coastguard Worker *str = linear_vasprintf(ctx, fmt, args);
1258*61046927SAndroid Build Coastguard Worker *start = strlen(*str);
1259*61046927SAndroid Build Coastguard Worker return true;
1260*61046927SAndroid Build Coastguard Worker }
1261*61046927SAndroid Build Coastguard Worker
1262*61046927SAndroid Build Coastguard Worker new_length = u_printf_length(fmt, args);
1263*61046927SAndroid Build Coastguard Worker
1264*61046927SAndroid Build Coastguard Worker ptr = linear_alloc_child(ctx, *start + new_length + 1);
1265*61046927SAndroid Build Coastguard Worker if (unlikely(ptr == NULL))
1266*61046927SAndroid Build Coastguard Worker return false;
1267*61046927SAndroid Build Coastguard Worker
1268*61046927SAndroid Build Coastguard Worker memcpy(ptr, *str, *start);
1269*61046927SAndroid Build Coastguard Worker
1270*61046927SAndroid Build Coastguard Worker vsnprintf(ptr + *start, new_length + 1, fmt, args);
1271*61046927SAndroid Build Coastguard Worker *str = ptr;
1272*61046927SAndroid Build Coastguard Worker *start += new_length;
1273*61046927SAndroid Build Coastguard Worker return true;
1274*61046927SAndroid Build Coastguard Worker }
1275*61046927SAndroid Build Coastguard Worker
1276*61046927SAndroid Build Coastguard Worker /* helper routine for strcat/strncat - n is the exact amount to copy */
1277*61046927SAndroid Build Coastguard Worker static bool
linear_cat(linear_ctx * ctx,char ** dest,const char * str,unsigned n)1278*61046927SAndroid Build Coastguard Worker linear_cat(linear_ctx *ctx, char **dest, const char *str, unsigned n)
1279*61046927SAndroid Build Coastguard Worker {
1280*61046927SAndroid Build Coastguard Worker char *both;
1281*61046927SAndroid Build Coastguard Worker unsigned existing_length;
1282*61046927SAndroid Build Coastguard Worker assert(dest != NULL && *dest != NULL);
1283*61046927SAndroid Build Coastguard Worker
1284*61046927SAndroid Build Coastguard Worker existing_length = strlen(*dest);
1285*61046927SAndroid Build Coastguard Worker both = linear_alloc_child(ctx, existing_length + n + 1);
1286*61046927SAndroid Build Coastguard Worker if (unlikely(both == NULL))
1287*61046927SAndroid Build Coastguard Worker return false;
1288*61046927SAndroid Build Coastguard Worker
1289*61046927SAndroid Build Coastguard Worker memcpy(both, *dest, existing_length);
1290*61046927SAndroid Build Coastguard Worker memcpy(both + existing_length, str, n);
1291*61046927SAndroid Build Coastguard Worker both[existing_length + n] = '\0';
1292*61046927SAndroid Build Coastguard Worker
1293*61046927SAndroid Build Coastguard Worker *dest = both;
1294*61046927SAndroid Build Coastguard Worker return true;
1295*61046927SAndroid Build Coastguard Worker }
1296*61046927SAndroid Build Coastguard Worker
1297*61046927SAndroid Build Coastguard Worker bool
linear_strcat(linear_ctx * ctx,char ** dest,const char * str)1298*61046927SAndroid Build Coastguard Worker linear_strcat(linear_ctx *ctx, char **dest, const char *str)
1299*61046927SAndroid Build Coastguard Worker {
1300*61046927SAndroid Build Coastguard Worker return linear_cat(ctx, dest, str, strlen(str));
1301*61046927SAndroid Build Coastguard Worker }
1302*61046927SAndroid Build Coastguard Worker
1303*61046927SAndroid Build Coastguard Worker void *
linear_alloc_child_array(linear_ctx * ctx,size_t size,unsigned count)1304*61046927SAndroid Build Coastguard Worker linear_alloc_child_array(linear_ctx *ctx, size_t size, unsigned count)
1305*61046927SAndroid Build Coastguard Worker {
1306*61046927SAndroid Build Coastguard Worker if (count > SIZE_MAX/size)
1307*61046927SAndroid Build Coastguard Worker return NULL;
1308*61046927SAndroid Build Coastguard Worker
1309*61046927SAndroid Build Coastguard Worker return linear_alloc_child(ctx, size * count);
1310*61046927SAndroid Build Coastguard Worker }
1311*61046927SAndroid Build Coastguard Worker
1312*61046927SAndroid Build Coastguard Worker void *
linear_zalloc_child_array(linear_ctx * ctx,size_t size,unsigned count)1313*61046927SAndroid Build Coastguard Worker linear_zalloc_child_array(linear_ctx *ctx, size_t size, unsigned count)
1314*61046927SAndroid Build Coastguard Worker {
1315*61046927SAndroid Build Coastguard Worker if (count > SIZE_MAX/size)
1316*61046927SAndroid Build Coastguard Worker return NULL;
1317*61046927SAndroid Build Coastguard Worker
1318*61046927SAndroid Build Coastguard Worker return linear_zalloc_child(ctx, size * count);
1319*61046927SAndroid Build Coastguard Worker }
1320*61046927SAndroid Build Coastguard Worker
1321*61046927SAndroid Build Coastguard Worker typedef struct {
1322*61046927SAndroid Build Coastguard Worker FILE *f;
1323*61046927SAndroid Build Coastguard Worker unsigned indent;
1324*61046927SAndroid Build Coastguard Worker
1325*61046927SAndroid Build Coastguard Worker unsigned ralloc_count;
1326*61046927SAndroid Build Coastguard Worker unsigned linear_count;
1327*61046927SAndroid Build Coastguard Worker unsigned gc_count;
1328*61046927SAndroid Build Coastguard Worker
1329*61046927SAndroid Build Coastguard Worker /* These don't include padding or metadata from suballocators. */
1330*61046927SAndroid Build Coastguard Worker unsigned content_bytes;
1331*61046927SAndroid Build Coastguard Worker unsigned ralloc_metadata_bytes;
1332*61046927SAndroid Build Coastguard Worker unsigned linear_metadata_bytes;
1333*61046927SAndroid Build Coastguard Worker unsigned gc_metadata_bytes;
1334*61046927SAndroid Build Coastguard Worker
1335*61046927SAndroid Build Coastguard Worker bool inside_linear;
1336*61046927SAndroid Build Coastguard Worker bool inside_gc;
1337*61046927SAndroid Build Coastguard Worker } ralloc_print_info_state;
1338*61046927SAndroid Build Coastguard Worker
1339*61046927SAndroid Build Coastguard Worker static void
ralloc_print_info_helper(ralloc_print_info_state * state,const ralloc_header * info)1340*61046927SAndroid Build Coastguard Worker ralloc_print_info_helper(ralloc_print_info_state *state, const ralloc_header *info)
1341*61046927SAndroid Build Coastguard Worker {
1342*61046927SAndroid Build Coastguard Worker FILE *f = state->f;
1343*61046927SAndroid Build Coastguard Worker
1344*61046927SAndroid Build Coastguard Worker if (f) {
1345*61046927SAndroid Build Coastguard Worker for (unsigned i = 0; i < state->indent; i++) fputc(' ', f);
1346*61046927SAndroid Build Coastguard Worker fprintf(f, "%p", info);
1347*61046927SAndroid Build Coastguard Worker }
1348*61046927SAndroid Build Coastguard Worker
1349*61046927SAndroid Build Coastguard Worker /* TODO: Account for padding used in various places. */
1350*61046927SAndroid Build Coastguard Worker
1351*61046927SAndroid Build Coastguard Worker #ifndef NDEBUG
1352*61046927SAndroid Build Coastguard Worker assert(info->canary == CANARY);
1353*61046927SAndroid Build Coastguard Worker if (f) fprintf(f, " (%d bytes)", info->size);
1354*61046927SAndroid Build Coastguard Worker state->content_bytes += info->size;
1355*61046927SAndroid Build Coastguard Worker state->ralloc_metadata_bytes += sizeof(ralloc_header);
1356*61046927SAndroid Build Coastguard Worker
1357*61046927SAndroid Build Coastguard Worker const void *ptr = PTR_FROM_HEADER(info);
1358*61046927SAndroid Build Coastguard Worker const linear_ctx *lin_ctx = ptr;
1359*61046927SAndroid Build Coastguard Worker const gc_ctx *gc_ctx = ptr;
1360*61046927SAndroid Build Coastguard Worker
1361*61046927SAndroid Build Coastguard Worker if (lin_ctx->magic == LMAGIC_CONTEXT) {
1362*61046927SAndroid Build Coastguard Worker if (f) fprintf(f, " (linear context)");
1363*61046927SAndroid Build Coastguard Worker assert(!state->inside_gc && !state->inside_linear);
1364*61046927SAndroid Build Coastguard Worker state->inside_linear = true;
1365*61046927SAndroid Build Coastguard Worker state->linear_metadata_bytes += sizeof(linear_ctx);
1366*61046927SAndroid Build Coastguard Worker state->content_bytes -= sizeof(linear_ctx);
1367*61046927SAndroid Build Coastguard Worker state->linear_count++;
1368*61046927SAndroid Build Coastguard Worker } else if (gc_ctx->canary == GC_CONTEXT_CANARY) {
1369*61046927SAndroid Build Coastguard Worker if (f) fprintf(f, " (gc context)");
1370*61046927SAndroid Build Coastguard Worker assert(!state->inside_gc && !state->inside_linear);
1371*61046927SAndroid Build Coastguard Worker state->inside_gc = true;
1372*61046927SAndroid Build Coastguard Worker state->gc_metadata_bytes += sizeof(gc_block_header);
1373*61046927SAndroid Build Coastguard Worker } else if (state->inside_linear) {
1374*61046927SAndroid Build Coastguard Worker const linear_node_canary *lin_node = ptr;
1375*61046927SAndroid Build Coastguard Worker if (lin_node->magic == LMAGIC_NODE) {
1376*61046927SAndroid Build Coastguard Worker if (f) fprintf(f, " (linear node buffer)");
1377*61046927SAndroid Build Coastguard Worker state->content_bytes -= sizeof(linear_node_canary);
1378*61046927SAndroid Build Coastguard Worker state->linear_metadata_bytes += sizeof(linear_node_canary);
1379*61046927SAndroid Build Coastguard Worker state->linear_count++;
1380*61046927SAndroid Build Coastguard Worker }
1381*61046927SAndroid Build Coastguard Worker } else if (state->inside_gc) {
1382*61046927SAndroid Build Coastguard Worker if (f) fprintf(f, " (gc slab or large block)");
1383*61046927SAndroid Build Coastguard Worker state->gc_count++;
1384*61046927SAndroid Build Coastguard Worker }
1385*61046927SAndroid Build Coastguard Worker #endif
1386*61046927SAndroid Build Coastguard Worker
1387*61046927SAndroid Build Coastguard Worker state->ralloc_count++;
1388*61046927SAndroid Build Coastguard Worker if (f) fprintf(f, "\n");
1389*61046927SAndroid Build Coastguard Worker
1390*61046927SAndroid Build Coastguard Worker const ralloc_header *c = info->child;
1391*61046927SAndroid Build Coastguard Worker state->indent += 2;
1392*61046927SAndroid Build Coastguard Worker while (c != NULL) {
1393*61046927SAndroid Build Coastguard Worker ralloc_print_info_helper(state, c);
1394*61046927SAndroid Build Coastguard Worker c = c->next;
1395*61046927SAndroid Build Coastguard Worker }
1396*61046927SAndroid Build Coastguard Worker state->indent -= 2;
1397*61046927SAndroid Build Coastguard Worker
1398*61046927SAndroid Build Coastguard Worker #ifndef NDEBUG
1399*61046927SAndroid Build Coastguard Worker if (lin_ctx->magic == LMAGIC_CONTEXT) state->inside_linear = false;
1400*61046927SAndroid Build Coastguard Worker else if (gc_ctx->canary == GC_CONTEXT_CANARY) state->inside_gc = false;
1401*61046927SAndroid Build Coastguard Worker #endif
1402*61046927SAndroid Build Coastguard Worker }
1403*61046927SAndroid Build Coastguard Worker
1404*61046927SAndroid Build Coastguard Worker void
ralloc_print_info(FILE * f,const void * p,unsigned flags)1405*61046927SAndroid Build Coastguard Worker ralloc_print_info(FILE *f, const void *p, unsigned flags)
1406*61046927SAndroid Build Coastguard Worker {
1407*61046927SAndroid Build Coastguard Worker ralloc_print_info_state state = {
1408*61046927SAndroid Build Coastguard Worker .f = ((flags & RALLOC_PRINT_INFO_SUMMARY_ONLY) == 1) ? NULL : f,
1409*61046927SAndroid Build Coastguard Worker };
1410*61046927SAndroid Build Coastguard Worker
1411*61046927SAndroid Build Coastguard Worker const ralloc_header *info = get_header(p);
1412*61046927SAndroid Build Coastguard Worker ralloc_print_info_helper(&state, info);
1413*61046927SAndroid Build Coastguard Worker
1414*61046927SAndroid Build Coastguard Worker fprintf(f, "==== RALLOC INFO ptr=%p info=%p\n"
1415*61046927SAndroid Build Coastguard Worker "ralloc allocations = %d\n"
1416*61046927SAndroid Build Coastguard Worker " - linear = %d\n"
1417*61046927SAndroid Build Coastguard Worker " - gc = %d\n"
1418*61046927SAndroid Build Coastguard Worker " - other = %d\n",
1419*61046927SAndroid Build Coastguard Worker p, info,
1420*61046927SAndroid Build Coastguard Worker state.ralloc_count,
1421*61046927SAndroid Build Coastguard Worker state.linear_count,
1422*61046927SAndroid Build Coastguard Worker state.gc_count,
1423*61046927SAndroid Build Coastguard Worker state.ralloc_count - state.linear_count - state.gc_count);
1424*61046927SAndroid Build Coastguard Worker
1425*61046927SAndroid Build Coastguard Worker if (state.content_bytes) {
1426*61046927SAndroid Build Coastguard Worker fprintf(f,
1427*61046927SAndroid Build Coastguard Worker "content bytes = %d\n"
1428*61046927SAndroid Build Coastguard Worker "ralloc metadata bytes = %d\n"
1429*61046927SAndroid Build Coastguard Worker "linear metadata bytes = %d\n",
1430*61046927SAndroid Build Coastguard Worker state.content_bytes,
1431*61046927SAndroid Build Coastguard Worker state.ralloc_metadata_bytes,
1432*61046927SAndroid Build Coastguard Worker state.linear_metadata_bytes);
1433*61046927SAndroid Build Coastguard Worker }
1434*61046927SAndroid Build Coastguard Worker
1435*61046927SAndroid Build Coastguard Worker fprintf(f, "====\n");
1436*61046927SAndroid Build Coastguard Worker }
1437*61046927SAndroid Build Coastguard Worker
1438