Skip to content

Commit 9d07d72

Browse files
committed
Create only one base allocator for all pools and providers
Signed-off-by: Lukasz Dorau <[email protected]>
1 parent 786a5f2 commit 9d07d72

File tree

5 files changed

+73
-37
lines changed

5 files changed

+73
-37
lines changed

src/memory_pool_default.c

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@
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 "memory_pool_internal.h"
16+
#include "memory_provider_internal.h"
1617

1718
umf_result_t umfPoolCreateInternal(const umf_memory_pool_ops_t *ops,
1819
umf_memory_provider_handle_t provider,
@@ -24,28 +25,28 @@ umf_result_t umfPoolCreateInternal(const umf_memory_pool_ops_t *ops,
2425

2526
umf_result_t ret = UMF_RESULT_SUCCESS;
2627

27-
umf_ba_linear_pool_t *pool_linear =
28-
umf_ba_linear_create(0 /* minimum pool size */);
29-
if (!pool_linear) {
28+
int rv = umfBaseAllocatorCreate();
29+
if (rv) {
3030
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
3131
}
3232

33-
umf_memory_pool_handle_t pool =
34-
umf_ba_linear_alloc(pool_linear, sizeof(umf_memory_pool_t));
33+
assert(sizeof(umf_memory_pool_t) < SIZE_LBA_POOL_CHUNK);
34+
35+
umf_memory_pool_handle_t pool = umf_ba_alloc(LBA_pool);
3536
if (!pool) {
3637
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
3738
}
3839

3940
assert(ops->version == UMF_VERSION_CURRENT);
4041

41-
pool->pool_linear = pool_linear;
4242
pool->provider = provider;
4343
pool->own_provider = false;
4444

4545
pool->ops = *ops;
4646
ret = ops->initialize(pool->provider, params, &pool->pool_priv);
4747
if (ret != UMF_RESULT_SUCCESS) {
48-
umf_ba_linear_destroy(pool_linear); // it frees pool
48+
umf_ba_free(LBA_pool, pool);
49+
umfBaseAllocatorDestroy();
4950
return ret;
5051
}
5152

@@ -61,7 +62,8 @@ void umfPoolDestroy(umf_memory_pool_handle_t hPool) {
6162
umfPoolGetMemoryProvider(hPool, &hProvider);
6263
umfMemoryProviderDestroy(hProvider);
6364
}
64-
umf_ba_linear_destroy(hPool->pool_linear); // it frees hPool
65+
umf_ba_free(LBA_pool, hPool);
66+
umfBaseAllocatorDestroy();
6567
}
6668

6769
umf_result_t umfFree(void *ptr) {

src/memory_pool_internal.h

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

24-
#include "base_alloc_linear.h"
25-
2624
typedef struct umf_memory_pool_t {
2725
void *pool_priv;
2826
umf_memory_pool_ops_t ops;
@@ -31,9 +29,6 @@ typedef struct umf_memory_pool_t {
3129
umf_memory_provider_handle_t provider;
3230
// Tells whether memory provider is owned by the pool.
3331
bool own_provider;
34-
35-
// linear base allocator used by the pool
36-
umf_ba_linear_pool_t *pool_linear;
3732
} umf_memory_pool_t;
3833

3934
umf_result_t umfPoolCreateInternal(const umf_memory_pool_ops_t *ops,

src/memory_pool_tracking.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,20 +26,18 @@ umf_result_t umfPoolCreateInternal(const umf_memory_pool_ops_t *ops,
2626

2727
umf_result_t ret = UMF_RESULT_SUCCESS;
2828

29-
umf_ba_linear_pool_t *pool_linear =
30-
umf_ba_linear_create(0 /* minimum pool size */);
31-
if (!pool_linear) {
29+
int rv = umfBaseAllocatorCreate();
30+
if (rv) {
3231
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
3332
}
3433

35-
umf_memory_pool_handle_t pool =
36-
umf_ba_linear_alloc(pool_linear, sizeof(umf_memory_pool_t));
34+
assert(sizeof(umf_memory_pool_t) < SIZE_LBA_POOL_CHUNK);
35+
36+
umf_memory_pool_handle_t pool = umf_ba_alloc(LBA_pool);
3737
if (!pool) {
3838
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
3939
}
4040

41-
pool->pool_linear = pool_linear;
42-
4341
assert(ops->version == UMF_VERSION_CURRENT);
4442

4543
// wrap provider with memory tracking provider
@@ -61,7 +59,8 @@ umf_result_t umfPoolCreateInternal(const umf_memory_pool_ops_t *ops,
6159
err_pool_init:
6260
umfMemoryProviderDestroy(pool->provider);
6361
err_provider_create:
64-
umf_ba_linear_destroy(pool_linear); // it frees pool
62+
umf_ba_free(LBA_pool, pool);
63+
umfBaseAllocatorDestroy();
6564

6665
return ret;
6766
}
@@ -76,7 +75,8 @@ void umfPoolDestroy(umf_memory_pool_handle_t hPool) {
7675
}
7776
// Destroy tracking provider.
7877
umfMemoryProviderDestroy(hPool->provider);
79-
umf_ba_linear_destroy(hPool->pool_linear); // it frees hPool
78+
umf_ba_free(LBA_pool, hPool);
79+
umfBaseAllocatorDestroy();
8080
}
8181

8282
umf_result_t umfFree(void *ptr) {

src/memory_provider.c

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,46 +13,74 @@
1313

1414
#include <umf/memory_provider.h>
1515

16-
#include "base_alloc_linear.h"
1716
#include "memory_provider_internal.h"
1817
#include "utils_common.h"
18+
#include "utils_concurrency.h"
19+
20+
// base allocator used by all providers and pools
21+
umf_ba_pool_t *LBA_pool = NULL;
22+
23+
// reference counter of the global base allocator
24+
static int N_refs_LBA_pool = 0;
1925

2026
typedef struct umf_memory_provider_t {
2127
umf_memory_provider_ops_t ops;
2228
void *provider_priv;
23-
24-
// linear base allocator used by the provider
25-
umf_ba_linear_pool_t *pool_linear;
2629
} umf_memory_provider_t;
2730

31+
int umfBaseAllocatorCreate(void) {
32+
if (LBA_pool == NULL) {
33+
umf_ba_pool_t *ba_pool = umf_ba_create(SIZE_LBA_POOL_CHUNK);
34+
if (!ba_pool) {
35+
return -1;
36+
}
37+
38+
if (!util_atomic_bool_compare_exchange(&LBA_pool, NULL, ba_pool)) {
39+
umf_ba_destroy(ba_pool);
40+
}
41+
}
42+
43+
util_atomic_increment(&N_refs_LBA_pool);
44+
45+
assert(LBA_pool != NULL);
46+
return 0;
47+
}
48+
49+
void umfBaseAllocatorDestroy(void) {
50+
if (util_atomic_decrement(&N_refs_LBA_pool) == 0) {
51+
umf_ba_destroy(LBA_pool);
52+
LBA_pool = NULL;
53+
}
54+
}
55+
2856
umf_result_t umfMemoryProviderCreate(const umf_memory_provider_ops_t *ops,
2957
void *params,
3058
umf_memory_provider_handle_t *hProvider) {
3159
if (!ops || !hProvider) {
3260
return UMF_RESULT_ERROR_INVALID_ARGUMENT;
3361
}
3462

35-
umf_ba_linear_pool_t *pool_linear =
36-
umf_ba_linear_create(0 /* minimum pool size */);
37-
if (!pool_linear) {
63+
int rv = umfBaseAllocatorCreate();
64+
if (rv) {
3865
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
3966
}
4067

41-
umf_memory_provider_handle_t provider =
42-
umf_ba_linear_alloc(pool_linear, sizeof(umf_memory_provider_t));
68+
assert(sizeof(umf_memory_provider_t) < SIZE_LBA_POOL_CHUNK);
69+
70+
umf_memory_provider_handle_t provider = umf_ba_alloc(LBA_pool);
4371
if (!provider) {
4472
return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY;
4573
}
4674

4775
assert(ops->version == UMF_VERSION_CURRENT);
4876

49-
provider->pool_linear = pool_linear;
5077
provider->ops = *ops;
5178

5279
void *provider_priv;
5380
umf_result_t ret = ops->initialize(params, &provider_priv);
5481
if (ret != UMF_RESULT_SUCCESS) {
55-
umf_ba_linear_destroy(pool_linear); // it frees provider
82+
umf_ba_free(LBA_pool, provider);
83+
umfBaseAllocatorDestroy();
5684
return ret;
5785
}
5886

@@ -65,7 +93,8 @@ umf_result_t umfMemoryProviderCreate(const umf_memory_provider_ops_t *ops,
6593

6694
void umfMemoryProviderDestroy(umf_memory_provider_handle_t hProvider) {
6795
hProvider->ops.finalize(hProvider->provider_priv);
68-
umf_ba_linear_destroy(hProvider->pool_linear); // it frees hProvider
96+
umf_ba_free(LBA_pool, hProvider);
97+
umfBaseAllocatorDestroy();
6998
}
7099

71100
static void

src/memory_provider_internal.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,16 @@
1616
extern "C" {
1717
#endif
1818

19+
#include "base_alloc.h"
20+
21+
#define SIZE_LBA_POOL_CHUNK 128
22+
23+
// base allocator used by all providers and pools
24+
extern umf_ba_pool_t *LBA_pool;
25+
26+
int umfBaseAllocatorCreate(void);
27+
void umfBaseAllocatorDestroy(void);
28+
1929
void *umfMemoryProviderGetPriv(umf_memory_provider_handle_t hProvider);
2030
umf_memory_provider_handle_t *umfGetLastFailedMemoryProviderPtr(void);
2131

0 commit comments

Comments
 (0)