Skip to content

Commit 57751ad

Browse files
Merge pull request #223 from igchor/multi_pool_test
Add tests with multiple pools
2 parents 310dbe1 + bdf2eca commit 57751ad

File tree

2 files changed

+91
-0
lines changed

2 files changed

+91
-0
lines changed

test/CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,15 @@ if(UMF_BUILD_LIBUMF_POOL_DISJOINT)
7777
LIBS disjoint_pool)
7878
endif()
7979

80+
if(UMF_BUILD_LIBUMF_POOL_DISJOINT
81+
AND UMF_BUILD_LIBUMF_POOL_JEMALLOC
82+
AND UMF_BUILD_OS_MEMORY_PROVIDER
83+
AND UMF_ENABLE_POOL_TRACKING)
84+
add_umf_test(NAME c_api_multi_pool
85+
SRCS c_api/multi_pool.c
86+
LIBS disjoint_pool jemalloc_pool)
87+
endif()
88+
8089
if(UMF_BUILD_OS_MEMORY_PROVIDER)
8190
if(UMF_BUILD_LIBUMF_POOL_JEMALLOC)
8291
# TODO: we link directly with libc to force using malloc from glibc

test/c_api/multi_pool.c

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// Copyright (C) 2023-2024 Intel Corporation
2+
// Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
3+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4+
5+
#include <stdlib.h>
6+
7+
#include <umf/memory_pool.h>
8+
#include <umf/pools/pool_disjoint.h>
9+
#include <umf/pools/pool_jemalloc.h>
10+
#include <umf/pools/pool_proxy.h>
11+
#include <umf/providers/provider_os_memory.h>
12+
13+
#include "test_helpers.h"
14+
15+
umf_memory_pool_handle_t
16+
createDisjointPool(umf_memory_provider_handle_t provider) {
17+
umf_memory_pool_handle_t pool = NULL;
18+
umf_disjoint_pool_params_t params = umfDisjointPoolParamsDefault();
19+
umf_result_t ret =
20+
umfPoolCreate(umfDisjointPoolOps(), provider, &params, 0, &pool);
21+
UT_ASSERTeq(ret, UMF_RESULT_SUCCESS);
22+
return pool;
23+
}
24+
25+
umf_memory_pool_handle_t
26+
createProxyPool(umf_memory_provider_handle_t provider) {
27+
umf_memory_pool_handle_t pool = NULL;
28+
umf_result_t ret =
29+
umfPoolCreate(umfProxyPoolOps(), provider, NULL, 0, &pool);
30+
UT_ASSERTeq(ret, UMF_RESULT_SUCCESS);
31+
return pool;
32+
}
33+
34+
umf_memory_pool_handle_t
35+
createJemallocPool(umf_memory_provider_handle_t provider) {
36+
umf_memory_pool_handle_t pool = NULL;
37+
umf_result_t ret =
38+
umfPoolCreate(umfJemallocPoolOps(), provider, NULL, 0, &pool);
39+
UT_ASSERTeq(ret, UMF_RESULT_SUCCESS);
40+
return pool;
41+
}
42+
43+
#define ALLOC_SIZE 64
44+
45+
int main(void) {
46+
umf_os_memory_provider_params_t params = umfOsMemoryProviderParamsDefault();
47+
48+
umf_memory_provider_handle_t hProvider;
49+
umf_result_t ret =
50+
umfMemoryProviderCreate(umfOsMemoryProviderOps(), &params, &hProvider);
51+
UT_ASSERTeq(ret, UMF_RESULT_SUCCESS);
52+
53+
umf_memory_pool_handle_t pools[3];
54+
55+
pools[0] = createDisjointPool(hProvider);
56+
pools[1] = createProxyPool(hProvider);
57+
pools[2] = createJemallocPool(hProvider);
58+
59+
void *ptrs[3];
60+
61+
for (int i = 0; i < 3; i++) {
62+
UT_ASSERTne(pools[i], NULL);
63+
ptrs[i] = umfPoolMalloc(pools[i], ALLOC_SIZE);
64+
UT_ASSERTne(ptrs[i], NULL);
65+
}
66+
67+
for (int i = 0; i < 3; i++) {
68+
UT_ASSERTeq(umfPoolByPtr(ptrs[i]), pools[i]);
69+
}
70+
71+
for (int i = 0; i < 3; i++) {
72+
umfFree(ptrs[i]);
73+
}
74+
75+
for (int i = 0; i < 3; i++) {
76+
umfPoolDestroy(pools[i]);
77+
}
78+
79+
umfMemoryProviderDestroy(hProvider);
80+
81+
return 0;
82+
}

0 commit comments

Comments
 (0)