Skip to content

Add null/trace pool and provider implementations #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmake/helpers.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ endfunction()

function(add_umf_library name)
add_library(${name} ${ARGN})
target_include_directories(${name} PRIVATE ${CMAKE_SOURCE_DIR}/include)
add_umf_target_compile_options(${name})
add_umf_target_link_options(${name})
endfunction()
20 changes: 20 additions & 0 deletions include/pool/pool_null.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (C) 2023 Intel Corporation
// Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#ifndef UMF_NULL_POOL_H
#define UMF_NULL_POOL_H

#include <umf/memory_pool.h>

#if defined(__cplusplus)
extern "C" {
#endif

extern struct umf_memory_pool_ops_t UMF_NULL_POOL_OPS;

#if defined(__cplusplus)
}
#endif

#endif // UMF_NULL_POOL_H
25 changes: 25 additions & 0 deletions include/pool/pool_trace.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (C) 2023 Intel Corporation
// Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#ifndef UMF_TRACE_POOL_H
#define UMF_TRACE_POOL_H

#include <umf/memory_pool.h>

#if defined(__cplusplus)
extern "C" {
#endif

struct umf_pool_trace_params {
umf_memory_pool_handle_t hUpstreamPool;
void (*trace)(const char *);
};

extern struct umf_memory_pool_ops_t UMF_TRACE_POOL_OPS;

#if defined(__cplusplus)
}
#endif

#endif // UMF_TRACE_POOL_H
20 changes: 20 additions & 0 deletions include/provider/provider_null.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright (C) 2023 Intel Corporation
// Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#ifndef UMF_NULL_PROVIDER_H
#define UMF_NULL_PROVIDER_H

#include <umf/memory_provider.h>

#if defined(__cplusplus)
extern "C" {
#endif

extern struct umf_memory_provider_ops_t UMF_NULL_PROVIDER_OPS;

#if defined(__cplusplus)
}
#endif

#endif // UMF_NULL_PROVIDER_H
25 changes: 25 additions & 0 deletions include/provider/provider_trace.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (C) 2023 Intel Corporation
// Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#ifndef UMF_TRACE_PROVIDER_H
#define UMF_TRACE_PROVIDER_H

#include <umf/memory_provider.h>

#if defined(__cplusplus)
extern "C" {
#endif

struct umf_provider_trace_params {
umf_memory_provider_handle_t hUpstreamProvider;
void (*trace)(const char *);
};

extern struct umf_memory_provider_ops_t UMF_TRACE_PROVIDER_OPS;

#if defined(__cplusplus)
}
#endif

#endif // UMF_TRACE_PROVIDER_H
4 changes: 4 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ set(UMF_SOURCES
memory_pool.c
memory_provider.c
memory_provider_get_last_failed.c
pool/pool_null.c
pool/pool_trace.c
provider/provider_null.c
provider/provider_trace.c
)

if(MSVC)
Expand Down
80 changes: 80 additions & 0 deletions src/pool/pool_null.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright (C) 2023 Intel Corporation
// Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception


#include <assert.h>
#include <stdlib.h>

#include <umf/memory_pool_ops.h>
#include <pool/pool_null.h>

static enum umf_result_t nullInitialize(umf_memory_provider_handle_t *providers,
size_t numProviders, void *params,
void **pool) {
(void)providers;
(void)numProviders;
(void)params;
assert(providers && numProviders);
*pool = NULL;
return UMF_RESULT_SUCCESS;
}

static void nullFinalize(void *pool) { (void)pool; }

static void *nullMalloc(void *pool, size_t size) {
(void)pool;
(void)size;
return NULL;
}

static void *nullCalloc(void *pool, size_t num, size_t size) {
(void)pool;
(void)num;
(void)size;
return NULL;
}

static void *nullRealloc(void *pool, void *ptr, size_t size) {
(void)pool;
(void)ptr;
(void)size;
return NULL;
}

static void *nullAlignedMalloc(void *pool, size_t size, size_t alignment) {
(void)pool;
(void)size;
(void)alignment;
return NULL;
}

static size_t nullMallocUsableSize(void *pool, void *ptr) {
(void)ptr;
(void)pool;
return 0;
}

static enum umf_result_t nullFree(void *pool, void *ptr) {
(void)pool;
(void)ptr;
return UMF_RESULT_SUCCESS;
}

static enum umf_result_t nullGetLastStatus(void *pool) {
(void)pool;
return UMF_RESULT_SUCCESS;
}

struct umf_memory_pool_ops_t UMF_NULL_POOL_OPS = {
.version = UMF_VERSION_CURRENT,
.initialize = nullInitialize,
.finalize = nullFinalize,
.malloc = nullMalloc,
.realloc = nullRealloc,
.calloc = nullCalloc,
.aligned_malloc = nullAlignedMalloc,
.malloc_usable_size = nullMallocUsableSize,
.free = nullFree,
.get_last_allocation_error = nullGetLastStatus,
};
104 changes: 104 additions & 0 deletions src/pool/pool_trace.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
// Copyright (C) 2023 Intel Corporation
// Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#include <assert.h>
#include <stdlib.h>

#include <umf/memory_pool_ops.h>
#include <pool/pool_trace.h>

struct umf_pool_trace_params_priv {
umf_memory_pool_handle_t hUpstreamPool;
void (*trace)(const char *);
};

struct trace_pool {
struct umf_pool_trace_params_priv params;
};

