File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -310,6 +310,13 @@ matras_dealloc_range(struct matras *m, matras_id_t range_count);
310310int
311311matras_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 */
Original file line number Diff line number Diff line change 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); \
Original file line number Diff line number Diff line change 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 */
You can’t perform that action at this time.
0 commit comments