Skip to content

Use base allocator in memory pool, provider, target and memspace #184

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 src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ set(UMF_LIBS umf_utils)
set(UMF_SOURCES
base_alloc/base_alloc.c
base_alloc/base_alloc_linear.c
base_alloc/base_alloc_global.c
memory_pool.c
memory_provider.c
memory_provider_get_last_failed.c
Expand Down
8 changes: 8 additions & 0 deletions src/base_alloc/base_alloc.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,19 @@

#include <stddef.h>

#ifdef __cplusplus
extern "C" {
#endif

typedef struct umf_ba_pool_t umf_ba_pool_t;

umf_ba_pool_t *umf_ba_create(size_t size);
void *umf_ba_alloc(umf_ba_pool_t *pool);
void umf_ba_free(umf_ba_pool_t *pool, void *ptr);
void umf_ba_destroy(umf_ba_pool_t *pool);

#ifdef __cplusplus
}
#endif

#endif /* UMF_BASE_ALLOC_H */
44 changes: 44 additions & 0 deletions src/base_alloc/base_alloc_global.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (C) 2024 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 "base_alloc.h"

#define SIZE_BA_POOL_CHUNK 128

// global base allocator used by all providers and pools
static umf_ba_pool_t *BA_pool = NULL;

int umf_ba_create_global(void) {
assert(BA_pool == NULL);

BA_pool = umf_ba_create(SIZE_BA_POOL_CHUNK);
if (!BA_pool) {
return -1;
}

return 0;
}

void umf_ba_destroy_global(void) {
assert(BA_pool);
umf_ba_destroy(BA_pool);
BA_pool = NULL;
}

umf_ba_pool_t *umf_ba_get_pool(size_t size) {
// TODO: a specific class-size base allocator can be returned here
assert(BA_pool);
assert(size <= SIZE_BA_POOL_CHUNK);

if (size > SIZE_BA_POOL_CHUNK) {
return NULL;
}

return BA_pool;
}
25 changes: 25 additions & 0 deletions src/base_alloc/base_alloc_global.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Copyright (C) 2024 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_BASE_ALLOC_GLOBAL_H
#define UMF_BASE_ALLOC_GLOBAL_H 1

#include "base_alloc.h"

#ifdef __cplusplus
extern "C" {
#endif

int umf_ba_create_global(void);
void umf_ba_destroy_global(void);
umf_ba_pool_t *umf_ba_get_pool(size_t size);

#ifdef __cplusplus
}
#endif

#endif /* UMF_BASE_ALLOC_GLOBAL_H */
8 changes: 8 additions & 0 deletions src/base_alloc/base_alloc_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,16 @@
#include <unistd.h>
#endif

#ifdef __cplusplus
extern "C" {
#endif

void *ba_os_alloc(size_t size);
void ba_os_free(void *ptr, size_t size);
size_t ba_os_get_page_size(void);

#ifdef __cplusplus
}
#endif

#endif /* UMF_BASE_ALLOC_INTERNAL_H */
1 change: 1 addition & 0 deletions src/base_alloc/base_alloc_linear.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ void *umf_ba_linear_alloc(umf_ba_linear_pool_t *pool, size_t size) {
"space left: %zu)\n",
aligned_size, pool->metadata.size_left);
util_mutex_unlock(&pool->metadata.lock);
assert(pool->metadata.size_left >= aligned_size);
return NULL; // out of memory
}

Expand Down
8 changes: 8 additions & 0 deletions src/base_alloc/base_alloc_linear.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,18 @@

#include <stddef.h>

#ifdef __cplusplus
extern "C" {
#endif

typedef struct umf_ba_linear_pool umf_ba_linear_pool_t;

umf_ba_linear_pool_t *umf_ba_linear_create(size_t pool_size);
void *umf_ba_linear_alloc(umf_ba_linear_pool_t *pool, size_t size);
void umf_ba_linear_destroy(umf_ba_linear_pool_t *pool);

#ifdef __cplusplus
}
#endif

#endif /* UMF_BASE_ALLOC_LINEAR_H */
7 changes: 6 additions & 1 deletion src/base_alloc/base_alloc_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
*/

#include <assert.h>
#include <stdio.h>
#include <sys/mman.h>
#include <sys/syscall.h>
Expand All @@ -17,6 +18,10 @@ void *ba_os_alloc(size_t size) {
-1, 0);
}

void ba_os_free(void *ptr, size_t size) { munmap(ptr, size); }
void ba_os_free(void *ptr, size_t size) {
int ret = munmap(ptr, size);
assert(ret == 0);
(void)ret; // unused
}

size_t ba_os_get_page_size(void) { return sysconf(_SC_PAGE_SIZE); }
8 changes: 6 additions & 2 deletions src/libumf_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,20 @@
*
*/

#include "provider_tracking.h"

#include <stddef.h>

#include "base_alloc_global.h"
#include "provider_tracking.h"

umf_memory_tracker_handle_t TRACKER = NULL;

void __attribute__((constructor)) umfCreate(void) {
TRACKER = umfMemoryTrackerCreate();
umf_ba_create_global();
}

void __attribute__((destructor)) umfDestroy(void) {
umf_ba_destroy_global();
umfMemoryTrackerDestroy(TRACKER);
}

Expand Down
23 changes: 16 additions & 7 deletions src/libumf_windows.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,30 @@
*
*/

#include "provider_tracking.h"

#include <stdlib.h>
#include <windows.h>

#include "base_alloc_global.h"
#include "provider_tracking.h"

umf_memory_tracker_handle_t TRACKER = NULL;

static void providerFini(void) { umfMemoryTrackerDestroy(TRACKER); }
static void umfCreate(void) {
TRACKER = umfMemoryTrackerCreate();
umf_ba_create_global();
}

