2011-03-19 08:56:14 +08:00
|
|
|
#define JEMALLOC_RTREE_C_
|
2010-09-06 01:35:13 +08:00
|
|
|
#include "jemalloc/internal/jemalloc_internal.h"
|
|
|
|
|
|
|
|
rtree_t *
|
2014-01-03 08:08:28 +08:00
|
|
|
rtree_new(unsigned bits, rtree_alloc_t *alloc, rtree_dalloc_t *dalloc)
|
2010-09-06 01:35:13 +08:00
|
|
|
{
|
|
|
|
rtree_t *ret;
|
2014-01-03 09:36:38 +08:00
|
|
|
unsigned bits_per_level, bits_in_leaf, height, i;
|
2010-09-06 01:35:13 +08:00
|
|
|
|
2014-01-03 08:08:28 +08:00
|
|
|
assert(bits > 0 && bits <= (sizeof(uintptr_t) << 3));
|
|
|
|
|
2014-05-29 10:37:02 +08:00
|
|
|
bits_per_level = jemalloc_ffs(pow2_ceil((RTREE_NODESIZE / sizeof(void *)))) - 1;
|
|
|
|
bits_in_leaf = jemalloc_ffs(pow2_ceil((RTREE_NODESIZE / sizeof(uint8_t)))) - 1;
|
2014-01-03 09:36:38 +08:00
|
|
|
if (bits > bits_in_leaf) {
|
|
|
|
height = 1 + (bits - bits_in_leaf) / bits_per_level;
|
|
|
|
if ((height-1) * bits_per_level + bits_in_leaf != bits)
|
|
|
|
height++;
|
|
|
|
} else {
|
|
|
|
height = 1;
|
|
|
|
}
|
|
|
|
assert((height-1) * bits_per_level + bits_in_leaf >= bits);
|
2010-09-06 01:35:13 +08:00
|
|
|
|
2014-01-03 08:08:28 +08:00
|
|
|
ret = (rtree_t*)alloc(offsetof(rtree_t, level2bits) +
|
2010-10-02 09:02:43 +08:00
|
|
|
(sizeof(unsigned) * height));
|
2010-09-06 01:35:13 +08:00
|
|
|
if (ret == NULL)
|
|
|
|
return (NULL);
|
2010-10-02 09:02:43 +08:00
|
|
|
memset(ret, 0, offsetof(rtree_t, level2bits) + (sizeof(unsigned) *
|
|
|
|
height));
|
2010-09-06 01:35:13 +08:00
|
|
|
|
2014-01-03 08:08:28 +08:00
|
|
|
ret->alloc = alloc;
|
|
|
|
ret->dalloc = dalloc;
|
2011-03-16 05:25:56 +08:00
|
|
|
if (malloc_mutex_init(&ret->mutex)) {
|
2014-01-03 09:36:38 +08:00
|
|
|
if (dalloc != NULL)
|
|
|
|
dalloc(ret);
|
2011-03-16 05:25:56 +08:00
|
|
|
return (NULL);
|
|
|
|
}
|
2010-09-06 01:35:13 +08:00
|
|
|
ret->height = height;
|
2014-01-03 09:36:38 +08:00
|
|
|
if (height > 1) {
|
|
|
|
if ((height-1) * bits_per_level + bits_in_leaf > bits) {
|
|
|
|
ret->level2bits[0] = (bits - bits_in_leaf) %
|
|
|
|
bits_per_level;
|
|
|
|
} else
|
|
|
|
ret->level2bits[0] = bits_per_level;
|
|
|
|
for (i = 1; i < height-1; i++)
|
|
|
|
ret->level2bits[i] = bits_per_level;
|
|
|
|
ret->level2bits[height-1] = bits_in_leaf;
|
|
|
|
} else
|
|
|
|
ret->level2bits[0] = bits;
|
2010-09-06 01:35:13 +08:00
|
|
|
|
2014-01-03 08:08:28 +08:00
|
|
|
ret->root = (void**)alloc(sizeof(void *) << ret->level2bits[0]);
|
2010-09-06 01:35:13 +08:00
|
|
|
if (ret->root == NULL) {
|
2014-01-03 09:36:38 +08:00
|
|
|
if (dalloc != NULL)
|
|
|
|
dalloc(ret);
|
2010-09-06 01:35:13 +08:00
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
memset(ret->root, 0, sizeof(void *) << ret->level2bits[0]);
|
|
|
|
|
|
|
|
return (ret);
|
|
|
|
}
|
2012-10-10 05:46:22 +08:00
|
|
|
|
2014-01-03 08:08:28 +08:00
|
|
|
static void
|
|
|
|
rtree_delete_subtree(rtree_t *rtree, void **node, unsigned level)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (level < rtree->height - 1) {
|
|
|
|
size_t nchildren, i;
|
|
|
|
|
|
|
|
nchildren = ZU(1) << rtree->level2bits[level];
|
|
|
|
for (i = 0; i < nchildren; i++) {
|
|
|
|
void **child = (void **)node[i];
|
|
|
|
if (child != NULL)
|
|
|
|
rtree_delete_subtree(rtree, child, level + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rtree->dalloc(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rtree_delete(rtree_t *rtree)
|
|
|
|
{
|
|
|
|
|
|
|
|
rtree_delete_subtree(rtree, rtree->root, 0);
|
|
|
|
rtree->dalloc(rtree);
|
|
|
|
}
|
|
|
|
|
2012-10-10 05:46:22 +08:00
|
|
|
void
|
|
|
|
rtree_prefork(rtree_t *rtree)
|
|
|
|
{
|
|
|
|
|
|
|
|
malloc_mutex_prefork(&rtree->mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rtree_postfork_parent(rtree_t *rtree)
|
|
|
|
{
|
|
|
|
|
|
|
|
malloc_mutex_postfork_parent(&rtree->mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
rtree_postfork_child(rtree_t *rtree)
|
|
|
|
{
|
|
|
|
|
|
|
|
malloc_mutex_postfork_child(&rtree->mutex);
|
|
|
|
}
|