static enum umf_result_t
traceInitialize(umf_memory_provider_handle_t *providers, size_t numProviders,
void *params, void **pool)
{
struct trace_pool *trace_pool =
(struct trace_pool *)malloc(sizeof(struct trace_pool));
if (NULL == trace_pool)
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;

struct umf_pool_trace_params *pub_params = params;
trace_pool->params.hUpstreamPool = pub_params->hUpstreamPool;
trace_pool->params.trace = pub_params->trace;

(void)providers;
(void)numProviders;
assert(providers && numProviders);

*pool = trace_pool;
return UMF_RESULT_SUCCESS;
}

static void traceFinalize(void *pool) { free(pool); }

static void *traceMalloc(void *pool, size_t size) {
struct trace_pool *trace_pool = (struct trace_pool *)pool;

trace_pool->params.trace("malloc");
return umfPoolMalloc(trace_pool->params.hUpstreamPool, size);
}

static void *traceCalloc(void *pool, size_t num, size_t size) {
struct trace_pool *trace_pool = (struct trace_pool *)pool;

trace_pool->params.trace("calloc");
return umfPoolCalloc(trace_pool->params.hUpstreamPool, num, size);
}

static void *traceRealloc(void *pool, void *ptr, size_t size) {
struct trace_pool *trace_pool = (struct trace_pool *)pool;

trace_pool->params.trace("realloc");
return umfPoolRealloc(trace_pool->params.hUpstreamPool, ptr, size);
}

static void *traceAlignedMalloc(void *pool, size_t size, size_t alignment) {
struct trace_pool *trace_pool = (struct trace_pool *)pool;

trace_pool->params.trace("aligned_malloc");
return umfPoolAlignedMalloc(trace_pool->params.hUpstreamPool, size,
alignment);
}

static size_t traceMallocUsableSize(void *pool, void *ptr) {
struct trace_pool *trace_pool = (struct trace_pool *)pool;

trace_pool->params.trace("malloc_usable_size");
return umfPoolMallocUsableSize(trace_pool->params.hUpstreamPool, ptr);
}

static enum umf_result_t traceFree(void *pool, void *ptr) {
struct trace_pool *trace_pool = (struct trace_pool *)pool;

trace_pool->params.trace("free");
return umfPoolFree(trace_pool->params.hUpstreamPool, ptr);
}

static enum umf_result_t traceGetLastStatus(void *pool) {
struct trace_pool *trace_pool = (struct trace_pool *)pool;

trace_pool->params.trace("get_last_native_error");
return umfPoolGetLastAllocationError(trace_pool->params.hUpstreamPool);
}

struct umf_memory_pool_ops_t UMF_TRACE_POOL_OPS = {
.version = UMF_VERSION_CURRENT,
.initialize = traceInitialize,
.finalize = traceFinalize,
.malloc = traceMalloc,
.realloc = traceRealloc,
.calloc = traceCalloc,
.aligned_malloc = traceAlignedMalloc,
.malloc_usable_size = traceMallocUsableSize,
.free = traceFree,
.get_last_allocation_error = traceGetLastStatus,
};
91 changes: 91 additions & 0 deletions src/provider/provider_null.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
// Copyright (C) 2023 Intel Corporation
// Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#include <assert.h>
#include <stdlib.h>

#include <umf/memory_provider_ops.h>
#include <provider/provider_null.h>

static enum umf_result_t nullInitialize(void *params, void **pool) {
(void)params;
*pool = NULL;
return UMF_RESULT_SUCCESS;
}

static void nullFinalize(void *pool) { (void)pool; }

static enum umf_result_t nullAlloc(void *provider, size_t size,
size_t alignment, void **ptr) {
(void)provider;
(void)size;
(void)alignment;
*ptr = NULL;
return UMF_RESULT_SUCCESS;
}

static enum umf_result_t nullFree(void *provider, void *ptr, size_t size) {
(void)provider;
(void)ptr;
(void)size;
return UMF_RESULT_SUCCESS;
}

static void nullGetLastError(void *provider, const char **ppMsg,
int32_t *pError) {
(void)provider;
(void)ppMsg;
(void)pError;
}

static enum umf_result_t nullGetRecommendedPageSize(void *provider, size_t size,
size_t *pageSize) {
(void)provider;
(void)size;
(void)pageSize;
return UMF_RESULT_SUCCESS;
}

static enum umf_result_t nullGetPageSize(void *provider, void *ptr,

size_t *pageSize) {
(void)provider;
(void)ptr;
(void)pageSize;
return UMF_RESULT_SUCCESS;
}

static enum umf_result_t nullPurgeLazy(void *provider, void *ptr, size_t size) {
(void)provider;
(void)ptr;
(void)size;
return UMF_RESULT_SUCCESS;
}

static enum umf_result_t nullPurgeForce(void *provider, void *ptr,
size_t size) {
(void)provider;
(void)ptr;
(void)size;
return UMF_RESULT_SUCCESS;
}

static const char *nullName(void *provider) {
(void)provider;
return "null";
}

struct umf_memory_provider_ops_t UMF_NULL_PROVIDER_OPS = {
.version = UMF_VERSION_CURRENT,
.initialize = nullInitialize,
.finalize = nullFinalize,
.alloc = nullAlloc,
.free = nullFree,
.get_last_native_error = nullGetLastError,
.get_recommended_page_size = nullGetRecommendedPageSize,
.get_min_page_size = nullGetPageSize,
.purge_lazy = nullPurgeLazy,
.purge_force = nullPurgeForce,
.get_name = nullName,
};
Loading