blob: 90c6935a0edd531f2b3d6169b3c14ae84fa76392 [file] [log] [blame]
Jason Evans0657f122011-03-18 17:56:14 -07001#define JEMALLOC_RTREE_C_
Jason Evans2dbecf12010-09-05 10:35:13 -07002#include "jemalloc/internal/jemalloc_internal.h"
3
4rtree_t *
5rtree_new(unsigned bits)
6{
7 rtree_t *ret;
8 unsigned bits_per_level, height, i;
9
10 bits_per_level = ffs(pow2_ceil((RTREE_NODESIZE / sizeof(void *)))) - 1;
11 height = bits / bits_per_level;
12 if (height * bits_per_level != bits)
13 height++;
14 assert(height * bits_per_level >= bits);
15
Jason Evansc2fc8c82010-10-01 18:02:43 -070016 ret = (rtree_t*)base_alloc(offsetof(rtree_t, level2bits) +
17 (sizeof(unsigned) * height));
Jason Evans2dbecf12010-09-05 10:35:13 -070018 if (ret == NULL)
19 return (NULL);
Jason Evansc2fc8c82010-10-01 18:02:43 -070020 memset(ret, 0, offsetof(rtree_t, level2bits) + (sizeof(unsigned) *
21 height));
Jason Evans2dbecf12010-09-05 10:35:13 -070022
Jason Evans819d11b2011-03-15 14:25:56 -070023 if (malloc_mutex_init(&ret->mutex)) {
24 /* Leak the rtree. */
25 return (NULL);
26 }
Jason Evans2dbecf12010-09-05 10:35:13 -070027 ret->height = height;
28 if (bits_per_level * height > bits)
29 ret->level2bits[0] = bits % bits_per_level;
30 else
31 ret->level2bits[0] = bits_per_level;
32 for (i = 1; i < height; i++)
33 ret->level2bits[i] = bits_per_level;
34
35 ret->root = (void**)base_alloc(sizeof(void *) << ret->level2bits[0]);
36 if (ret->root == NULL) {
37 /*
38 * We leak the rtree here, since there's no generic base
39 * deallocation.
40 */
41 return (NULL);
42 }
43 memset(ret->root, 0, sizeof(void *) << ret->level2bits[0]);
44
45 return (ret);
46}
Jason Evans20f1fc92012-10-09 14:46:22 -070047
48void
49rtree_prefork(rtree_t *rtree)
50{
51
52 malloc_mutex_prefork(&rtree->mutex);
53}
54
55void
56rtree_postfork_parent(rtree_t *rtree)
57{
58
59 malloc_mutex_postfork_parent(&rtree->mutex);
60}
61
62void
63rtree_postfork_child(rtree_t *rtree)
64{
65
66 malloc_mutex_postfork_child(&rtree->mutex);
67}