Skip to content

Commit f7c1b04

Browse files
committed
Add null/trace pool and provider implementations
Signed-off-by: Lukasz Dorau <[email protected]>
1 parent e6f6ea7 commit f7c1b04

File tree

10 files changed

+484
-0
lines changed

10 files changed

+484
-0
lines changed

cmake/helpers.cmake

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ endfunction()
8181

8282
function(add_umf_library name)
8383
add_library(${name} ${ARGN})
84+
target_include_directories(${name} PRIVATE ${CMAKE_SOURCE_DIR}/include)
8485
add_umf_target_compile_options(${name})
8586
add_umf_target_link_options(${name})
8687
endfunction()

include/pool/pool_null.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright (C) 2023 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+
#ifndef UMF_NULL_POOL_H
6+
#define UMF_NULL_POOL_H
7+
8+
#include <umf/memory_pool.h>
9+
10+
#if defined(__cplusplus)
11+
extern "C" {
12+
#endif
13+
14+
extern struct umf_memory_pool_ops_t UMF_NULL_POOL_OPS;
15+
16+
#if defined(__cplusplus)
17+
}
18+
#endif
19+
20+
#endif // UMF_NULL_POOL_H

include/pool/pool_trace.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright (C) 2023 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+
#ifndef UMF_TRACE_POOL_H
6+
#define UMF_TRACE_POOL_H
7+
8+
#include <umf/memory_pool.h>
9+
10+
#if defined(__cplusplus)
11+
extern "C" {
12+
#endif
13+
14+
struct umf_pool_trace_params {
15+
umf_memory_pool_handle_t hUpstreamPool;
16+
void (*trace)(const char *);
17+
};
18+
19+
extern struct umf_memory_pool_ops_t UMF_TRACE_POOL_OPS;
20+
21+
#if defined(__cplusplus)
22+
}
23+
#endif
24+
25+
#endif // UMF_TRACE_POOL_H

include/provider/provider_null.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright (C) 2023 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+
#ifndef UMF_NULL_PROVIDER_H
6+
#define UMF_NULL_PROVIDER_H
7+
8+
#include <umf/memory_provider.h>
9+
10+
#if defined(__cplusplus)
11+
extern "C" {
12+
#endif
13+
14+
extern struct umf_memory_provider_ops_t UMF_NULL_PROVIDER_OPS;
15+
16+
#if defined(__cplusplus)
17+
}
18+
#endif
19+
20+
#endif // UMF_NULL_PROVIDER_H

include/provider/provider_trace.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright (C) 2023 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+
#ifndef UMF_TRACE_PROVIDER_H
6+
#define UMF_TRACE_PROVIDER_H
7+
8+
#include <umf/memory_provider.h>
9+
10+
#if defined(__cplusplus)
11+
extern "C" {
12+
#endif
13+
14+
struct umf_provider_trace_params {
15+
umf_memory_provider_handle_t hUpstreamProvider;
16+
void (*trace)(const char *);
17+
};
18+
19+
extern struct umf_memory_provider_ops_t UMF_TRACE_PROVIDER_OPS;
20+
21+
#if defined(__cplusplus)
22+
}
23+
#endif
24+
25+
#endif // UMF_TRACE_PROVIDER_H

src/CMakeLists.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ set(UMF_SOURCES
66
memory_pool.c
77
memory_provider.c
88
memory_provider_get_last_failed.c
9+
pool/pool_null.c
10+
pool/pool_trace.c
11+
provider/provider_null.c
12+
provider/provider_trace.c
913
)
1014

1115
if(MSVC)

