Home | History | Annotate | Line # | Download | only in ksh
alloc.c revision 1.3
      1  1.3  christos /*	$NetBSD: alloc.c,v 1.3 1997/07/20 17:41:59 christos Exp $	*/
      2  1.2       tls 
      3  1.1       jtc /*
      4  1.1       jtc  * area-based allocation built on malloc/free
      5  1.1       jtc  */
      6  1.1       jtc 
      7  1.1       jtc #include "sh.h"
      8  1.1       jtc #ifdef MEM_DEBUG
      9  1.1       jtc # undef alloc
     10  1.1       jtc # undef aresize
     11  1.1       jtc # undef afree
     12  1.1       jtc #endif /* MEM_DEBUG */
     13  1.1       jtc 
     14  1.1       jtc #define	ICELLS	100		/* number of Cells in small Block */
     15  1.1       jtc 
     16  1.1       jtc typedef union Cell Cell;
     17  1.1       jtc typedef struct Block Block;
     18  1.1       jtc 
     19  1.1       jtc /*
     20  1.1       jtc  * The Cells in a Block are organized as a set of objects.
     21  1.1       jtc  * Each object (pointed to by dp) begins with a size in (dp-1)->size,
     22  1.1       jtc  * followed with "size" data Cells.  Free objects are
     23  1.1       jtc  * linked together via dp->next.
     24  1.1       jtc  */
     25  1.1       jtc 
     26  1.1       jtc union Cell {
     27  1.1       jtc 	size_t	size;
     28  1.1       jtc 	Cell   *next;
     29  1.1       jtc 	struct {int _;} junk;	/* alignment */
     30  1.1       jtc };
     31  1.1       jtc 
     32  1.1       jtc struct Block {
     33  1.1       jtc 	Block  *next;		/* list of Blocks in Area */
     34  1.1       jtc 	Cell   *freelist;	/* object free list */
     35  1.1       jtc 	Cell   *last;		/* &b.cell[size] */
     36  1.1       jtc 	Cell	cell [1];	/* [size] Cells for allocation */
     37  1.1       jtc };
     38  1.1       jtc 
     39  1.1       jtc static Block aempty = {&aempty, aempty.cell, aempty.cell};
     40  1.1       jtc 
     41  1.1       jtc /* create empty Area */
     42  1.1       jtc Area *
     43  1.1       jtc ainit(ap)
     44  1.1       jtc 	register Area *ap;
     45  1.1       jtc {
     46  1.1       jtc 	ap->freelist = &aempty;
     47  1.1       jtc 	return ap;
     48  1.1       jtc }
     49  1.1       jtc 
     50  1.1       jtc /* free all object in Area */
     51  1.1       jtc void
     52  1.1       jtc afreeall(ap)
     53  1.1       jtc 	register Area *ap;
     54  1.1       jtc {
     55  1.1       jtc 	register Block *bp;
     56  1.1       jtc 	register Block *tmp;
     57  1.1       jtc 
     58  1.1       jtc 	bp = ap->freelist;
     59  1.1       jtc 	if (bp != NULL && bp != &aempty) {
     60  1.1       jtc 		do {
     61  1.1       jtc 			tmp = bp->next;
     62  1.1       jtc 			free((void*)bp);
     63  1.1       jtc 			bp = tmp;
     64  1.1       jtc 		} while (bp != ap->freelist);
     65  1.1       jtc 		ap->freelist = &aempty;
     66  1.1       jtc 	}
     67  1.1       jtc }
     68  1.1       jtc 
     69  1.1       jtc /* allocate object from Area */
     70  1.1       jtc void *
     71  1.1       jtc alloc(size, ap)
     72  1.1       jtc 	size_t size;
     73  1.1       jtc 	register Area *ap;
     74  1.1       jtc {
     75  1.1       jtc 	int cells, split;
     76  1.1       jtc 	register Block *bp;
     77  1.1       jtc 	register Cell *dp, *fp, *fpp;
     78  1.1       jtc 
     79  1.1       jtc 	if (size <= 0) {
     80  1.1       jtc 		aerror(ap, "allocate bad size");
     81  1.1       jtc 		return NULL;
     82  1.1       jtc 	}
     83  1.1       jtc 	cells = (unsigned)(size - 1) / sizeof(Cell) + 1;
     84  1.1       jtc 
     85  1.1       jtc 	/* find Cell large enough */
     86  1.1       jtc 	for (bp = ap->freelist; ; bp = bp->next) {
     87  1.1       jtc 		for (fpp = NULL, fp = bp->freelist;
     88  1.1       jtc 		     fp != bp->last; fpp = fp, fp = fpp->next)
     89  1.1       jtc 			if ((fp-1)->size >= cells)
     90  1.1       jtc 				goto Found;
     91  1.1       jtc 
     92  1.1       jtc 		/* wrapped around Block list, create new Block */
     93  1.1       jtc 		if (bp->next == ap->freelist) {
     94  1.1       jtc 			bp = (Block*) malloc(offsetof(Block, cell[ICELLS])
     95  1.1       jtc 					     + sizeof(bp->cell[0]) * cells);
     96  1.1       jtc 			if (bp == NULL) {
     97  1.1       jtc 				aerror(ap, "cannot allocate");
     98  1.1       jtc 				return NULL;
     99  1.1       jtc 			}
    100  1.1       jtc 			if (ap->freelist == &aempty)
    101  1.1       jtc 				bp->next = bp;
    102  1.1       jtc 			else {
    103  1.1       jtc 				bp->next = ap->freelist->next;
    104  1.1       jtc 				ap->freelist->next = bp;
    105  1.1       jtc 			}
    106  1.1       jtc 			bp->last = bp->cell + ICELLS + cells;
    107  1.1       jtc 			fp = bp->freelist = bp->cell + 1; /* initial free list */
    108  1.1       jtc 			(fp-1)->size = ICELLS + cells - 1;
    109  1.1       jtc 			fp->next = bp->last;
    110  1.1       jtc 			fpp = NULL;
    111  1.1       jtc 			break;
    112  1.1       jtc 		}
    113  1.1       jtc 	}
    114  1.1       jtc   Found:
    115  1.1       jtc 	ap->freelist = bp;
    116  1.1       jtc 	dp = fp;		/* allocated object */
    117  1.1       jtc 	split = (dp-1)->size - cells;
    118  1.1       jtc 	if (split < 0)
    119  1.1       jtc 		aerror(ap, "allocated object too small");
    120  1.1       jtc 	if (--split <= 0) {	/* allocate all */
    121  1.1       jtc 		fp = fp->next;
    122  1.1       jtc 	} else {		/* allocate head, free tail */
    123  1.1       jtc 		(fp-1)->size = cells;
    124  1.1       jtc 		fp += cells + 1;
    125  1.1       jtc 		(fp-1)->size = split;
    126  1.1       jtc 		fp->next = dp->next;
    127  1.1       jtc 	}
    128  1.1       jtc 	if (fpp == NULL)
    129  1.1       jtc 		bp->freelist = fp;
    130  1.1       jtc 	else
    131  1.1       jtc 		fpp->next = fp;
    132  1.1       jtc 	return (void*) dp;
    133  1.1       jtc }
    134  1.1       jtc 
    135  1.1       jtc /* change size of object -- like realloc */
    136  1.1       jtc void *
    137  1.1       jtc aresize(ptr, size, ap)
    138  1.1       jtc 	register void *ptr;
    139  1.1       jtc 	size_t size;
    140  1.1       jtc 	Area *ap;
    141  1.1       jtc {
    142  1.1       jtc 	int cells;
    143  1.1       jtc 	register Cell *dp = (Cell*) ptr;
    144  1.1       jtc 
    145  1.1       jtc 	if (size <= 0) {
    146  1.1       jtc 		aerror(ap, "allocate bad size");
    147  1.1       jtc 		return NULL;
    148  1.1       jtc 	}
    149  1.1       jtc 	cells = (unsigned)(size - 1) / sizeof(Cell) + 1;
    150  1.1       jtc 
    151  1.1       jtc 	if (dp == NULL || (dp-1)->size < cells) { /* enlarge object */
    152  1.1       jtc 		/* XXX check for available adjacent free block */
    153  1.1       jtc 		ptr = alloc(size, ap);
    154  1.1       jtc 		if (dp != NULL) {
    155  1.1       jtc 			memcpy(ptr, dp, (dp-1)->size * sizeof(Cell));
    156  1.1       jtc 			afree((void *) dp, ap);
    157  1.1       jtc 		}
    158  1.1       jtc 	} else {		/* shrink object */
    159  1.1       jtc 		int split;
    160  1.1       jtc 
    161  1.1       jtc 		split = (dp-1)->size - cells;
    162  1.1       jtc 		if (--split <= 0) /* cannot split */
    163  1.1       jtc 			;
    164  1.1       jtc 		else {		/* shrink head, free tail */
    165  1.1       jtc 			(dp-1)->size = cells;
    166  1.1       jtc 			dp += cells + 1;
    167  1.1       jtc 			(dp-1)->size = split;
    168  1.1       jtc 			afree((void*)dp, ap);
    169  1.1       jtc 		}
    170  1.1       jtc 	}
    171  1.1       jtc 	return (void*) ptr;
    172  1.1       jtc }
    173  1.1       jtc 
    174  1.1       jtc void
    175  1.1       jtc afree(ptr, ap)
    176  1.1       jtc 	void *ptr;
    177  1.1       jtc 	register Area *ap;
    178  1.1       jtc {
    179  1.1       jtc 	register Block *bp;
    180  1.1       jtc 	register Cell *fp, *fpp;
    181  1.1       jtc 	register Cell *dp = (Cell*)ptr;
    182  1.1       jtc 
    183  1.1       jtc 	/* find Block containing Cell */
    184  1.1       jtc 	for (bp = ap->freelist; ; bp = bp->next) {
    185  1.1       jtc 		if (bp->cell <= dp && dp < bp->last)
    186  1.1       jtc 			break;
    187  1.1       jtc 		if (bp->next == ap->freelist) {
    188  1.1       jtc 			aerror(ap, "freeing with invalid area");
    189  1.1       jtc 			return;
    190  1.1       jtc 		}
    191  1.1       jtc 	}
    192  1.1       jtc 
    193  1.1       jtc 	/* find position in free list */
    194  1.1       jtc 	for (fpp = NULL, fp = bp->freelist; fp < dp; fpp = fp, fp = fpp->next)
    195  1.1       jtc 		;
    196  1.1       jtc 
    197  1.1       jtc 	if (fp == dp) {
    198  1.1       jtc 		aerror(ap, "freeing free object");
    199  1.1       jtc 		return;
    200  1.1       jtc 	}
    201  1.1       jtc 
    202  1.1       jtc 	/* join object with next */
    203  1.1       jtc 	if (dp + (dp-1)->size == fp-1) { /* adjacent */
    204  1.1       jtc 		(dp-1)->size += (fp-1)->size + 1;
    205  1.1       jtc 		dp->next = fp->next;
    206  1.1       jtc 	} else			/* non-adjacent */
    207  1.1       jtc 		dp->next = fp;
    208  1.1       jtc 
    209  1.1       jtc 	/* join previous with object */
    210  1.1       jtc 	if (fpp == NULL)
    211  1.1       jtc 		bp->freelist = dp;
    212  1.1       jtc 	else if (fpp + (fpp-1)->size == dp-1) { /* adjacent */
    213  1.1       jtc 		(fpp-1)->size += (dp-1)->size + 1;
    214  1.1       jtc 		fpp->next = dp->next;
    215  1.1       jtc 	} else			/* non-adjacent */
    216  1.1       jtc 		fpp->next = dp;
    217  1.1       jtc }
    218  1.1       jtc 
    219  1.1       jtc #if DEBUG_ALLOC
    220  1.1       jtc void
    221  1.1       jtc aprint(ap, ptr, size)
    222  1.1       jtc 	register Area *ap;
    223  1.1       jtc 	void *ptr;
    224  1.1       jtc 	size_t size;
    225  1.1       jtc {
    226  1.1       jtc 	Block *bp;
    227  1.1       jtc 
    228  1.1       jtc 	if (!ap)
    229  1.1       jtc 		shellf("aprint: null area pointer\n");
    230  1.1       jtc 	else if (!(bp = ap->freelist))
    231  1.1       jtc 		shellf("aprint: null area freelist\n");
    232  1.1       jtc 	else if (bp == &aempty)
    233  1.1       jtc 		shellf("aprint: area is empty\n");
    234  1.1       jtc 	else {
    235  1.1       jtc 		int i;
    236  1.1       jtc 		Cell *fp;
    237  1.1       jtc 
    238  1.1       jtc 		for (i = 0; !i || bp != ap->freelist; bp = bp->next, i++) {
    239  1.1       jtc 			if (ptr) {
    240  1.1       jtc 				void *eptr = (void *) (((char *) ptr) + size);
    241  1.1       jtc 				/* print block only if it overlaps ptr/size */
    242  1.1       jtc 				if (!((ptr >= (void *) bp
    243  1.1       jtc 				       && ptr <= (void *) bp->last)
    244  1.1       jtc 				      || (eptr >= (void *) bp
    245  1.1       jtc 				         && eptr <= (void *) bp->last)))
    246  1.1       jtc 					continue;
    247  1.1       jtc 				shellf("aprint: overlap of 0x%p .. 0x%p\n",
    248  1.1       jtc 					ptr, eptr);
    249  1.1       jtc 			}
    250  1.1       jtc 			shellf("aprint: block %2d: 0x%p .. 0x%p (%d)\n", i,
    251  1.1       jtc 				bp->cell, bp->last,
    252  1.1       jtc 				(char *) bp->last - (char *) bp->cell);
    253  1.1       jtc 			for (fp = bp->freelist; fp != bp->last; fp = fp->next)
    254  1.1       jtc 				shellf(
    255  1.1       jtc 				    "aprint:   0x%p .. 0x%p (%d) free\n",
    256  1.1       jtc 					(fp-1), (fp-1) + (fp-1)->size,
    257  1.1       jtc 					(fp-1)->size * sizeof(Cell));
    258  1.1       jtc 		}
    259  1.1       jtc 	}
    260  1.1       jtc }
    261  1.1       jtc #endif /* DEBUG_ALLOC */
    262  1.1       jtc 
    263  1.1       jtc 
    264  1.1       jtc #ifdef TEST_ALLOC
    265  1.1       jtc 
    266  1.1       jtc Area a;
    267  1.1       jtc 
    268  1.1       jtc main(int argc, char **argv) {
    269  1.1       jtc 	int i;
    270  1.1       jtc 	char *p [9];
    271  1.1       jtc 
    272  1.1       jtc 	ainit(&a);
    273  1.1       jtc 	for (i = 0; i < 9; i++) {
    274  1.1       jtc 		p[i] = alloc(124, &a);
    275  1.1       jtc 		printf("alloc: %x\n", p[i]);
    276  1.1       jtc 	}
    277  1.1       jtc 	for (i = 1; i < argc; i++)
    278  1.1       jtc 		afree(p[atoi(argv[i])], &a);
    279  1.1       jtc 	afreeall(&a);
    280  1.1       jtc 	return 0;
    281  1.1       jtc }
    282  1.1       jtc 
    283  1.1       jtc void aerror(Area *ap, const char *msg) {
    284  1.1       jtc 	abort();
    285  1.1       jtc }
    286  1.1       jtc 
    287  1.1       jtc #endif
    288  1.1       jtc 
    289