Jason Evans | 0657f12 | 2011-03-18 17:56:14 -0700 | [diff] [blame] | 1 | #define JEMALLOC_RTREE_C_ |
Jason Evans | 2dbecf1 | 2010-09-05 10:35:13 -0700 | [diff] [blame] | 2 | #include "jemalloc/internal/jemalloc_internal.h" |
| 3 | |
| 4 | rtree_t * |
| 5 | rtree_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 Evans | c2fc8c8 | 2010-10-01 18:02:43 -0700 | [diff] [blame] | 16 | ret = (rtree_t*)base_alloc(offsetof(rtree_t, level2bits) + |
| 17 | (sizeof(unsigned) * height)); |
Jason Evans | 2dbecf1 | 2010-09-05 10:35:13 -0700 | [diff] [blame] | 18 | if (ret == NULL) |
| 19 | return (NULL); |
Jason Evans | c2fc8c8 | 2010-10-01 18:02:43 -0700 | [diff] [blame] | 20 | memset(ret, 0, offsetof(rtree_t, level2bits) + (sizeof(unsigned) * |
| 21 | height)); |
Jason Evans | 2dbecf1 | 2010-09-05 10:35:13 -0700 | [diff] [blame] | 22 | |
Jason Evans | 819d11b | 2011-03-15 14:25:56 -0700 | [diff] [blame] | 23 | if (malloc_mutex_init(&ret->mutex)) { |
| 24 | /* Leak the rtree. */ |
| 25 | return (NULL); |
| 26 | } |
Jason Evans | 2dbecf1 | 2010-09-05 10:35:13 -0700 | [diff] [blame] | 27 | 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 Evans | 20f1fc9 | 2012-10-09 14:46:22 -0700 | [diff] [blame^] | 47 | |
| 48 | void |
| 49 | rtree_prefork(rtree_t *rtree) |
| 50 | { |
| 51 | |
| 52 | malloc_mutex_prefork(&rtree->mutex); |
| 53 | } |
| 54 | |
| 55 | void |
| 56 | rtree_postfork_parent(rtree_t *rtree) |
| 57 | { |
| 58 | |
| 59 | malloc_mutex_postfork_parent(&rtree->mutex); |
| 60 | } |
| 61 | |
| 62 | void |
| 63 | rtree_postfork_child(rtree_t *rtree) |
| 64 | { |
| 65 | |
| 66 | malloc_mutex_postfork_child(&rtree->mutex); |
| 67 | } |