Skip to content

Commit c7cf5ad

Browse files
authored
Merge pull request #184 from ldorau/Use_base_allocator_in_memory_pool_provider_and_target
Use base allocator in memory pool, provider, target and memspace
2 parents be3cf23 + 7beb87a commit c7cf5ad

21 files changed

+276
-61
lines changed

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ set(UMF_LIBS umf_utils)
1111
set(UMF_SOURCES
1212
base_alloc/base_alloc.c
1313
base_alloc/base_alloc_linear.c
14+
base_alloc/base_alloc_global.c
1415
memory_pool.c
1516
memory_provider.c
1617
memory_provider_get_last_failed.c

src/base_alloc/base_alloc.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,19 @@
1212

1313
#include <stddef.h>
1414

15+
#ifdef __cplusplus
16+
extern "C" {
17+
#endif
18+
1519
typedef struct umf_ba_pool_t umf_ba_pool_t;
1620

1721
umf_ba_pool_t *umf_ba_create(size_t size);
1822
void *umf_ba_alloc(umf_ba_pool_t *pool);
1923
void umf_ba_free(umf_ba_pool_t *pool, void *ptr);
2024
void umf_ba_destroy(umf_ba_pool_t *pool);
2125

26+
#ifdef __cplusplus
27+
}
28+
#endif
29+
2230
#endif /* UMF_BASE_ALLOC_H */

src/base_alloc/base_alloc_global.c

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright (C) 2024 Intel Corporation
3+
*
4+
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
5+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
*/
7+
8+
#include <assert.h>
9+
10+
#include "base_alloc.h"
11+
12+
#define SIZE_BA_POOL_CHUNK 128
13+
14+
// global base allocator used by all providers and pools
15+
static umf_ba_pool_t *BA_pool = NULL;
16+
17+
int umf_ba_create_global(void) {
18+
assert(BA_pool == NULL);
19+
20+
BA_pool = umf_ba_create(SIZE_BA_POOL_CHUNK);
21+
if (!BA_pool) {
22+
return -1;
23+
}
24+
25+
return 0;
26+
}
27+
28+
void umf_ba_destroy_global(void) {
29+
assert(BA_pool);
30+
umf_ba_destroy(BA_pool);
31+
BA_pool = NULL;
32+
}
33+
34+
umf_ba_pool_t *umf_ba_get_pool(size_t size) {
35+
// TODO: a specific class-size base allocator can be returned here
36+
assert(BA_pool);
37+
assert(size <= SIZE_BA_POOL_CHUNK);
38+
39+
if (size > SIZE_BA_POOL_CHUNK) {
40+
return NULL;
41+
}
42+
43+
return BA_pool;
44+
}

src/base_alloc/base_alloc_global.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
* Copyright (C) 2024 Intel Corporation
3+
*
4+
* Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT.
5+
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
*/
7+
8+
#ifndef UMF_BASE_ALLOC_GLOBAL_H
9+
#define UMF_BASE_ALLOC_GLOBAL_H 1
10+
11+
#include "base_alloc.h"
12+
13+
#ifdef __cplusplus
14+
extern "C" {
15+
#endif
16+
17+
int umf_ba_create_global(void);
18+
void umf_ba_destroy_global(void);
19+
umf_ba_pool_t *umf_ba_get_pool(size_t size);
20+
21+
#ifdef __cplusplus
22+
}
23+
#endif
24+
25+
#endif /* UMF_BASE_ALLOC_GLOBAL_H */

src/base_alloc/base_alloc_internal.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,16 @@
1414
#include <unistd.h>
1515
#endif
1616

17+
#ifdef __cplusplus
18+
extern "C" {
19+
#endif
20+
1721
void *ba_os_alloc(size_t size);
1822
void ba_os_free(void *ptr, size_t size);
1923
size_t ba_os_get_page_size(void);
2024

25+
#ifdef __cplusplus
26+
}
27+
#endif
28+
2129
#endif /* UMF_BASE_ALLOC_INTERNAL_H */

src/base_alloc/base_alloc_linear.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ void *umf_ba_linear_alloc(umf_ba_linear_pool_t *pool, size_t size) {
7777
"space left: %zu)\n",
7878
aligned_size, pool->metadata.size_left);
7979
util_mutex_unlock(&pool->metadata.lock);
80+
assert(pool->metadata.size_left >= aligned_size);
8081
return NULL; // out of memory
8182
}
8283

