|
| 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, ¶ms, 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(), ¶ms, &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