Add cramlimit function

parent db13fe36
...@@ -194,6 +194,27 @@ npages(size_t size, unsigned bits) ...@@ -194,6 +194,27 @@ npages(size_t size, unsigned bits)
return (s); return (s);
} }
/*
* limit a cram value such that an allocation returned by buddy can
* not be smaller than 1<<bits
*/
int8_t
BUDDYF(cramlimit_bits)(size_t sz, int8_t cram, int8_t bits)
{
int8_t up;
assert(sz >= (size_t)1 << bits);
if (cram == 0)
return (cram);
up = (int8_t)(uint8_t)log2up(sz);
assert(up >= bits);
up -= bits;
if (abs(cram) <= up)
return (cram);
return (cram < 0 ? 0 - up : up);
}
/************************************************************ /************************************************************
* init free map * init free map
* *
...@@ -2030,6 +2051,40 @@ BUDDYF(return)(struct buddy_returns *rets) ...@@ -2030,6 +2051,40 @@ BUDDYF(return)(struct buddy_returns *rets)
/************************************************************ /************************************************************
* tests * tests
*/ */
static void
t_cramlimit(void)
{
const int8_t min = 12;
int8_t cram, t, bits, l;
size_t sz;
for (cram = -10; cram <= 10; cram++) {
for (bits = min; bits < 20; bits++) {
// valid cram is bits - min
sz = (size_t)1 << bits;
t = BUDDYF(cramlimit_bits)(sz, cram, min);
l = bits - min;
assert(abs(t) <= l);
assert(t == 0 || (t < 0) == (cram < 0));
// test round up from pow2 + 1
sz++;
l++;
t = BUDDYF(cramlimit_bits)(sz, cram, min);
assert(abs(t) <= l);
assert(t == 0 || (t < 0) == (cram < 0));
// test round up from (pow2 + 1) - 1
sz = (size_t)1 << (bits + 1);
sz--;
t = BUDDYF(cramlimit_bits)(sz, cram, min);
assert(abs(t) <= l);
assert(t == 0 || (t < 0) == (cram < 0));
}
}
}
int t[] = { int t[] = {
-1, -1,
255, 255,
...@@ -2732,6 +2787,7 @@ t_buddy(size_t free) ...@@ -2732,6 +2787,7 @@ t_buddy(size_t free)
int main(void) int main(void)
{ {
t_cramlimit();
t_bitf(); t_bitf();
t_buddy(1 << 15); t_buddy(1 << 15);
t_buddy((1 << 15) - 1); t_buddy((1 << 15) - 1);
......
...@@ -86,6 +86,7 @@ typedef struct BUDDY buddy_t; ...@@ -86,6 +86,7 @@ typedef struct BUDDY buddy_t;
*/ */
void * BUDDYF(mmap)(size_t *, void *); void * BUDDYF(mmap)(size_t *, void *);
void BUDDYF(unmap)(void **, size_t, void **); void BUDDYF(unmap)(void **, size_t, void **);
int8_t BUDDYF(cramlimit_bits)(size_t, int8_t, int8_t);
static inline int static inline int
abslimit(int val, int lim) abslimit(int val, int lim)
{ {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment