mirror of
https://github.com/postgres/postgres.git
synced 2025-11-15 03:41:20 +03:00
test_dsa: Avoid leaking LWLock tranches.
Since this is a test module, leaking a couple of LWLock tranches is fine, but we want to discourage that pattern in third-party code. This commit teaches the module to create only one tranche and to store its ID in shared memory for use by other backends. Reported-by: Alexander Lakhin <exclusion@gmail.com> Reviewed-by: Sami Imseih <samimseih@gmail.com> Discussion: https://postgr.es/m/dd36d384-55df-4fc2-825c-5bc56c950fa9%40gmail.com
This commit is contained in:
@@ -13,25 +13,35 @@
|
|||||||
#include "postgres.h"
|
#include "postgres.h"
|
||||||
|
|
||||||
#include "fmgr.h"
|
#include "fmgr.h"
|
||||||
|
#include "storage/dsm_registry.h"
|
||||||
#include "storage/lwlock.h"
|
#include "storage/lwlock.h"
|
||||||
#include "utils/dsa.h"
|
#include "utils/dsa.h"
|
||||||
#include "utils/resowner.h"
|
#include "utils/resowner.h"
|
||||||
|
|
||||||
PG_MODULE_MAGIC;
|
PG_MODULE_MAGIC;
|
||||||
|
|
||||||
|
static void
|
||||||
|
init_tranche(void *ptr)
|
||||||
|
{
|
||||||
|
int *tranche_id = (int *) ptr;
|
||||||
|
|
||||||
|
*tranche_id = LWLockNewTrancheId("test_dsa");
|
||||||
|
}
|
||||||
|
|
||||||
/* Test basic DSA functionality */
|
/* Test basic DSA functionality */
|
||||||
PG_FUNCTION_INFO_V1(test_dsa_basic);
|
PG_FUNCTION_INFO_V1(test_dsa_basic);
|
||||||
Datum
|
Datum
|
||||||
test_dsa_basic(PG_FUNCTION_ARGS)
|
test_dsa_basic(PG_FUNCTION_ARGS)
|
||||||
{
|
{
|
||||||
int tranche_id;
|
int *tranche_id;
|
||||||
|
bool found;
|
||||||
dsa_area *a;
|
dsa_area *a;
|
||||||
dsa_pointer p[100];
|
dsa_pointer p[100];
|
||||||
|
|
||||||
/* XXX: this tranche is leaked */
|
tranche_id = GetNamedDSMSegment("test_dsa", sizeof(int),
|
||||||
tranche_id = LWLockNewTrancheId("test_dsa");
|
init_tranche, &found);
|
||||||
|
|
||||||
a = dsa_create(tranche_id);
|
a = dsa_create(*tranche_id);
|
||||||
for (int i = 0; i < 100; i++)
|
for (int i = 0; i < 100; i++)
|
||||||
{
|
{
|
||||||
p[i] = dsa_allocate(a, 1000);
|
p[i] = dsa_allocate(a, 1000);
|
||||||
@@ -62,17 +72,18 @@ PG_FUNCTION_INFO_V1(test_dsa_resowners);
|
|||||||
Datum
|
Datum
|
||||||
test_dsa_resowners(PG_FUNCTION_ARGS)
|
test_dsa_resowners(PG_FUNCTION_ARGS)
|
||||||
{
|
{
|
||||||
int tranche_id;
|
int *tranche_id;
|
||||||
|
bool found;
|
||||||
dsa_area *a;
|
dsa_area *a;
|
||||||
dsa_pointer p[10000];
|
dsa_pointer p[10000];
|
||||||
ResourceOwner oldowner;
|
ResourceOwner oldowner;
|
||||||
ResourceOwner childowner;
|
ResourceOwner childowner;
|
||||||
|
|
||||||
/* XXX: this tranche is leaked */
|
tranche_id = GetNamedDSMSegment("test_dsa", sizeof(int),
|
||||||
tranche_id = LWLockNewTrancheId("test_dsa");
|
init_tranche, &found);
|
||||||
|
|
||||||
/* Create DSA in parent resource owner */
|
/* Create DSA in parent resource owner */
|
||||||
a = dsa_create(tranche_id);
|
a = dsa_create(*tranche_id);
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Switch to child resource owner, and do a bunch of allocations in the
|
* Switch to child resource owner, and do a bunch of allocations in the
|
||||||
|
|||||||
Reference in New Issue
Block a user