src/pool/pool_null.c

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// Copyright (C) 2023 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+
6+
#include <assert.h>
7+
#include <stdlib.h>
8+
9+
#include <umf/memory_pool_ops.h>
10+
#include <pool/pool_null.h>
11+
12+
static enum umf_result_t nullInitialize(umf_memory_provider_handle_t *providers,
13+
size_t numProviders, void *params,
14+
void **pool) {
15+
(void)providers;
16+
(void)numProviders;
17+
(void)params;
18+
assert(providers && numProviders);
19+
*pool = NULL;
20+
return UMF_RESULT_SUCCESS;
21+
}
22+
23+
static void nullFinalize(void *pool) { (void)pool; }
24+
25+
static void *nullMalloc(void *pool, size_t size) {
26+
(void)pool;
27+
(void)size;
28+
return NULL;
29+
}
30+
31+
static void *nullCalloc(void *pool, size_t num, size_t size) {
32+
(void)pool;
33+
(void)num;
34+
(void)size;
35+
return NULL;
36+
}
37+
38+
static void *nullRealloc(void *pool, void *ptr, size_t size) {
39+
(void)pool;
40+
(void)ptr;
41+
(void)size;
42+
return NULL;
43+
}
44+
45+
static void *nullAlignedMalloc(void *pool, size_t size, size_t alignment) {
46+
(void)pool;
47+
(void)size;
48+
(void)alignment;
49+
return NULL;
50+
}
51+
52+
static size_t nullMallocUsableSize(void *pool, void *ptr) {
53+
(void)ptr;
54+
(void)pool;
55+
return 0;
56+
}
57+
58+
static enum umf_result_t nullFree(void *pool, void *ptr) {
59+
(void)pool;
60+
(void)ptr;
61+
return UMF_RESULT_SUCCESS;
62+
}
63+
64+
static enum umf_result_t nullGetLastStatus(void *pool) {
65+
(void)pool;
66+
return UMF_RESULT_SUCCESS;
67+
}
68+
69+
struct umf_memory_pool_ops_t UMF_NULL_POOL_OPS = {
70+
.version = UMF_VERSION_CURRENT,
71+
.initialize = nullInitialize,
72+
.finalize = nullFinalize,
73+
.malloc = nullMalloc,
74+
.realloc = nullRealloc,
75+
.calloc = nullCalloc,
76+
.aligned_malloc = nullAlignedMalloc,
77+
.malloc_usable_size = nullMallocUsableSize,
78+
.free = nullFree,
79+
.get_last_allocation_error = nullGetLastStatus,
80+
};

src/pool/pool_trace.c

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
// Copyright (C) 2023 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 <assert.h>
6+
#include <stdlib.h>
7+
8+
#include <umf/memory_pool_ops.h>
9+
#include <pool/pool_trace.h>
10+
11+
struct umf_pool_trace_params_priv {
12+
umf_memory_pool_handle_t hUpstreamPool;
13+
void (*trace)(const char *);
14+
};
15+
16+
struct trace_pool {
17+
struct umf_pool_trace_params_priv params;
18+
};
19+
20+
static enum umf_result_t
21+
traceInitialize(umf_memory_provider_handle_t *providers, size_t numProviders,
22+
void *params, void **pool)
23+
{
24+
struct trace_pool *trace_pool =
25+
(struct trace_pool *)malloc(sizeof(struct trace_pool));
26+
if (NULL == trace_pool)
27+
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
28+
29+
struct umf_pool_trace_params *pub_params = params;
30+
trace_pool->params.hUpstreamPool = pub_params->hUpstreamPool;
31+
trace_pool->params.trace = pub_params->trace;
32+
33+
(void)providers;
34+
(void)numProviders;
35+
assert(providers && numProviders);
36+
37+
*pool = trace_pool;
38+
return UMF_RESULT_SUCCESS;
39+
}
40+
41+
static void traceFinalize(void *pool) { free(pool); }
42+
43+
static void *traceMalloc(void *pool, size_t size) {
44+
struct trace_pool *trace_pool = (struct trace_pool *)pool;
45+
46+
trace_pool->params.trace("malloc");
47+
return umfPoolMalloc(trace_pool->params.hUpstreamPool, size);
48+
}
49+
50+
static void *traceCalloc(void *pool, size_t num, size_t size) {
51+
struct trace_pool *trace_pool = (struct trace_pool *)pool;
52+
53+
trace_pool->params.trace("calloc");
54+
return umfPoolCalloc(trace_pool->params.hUpstreamPool, num, size);
55+
}
56+
57+
static void *traceRealloc(void *pool, void *ptr, size_t size) {
58+
struct trace_pool *trace_pool = (struct trace_pool *)pool;
59+
60+
trace_pool->params.trace("realloc");
61+
return umfPoolRealloc(trace_pool->params.hUpstreamPool, ptr, size);
62+
}
63+
64+
static void *traceAlignedMalloc(void *pool, size_t size, size_t alignment) {
65+
struct trace_pool *trace_pool = (struct trace_pool *)pool;
66+
67+
trace_pool->params.trace("aligned_malloc");
68+
return umfPoolAlignedMalloc(trace_pool->params.hUpstreamPool, size,
69+
alignment);
70+
}
71+
72+
static size_t traceMallocUsableSize(void *pool, void *ptr) {
73+
struct trace_pool *trace_pool = (struct trace_pool *)pool;
74+
75+
trace_pool->params.trace("malloc_usable_size");
76+
return umfPoolMallocUsableSize(trace_pool->params.hUpstreamPool, ptr);
77+
}
78+
79+
static enum umf_result_t traceFree(void *pool, void *ptr) {
80+
struct trace_pool *trace_pool = (struct trace_pool *)pool;
81+
82+
trace_pool->params.trace("free");
83+
return umfPoolFree(trace_pool->params.hUpstreamPool, ptr);
84+
}
85+
86+
static enum umf_result_t traceGetLastStatus(void *pool) {
87+
struct trace_pool *trace_pool = (struct trace_pool *)pool;
88+
89+
trace_pool->params.trace("get_last_native_error");
90+
return umfPoolGetLastAllocationError(trace_pool->params.hUpstreamPool);
91+
}
92+
93+
struct umf_memory_pool_ops_t UMF_TRACE_POOL_OPS = {
94+
.version = UMF_VERSION_CURRENT,
95+
.initialize = traceInitialize,
96+
.finalize = traceFinalize,
97+
.malloc = traceMalloc,
98+
.realloc = traceRealloc,
99+
.calloc = traceCalloc,
100+
.aligned_malloc = traceAlignedMalloc,
101+
.malloc_usable_size = traceMallocUsableSize,
102+
.free = traceFree,
103+
.get_last_allocation_error = traceGetLastStatus,
104+
};

