When the pool is destroyed, all its external references are unreferenced.
#include "macros.h"
#include "failures.h"
+typedef struct buffer buffer_t;
+typedef struct buffer string_t;
+
+#include "array-decl.h" /* ARRAY*()s may exist in any header */
#include "malloc-overflow.h"
#include "data-stack.h"
#include "mempool.h"
typedef void lib_atexit_callback_t(void);
-#include "array-decl.h" /* ARRAY*()s may exist in any header */
#include "bits.h"
#include "hash-decl.h" /* HASH_TABLE*()s may exist in any header */
#include "strfuncs.h"
if (--apool->refcount > 0)
return;
+ pool_external_refs_unref(&apool->pool);
pool_alloconly_destroy(apool);
}
/* Copyright (c) 2005-2018 Dovecot authors, see the included COPYING file */
#include "lib.h"
+#include "array.h"
/* The various implementations of pools API assume that they'll never be
asked for more than SSIZE_T_MAX bytes. This is a sanity check to make
i_assert(exp_size >= min_size);
return exp_size;
}
+
+void pool_add_external_ref(pool_t pool, pool_t ref_pool)
+{
+ if (!array_is_created(&pool->external_refs))
+ i_array_init(&pool->external_refs, 1);
+ array_push_back(&pool->external_refs, &ref_pool);
+ pool_ref(ref_pool);
+}
+
+void pool_external_refs_unref(pool_t pool)
+{
+ if (array_is_created(&pool->external_refs)) {
+ pool_t external_pool;
+ array_foreach_elem(&pool->external_refs, external_pool)
+ pool_unref(&external_pool);
+ array_free(&pool->external_refs);
+ }
+}
struct pool {
const struct pool_vfuncs *v;
+ ARRAY(pool_t) external_refs;
bool alloconly_pool:1;
bool datastack_pool:1;
old_size + 1. */
size_t pool_get_exp_grown_size(pool_t pool, size_t old_size, size_t min_size);
+/* Reference another memory pool in the given pool. When the pool is freed,
+ the referenced memory pools are also unreferenced. */
+void pool_add_external_ref(pool_t pool, pool_t ref_pool);
+
/* We require sizeof(type) to be <= UINT_MAX. This allows compiler to optimize
away the entire MALLOC_MULTIPLY() call on 64bit systems. */
#define p_new(pool, type, count) \
/* private: */
void pool_system_free(pool_t pool, void *mem);
+void pool_external_refs_unref(pool_t pool);
#endif