src/base_alloc/base_alloc_linear.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,18 @@
1717

1818
#include <stddef.h>
1919

20+
#ifdef __cplusplus
21+
extern "C" {
22+
#endif
23+
2024
typedef struct umf_ba_linear_pool umf_ba_linear_pool_t;
2125

2226
umf_ba_linear_pool_t *umf_ba_linear_create(size_t pool_size);
2327
void *umf_ba_linear_alloc(umf_ba_linear_pool_t *pool, size_t size);
2428
void umf_ba_linear_destroy(umf_ba_linear_pool_t *pool);
2529

30+
#ifdef __cplusplus
31+
}
32+
#endif
33+
2634
#endif /* UMF_BASE_ALLOC_LINEAR_H */

src/base_alloc/base_alloc_linux.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
66
*/
77

8+
#include <assert.h>
89
#include <stdio.h>
910
#include <sys/mman.h>
1011
#include <sys/syscall.h>
@@ -17,6 +18,10 @@ void *ba_os_alloc(size_t size) {
1718
-1, 0);
1819
}
1920

20-
void ba_os_free(void *ptr, size_t size) { munmap(ptr, size); }
21+
void ba_os_free(void *ptr, size_t size) {
22+
int ret = munmap(ptr, size);
23+
assert(ret == 0);
24+
(void)ret; // unused
25+
}
2126

2227
size_t ba_os_get_page_size(void) { return sysconf(_SC_PAGE_SIZE); }

src/libumf_linux.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,20 @@
77
*
88
*/
99

10-
#include "provider_tracking.h"
11-
1210
#include <stddef.h>
1311

12+
#include "base_alloc_global.h"
13+
#include "provider_tracking.h"
14+
1415
umf_memory_tracker_handle_t TRACKER = NULL;
1516

1617
void __attribute__((constructor)) umfCreate(void) {
1718
TRACKER = umfMemoryTrackerCreate();
19+
umf_ba_create_global();
1820
}
21+
1922
void __attribute__((destructor)) umfDestroy(void) {
23+
umf_ba_destroy_global();
2024
umfMemoryTrackerDestroy(TRACKER);
2125
}
2226

src/libumf_windows.c

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,30 @@
77
*
88
*/
99

10-
#include "provider_tracking.h"
11-
1210
#include <stdlib.h>
1311
#include <windows.h>
1412

13+
#include "base_alloc_global.h"
14+
#include "provider_tracking.h"
15+
1516
umf_memory_tracker_handle_t TRACKER = NULL;
1617

17-
static void providerFini(void) { umfMemoryTrackerDestroy(TRACKER); }
18+
static void umfCreate(void) {
19+
TRACKER = umfMemoryTrackerCreate();
20+
umf_ba_create_global();
21+
}
22+
23+
static void umfDestroy(void) {
24+
umf_ba_destroy_global();
25+
umfMemoryTrackerDestroy(TRACKER);
26+
}
1827

1928
#if defined(UMF_SHARED_LIBRARY)
2029
BOOL APIENTRY DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {
2130
if (fdwReason == DLL_PROCESS_DETACH) {
22-
providerFini();
31+
umfDestroy();
2332
} else if (fdwReason == DLL_PROCESS_ATTACH) {
24-
TRACKER = umfMemoryTrackerCreate();
33+
umfCreate();
2534
}
2635
return TRUE;
2736
}
@@ -34,8 +43,8 @@ INIT_ONCE init_once_flag = INIT_ONCE_STATIC_INIT;
3443

3544
BOOL CALLBACK initOnceCb(PINIT_ONCE InitOnce, PVOID Parameter,
3645
PVOID *lpContext) {
37-
TRACKER = umfMemoryTrackerCreate();
38-
atexit(providerFini);
46+
umfCreate();
47+
atexit(umfDestroy);
3948
return TRACKER ? TRUE : FALSE;
4049
}
4150

src/memory_pool_default.c

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,14 @@
77
*
88
*/
99

10-
#include "memory_pool_internal.h"
10+
#include <assert.h>
11+
#include <stdlib.h>
1112

1213
#include <umf/memory_pool.h>
1314

14-
#include <assert.h>
15-
#include <stdlib.h>
15+
#include "base_alloc_global.h"
16+
#include "memory_pool_internal.h"
17+
#include "memory_provider_internal.h"
1618

