#include <sys/types.h>
#include <sys/queue.h>
#include <sys/bitstring.h>
#ifdef _KERNEL
#include <sys/param.h>
#include <sys/malloc.h>
#include <sys/kernel.h>
#include <sys/systm.h>
#include <sys/limits.h>
#include <sys/lock.h>
#include <sys/proc.h>
static MALLOC_DEFINE(M_UNIT, "Unitno", "Unit number allocation");
#define Malloc(foo) kmalloc(foo, M_UNIT, M_WAITOK | M_ZERO)
#define Free(foo) kfree(foo, M_UNIT)
static struct lock unit_lock;
LOCK_SYSINIT(unit, &unit_lock, "unit# allocation", LK_CANRECURSE);
#else
#endif
struct unr {
TAILQ_ENTRY(unr) list;
u_int len;
void *ptr;
};
struct unrb {
u_char busy;
bitstr_t map[sizeof(struct unr) - 1];
};
CTASSERT(sizeof(struct unr) == sizeof(struct unrb));
#define NBITS ((int)sizeof(((struct unrb *)NULL)->map) * 8)
struct unrhdr {
TAILQ_HEAD(unrhd,unr) head;
u_int low;
u_int high;
u_int busy;
u_int alloc;
u_int first;
u_int last;
struct lock *lock;
};
#if defined(DIAGNOSTIC) || !defined(_KERNEL)
static void
check_unrhdr(struct unrhdr *uh, int line)
{
struct unr *up;
struct unrb *ub;
u_int x, y, z, w;
y = uh->first;
z = 0;
TAILQ_FOREACH(up, &uh->head, list) {
z++;
if (up->ptr != uh && up->ptr != NULL) {
ub = up->ptr;
KASSERT (up->len <= NBITS,
("UNR inconsistency: len %u max %d (line %d)\n",
up->len, NBITS, line));
z++;
w = 0;
for (x = 0; x < up->len; x++)
if (bit_test(ub->map, x))
w++;
KASSERT (w == ub->busy,
("UNR inconsistency: busy %u found %u (line %d)\n",
ub->busy, w, line));
y += w;
} else if (up->ptr != NULL)
y += up->len;
}
KASSERT (y == uh->busy,
("UNR inconsistency: items %u found %u (line %d)\n",
uh->busy, y, line));
KASSERT (z == uh->alloc,
("UNR inconsistency: chunks %u found %u (line %d)\n",
uh->alloc, z, line));
}
#else
static __inline void
check_unrhdr(struct unrhdr *uh, int line)
{
}
#endif
static __inline void *
new_unr(struct unrhdr *uh, void **p1, void **p2)
{
void *p;
uh->alloc++;
KASSERT(*p1 != NULL || *p2 != NULL, ("Out of cached memory"));
if (*p1 != NULL) {
p = *p1;
*p1 = NULL;
return (p);
} else {
p = *p2;
*p2 = NULL;
return (p);
}
}
static __inline void
delete_unr(struct unrhdr *uh, void *ptr)
{
uh->alloc--;
Free(ptr);
}
struct unrhdr *
new_unrhdr(int low, int high, struct lock *lock)
{
struct unrhdr *uh;
KASSERT(low <= high,
("UNR: use error: new_unrhdr(%u, %u)", low, high));
uh = Malloc(sizeof *uh);
if (lock != NULL)
uh->lock = lock;
else
uh->lock = &unit_lock;
TAILQ_INIT(&uh->head);
uh->low = low;
uh->high = high;
uh->first = 0;
uh->last = 1 + (high - low);
check_unrhdr(uh, __LINE__);
return (uh);
}
void
delete_unrhdr(struct unrhdr *uh)
{
check_unrhdr(uh, __LINE__);
KASSERT(uh->busy == 0, ("unrhdr has %u allocations", uh->busy));
KASSERT(uh->alloc == 0, ("UNR memory leak in delete_unrhdr"));
Free(uh);
}
static __inline int
is_bitmap(struct unrhdr *uh, struct unr *up)
{
return (up->ptr != uh && up->ptr != NULL);
}
static int
optimize_unr(struct unrhdr *uh)
{
struct unr *up, *uf, *us;
struct unrb *ub, *ubf;
u_int a, l, ba;
us = NULL;
ba = 0;
TAILQ_FOREACH(uf, &uh->head, list) {
if (uf->len >= NBITS)
continue;
a = 1;
if (is_bitmap(uh, uf))
a++;
l = uf->len;
up = uf;
while (1) {
up = TAILQ_NEXT(up, list);
if (up == NULL)
break;
if ((up->len + l) > NBITS)
break;
a++;
if (is_bitmap(uh, up))
a++;
l += up->len;
}
if (a > ba) {
ba = a;
us = uf;
}
}
if (ba < 3)
return (0);
if (!is_bitmap(uh, us)) {
uf = TAILQ_NEXT(us, list);
TAILQ_REMOVE(&uh->head, us, list);
a = us->len;
l = us->ptr == uh ? 1 : 0;
ub = (void *)us;
ub->busy = 0;
if (l) {
bit_nset(ub->map, 0, a);
ub->busy += a;
} else {
bit_nclear(ub->map, 0, a);
}
if (!is_bitmap(uh, uf)) {
if (uf->ptr == NULL) {
bit_nclear(ub->map, a, a + uf->len - 1);
} else {
bit_nset(ub->map, a, a + uf->len - 1);
ub->busy += uf->len;
}
uf->ptr = ub;
uf->len += a;
us = uf;
} else {
ubf = uf->ptr;
for (l = 0; l < uf->len; l++, a++) {
if (bit_test(ubf->map, l)) {
bit_set(ub->map, a);
ub->busy++;
} else {
bit_clear(ub->map, a);
}
}
uf->len = a;
delete_unr(uh, uf->ptr);
uf->ptr = ub;
us = uf;
}
}
ub = us->ptr;
while (1) {
uf = TAILQ_NEXT(us, list);
if (uf == NULL)
return (1);
if (uf->len + us->len > NBITS)
return (1);
if (uf->ptr == NULL) {
bit_nclear(ub->map, us->len, us->len + uf->len - 1);
us->len += uf->len;
TAILQ_REMOVE(&uh->head, uf, list);
delete_unr(uh, uf);
} else if (uf->ptr == uh) {
bit_nset(ub->map, us->len, us->len + uf->len - 1);
ub->busy += uf->len;
us->len += uf->len;
TAILQ_REMOVE(&uh->head, uf, list);
delete_unr(uh, uf);
} else {
ubf = uf->ptr;
for (l = 0; l < uf->len; l++, us->len++) {
if (bit_test(ubf->map, l)) {
bit_set(ub->map, us->len);
ub->busy++;
} else {
bit_clear(ub->map, us->len);
}
}
TAILQ_REMOVE(&uh->head, uf, list);
delete_unr(uh, ubf);
delete_unr(uh, uf);
}
}
}
static void
collapse_unr(struct unrhdr *uh, struct unr *up)
{
struct unr *upp;
struct unrb *ub;
if (is_bitmap(uh, up)) {
ub = up->ptr;
if (ub->busy == up->len) {
delete_unr(uh, up->ptr);
up->ptr = uh;
} else if (ub->busy == 0) {
delete_unr(uh, up->ptr);
up->ptr = NULL;
}
}
if (up->len == 0) {
upp = TAILQ_PREV(up, unrhd, list);
if (upp == NULL)
upp = TAILQ_NEXT(up, list);
TAILQ_REMOVE(&uh->head, up, list);
delete_unr(uh, up);
up = upp;
}
if (up != NULL) {
upp = TAILQ_PREV(up, unrhd, list);
if (upp != NULL && up->ptr == upp->ptr) {
up->len += upp->len;
TAILQ_REMOVE(&uh->head, upp, list);
delete_unr(uh, upp);
}
upp = TAILQ_NEXT(up, list);
if (upp != NULL && up->ptr == upp->ptr) {
up->len += upp->len;
TAILQ_REMOVE(&uh->head, upp, list);
delete_unr(uh, upp);
}
}
upp = TAILQ_FIRST(&uh->head);
if (upp != NULL && upp->ptr == uh) {
uh->first += upp->len;
TAILQ_REMOVE(&uh->head, upp, list);
delete_unr(uh, upp);
if (up == upp)
up = NULL;
}
upp = TAILQ_LAST(&uh->head, unrhd);
if (upp != NULL && upp->ptr == NULL) {
uh->last += upp->len;
TAILQ_REMOVE(&uh->head, upp, list);
delete_unr(uh, upp);
if (up == upp)
up = NULL;
}
while (optimize_unr(uh))
continue;
}
int
alloc_unrl(struct unrhdr *uh)
{
struct unr *up;
struct unrb *ub;
u_int x;
int y;
KKASSERT(lockowned(uh->lock));
check_unrhdr(uh, __LINE__);
x = uh->low + uh->first;
up = TAILQ_FIRST(&uh->head);
if (up == NULL && uh->last > 0) {
uh->first++;
uh->last--;
uh->busy++;
return (x);
}
if (up == NULL)
return (-1);
KASSERT(up->ptr != uh, ("UNR first element is allocated"));
if (up->ptr == NULL) {
uh->first++;
up->len--;
} else {
ub = up->ptr;
KASSERT(ub->busy < up->len, ("UNR bitmap confusion"));
bit_ffc(ub->map, up->len, &y);
KASSERT(y != -1, ("UNR corruption: No clear bit in bitmap."));
bit_set(ub->map, y);
ub->busy++;
x += y;
}
uh->busy++;
collapse_unr(uh, up);
return (x);
}
int
alloc_unr(struct unrhdr *uh)
{
int i;
lockmgr(uh->lock, LK_EXCLUSIVE);
i = alloc_unrl(uh);
lockmgr(uh->lock, LK_RELEASE);
return (i);
}
static void
free_unrl(struct unrhdr *uh, u_int item, void **p1, void **p2)
{
struct unr *up, *upp, *upn;
struct unrb *ub;
u_int pl;
KASSERT(item >= uh->low && item <= uh->high,
("UNR: free_unr(%u) out of range [%u...%u]",
item, uh->low, uh->high));
check_unrhdr(uh, __LINE__);
item -= uh->low;
upp = TAILQ_FIRST(&uh->head);
if (item + 1 == uh->first && upp == NULL) {
uh->last++;
uh->first--;
uh->busy--;
check_unrhdr(uh, __LINE__);
return;
}
if (item < uh->first) {
up = new_unr(uh, p1, p2);
up->ptr = uh;
up->len = uh->first - item;
TAILQ_INSERT_HEAD(&uh->head, up, list);
uh->first -= up->len;
}
item -= uh->first;
TAILQ_FOREACH(up, &uh->head, list) {
if (up->len > item)
break;
item -= up->len;
}
if (is_bitmap(uh, up)) {
ub = up->ptr;
KASSERT(bit_test(ub->map, item) != 0,
("UNR: Freeing free item %d (bitmap)\n", item));
bit_clear(ub->map, item);
uh->busy--;
ub->busy--;
collapse_unr(uh, up);
return;
}
KASSERT(up->ptr == uh, ("UNR Freeing free item %d (run))\n", item));
if (up->len == 1) {
up->ptr = NULL;
uh->busy--;
collapse_unr(uh, up);
return;
}
upp = TAILQ_PREV(up, unrhd, list);
if (item == 0 && upp != NULL && upp->ptr == NULL) {
upp->len++;
up->len--;
uh->busy--;
collapse_unr(uh, up);
return;
}
upn = TAILQ_NEXT(up, list);
if (item == up->len - 1 && upn != NULL && upn->ptr == NULL) {
upn->len++;
up->len--;
uh->busy--;
collapse_unr(uh, up);
return;
}
pl = up->len - (1 + item);
if (pl > 0) {
upp = new_unr(uh, p1, p2);
upp->ptr = uh;
upp->len = pl;
TAILQ_INSERT_AFTER(&uh->head, up, upp, list);
}
if (item > 0) {
upp = new_unr(uh, p1, p2);
upp->len = item;
upp->ptr = uh;
TAILQ_INSERT_BEFORE(up, upp, list);
}
up->len = 1;
up->ptr = NULL;
uh->busy--;
collapse_unr(uh, up);
}
void
free_unr(struct unrhdr *uh, u_int item)
{
void *p1, *p2;
p1 = Malloc(sizeof(struct unr));
p2 = Malloc(sizeof(struct unr));
lockmgr(uh->lock, LK_EXCLUSIVE);
free_unrl(uh, item, &p1, &p2);
lockmgr(uh->lock, LK_RELEASE);
if (p1 != NULL)
Free(p1);
if (p2 != NULL)
Free(p2);
}
#ifndef _KERNEL
static void
print_unr(struct unrhdr *uh, struct unr *up)
{
u_int x;
struct unrb *ub;
printf(" %p len = %5u ", up, up->len);
if (up->ptr == NULL)
printf("free\n");
else if (up->ptr == uh)
printf("alloc\n");
else {
ub = up->ptr;
printf("bitmap(%d) [", ub->busy);
for (x = 0; x < up->len; x++) {
if (bit_test(ub->map, x))
printf("#");
else
printf(" ");
}
printf("]\n");
}
}
static void
print_unrhdr(struct unrhdr *uh)
{
struct unr *up;
u_int x;
printf(
"%p low = %u high = %u first = %u last = %u busy %u chunks = %u\n",
uh, uh->low, uh->high, uh->first, uh->last, uh->busy, uh->alloc);
x = uh->low + uh->first;
TAILQ_FOREACH(up, &uh->head, list) {
printf(" from = %5u", x);
print_unr(uh, up);
if (up->ptr == NULL || up->ptr == uh)
x += up->len;
else
x += NBITS;
}
}
#define NN 10000
int
main(int argc __unused, const char **argv __unused)
{
struct unrhdr *uh;
u_int i, x, m, j;
char a[NN];
setbuf(stdout, NULL);
uh = new_unrhdr(0, NN - 1, NULL);
print_unrhdr(uh);
memset(a, 0, sizeof a);
fprintf(stderr, "sizeof(struct unr) %d\n", sizeof (struct unr));
fprintf(stderr, "sizeof(struct unrb) %d\n", sizeof (struct unrb));
fprintf(stderr, "sizeof(struct unrhdr) %d\n", sizeof (struct unrhdr));
fprintf(stderr, "NBITS %d\n", NBITS);
x = 1;
for (m = 0; m < NN * 100; m++) {
j = random();
i = (j >> 1) % NN;
#if 0
if (a[i] && (j & 1))
continue;
#endif
if (a[i]) {
printf("F %u\n", i);
free_unr(uh, i);
a[i] = 0;
} else {
no_alloc = 1;
i = alloc_unr(uh);
if (i != -1) {
a[i] = 1;
printf("A %u\n", i);
}
no_alloc = 0;
}
if (1)
print_unrhdr(uh);
check_unrhdr(uh, __LINE__);
}
for (i = 0; i < NN; i++) {
if (a[i]) {
printf("C %u\n", i);
free_unr(uh, i);
print_unrhdr(uh);
}
}
print_unrhdr(uh);
delete_unrhdr(uh);
return (0);
}
#endif