Skip to content

Commit afbfa57

Browse files
committed
Add allocator context to matras
So that the user wouldn't be bound to use the global allocator, but could specify per environment one. We need this for: tarantool/tarantool#1767
1 parent 259e3ce commit afbfa57

File tree

3 files changed

+30
-20
lines changed

3 files changed

+30
-20
lines changed

small/matras.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ matras_log2(matras_id_t val)
3737
*/
3838
void
3939
matras_create(struct matras *m, matras_id_t extent_size, matras_id_t block_size,
40-
matras_alloc_func alloc_func, matras_free_func free_func)
40+
matras_alloc_func alloc_func, matras_free_func free_func,
41+
void *alloc_ctx)
4142
{
4243
/*extent_size must be power of 2 */
4344
assert((extent_size & (extent_size - 1)) == 0);
@@ -56,6 +57,7 @@ matras_create(struct matras *m, matras_id_t extent_size, matras_id_t block_size,
5657
m->extent_count = 0;
5758
m->alloc_func = alloc_func;
5859
m->free_func = free_func;
60+
m->alloc_ctx = alloc_ctx;
5961

6062
matras_id_t log1 = matras_log2(extent_size);
6163
matras_id_t log2 = matras_log2(block_size);
@@ -86,7 +88,7 @@ matras_reset(struct matras *m)
8688
static inline void *
8789
matras_alloc_extent(struct matras *m)
8890
{
89-
void *ext = m->alloc_func();
91+
void *ext = m->alloc_func(m->alloc_ctx);
9092
if (ext)
9193
m->extent_count++;
9294
return ext;
@@ -98,7 +100,7 @@ matras_alloc_extent(struct matras *m)
98100
static inline void
99101
matras_free_extent(struct matras *m, void *ext)
100102
{
101-
m->free_func(ext);
103+
m->free_func(m->alloc_ctx, ext);
102104
m->extent_count--;
103105
}
104106

small/matras.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ typedef uint32_t matras_id_t;
136136
* of size M). Is allowed to return NULL, but is not allowed
137137
* to throw an exception
138138
*/
139-
typedef void *(*matras_alloc_func)();
140-
typedef void (*matras_free_func)(void *);
139+
typedef void *(*matras_alloc_func)(void *ctx);
140+
typedef void (*matras_free_func)(void *ctx, void *ptr);
141141

142142
/**
143143
* sruct matras_view represents appropriate mapping between
@@ -177,6 +177,8 @@ struct matras {
177177
matras_alloc_func alloc_func;
178178
/* External extent deallocator */
179179
matras_free_func free_func;
180+
/* Argument passed to extent allocator */
181+
void *alloc_ctx;
180182
};
181183

182184
/*
@@ -207,7 +209,8 @@ struct matras {
207209
*/
208210
void
209211
matras_create(struct matras *m, matras_id_t extent_size, matras_id_t block_size,
210-
matras_alloc_func alloc_func, matras_free_func free_func);
212+
matras_alloc_func alloc_func, matras_free_func free_func,
213+
void *alloc_ctx);
211214

