2008-11-19 13:26:10 +01:00
|
|
|
|
|
|
|
#define _SYSTEM 1
|
|
|
|
|
|
|
|
#include <minix/callnr.h>
|
|
|
|
#include <minix/com.h>
|
|
|
|
#include <minix/config.h>
|
|
|
|
#include <minix/const.h>
|
|
|
|
#include <minix/ds.h>
|
|
|
|
#include <minix/endpoint.h>
|
|
|
|
#include <minix/keymap.h>
|
|
|
|
#include <minix/minlib.h>
|
|
|
|
#include <minix/type.h>
|
|
|
|
#include <minix/ipc.h>
|
|
|
|
#include <minix/sysutil.h>
|
|
|
|
#include <minix/syslib.h>
|
2009-09-21 16:49:49 +02:00
|
|
|
#include <minix/bitmap.h>
|
|
|
|
#include <minix/debug.h>
|
2008-11-19 13:26:10 +01:00
|
|
|
|
2010-04-12 13:25:24 +02:00
|
|
|
#include <assert.h>
|
2008-11-19 13:26:10 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <env.h>
|
|
|
|
|
|
|
|
#include <memory.h>
|
|
|
|
|
|
|
|
#include "glo.h"
|
|
|
|
#include "proto.h"
|
|
|
|
#include "util.h"
|
|
|
|
#include "sanitycheck.h"
|
|
|
|
|
|
|
|
#define SLABSIZES 60
|
|
|
|
|
2009-09-21 16:49:49 +02:00
|
|
|
#define ITEMSPERPAGE(bytes) (DATABYTES / (bytes))
|
2008-11-19 13:26:10 +01:00
|
|
|
|
|
|
|
#define ELBITS (sizeof(element_t)*8)
|
|
|
|
#define BITPAT(b) (1UL << ((b) % ELBITS))
|
|
|
|
#define BITEL(f, b) (f)->sdh.usebits[(b)/ELBITS]
|
|
|
|
|
|
|
|
|
2010-04-12 14:37:28 +02:00
|
|
|
#define OFF(f, b) assert(!GETBIT(f, b))
|
|
|
|
#define ON(f, b) assert(GETBIT(f, b))
|
2008-11-19 13:26:10 +01:00
|
|
|
|
2009-09-21 16:49:49 +02:00
|
|
|
#if SANITYCHECKS
|
|
|
|
#define SLABDATAWRITABLE(data, wr) do { \
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(data->sdh.writable == WRITABLE_NONE); \
|
|
|
|
assert(wr != WRITABLE_NONE); \
|
2009-09-21 16:49:49 +02:00
|
|
|
vm_pagelock(data, 0); \
|
|
|
|
data->sdh.writable = wr; \
|
|
|
|
} while(0)
|
|
|
|
|
|
|
|
#define SLABDATAUNWRITABLE(data) do { \
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(data->sdh.writable != WRITABLE_NONE); \
|
2009-09-21 16:49:49 +02:00
|
|
|
data->sdh.writable = WRITABLE_NONE; \
|
|
|
|
vm_pagelock(data, 1); \
|
|
|
|
} while(0)
|
|
|
|
|
|
|
|
#define SLABDATAUSE(data, code) do { \
|
|
|
|
SLABDATAWRITABLE(data, WRITABLE_HEADER); \
|
|
|
|
code \
|
|
|
|
SLABDATAUNWRITABLE(data); \
|
|
|
|
} while(0)
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#define SLABDATAWRITABLE(data, wr)
|
|
|
|
#define SLABDATAUNWRITABLE(data)
|
|
|
|
#define SLABDATAUSE(data, code) do { code } while(0)
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2008-11-19 13:26:10 +01:00
|
|
|
#define GETBIT(f, b) (BITEL(f,b) & BITPAT(b))
|
2009-09-21 16:49:49 +02:00
|
|
|
#define SETBIT(f, b) {OFF(f,b); SLABDATAUSE(f, BITEL(f,b)|= BITPAT(b); (f)->sdh.nused++;); }
|
|
|
|
#define CLEARBIT(f, b) {ON(f, b); SLABDATAUSE(f, BITEL(f,b)&=~BITPAT(b); (f)->sdh.nused--; (f)->sdh.freeguess = (b);); }
|
2008-11-19 13:26:10 +01:00
|
|
|
|
|
|
|
#define MINSIZE 8
|
|
|
|
#define MAXSIZE (SLABSIZES-1+MINSIZE)
|
|
|
|
#define USEELEMENTS (1+(VM_PAGE_SIZE/MINSIZE/8))
|
|
|
|
|
|
|
|
PRIVATE int pages = 0;
|
|
|
|
|
|
|
|
typedef u8_t element_t;
|
|
|
|
#define BITS_FULL (~(element_t)0)
|
|
|
|
typedef element_t elements_t[USEELEMENTS];
|
|
|
|
|
|
|
|
/* This file is too low-level to have global SANITYCHECKs everywhere,
|
|
|
|
* as the (other) data structures are often necessarily in an
|
|
|
|
* inconsistent state during a slaballoc() / slabfree(). So only do
|
|
|
|
* our own sanity checks here, with SLABSANITYCHECK.
|
|
|
|
*/
|
2009-09-21 16:49:49 +02:00
|
|
|
|
|
|
|
|
|
|
|
/* Special writable values. */
|
|
|
|
#define WRITABLE_NONE -2
|
|
|
|
#define WRITABLE_HEADER -1
|
2008-11-19 13:26:10 +01:00
|
|
|
|
|
|
|
struct sdh {
|
|
|
|
#if SANITYCHECKS
|
2009-09-21 16:49:49 +02:00
|
|
|
u32_t magic1;
|
2008-11-19 13:26:10 +01:00
|
|
|
#endif
|
2009-09-21 16:49:49 +02:00
|
|
|
u8_t list;
|
|
|
|
u16_t nused; /* Number of data items used in this slab. */
|
2008-11-19 13:26:10 +01:00
|
|
|
int freeguess;
|
|
|
|
struct slabdata *next, *prev;
|
|
|
|
elements_t usebits;
|
|
|
|
phys_bytes phys;
|
2009-09-21 16:49:49 +02:00
|
|
|
#if SANITYCHECKS
|
|
|
|
int writable; /* data item number or WRITABLE_* */
|
|
|
|
u32_t magic2;
|
|
|
|
#endif
|
2008-11-19 13:26:10 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
#define DATABYTES (VM_PAGE_SIZE-sizeof(struct sdh))
|
|
|
|
|
2009-09-21 16:49:49 +02:00
|
|
|
#define MAGIC1 0x1f5b842f
|
|
|
|
#define MAGIC2 0x8bb5a420
|
2008-11-19 13:26:10 +01:00
|
|
|
#define JUNK 0xdeadbeef
|
|
|
|
#define NOJUNK 0xc0ffee
|
|
|
|
|
|
|
|
#define LIST_UNUSED 0
|
|
|
|
#define LIST_FREE 1
|
|
|
|
#define LIST_USED 2
|
|
|
|
#define LIST_FULL 3
|
|
|
|
#define LIST_NUMBER 4
|
|
|
|
|
|
|
|
PRIVATE struct slabheader {
|
|
|
|
struct slabdata {
|
|
|
|
struct sdh sdh;
|
|
|
|
u8_t data[DATABYTES];
|
|
|
|
} *list_head[LIST_NUMBER];
|
|
|
|
} slabs[SLABSIZES];
|
|
|
|
|
2009-04-22 14:39:29 +02:00
|
|
|
FORWARD _PROTOTYPE( int objstats, (void *, int, struct slabheader **, struct slabdata **, int *));
|
|
|
|
|
2008-11-19 13:26:10 +01:00
|
|
|
#define GETSLAB(b, s) { \
|
|
|
|
int i; \
|
2010-04-12 14:37:28 +02:00
|
|
|
assert((b) >= MINSIZE); \
|
2008-11-19 13:26:10 +01:00
|
|
|
i = (b) - MINSIZE; \
|
2010-04-12 14:37:28 +02:00
|
|
|
assert((i) < SLABSIZES); \
|
|
|
|
assert((i) >= 0); \
|
2008-11-19 13:26:10 +01:00
|
|
|
s = &slabs[i]; \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define LH(sl, l) (sl)->list_head[l]
|
|
|
|
|
2009-09-21 16:49:49 +02:00
|
|
|
/* move head of list l1 to list of l2 in slabheader sl. */
|
2008-11-19 13:26:10 +01:00
|
|
|
#define MOVEHEAD(sl, l1, l2) { \
|
|
|
|
struct slabdata *t; \
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(LH(sl,l1)); \
|
2008-11-19 13:26:10 +01:00
|
|
|
REMOVEHEAD(sl, l1, t); \
|
|
|
|
ADDHEAD(t, sl, l2); \
|
|
|
|
}
|
|
|
|
|
2009-09-21 16:49:49 +02:00
|
|
|
/* remove head of list 'list' in sl, assign it unlinked to 'to'. */
|
2008-11-19 13:26:10 +01:00
|
|
|
#define REMOVEHEAD(sl, list, to) { \
|
2009-09-21 16:49:49 +02:00
|
|
|
struct slabdata *dat; \
|
|
|
|
dat = (to) = LH(sl, list); \
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(dat); \
|
2009-09-21 16:49:49 +02:00
|
|
|
LH(sl, list) = dat->sdh.next; \
|
|
|
|
UNLINKNODE(dat); \
|
2008-11-19 13:26:10 +01:00
|
|
|
}
|
|
|
|
|
2009-09-21 16:49:49 +02:00
|
|
|
/* move slabdata nw to slabheader sl under list number l. */
|
2008-11-19 13:26:10 +01:00
|
|
|
#define ADDHEAD(nw, sl, l) { \
|
2009-09-21 16:49:49 +02:00
|
|
|
SLABDATAUSE(nw, \
|
|
|
|
(nw)->sdh.next = LH(sl, l); \
|
|
|
|
(nw)->sdh.prev = NULL; \
|
|
|
|
(nw)->sdh.list = l;); \
|
2008-11-19 13:26:10 +01:00
|
|
|
LH(sl, l) = (nw); \
|
2009-09-21 16:49:49 +02:00
|
|
|
if((nw)->sdh.next) { \
|
|
|
|
SLABDATAUSE((nw)->sdh.next, \
|
|
|
|
(nw)->sdh.next->sdh.prev = (nw);); \
|
|
|
|
} \
|
2008-11-19 13:26:10 +01:00
|
|
|
}
|
|
|
|
|
2009-09-21 16:49:49 +02:00
|
|
|
#define UNLINKNODE(node) { \
|
|
|
|
struct slabdata *next, *prev; \
|
|
|
|
prev = (node)->sdh.prev; \
|
|
|
|
next = (node)->sdh.next; \
|
|
|
|
if(prev) { SLABDATAUSE(prev, prev->sdh.next = next;); } \
|
|
|
|
if(next) { SLABDATAUSE(next, next->sdh.prev = prev;); } \
|
|
|
|
}
|
2008-11-19 13:26:10 +01:00
|
|
|
|
|
|
|
struct slabdata *newslabdata(int list)
|
|
|
|
{
|
|
|
|
struct slabdata *n;
|
|
|
|
phys_bytes p;
|
|
|
|
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(sizeof(*n) == VM_PAGE_SIZE);
|
2008-11-19 13:26:10 +01:00
|
|
|
|
2009-09-23 15:33:01 +02:00
|
|
|
if(!(n = vm_allocpage(&p, VMP_SLAB))) {
|
|
|
|
printf("newslabdata: vm_allocpage failed\n");
|
2008-11-19 13:26:10 +01:00
|
|
|
return NULL;
|
2009-09-23 15:33:01 +02:00
|
|
|
}
|
2008-11-19 13:26:10 +01:00
|
|
|
memset(n->sdh.usebits, 0, sizeof(n->sdh.usebits));
|
|
|
|
pages++;
|
|
|
|
|
|
|
|
n->sdh.phys = p;
|
|
|
|
#if SANITYCHECKS
|
2009-09-21 16:49:49 +02:00
|
|
|
n->sdh.magic1 = MAGIC1;
|
|
|
|
n->sdh.magic2 = MAGIC2;
|
2008-11-19 13:26:10 +01:00
|
|
|
#endif
|
|
|
|
n->sdh.nused = 0;
|
|
|
|
n->sdh.freeguess = 0;
|
|
|
|
n->sdh.list = list;
|
|
|
|
|
2009-09-21 16:49:49 +02:00
|
|
|
#if SANITYCHECKS
|
|
|
|
n->sdh.writable = WRITABLE_HEADER;
|
|
|
|
SLABDATAUNWRITABLE(n);
|
|
|
|
#endif
|
|
|
|
|
2008-11-19 13:26:10 +01:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if SANITYCHECKS
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* checklist *
|
|
|
|
*===========================================================================*/
|
|
|
|
PRIVATE int checklist(char *file, int line,
|
|
|
|
struct slabheader *s, int l, int bytes)
|
|
|
|
{
|
|
|
|
struct slabdata *n = s->list_head[l];
|
|
|
|
int ch = 0;
|
|
|
|
|
|
|
|
while(n) {
|
|
|
|
int count = 0, i;
|
2009-09-21 16:49:49 +02:00
|
|
|
MYASSERT(n->sdh.magic1 == MAGIC1);
|
|
|
|
MYASSERT(n->sdh.magic2 == MAGIC2);
|
2008-11-19 13:26:10 +01:00
|
|
|
MYASSERT(n->sdh.list == l);
|
2009-09-21 16:49:49 +02:00
|
|
|
MYASSERT(usedpages_add(n->sdh.phys, VM_PAGE_SIZE) == OK);
|
2008-11-19 13:26:10 +01:00
|
|
|
if(n->sdh.prev)
|
|
|
|
MYASSERT(n->sdh.prev->sdh.next == n);
|
|
|
|
else
|
|
|
|
MYASSERT(s->list_head[l] == n);
|
|
|
|
if(n->sdh.next) MYASSERT(n->sdh.next->sdh.prev == n);
|
|
|
|
for(i = 0; i < USEELEMENTS*8; i++)
|
2009-09-21 16:49:49 +02:00
|
|
|
if(i >= ITEMSPERPAGE(bytes))
|
2008-11-19 13:26:10 +01:00
|
|
|
MYASSERT(!GETBIT(n, i));
|
|
|
|
else
|
|
|
|
if(GETBIT(n,i))
|
|
|
|
count++;
|
|
|
|
MYASSERT(count == n->sdh.nused);
|
|
|
|
ch += count;
|
|
|
|
n = n->sdh.next;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ch;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* void slab_sanitycheck *
|
|
|
|
*===========================================================================*/
|
|
|
|
PUBLIC void slab_sanitycheck(char *file, int line)
|
|
|
|
{
|
|
|
|
int s;
|
|
|
|
for(s = 0; s < SLABSIZES; s++) {
|
|
|
|
int l;
|
|
|
|
for(l = 0; l < LIST_NUMBER; l++) {
|
|
|
|
checklist(file, line, &slabs[s], l, s + MINSIZE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-22 14:39:29 +02:00
|
|
|
/*===========================================================================*
|
|
|
|
* int slabsane *
|
|
|
|
*===========================================================================*/
|
2009-09-21 16:49:49 +02:00
|
|
|
PUBLIC int slabsane_f(char *file, int line, void *mem, int bytes)
|
2009-04-22 14:39:29 +02:00
|
|
|
{
|
|
|
|
struct slabheader *s;
|
|
|
|
struct slabdata *f;
|
|
|
|
int i;
|
2009-09-21 16:49:49 +02:00
|
|
|
|
2009-04-22 14:39:29 +02:00
|
|
|
return (objstats(mem, bytes, &s, &f, &i) == OK);
|
|
|
|
}
|
2008-11-19 13:26:10 +01:00
|
|
|
#endif
|
|
|
|
|
2009-09-21 16:49:49 +02:00
|
|
|
static int nojunkwarning = 0;
|
|
|
|
|
2008-11-19 13:26:10 +01:00
|
|
|
/*===========================================================================*
|
|
|
|
* void *slaballoc *
|
|
|
|
*===========================================================================*/
|
|
|
|
PUBLIC void *slaballoc(int bytes)
|
|
|
|
{
|
2009-09-21 16:49:49 +02:00
|
|
|
int i;
|
|
|
|
int count = 0;
|
2008-11-19 13:26:10 +01:00
|
|
|
struct slabheader *s;
|
|
|
|
struct slabdata *firstused;
|
|
|
|
|
|
|
|
SLABSANITYCHECK(SCL_FUNCTIONS);
|
|
|
|
|
|
|
|
/* Retrieve entry in slabs[]. */
|
|
|
|
GETSLAB(bytes, s);
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(s);
|
2008-11-19 13:26:10 +01:00
|
|
|
|
|
|
|
/* To make the common case more common, make space in the 'used'
|
|
|
|
* queue first.
|
|
|
|
*/
|
|
|
|
if(!LH(s, LIST_USED)) {
|
|
|
|
/* Make sure there is something on the freelist. */
|
|
|
|
SLABSANITYCHECK(SCL_DETAIL);
|
|
|
|
if(!LH(s, LIST_FREE)) {
|
2009-09-21 16:49:49 +02:00
|
|
|
struct slabdata *nd = newslabdata(LIST_FREE);
|
2008-11-19 13:26:10 +01:00
|
|
|
SLABSANITYCHECK(SCL_DETAIL);
|
2009-09-21 16:49:49 +02:00
|
|
|
if(!nd) return NULL;
|
|
|
|
ADDHEAD(nd, s, LIST_FREE);
|
2008-11-19 13:26:10 +01:00
|
|
|
SLABSANITYCHECK(SCL_DETAIL);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SLABSANITYCHECK(SCL_DETAIL);
|
|
|
|
MOVEHEAD(s, LIST_FREE, LIST_USED);
|
|
|
|
SLABSANITYCHECK(SCL_DETAIL);
|
|
|
|
|
|
|
|
}
|
|
|
|
SLABSANITYCHECK(SCL_DETAIL);
|
|
|
|
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(s);
|
2008-11-19 13:26:10 +01:00
|
|
|
firstused = LH(s, LIST_USED);
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(firstused);
|
2010-04-12 13:25:24 +02:00
|
|
|
#if SANITYCHECKS
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(firstused->sdh.magic1 == MAGIC1);
|
|
|
|
assert(firstused->sdh.magic2 == MAGIC2);
|
2010-04-12 13:25:24 +02:00
|
|
|
#endif
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(firstused->sdh.nused < ITEMSPERPAGE(bytes));
|
2008-11-19 13:26:10 +01:00
|
|
|
|
2009-09-21 16:49:49 +02:00
|
|
|
for(i = firstused->sdh.freeguess;
|
|
|
|
count < ITEMSPERPAGE(bytes); count++, i++) {
|
2008-11-19 13:26:10 +01:00
|
|
|
SLABSANITYCHECK(SCL_DETAIL);
|
2009-09-21 16:49:49 +02:00
|
|
|
i = i % ITEMSPERPAGE(bytes);
|
2008-11-19 13:26:10 +01:00
|
|
|
|
|
|
|
if(!GETBIT(firstused, i)) {
|
|
|
|
struct slabdata *f;
|
|
|
|
char *ret;
|
|
|
|
SETBIT(firstused, i);
|
|
|
|
SLABSANITYCHECK(SCL_DETAIL);
|
2009-09-21 16:49:49 +02:00
|
|
|
if(firstused->sdh.nused == ITEMSPERPAGE(bytes)) {
|
2008-11-19 13:26:10 +01:00
|
|
|
SLABSANITYCHECK(SCL_DETAIL);
|
|
|
|
MOVEHEAD(s, LIST_USED, LIST_FULL);
|
|
|
|
SLABSANITYCHECK(SCL_DETAIL);
|
|
|
|
}
|
|
|
|
SLABSANITYCHECK(SCL_DETAIL);
|
|
|
|
ret = ((char *) firstused->data) + i*bytes;
|
|
|
|
|
|
|
|
#if SANITYCHECKS
|
2009-09-21 16:49:49 +02:00
|
|
|
nojunkwarning++;
|
|
|
|
slabunlock(ret, bytes);
|
|
|
|
nojunkwarning--;
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(!nojunkwarning);
|
2008-11-19 13:26:10 +01:00
|
|
|
*(u32_t *) ret = NOJUNK;
|
2009-09-21 16:49:49 +02:00
|
|
|
slablock(ret, bytes);
|
2008-11-19 13:26:10 +01:00
|
|
|
#endif
|
|
|
|
SLABSANITYCHECK(SCL_FUNCTIONS);
|
2009-09-21 16:49:49 +02:00
|
|
|
SLABDATAUSE(firstused, firstused->sdh.freeguess = i+1;);
|
2009-04-22 14:39:29 +02:00
|
|
|
|
|
|
|
#if SANITYCHECKS
|
2009-09-21 16:49:49 +02:00
|
|
|
if(bytes >= SLABSIZES+MINSIZE) {
|
|
|
|
printf("slaballoc: odd, bytes %d?\n", bytes);
|
|
|
|
}
|
|
|
|
if(!slabsane_f(__FILE__, __LINE__, ret, bytes))
|
2010-03-05 16:05:11 +01:00
|
|
|
panic("slaballoc: slabsane failed");
|
2009-04-22 14:39:29 +02:00
|
|
|
#endif
|
|
|
|
|
2008-11-19 13:26:10 +01:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
SLABSANITYCHECK(SCL_DETAIL);
|
|
|
|
|
|
|
|
}
|
|
|
|
SLABSANITYCHECK(SCL_FUNCTIONS);
|
|
|
|
|
2010-03-05 16:05:11 +01:00
|
|
|
panic("slaballoc: no space in 'used' slabdata");
|
2008-11-19 13:26:10 +01:00
|
|
|
|
|
|
|
/* Not reached. */
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*
|
2009-04-22 14:39:29 +02:00
|
|
|
* int objstats *
|
2008-11-19 13:26:10 +01:00
|
|
|
*===========================================================================*/
|
2009-04-22 14:39:29 +02:00
|
|
|
PRIVATE int objstats(void *mem, int bytes,
|
|
|
|
struct slabheader **sp, struct slabdata **fp, int *ip)
|
2008-11-19 13:26:10 +01:00
|
|
|
{
|
2009-09-21 16:49:49 +02:00
|
|
|
#if SANITYCHECKS
|
2009-04-22 14:39:29 +02:00
|
|
|
#define OBJSTATSCHECK(cond) \
|
|
|
|
if(!(cond)) { \
|
2009-09-21 16:49:49 +02:00
|
|
|
printf("VM: objstats: %s failed for ptr 0x%p, %d bytes\n", \
|
2009-04-22 14:39:29 +02:00
|
|
|
#cond, mem, bytes); \
|
|
|
|
return EINVAL; \
|
|
|
|
}
|
2009-09-21 16:49:49 +02:00
|
|
|
#else
|
|
|
|
#define OBJSTATSCHECK(cond)
|
|
|
|
#endif
|
2009-04-22 14:39:29 +02:00
|
|
|
|
2008-11-19 13:26:10 +01:00
|
|
|
struct slabheader *s;
|
|
|
|
struct slabdata *f;
|
2009-04-22 14:39:29 +02:00
|
|
|
int i;
|
2008-11-19 13:26:10 +01:00
|
|
|
|
2009-04-22 14:39:29 +02:00
|
|
|
OBJSTATSCHECK((char *) mem >= (char *) VM_PAGE_SIZE);
|
2008-11-19 13:26:10 +01:00
|
|
|
|
|
|
|
#if SANITYCHECKS
|
2009-09-21 16:49:49 +02:00
|
|
|
if(*(u32_t *) mem == JUNK && !nojunkwarning) {
|
2009-04-22 14:39:29 +02:00
|
|
|
util_stacktrace();
|
|
|
|
printf("VM: WARNING: JUNK seen in slab object\n");
|
2008-11-19 13:26:10 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
/* Retrieve entry in slabs[]. */
|
|
|
|
GETSLAB(bytes, s);
|
|
|
|
|
|
|
|
/* Round address down to VM_PAGE_SIZE boundary to get header. */
|
|
|
|
f = (struct slabdata *) ((char *) mem - (vir_bytes) mem % VM_PAGE_SIZE);
|
|
|
|
|
2009-09-21 16:49:49 +02:00
|
|
|
OBJSTATSCHECK(f->sdh.magic1 == MAGIC1);
|
|
|
|
OBJSTATSCHECK(f->sdh.magic2 == MAGIC2);
|
2009-04-22 14:39:29 +02:00
|
|
|
OBJSTATSCHECK(f->sdh.list == LIST_USED || f->sdh.list == LIST_FULL);
|
2008-11-19 13:26:10 +01:00
|
|
|
|
|
|
|
/* Make sure it's in range. */
|
2009-04-22 14:39:29 +02:00
|
|
|
OBJSTATSCHECK((char *) mem >= (char *) f->data);
|
|
|
|
OBJSTATSCHECK((char *) mem < (char *) f->data + sizeof(f->data));
|
2008-11-19 13:26:10 +01:00
|
|
|
|
|
|
|
/* Get position. */
|
|
|
|
i = (char *) mem - (char *) f->data;
|
2009-04-22 14:39:29 +02:00
|
|
|
OBJSTATSCHECK(!(i % bytes));
|
2008-11-19 13:26:10 +01:00
|
|
|
i = i / bytes;
|
|
|
|
|
2009-04-22 14:39:29 +02:00
|
|
|
/* Make sure it is marked as allocated. */
|
|
|
|
OBJSTATSCHECK(GETBIT(f, i));
|
|
|
|
|
|
|
|
/* return values */
|
|
|
|
*ip = i;
|
|
|
|
*fp = f;
|
|
|
|
*sp = s;
|
|
|
|
|
|
|
|
return OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* void *slabfree *
|
|
|
|
*===========================================================================*/
|
|
|
|
PUBLIC void slabfree(void *mem, int bytes)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct slabheader *s;
|
|
|
|
struct slabdata *f;
|
|
|
|
|
|
|
|
SLABSANITYCHECK(SCL_FUNCTIONS);
|
|
|
|
|
2009-09-21 16:49:49 +02:00
|
|
|
if(objstats(mem, bytes, &s, &f, &i) != OK) {
|
2010-03-05 16:05:11 +01:00
|
|
|
panic("slabfree objstats failed");
|
2009-09-21 16:49:49 +02:00
|
|
|
}
|
|
|
|
|
2009-04-22 14:39:29 +02:00
|
|
|
#if SANITYCHECKS
|
|
|
|
if(*(u32_t *) mem == JUNK) {
|
|
|
|
printf("VM: WARNING: likely double free, JUNK seen\n");
|
|
|
|
}
|
2009-09-21 16:49:49 +02:00
|
|
|
|
|
|
|
slabunlock(mem, bytes);
|
|
|
|
*(u32_t *) mem = JUNK;
|
|
|
|
nojunkwarning++;
|
|
|
|
slablock(mem, bytes);
|
|
|
|
nojunkwarning--;
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(!nojunkwarning);
|
2009-04-22 14:39:29 +02:00
|
|
|
#endif
|
2008-11-19 13:26:10 +01:00
|
|
|
|
|
|
|
/* Free this data. */
|
|
|
|
CLEARBIT(f, i);
|
|
|
|
|
|
|
|
/* Check if this slab changes lists. */
|
|
|
|
if(f->sdh.nused == 0) {
|
|
|
|
/* Now become FREE; must've been USED */
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(f->sdh.list == LIST_USED);
|
2008-11-19 13:26:10 +01:00
|
|
|
UNLINKNODE(f);
|
|
|
|
if(f == LH(s, LIST_USED))
|
|
|
|
LH(s, LIST_USED) = f->sdh.next;
|
|
|
|
ADDHEAD(f, s, LIST_FREE);
|
|
|
|
SLABSANITYCHECK(SCL_DETAIL);
|
2009-09-21 16:49:49 +02:00
|
|
|
} else if(f->sdh.nused == ITEMSPERPAGE(bytes)-1) {
|
2008-11-19 13:26:10 +01:00
|
|
|
/* Now become USED; must've been FULL */
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(f->sdh.list == LIST_FULL);
|
2008-11-19 13:26:10 +01:00
|
|
|
UNLINKNODE(f);
|
|
|
|
if(f == LH(s, LIST_FULL))
|
|
|
|
LH(s, LIST_FULL) = f->sdh.next;
|
|
|
|
ADDHEAD(f, s, LIST_USED);
|
|
|
|
SLABSANITYCHECK(SCL_DETAIL);
|
|
|
|
} else {
|
|
|
|
/* Stay USED */
|
2010-04-12 14:37:28 +02:00
|
|
|
assert(f->sdh.list == LIST_USED);
|
2008-11-19 13:26:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
SLABSANITYCHECK(SCL_FUNCTIONS);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-09-21 16:49:49 +02:00
|
|
|
/*===========================================================================*
|
|
|
|
* void *slablock *
|
|
|
|
*===========================================================================*/
|
|
|
|
PUBLIC void slablock(void *mem, int bytes)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct slabheader *s;
|
|
|
|
struct slabdata *f;
|
|
|
|
|
|
|
|
if(objstats(mem, bytes, &s, &f, &i) != OK)
|
2010-03-05 16:05:11 +01:00
|
|
|
panic("slablock objstats failed");
|
2009-09-21 16:49:49 +02:00
|
|
|
|
|
|
|
SLABDATAUNWRITABLE(f);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*===========================================================================*
|
|
|
|
* void *slabunlock *
|
|
|
|
*===========================================================================*/
|
|
|
|
PUBLIC void slabunlock(void *mem, int bytes)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct slabheader *s;
|
|
|
|
struct slabdata *f;
|
|
|
|
|
|
|
|
if(objstats(mem, bytes, &s, &f, &i) != OK)
|
2010-03-05 16:05:11 +01:00
|
|
|
panic("slablock objstats failed");
|
2009-09-21 16:49:49 +02:00
|
|
|
|
|
|
|
SLABDATAWRITABLE(f, i);
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2008-11-19 13:26:10 +01:00
|
|
|
#if SANITYCHECKS
|
|
|
|
/*===========================================================================*
|
|
|
|
* void slabstats *
|
|
|
|
*===========================================================================*/
|
|
|
|
PUBLIC void slabstats(void)
|
|
|
|
{
|
|
|
|
int s, total = 0, totalbytes = 0;
|
|
|
|
static int n;
|
|
|
|
n++;
|
|
|
|
if(n%1000) return;
|
|
|
|
for(s = 0; s < SLABSIZES; s++) {
|
|
|
|
int l;
|
|
|
|
for(l = 0; l < LIST_NUMBER; l++) {
|
|
|
|
int b, t;
|
|
|
|
b = s + MINSIZE;
|
|
|
|
t = checklist(__FILE__, __LINE__, &slabs[s], l, b);
|
|
|
|
|
|
|
|
if(t > 0) {
|
|
|
|
int bytes = t * b;
|
|
|
|
printf("VMSTATS: %2d slabs: %d (%dkB)\n", b, t, bytes/1024);
|
|
|
|
totalbytes += bytes;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(pages > 0) {
|
|
|
|
printf("VMSTATS: %dK net used in slab objects in %d pages (%dkB): %d%% utilization\n",
|
|
|
|
totalbytes/1024, pages, pages*VM_PAGE_SIZE/1024,
|
|
|
|
100 * totalbytes / (pages*VM_PAGE_SIZE));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|