src/provider/provider_null.c

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// Copyright (C) 2023 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 <assert.h>
6+
#include <stdlib.h>
7+
8+
#include <umf/memory_provider_ops.h>
9+
#include <provider/provider_null.h>
10+
11+
static enum umf_result_t nullInitialize(void *params, void **pool) {
12+
(void)params;
13+
*pool = NULL;
14+
return UMF_RESULT_SUCCESS;
15+
}
16+
17+
static void nullFinalize(void *pool) { (void)pool; }
18+
19+
static enum umf_result_t nullAlloc(void *provider, size_t size,
20+
size_t alignment, void **ptr) {
21+
(void)provider;
22+
(void)size;
23+
(void)alignment;
24+
*ptr = NULL;
25+
return UMF_RESULT_SUCCESS;
26+
}
27+
28+
static enum umf_result_t nullFree(void *provider, void *ptr, size_t size) {
29+
(void)provider;
30+
(void)ptr;
31+
(void)size;
32+
return UMF_RESULT_SUCCESS;
33+
}
34+
35+
static void nullGetLastError(void *provider, const char **ppMsg,
36+
int32_t *pError) {
37+
(void)provider;
38+
(void)ppMsg;
39+
(void)pError;
40+
}
41+
42+
static enum umf_result_t nullGetRecommendedPageSize(void *provider, size_t size,
43+
size_t *pageSize) {
44+
(void)provider;
45+
(void)size;
46+
(void)pageSize;
47+
return UMF_RESULT_SUCCESS;
48+
}
49+
50+
static enum umf_result_t nullGetPageSize(void *provider, void *ptr,
51+
52+
size_t *pageSize) {
53+
(void)provider;
54+
(void)ptr;
55+
(void)pageSize;
56+
return UMF_RESULT_SUCCESS;
57+
}
58+
59+
static enum umf_result_t nullPurgeLazy(void *provider, void *ptr, size_t size) {
60+
(void)provider;
61+
(void)ptr;
62+
(void)size;
63+
return UMF_RESULT_SUCCESS;
64+
}
65+
66+
static enum umf_result_t nullPurgeForce(void *provider, void *ptr,
67+
size_t size) {
68+
(void)provider;
69+
(void)ptr;
70+
(void)size;
71+
return UMF_RESULT_SUCCESS;
72+
}
73+
74+
static const char *nullName(void *provider) {
75+
(void)provider;
76+
return "null";
77+
}
78+
79+
struct umf_memory_provider_ops_t UMF_NULL_PROVIDER_OPS = {
80+
.version = UMF_VERSION_CURRENT,
81+
.initialize = nullInitialize,
82+
.finalize = nullFinalize,
83+
.alloc = nullAlloc,
84+
.free = nullFree,
85+
.get_last_native_error = nullGetLastError,
86+
.get_recommended_page_size = nullGetRecommendedPageSize,
87+
.get_min_page_size = nullGetPageSize,
88+
.purge_lazy = nullPurgeLazy,
89+
.purge_force = nullPurgeForce,
90+
.get_name = nullName,
91+
};

0 commit comments

Comments
 (0)