Skip to content

Commit f20103d

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 f20103d

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

include/small/matras.h

+7
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
*/

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

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

15+
#include "util.h"
16+
1517
/**
1618
* Dummy thread-local matras_stats struct used if matras_stats wasn't
1719
* passed to matras_create().
@@ -364,6 +366,30 @@ matras_touch_reserve(struct matras *m, int count)
364366
return matras_allocator_reserve(m->allocator, max_extents_required);
365367
}
366368

369+
/**
370+
* Reserve the max amount of extents required to successfully allocate @p count
371+
* blocks. The extents are reserved in the allocator given on construction.
372+
*/
373+
int
374+
matras_alloc_reserve(struct matras *m, int count)
375+
{
376+
assert(count >= 0);
377+
378+
/* No allocations planned. */
379+
if (count == 0)
380+
return 0;
381+
382+
/*
383+
* This reserves up to 3 extents more than required, but it should be OK
384+
* since the reserved memory is generic for all matras_allocator users.
385+
*/
386+
int l3_count = SMALL_DIV_ROUND_UP(m->block_size * count,
387+
m->allocator->extent_size);
388+
int l2_count = SMALL_DIV_ROUND_UP(l3_count * sizeof(void *),
389+
m->allocator->extent_size);
390+
return matras_allocator_reserve(m->allocator, l3_count + l2_count + 1);
391+
}
392+
367393
/**
368394
* Return the number of allocated extents (of size m->extent_size each)
369395
*/

0 commit comments

Comments
 (0)