212215
/**
213216
* Free all memory used by an instance of matras and

test/matras.cc

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
#include <iostream>
99

1010
static void *
11-
pta_alloc();
11+
pta_alloc(void *ctx);
1212
static void
13-
pta_free(void *p);
13+
pta_free(void *ctx, void *p);
1414

1515
#define PROV_BLOCK_SIZE 16
1616
#define PROV_EXTENT_SIZE 64
@@ -41,8 +41,9 @@ unsigned int alloc_err_inj_countdown = 0;
4141
#define MATRAS_VERSION_COUNT 8
4242

4343
static void *
44-
pta_alloc()
44+
pta_alloc(void *ctx)
4545
{
46+
static_cast<void>(ctx);
4647
if (alloc_err_inj_enabled) {
4748
if (alloc_err_inj_countdown == 0)
4849
return 0;
@@ -54,8 +55,9 @@ pta_alloc()
5455
return p;
5556
}
5657
static void
57-
pta_free(void *p)
58+
pta_free(void *ctx, void *p)
5859
{
60+
static_cast<void>(ctx);
5961
check(AllocatedBlocks.find(p) != AllocatedBlocks.end(), "Bad free");
6062
AllocatedBlocks.erase(p);
6163
delete [] static_cast<char *>(p);
@@ -73,7 +75,7 @@ void matras_alloc_test()
7375

7476
alloc_err_inj_enabled = false;
7577
for (unsigned int i = 0; i <= maxCapacity; i++) {
76-
matras_create(&mat, PROV_EXTENT_SIZE, PROV_BLOCK_SIZE, pta_alloc, pta_free);
78+
matras_create(&mat, PROV_EXTENT_SIZE, PROV_BLOCK_SIZE, pta_alloc, pta_free, NULL);
7779
check(1u << mat.log2_capacity == maxCapacity, "Wrong capacity!");
7880
AllocatedItems.clear();
7981
for (unsigned int j = 0; j < i; j++) {
@@ -116,7 +118,7 @@ void matras_alloc_test()
116118
}
117119

118120
for (unsigned int i = 0; i <= maxCapacity; i++) {
119-
matras_create(&mat, PROV_EXTENT_SIZE, PROV_BLOCK_SIZE, pta_alloc, pta_free);
121+
matras_create(&mat, PROV_EXTENT_SIZE, PROV_BLOCK_SIZE, pta_alloc, pta_free, NULL);
120122
for (unsigned int j = 0; j < i; j++) {
121123
unsigned int res = 0;
122124
(void) matras_alloc(&mat, &res);
@@ -132,7 +134,7 @@ void matras_alloc_test()
132134

133135
alloc_err_inj_enabled = true;
134136
for (unsigned int i = 0; i <= maxCapacity; i++) {
135-
matras_create(&mat, PROV_EXTENT_SIZE, PROV_BLOCK_SIZE, pta_alloc, pta_free);
137+
matras_create(&mat, PROV_EXTENT_SIZE, PROV_BLOCK_SIZE, pta_alloc, pta_free, NULL);
136138

137139
alloc_err_inj_countdown = i;
138140

@@ -154,17 +156,18 @@ void matras_alloc_test()
154156

155157
typedef uint64_t type_t;
156158
const size_t VER_EXTENT_SIZE = 512;
157-
int extents_in_use = 0;
158159

159-
void *all()
160+
void *all(void *ctx)
160161
{
161-
extents_in_use++;
162+
long *extents_in_use = static_cast<long *>(ctx);
163+
++*extents_in_use;
162164
return malloc(VER_EXTENT_SIZE);
163165
}
164166

165-
void dea(void *p)
167+
void dea(void *ctx, void *p)
166168
{
167-
extents_in_use--;
169+
long *extents_in_use = static_cast<long *>(ctx);
170+
--*extents_in_use;
168171
free(p);
169172
}
170173

@@ -192,7 +195,8 @@ matras_vers_test()
192195
int use_mask = 1;
193196
int cur_num_or_ver = 1;
194197
struct matras local;
195-
matras_create(&local, VER_EXTENT_SIZE, sizeof(type_t), all, dea);
198+
long extents_in_use = 0;
199+
matras_create(&local, VER_EXTENT_SIZE, sizeof(type_t), all, dea, &extents_in_use);
196200
type_t val = 0;
197201
for (int s = 10; s < 8000; s = int(s * 1.5)) {
198202
for (int k = 0; k < 800; k++) {
@@ -267,7 +271,8 @@ matras_gh_1145_test()
267271
std::cout << "Testing matras gh-1145 test..." << std::endl;
268272

269273
struct matras local;
270-
matras_create(&local, VER_EXTENT_SIZE, sizeof(type_t), all, dea);
274+
long extents_in_use = 0;
275+
matras_create(&local, VER_EXTENT_SIZE, sizeof(type_t), all, dea, &extents_in_use);
271276
struct matras_view view;
272277
matras_create_read_view(&local, &view);
273278
matras_id_t id;

0 commit comments

Comments
 (0)