Skip to content

Commit c35b3a1

Browse files
committed
matras: introduce the matras_alloc_reserve function
The function is meant to be used in order to reserve the amount of extents possibly required to allocate the specified amount of blocks. Needed for tarantool/tarantool#10831
1 parent 8306060 commit c35b3a1

File tree

3 files changed

+45
-19
lines changed

3 files changed

+45
-19
lines changed

include/small/matras.h

+7-19
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,13 @@ matras_dealloc_range(struct matras *m, matras_id_t range_count);
310310
int
311311
matras_touch_reserve(struct matras *m, int count);
312312

313+
/**
314+
* Reserve the max amount of extents required to successfully allocate @p count
315+
* blocks. The extents are reserved in the allocator given on construction.
316+
*/
317+
int
318+
matras_alloc_reserve(struct matras *m, int count);
319+
313320
/**
314321
* Convert block id into block address.
315322
*/
@@ -469,25 +476,6 @@ matras_allocator_create(struct matras_allocator *allocator,
469476
void
470477
matras_allocator_destroy(struct matras_allocator *allocator);
471478

472-
/**
473-
* Request a new extent or take one from the reserved list.
474-
*/
475-
void *
476-
matras_allocator_alloc(struct matras_allocator *allocator);
477-
478-
/**
479-
* Return the given extent to the external allocator.
480-
*/
481-
void
482-
matras_allocator_free(struct matras_allocator *allocator, void *ext);
483-
484-
/**
485-
* Request and put into the reserved list new extents until its size is @p
486-
* count or greater. Returns 0 on success, -1 on memory error.
487-
*/
488-
int
489-
matras_allocator_reserve(struct matras_allocator *allocator, int count);
490-
491479
#if defined(__cplusplus)
492480
} /* extern "C" */
493481
#endif /* defined(__cplusplus) */

include/small/util.h

+2
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@
8888
# define lengthof(array) (sizeof (array) / sizeof ((array)[0]))
8989
#endif
9090

91+
#define SMALL_DIV_ROUND_UP(a, b) ((a) + (b) - 1) / (b)
92+
9193
#define small_xmalloc(size) \
9294
({ \
9395
void *ret = malloc(size); \

small/matras.c

+36
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,18 @@
1212
#pragma intrinsic (_BitScanReverse)
1313
#endif
1414

15+
#include "util.h"
16+
17+
/** See the definitions for details. */
18+
void *
19+
matras_allocator_alloc(struct matras_allocator *allocator);
20+
21+
void
22+
matras_allocator_free(struct matras_allocator *allocator, void *ext);
23+
24+
int
25+
matras_allocator_reserve(struct matras_allocator *allocator, int count);
26+
1527
/**
1628
* Dummy thread-local matras_stats struct used if matras_stats wasn't
1729
* passed to matras_create().
@@ -364,6 +376,30 @@ matras_touch_reserve(struct matras *m, int count)
364376
return matras_allocator_reserve(m->allocator, max_extents_required);
365377
}
366378

379+
/**
380+
* Reserve the max amount of extents required to successfully allocate @p count
381+
* blocks. The extents are reserved in the allocator given on construction.
382+
*/
383+
int
384+
matras_alloc_reserve(struct matras *m, int count)
385+
{
386+
assert(count >= 0);
387+
388+
/* No allocations planned. */
389+
if (count == 0)
390+
return 0;
391+
392+
/*
393+
* This reserves up to 3 extents more than required, but it should be OK
394+
* since the reserved memory is generic for all matras_allocator users.
395+
*/
396+
int l3_count = SMALL_DIV_ROUND_UP(m->block_size * count,
397+
m->allocator->extent_size);
398+
int l2_count = SMALL_DIV_ROUND_UP(l3_count * sizeof(void *),
399+
m->allocator->extent_size);
400+
return matras_allocator_reserve(m->allocator, l3_count + l2_count + 1);
401+
}
402+
367403
/**
368404
* Return the number of allocated extents (of size m->extent_size each)
369405
*/

0 commit comments

Comments
 (0)