Merge pull request #9792 from poettering/hashmap-mempool
minor hashmap fixes
This commit is contained in:
+22
-8
@@ -6,8 +6,9 @@
|
||||
#include <string.h>
|
||||
|
||||
#include "alloc-util.h"
|
||||
#include "hashmap.h"
|
||||
#include "env-util.h"
|
||||
#include "fileio.h"
|
||||
#include "hashmap.h"
|
||||
#include "macro.h"
|
||||
#include "mempool.h"
|
||||
#include "process-util.h"
|
||||
@@ -766,20 +767,31 @@ static void reset_direct_storage(HashmapBase *h) {
|
||||
memset(p, DIB_RAW_INIT, sizeof(dib_raw_t) * hi->n_direct_buckets);
|
||||
}
|
||||
|
||||
static bool use_pool(void) {
|
||||
static int b = -1;
|
||||
|
||||
if (!is_main_thread())
|
||||
return false;
|
||||
|
||||
if (b < 0)
|
||||
b = getenv_bool("SYSTEMD_MEMPOOL") != 0;
|
||||
|
||||
return b;
|
||||
}
|
||||
|
||||
static struct HashmapBase *hashmap_base_new(const struct hash_ops *hash_ops, enum HashmapType type HASHMAP_DEBUG_PARAMS) {
|
||||
HashmapBase *h;
|
||||
const struct hashmap_type_info *hi = &hashmap_type_info[type];
|
||||
bool use_pool;
|
||||
bool up;
|
||||
|
||||
use_pool = is_main_thread();
|
||||
|
||||
h = use_pool ? mempool_alloc0_tile(hi->mempool) : malloc0(hi->head_size);
|
||||
up = use_pool();
|
||||
|
||||
h = up ? mempool_alloc0_tile(hi->mempool) : malloc0(hi->head_size);
|
||||
if (!h)
|
||||
return NULL;
|
||||
|
||||
h->type = type;
|
||||
h->from_pool = use_pool;
|
||||
h->from_pool = up;
|
||||
h->hash_ops = hash_ops ? hash_ops : &trivial_hash_ops;
|
||||
|
||||
if (type == HASHMAP_TYPE_ORDERED) {
|
||||
@@ -857,9 +869,11 @@ static void hashmap_free_no_clear(HashmapBase *h) {
|
||||
assert_se(pthread_mutex_unlock(&hashmap_debug_list_mutex) == 0);
|
||||
#endif
|
||||
|
||||
if (h->from_pool)
|
||||
if (h->from_pool) {
|
||||
/* Ensure that the object didn't get migrated between threads. */
|
||||
assert_se(is_main_thread());
|
||||
mempool_free_tile(hashmap_type_info[h->type].mempool, h);
|
||||
else
|
||||
} else
|
||||
free(h);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user