1719
umf_result_t umfPoolCreateInternal(const umf_memory_pool_ops_t *ops,
1820
umf_memory_provider_handle_t provider,
@@ -23,20 +25,26 @@ umf_result_t umfPoolCreateInternal(const umf_memory_pool_ops_t *ops,
2325
}
2426

2527
umf_result_t ret = UMF_RESULT_SUCCESS;
26-
umf_memory_pool_handle_t pool = malloc(sizeof(umf_memory_pool_t));
28+
umf_ba_pool_t *base_allocator = umf_ba_get_pool(sizeof(umf_memory_pool_t));
29+
if (!base_allocator) {
30+
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
31+
}
32+
33+
umf_memory_pool_handle_t pool = umf_ba_alloc(base_allocator);
2734
if (!pool) {
2835
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
2936
}
3037

3138
assert(ops->version == UMF_VERSION_CURRENT);
3239

40+
pool->base_allocator = base_allocator;
3341
pool->provider = provider;
3442
pool->own_provider = false;
3543

3644
pool->ops = *ops;
3745
ret = ops->initialize(pool->provider, params, &pool->pool_priv);
3846
if (ret != UMF_RESULT_SUCCESS) {
39-
free(pool);
47+
umf_ba_free(base_allocator, pool);
4048
return ret;
4149
}
4250

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

5867
umf_result_t umfFree(void *ptr) {

src/memory_pool_internal.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
extern "C" {
2222
#endif
2323

24+
#include "base_alloc.h"
25+
2426
typedef struct umf_memory_pool_t {
2527
void *pool_priv;
2628
umf_memory_pool_ops_t ops;
@@ -29,6 +31,9 @@ typedef struct umf_memory_pool_t {
2931
umf_memory_provider_handle_t provider;
3032
// Tells whether memory provider is owned by the pool.
3133
bool own_provider;
34+
35+
// saved pointer to the global base allocator
36+
umf_ba_pool_t *base_allocator;
3237
} umf_memory_pool_t;
3338

3439
umf_result_t umfPoolCreateInternal(const umf_memory_pool_ops_t *ops,

src/memory_pool_tracking.c

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@
77
*
88
*/
99

10-
#include "memory_pool_internal.h"
11-
#include "memory_provider_internal.h"
12-
#include "provider_tracking.h"
10+
#include <assert.h>
11+
#include <stdlib.h>
1312

1413
#include <umf/memory_pool.h>
1514

16-
#include <assert.h>
17-
#include <stdlib.h>
15+
#include "base_alloc_global.h"
16+
#include "memory_pool_internal.h"
17+
#include "memory_provider_internal.h"
18+
#include "provider_tracking.h"
1819

1920
umf_result_t umfPoolCreateInternal(const umf_memory_pool_ops_t *ops,
2021
umf_memory_provider_handle_t provider,
@@ -25,7 +26,12 @@ umf_result_t umfPoolCreateInternal(const umf_memory_pool_ops_t *ops,
2526
}
2627

2728
umf_result_t ret = UMF_RESULT_SUCCESS;
28-
umf_memory_pool_handle_t pool = malloc(sizeof(umf_memory_pool_t));
29+
umf_ba_pool_t *base_allocator = umf_ba_get_pool(sizeof(umf_memory_pool_t));
30+
if (!base_allocator) {
31+
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
32+
}
33+
34+
umf_memory_pool_handle_t pool = umf_ba_alloc(base_allocator);
2935
if (!pool) {
3036
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
3137
}
@@ -37,6 +43,8 @@ umf_result_t umfPoolCreateInternal(const umf_memory_pool_ops_t *ops,
3743
if (ret != UMF_RESULT_SUCCESS) {
3844
goto err_provider_create;
3945
}
46+
47+
pool->base_allocator = base_allocator;
4048
pool->own_provider = false;
4149

4250
pool->ops = *ops;
@@ -51,8 +59,7 @@ umf_result_t umfPoolCreateInternal(const umf_memory_pool_ops_t *ops,
5159
err_pool_init:
5260
umfMemoryProviderDestroy(pool->provider);
5361
err_provider_create:
54-
free(pool);
55-
62+
umf_ba_free(base_allocator, pool);
5663
return ret;
5764
}
5865

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

7280
umf_result_t umfFree(void *ptr) {

0 commit comments

Comments
 (0)