Skip to content

Commit 9d3dc55

Browse files
committed
do not use global ctor/dtor for test params
1 parent 8a04465 commit 9d3dc55

9 files changed

+148
-101
lines changed

test/memoryPoolAPI.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2023-2024 Intel Corporation
1+
// Copyright (C) 2023-2025 Intel Corporation
22
// Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
33
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
44
// This file contains tests for UMF pool API
@@ -295,15 +295,17 @@ TEST_F(tagTest, SetAndGetInvalidPool) {
295295

296296
INSTANTIATE_TEST_SUITE_P(
297297
mallocPoolTest, umfPoolTest,
298-
::testing::Values(poolCreateExtParams{&MALLOC_POOL_OPS, nullptr,
299-
&UMF_NULL_PROVIDER_OPS, nullptr},
300-
poolCreateExtParams{umfProxyPoolOps(), nullptr,
301-
&BA_GLOBAL_PROVIDER_OPS, nullptr}));
298+
::testing::Values(poolCreateExtParams{&MALLOC_POOL_OPS, nullptr, nullptr,
299+
&UMF_NULL_PROVIDER_OPS, nullptr,
300+
nullptr},
301+
poolCreateExtParams{umfProxyPoolOps(), nullptr, nullptr,
302+
&BA_GLOBAL_PROVIDER_OPS, nullptr,
303+
nullptr}));
302304

303305
INSTANTIATE_TEST_SUITE_P(mallocMultiPoolTest, umfMultiPoolTest,
304306
::testing::Values(poolCreateExtParams{
305-
umfProxyPoolOps(), nullptr,
306-
&BA_GLOBAL_PROVIDER_OPS, nullptr}));
307+
umfProxyPoolOps(), nullptr, nullptr,
308+
&BA_GLOBAL_PROVIDER_OPS, nullptr, nullptr}));
307309

308310
INSTANTIATE_TEST_SUITE_P(umfPoolWithCreateFlagsTest, umfPoolWithCreateFlagsTest,
309311
::testing::Values(0,

test/poolFixtures.hpp

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,58 @@
1919

2020
#include "../malloc_compliance_tests.hpp"
2121

22-
using poolCreateExtParams = std::tuple<umf_memory_pool_ops_t *, void *,
23-
umf_memory_provider_ops_t *, void *>;
22+
typedef void *(*pfnPoolParamsCreate)();
23+
typedef umf_result_t (*pfnPoolParamsDestroy)(void *);
24+
25+
typedef void *(*pfnProviderParamsCreate)();
26+
typedef umf_result_t (*pfnProviderParamsDestroy)(void *);
27+
28+
using poolCreateExtParams =
29+
std::tuple<umf_memory_pool_ops_t *, pfnPoolParamsCreate,
30+
pfnPoolParamsDestroy, umf_memory_provider_ops_t *,
31+
pfnProviderParamsCreate, pfnProviderParamsDestroy>;
2432

2533
umf::pool_unique_handle_t poolCreateExtUnique(poolCreateExtParams params) {
26-
auto [pool_ops, pool_params, provider_ops, provider_params] = params;
34+
auto [pool_ops, poolParamsCreate, poolParamsDestroy, provider_ops,
35+
providerParamsCreate, providerParamsDestroy] = params;
2736

2837
umf_memory_provider_handle_t upstream_provider = nullptr;
2938
umf_memory_provider_handle_t provider = nullptr;
3039
umf_memory_pool_handle_t hPool = nullptr;
3140
umf_result_t ret;
3241

42+
void *provider_params = NULL;
43+
if (providerParamsCreate) {
44+
provider_params = providerParamsCreate();
45+
}
3346
ret = umfMemoryProviderCreate(provider_ops, provider_params,
3447
&upstream_provider);
3548
EXPECT_EQ(ret, UMF_RESULT_SUCCESS);
3649
EXPECT_NE(upstream_provider, nullptr);
3750

3851
provider = upstream_provider;
3952

53+
void *pool_params = NULL;
54+
if (poolParamsCreate) {
55+
pool_params = poolParamsCreate();
56+
}
57+
58+
// NOTE: we set the UMF_POOL_CREATE_FLAG_OWN_PROVIDER flag here so the pool
59+
// will destroy the provider when it is destroyed
4060
ret = umfPoolCreate(pool_ops, provider, pool_params,
4161
UMF_POOL_CREATE_FLAG_OWN_PROVIDER, &hPool);
4262
EXPECT_EQ(ret, UMF_RESULT_SUCCESS);
4363
EXPECT_NE(hPool, nullptr);
4464

65+
// we do not need params anymore
66+
if (poolParamsDestroy) {
67+
poolParamsDestroy(pool_params);
68+
}
69+
70+
if (providerParamsDestroy) {
71+
providerParamsDestroy(provider_params);
72+
}
73+
4574
return umf::pool_unique_handle_t(hPool, &umfPoolDestroy);
4675
}
4776

test/pools/jemalloc_coarse_devdax.cpp

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2024 Intel Corporation
1+
// Copyright (C) 2024-2025 Intel Corporation
22
// Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
33
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
44

@@ -7,18 +7,20 @@
77

88
#include "pool_coarse.hpp"
99

10-
using devdax_params_unique_handle_t =
11-
std::unique_ptr<umf_devdax_memory_provider_params_t,
12-
decltype(&umfDevDaxMemoryProviderParamsDestroy)>;
13-
14-
devdax_params_unique_handle_t create_devdax_params() {
10+
bool devDaxEnvSet() {
1511
char *path = getenv("UMF_TESTS_DEVDAX_PATH");
1612
char *size = getenv("UMF_TESTS_DEVDAX_SIZE");
1713
if (path == nullptr || path[0] == 0 || size == nullptr || size[0] == 0) {
18-
return devdax_params_unique_handle_t(
19-
nullptr, &umfDevDaxMemoryProviderParamsDestroy);
14+
return false;
2015
}
2116

17+
return true;
18+
}
19+
20+
void *createDevDaxParams() {
21+
char *path = getenv("UMF_TESTS_DEVDAX_PATH");
22+
char *size = getenv("UMF_TESTS_DEVDAX_SIZE");
23+
2224
umf_devdax_memory_provider_params_handle_t params = NULL;
2325
umf_result_t res =
2426
umfDevDaxMemoryProviderParamsCreate(&params, path, atol(size));
@@ -27,17 +29,16 @@ devdax_params_unique_handle_t create_devdax_params() {
2729
"Failed to create DevDax Memory Provider params");
2830
}
2931

30-
return devdax_params_unique_handle_t(params,
31-
&umfDevDaxMemoryProviderParamsDestroy);
32+
return params;
3233
}
3334

34-
auto devdaxParams = create_devdax_params();
35-
3635
static std::vector<poolCreateExtParams> poolParamsList =
37-
devdaxParams.get() ? std::vector<poolCreateExtParams>{poolCreateExtParams{
38-
umfJemallocPoolOps(), nullptr,
39-
umfDevDaxMemoryProviderOps(), devdaxParams.get()}}
40-
: std::vector<poolCreateExtParams>{};
36+
devDaxEnvSet()
37+
? std::vector<poolCreateExtParams>{poolCreateExtParams{
38+
umfJemallocPoolOps(), nullptr, nullptr,
39+
umfDevDaxMemoryProviderOps(), createDevDaxParams,
40+
(pfnProviderParamsDestroy)umfDevDaxMemoryProviderParamsDestroy}}
41+
: std::vector<poolCreateExtParams>{};
4142

4243
INSTANTIATE_TEST_SUITE_P(jemallocCoarseDevDaxTest, umfPoolTest,
4344
::testing::ValuesIn(poolParamsList));

test/pools/jemalloc_coarse_file.cpp

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2024 Intel Corporation
1+
// Copyright (C) 2024-2025 Intel Corporation
22
// Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
33
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
44

@@ -7,25 +7,21 @@
77

88
#include "pool_coarse.hpp"
99

10-
using file_params_unique_handle_t =
11-
std::unique_ptr<umf_file_memory_provider_params_t,
12-
decltype(&umfFileMemoryProviderParamsDestroy)>;
13-
14-
file_params_unique_handle_t get_file_params_default(char *path) {
10+
void *getFileParamsDefault() {
1511
umf_file_memory_provider_params_handle_t file_params = NULL;
16-
umf_result_t res = umfFileMemoryProviderParamsCreate(&file_params, path);
12+
umf_result_t res =
13+
umfFileMemoryProviderParamsCreate(&file_params, FILE_PATH);
1714
if (res != UMF_RESULT_SUCCESS) {
1815
throw std::runtime_error(
1916
"Failed to create File Memory Provider params");
2017
}
2118

22-
return file_params_unique_handle_t(file_params,
23-
&umfFileMemoryProviderParamsDestroy);
19+
return file_params;
2420
}
2521

26-
file_params_unique_handle_t fileParams = get_file_params_default(FILE_PATH);
27-
28-
INSTANTIATE_TEST_SUITE_P(jemallocCoarseFileTest, umfPoolTest,
29-
::testing::Values(poolCreateExtParams{
30-
umfJemallocPoolOps(), nullptr,
31-
umfFileMemoryProviderOps(), fileParams.get()}));
22+
INSTANTIATE_TEST_SUITE_P(
23+
jemallocCoarseFileTest, umfPoolTest,
24+
::testing::Values(poolCreateExtParams{
25+
umfJemallocPoolOps(), nullptr, nullptr, umfFileMemoryProviderOps(),
26+
getFileParamsDefault,
27+
(pfnProviderParamsDestroy)umfFileMemoryProviderParamsDestroy}));

test/pools/jemalloc_pool.cpp

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,22 @@ using os_params_unique_handle_t =
1515
std::unique_ptr<umf_os_memory_provider_params_t,
1616
decltype(&umfOsMemoryProviderParamsDestroy)>;
1717

18-
os_params_unique_handle_t createOsMemoryProviderParams() {
18+
void *createOsMemoryProviderParams() {
1919
umf_os_memory_provider_params_handle_t params = nullptr;
2020
umf_result_t res = umfOsMemoryProviderParamsCreate(&params);
2121
if (res != UMF_RESULT_SUCCESS) {
2222
throw std::runtime_error("Failed to create os memory provider params");
2323
}
2424

25-
return os_params_unique_handle_t(params, &umfOsMemoryProviderParamsDestroy);
25+
return params;
2626
}
27-
auto defaultParams = createOsMemoryProviderParams();
2827

29-
INSTANTIATE_TEST_SUITE_P(jemallocPoolTest, umfPoolTest,
30-
::testing::Values(poolCreateExtParams{
31-
umfJemallocPoolOps(), nullptr,
32-
umfOsMemoryProviderOps(), defaultParams.get()}));
28+
INSTANTIATE_TEST_SUITE_P(
29+
jemallocPoolTest, umfPoolTest,
30+
::testing::Values(poolCreateExtParams{
31+
umfJemallocPoolOps(), nullptr, nullptr, umfOsMemoryProviderOps(),
32+
createOsMemoryProviderParams,
33+
(pfnProviderParamsDestroy)umfOsMemoryProviderParamsDestroy}));
3334

3435
// this test makes sure that jemalloc does not use
3536
// memory provider to allocate metadata (and hence
@@ -41,17 +42,41 @@ TEST_F(test, metadataNotAllocatedUsingProvider) {
4142

4243
// set coarse grain allocations to PROT_NONE so that we can be sure
4344
// jemalloc does not touch any of the allocated memory
44-
umf_os_memory_provider_params_handle_t params = nullptr;
45-
umf_result_t res = umfOsMemoryProviderParamsCreate(&params);
46-
ASSERT_EQ(res, UMF_RESULT_SUCCESS);
47-
res = umfOsMemoryProviderParamsSetProtection(params, UMF_PROTECTION_NONE);
48-
ASSERT_EQ(res, UMF_RESULT_SUCCESS);
4945

50-
auto pool = poolCreateExtUnique(
51-
{umfJemallocPoolOps(), nullptr, umfOsMemoryProviderOps(), params});
46+
auto providerParamsCreate = []() {
47+
umf_os_memory_provider_params_handle_t params = nullptr;
48+
umf_result_t res = umfOsMemoryProviderParamsCreate(&params);
49+
if (res != UMF_RESULT_SUCCESS) {
50+
throw std::runtime_error(
51+
"Failed to create OS Memory Provider params");
52+
}
53+
res =
54+
umfOsMemoryProviderParamsSetProtection(params, UMF_PROTECTION_NONE);
55+
if (res != UMF_RESULT_SUCCESS) {
56+
throw std::runtime_error(
57+
"Failed to set OS Memory Provider params protection");
58+
}
59+
return (void *)params;
60+
};
61+
62+
auto providerParamsDestroy = [](void *params) {
63+
umf_result_t res = umfOsMemoryProviderParamsDestroy(
64+
(umf_os_memory_provider_params_handle_t)params);
65+
if (res != UMF_RESULT_SUCCESS) {
66+
throw std::runtime_error(
67+
"Failed to destroy OS Memory Provider params");
68+
}
69+
return res;
70+
};
5271

53-
res = umfOsMemoryProviderParamsDestroy(params);
54-
ASSERT_EQ(res, UMF_RESULT_SUCCESS);
72+
auto pool = poolCreateExtUnique({
73+
umfJemallocPoolOps(),
74+
nullptr,
75+
nullptr,
76+
umfOsMemoryProviderOps(),
77+
(pfnProviderParamsCreate)providerParamsCreate,
78+
(pfnProviderParamsDestroy)providerParamsDestroy,
79+
});
5580

5681
std::vector<std::shared_ptr<void>> allocs;
5782
for (size_t i = 0; i < numAllocs; i++) {

test/pools/pool_base_alloc.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,5 @@ umf_memory_pool_ops_t BA_POOL_OPS = umf::poolMakeCOps<base_alloc_pool, void>();
4747

4848
INSTANTIATE_TEST_SUITE_P(baPool, umfPoolTest,
4949
::testing::Values(poolCreateExtParams{
50-
&BA_POOL_OPS, nullptr,
51-
&umf_test::BASE_PROVIDER_OPS, nullptr}));
50+
&BA_POOL_OPS, nullptr, nullptr,
51+
&umf_test::BASE_PROVIDER_OPS, nullptr, nullptr}));

test/pools/scalable_coarse_devdax.cpp

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2024 Intel Corporation
1+
// Copyright (C) 2024-2025 Intel Corporation
22
// Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
33
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
44

@@ -7,18 +7,20 @@
77

88
#include "pool_coarse.hpp"
99

10-
using devdax_params_unique_handle_t =
11-
std::unique_ptr<umf_devdax_memory_provider_params_t,
12-
decltype(&umfDevDaxMemoryProviderParamsDestroy)>;
13-
14-
devdax_params_unique_handle_t create_devdax_params() {
10+
bool devDaxEnvSet() {
1511
char *path = getenv("UMF_TESTS_DEVDAX_PATH");
1612
char *size = getenv("UMF_TESTS_DEVDAX_SIZE");
1713
if (path == nullptr || path[0] == 0 || size == nullptr || size[0] == 0) {
18-
return devdax_params_unique_handle_t(
19-
nullptr, &umfDevDaxMemoryProviderParamsDestroy);
14+
return false;
2015
}
2116

17+
return true;
18+
}
19+
20+
void *createDevDaxParams() {
21+
char *path = getenv("UMF_TESTS_DEVDAX_PATH");
22+
char *size = getenv("UMF_TESTS_DEVDAX_SIZE");
23+
2224
umf_devdax_memory_provider_params_handle_t params = NULL;
2325
umf_result_t res =
2426
umfDevDaxMemoryProviderParamsCreate(&params, path, atol(size));
@@ -27,17 +29,16 @@ devdax_params_unique_handle_t create_devdax_params() {
2729
"Failed to create DevDax Memory Provider params");
2830
}
2931

30-
return devdax_params_unique_handle_t(params,
31-
&umfDevDaxMemoryProviderParamsDestroy);
32+
return params;
3233
}
3334

34-
auto devdaxParams = create_devdax_params();
35-
3635
static std::vector<poolCreateExtParams> poolParamsList =
37-
devdaxParams.get() ? std::vector<poolCreateExtParams>{poolCreateExtParams{
38-
umfScalablePoolOps(), nullptr,
39-
umfDevDaxMemoryProviderOps(), devdaxParams.get()}}
40-
: std::vector<poolCreateExtParams>{};
36+
devDaxEnvSet()
37+
? std::vector<poolCreateExtParams>{poolCreateExtParams{
38+
umfScalablePoolOps(), nullptr, nullptr,
39+
umfDevDaxMemoryProviderOps(), createDevDaxParams,
40+
(pfnProviderParamsDestroy)umfDevDaxMemoryProviderParamsDestroy}}
41+
: std::vector<poolCreateExtParams>{};
4142

4243
INSTANTIATE_TEST_SUITE_P(scalableCoarseDevDaxTest, umfPoolTest,
4344
::testing::ValuesIn(poolParamsList));

test/pools/scalable_coarse_file.cpp

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (C) 2024 Intel Corporation
1+
// Copyright (C) 2024-2025 Intel Corporation
22
// Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
33
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
44

@@ -7,25 +7,21 @@
77

88
#include "pool_coarse.hpp"
99

10-
using file_params_unique_handle_t =
11-
std::unique_ptr<umf_file_memory_provider_params_t,
12-
decltype(&umfFileMemoryProviderParamsDestroy)>;
13-
14-
file_params_unique_handle_t get_file_params_default(char *path) {
10+
void *getFileParamsDefault() {
1511
umf_file_memory_provider_params_handle_t file_params = NULL;
16-
umf_result_t res = umfFileMemoryProviderParamsCreate(&file_params, path);
12+
umf_result_t res =
13+
umfFileMemoryProviderParamsCreate(&file_params, FILE_PATH);
1714
if (res != UMF_RESULT_SUCCESS) {
1815
throw std::runtime_error(
1916
"Failed to create File Memory Provider params");
2017
}
2118

22-
return file_params_unique_handle_t(file_params,
23-
&umfFileMemoryProviderParamsDestroy);
19+
return file_params;
2420
}
2521

26-
file_params_unique_handle_t fileParams = get_file_params_default(FILE_PATH);
27-
28-
INSTANTIATE_TEST_SUITE_P(scalableCoarseFileTest, umfPoolTest,
29-
::testing::Values(poolCreateExtParams{
30-
umfScalablePoolOps(), nullptr,
31-
umfFileMemoryProviderOps(), fileParams.get()}));
22+
INSTANTIATE_TEST_SUITE_P(
23+
scalableCoarseFileTest, umfPoolTest,
24+
::testing::Values(poolCreateExtParams{
25+
umfScalablePoolOps(), nullptr, nullptr, umfFileMemoryProviderOps(),
26+
getFileParamsDefault,
27+
(pfnProviderParamsDestroy)umfFileMemoryProviderParamsDestroy}));

0 commit comments

Comments
 (0)