Skip to content

matras: introduce the matras_alloc_reserve function #103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions include/small/matras.h
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,13 @@ matras_dealloc_range(struct matras *m, matras_id_t range_count);
int
matras_touch_reserve(struct matras *m, int count);

/**
* Reserve the max amount of extents required to successfully allocate @p count
* blocks. The extents are reserved in the allocator given on construction.
*/
int
matras_alloc_reserve(struct matras *m, int count);

/**
* Convert block id into block address.
*/
Expand Down
2 changes: 2 additions & 0 deletions include/small/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@
# define lengthof(array) (sizeof (array) / sizeof ((array)[0]))
#endif

#define SMALL_DIV_ROUND_UP(a, b) ((a) + (b) - 1) / (b)

#define small_xmalloc(size) \
({ \
void *ret = malloc(size); \
Expand Down
26 changes: 26 additions & 0 deletions small/matras.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#pragma intrinsic (_BitScanReverse)
#endif

#include "util.h"

/**
* Dummy thread-local matras_stats struct used if matras_stats wasn't
* passed to matras_create().
Expand Down Expand Up @@ -364,6 +366,30 @@ matras_touch_reserve(struct matras *m, int count)
return matras_allocator_reserve(m->allocator, max_extents_required);
}

/**
* Reserve the max amount of extents required to successfully allocate @p count
* blocks. The extents are reserved in the allocator given on construction.
*/
int
matras_alloc_reserve(struct matras *m, int count)
{
assert(count >= 0);

/* No allocations planned. */
if (count == 0)
return 0;

/*
* This reserves up to 3 extents more than required, but it should be OK
* since the reserved memory is generic for all matras_allocator users.
*/
int l3_count = SMALL_DIV_ROUND_UP(m->block_size * count,
m->allocator->extent_size);
int l2_count = SMALL_DIV_ROUND_UP(l3_count * sizeof(void *),
m->allocator->extent_size);
return matras_allocator_reserve(m->allocator, l3_count + l2_count + 1);
}

/**
* Return the number of allocated extents (of size m->extent_size each)
*/
Expand Down