xref: /csrg-svn/sys/stand/alloc.c (revision 63370)
163270Smckusick /*-
2*63370Sbostic  * Copyright (c) 1993
3*63370Sbostic  *	The Regents of the University of California.  All rights reserved.
463270Smckusick  *
563270Smckusick  * This code is derived from software contributed to Berkeley by
663270Smckusick  * The Mach Operating System project at Carnegie-Mellon University.
763270Smckusick  *
863270Smckusick  * %sccs.include.redist.c%
963270Smckusick  *
10*63370Sbostic  *	@(#)alloc.c	8.1 (Berkeley) 06/11/93
1163270Smckusick  *
1263270Smckusick  *
1363270Smckusick  * Copyright (c) 1989, 1990, 1991 Carnegie Mellon University
1463270Smckusick  * All Rights Reserved.
1563270Smckusick  *
1663270Smckusick  * Author: Alessandro Forin
1763270Smckusick  *
1863270Smckusick  * Permission to use, copy, modify and distribute this software and its
1963270Smckusick  * documentation is hereby granted, provided that both the copyright
2063270Smckusick  * notice and this permission notice appear in all copies of the
2163270Smckusick  * software, derivative works or modified versions, and any portions
2263270Smckusick  * thereof, and that both notices appear in supporting documentation.
2363270Smckusick  *
2463270Smckusick  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
2563270Smckusick  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
2663270Smckusick  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
2763270Smckusick  *
2863270Smckusick  * Carnegie Mellon requests users of this software to return to
2963270Smckusick  *
3063270Smckusick  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
3163270Smckusick  *  School of Computer Science
3263270Smckusick  *  Carnegie Mellon University
3363270Smckusick  *  Pittsburgh PA 15213-3890
3463270Smckusick  *
3563270Smckusick  * any improvements or extensions that they make and grant Carnegie the
3663270Smckusick  * rights to redistribute these changes.
3763270Smckusick  */
3863270Smckusick 
3963270Smckusick /*
4063270Smckusick  *	Dynamic memory allocator
4163270Smckusick  */
4263270Smckusick struct fl {
4363270Smckusick 	struct fl	*next;
4463270Smckusick 	unsigned	size;
4563270Smckusick } *freelist = (struct fl *)0;
4663270Smckusick 
4763270Smckusick extern char end[];
4863270Smckusick static char *top = end;
4963270Smckusick 
5063270Smckusick void *
alloc(size)5163270Smckusick alloc(size)
5263270Smckusick 	unsigned size;
5363270Smckusick {
5463270Smckusick 	register struct fl *f = freelist, **prev;
5563270Smckusick 
5663270Smckusick 	prev = &freelist;
5763270Smckusick 	while (f && f->size < size) {
5863270Smckusick 		prev = &f->next;
5963270Smckusick 		f = f->next;
6063270Smckusick 	}
6163270Smckusick 	if (f == (struct fl *)0) {
6263270Smckusick 		f = (struct fl *)top;
6363270Smckusick 		top += (size + 3) & ~3;
6463270Smckusick 	} else
6563270Smckusick 		*prev = f->next;
6663270Smckusick 	return ((void *)f);
6763270Smckusick }
6863270Smckusick 
6963270Smckusick void
free(ptr,size)7063270Smckusick free(ptr, size)
7163270Smckusick 	void *ptr;
7263270Smckusick 	unsigned size;
7363270Smckusick {
7463270Smckusick 	register struct fl *f = (struct fl *)ptr;
7563270Smckusick 
7663270Smckusick 	f->size = (size + 3) & ~3;
7763270Smckusick 	f->next = freelist;
7863270Smckusick 	freelist = f;
7963270Smckusick }
80