static void umfDestroy(void) {
umf_ba_destroy_global();
umfMemoryTrackerDestroy(TRACKER);
}

#if defined(UMF_SHARED_LIBRARY)
BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {
if (fdwReason == DLL_PROCESS_DETACH) {
providerFini();
umfDestroy();
} else if (fdwReason == DLL_PROCESS_ATTACH) {
TRACKER = umfMemoryTrackerCreate();
umfCreate();
}
return TRUE;
}
Expand All @@ -34,8 +43,8 @@ INIT_ONCE init_once_flag = INIT_ONCE_STATIC_INIT;

BOOL CALLBACK initOnceCb(PINIT_ONCE InitOnce, PVOID Parameter,
PVOID *lpContext) {
TRACKER = umfMemoryTrackerCreate();
atexit(providerFini);
umfCreate();
atexit(umfDestroy);
return TRACKER ? TRUE : FALSE;
}

Expand Down
21 changes: 15 additions & 6 deletions src/memory_pool_default.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
*
*/

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

#include <umf/memory_pool.h>

#include <assert.h>
#include <stdlib.h>
#include "base_alloc_global.h"
#include "memory_pool_internal.h"
#include "memory_provider_internal.h"

umf_result_t umfPoolCreateInternal(const umf_memory_pool_ops_t *ops,
umf_memory_provider_handle_t provider,
Expand All @@ -23,20 +25,26 @@ umf_result_t umfPoolCreateInternal(const umf_memory_pool_ops_t *ops,
}

umf_result_t ret = UMF_RESULT_SUCCESS;
umf_memory_pool_handle_t pool = malloc(sizeof(umf_memory_pool_t));
umf_ba_pool_t *base_allocator = umf_ba_get_pool(sizeof(umf_memory_pool_t));
if (!base_allocator) {
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
}

umf_memory_pool_handle_t pool = umf_ba_alloc(base_allocator);
if (!pool) {
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
}

assert(ops->version == UMF_VERSION_CURRENT);

pool->base_allocator = base_allocator;
pool->provider = provider;
pool->own_provider = false;

pool->ops = *ops;
ret = ops->initialize(pool->provider, params, &pool->pool_priv);
if (ret != UMF_RESULT_SUCCESS) {
free(pool);
umf_ba_free(base_allocator, pool);
return ret;
}

Expand All @@ -52,7 +60,8 @@ void umfPoolDestroy(umf_memory_pool_handle_t hPool) {
umfPoolGetMemoryProvider(hPool, &hProvider);
umfMemoryProviderDestroy(hProvider);
}
free(hPool);
// TODO: this free keeps memory in base allocator, so it can lead to OOM in some scenarios (it should be optimized)
umf_ba_free(hPool->base_allocator, hPool);
}

umf_result_t umfFree(void *ptr) {
Expand Down
5 changes: 5 additions & 0 deletions src/memory_pool_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
extern "C" {
#endif

#include "base_alloc.h"

typedef struct umf_memory_pool_t {
void *pool_priv;
umf_memory_pool_ops_t ops;
Expand All @@ -29,6 +31,9 @@ typedef struct umf_memory_pool_t {
umf_memory_provider_handle_t provider;
// Tells whether memory provider is owned by the pool.
bool own_provider;

// saved pointer to the global base allocator
umf_ba_pool_t *base_allocator;
} umf_memory_pool_t;

umf_result_t umfPoolCreateInternal(const umf_memory_pool_ops_t *ops,
Expand Down
26 changes: 17 additions & 9 deletions src/memory_pool_tracking.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,15 @@
*
*/

#include "memory_pool_internal.h"
#include "memory_provider_internal.h"
#include "provider_tracking.h"
#include <assert.h>
#include <stdlib.h>

#include <umf/memory_pool.h>

#include <assert.h>
#include <stdlib.h>
#include "base_alloc_global.h"
#include "memory_pool_internal.h"
#include "memory_provider_internal.h"
#include "provider_tracking.h"

umf_result_t umfPoolCreateInternal(const umf_memory_pool_ops_t *ops,
umf_memory_provider_handle_t provider,
Expand All @@ -25,7 +26,12 @@ umf_result_t umfPoolCreateInternal(const umf_memory_pool_ops_t *ops,
}

umf_result_t ret = UMF_RESULT_SUCCESS;
umf_memory_pool_handle_t pool = malloc(sizeof(umf_memory_pool_t));
umf_ba_pool_t *base_allocator = umf_ba_get_pool(sizeof(umf_memory_pool_t));
if (!base_allocator) {
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
}

umf_memory_pool_handle_t pool = umf_ba_alloc(base_allocator);
if (!pool) {
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
}
Expand All @@ -37,6 +43,8 @@ umf_result_t umfPoolCreateInternal(const umf_memory_pool_ops_t *ops,
if (ret != UMF_RESULT_SUCCESS) {
goto err_provider_create;
}

pool->base_allocator = base_allocator;
pool->own_provider = false;

pool->ops = *ops;
Expand All @@ -51,8 +59,7 @@ umf_result_t umfPoolCreateInternal(const umf_memory_pool_ops_t *ops,
err_pool_init:
umfMemoryProviderDestroy(pool->provider);
err_provider_create:
free(pool);

umf_ba_free(base_allocator, pool);
return ret;
}

Expand All @@ -66,7 +73,8 @@ void umfPoolDestroy(umf_memory_pool_handle_t hPool) {
}
// Destroy tracking provider.
umfMemoryProviderDestroy(hPool->provider);
free(hPool);
// TODO: this free keeps memory in base allocator, so it can lead to OOM in some scenarios (it should be optimized)
umf_ba_free(hPool->base_allocator, hPool);
}

umf_result_t umfFree(void *ptr) {
Expand Down
Loading