xref: /csrg-svn/sys/stand/alloc.c (revision 63270)
1*63270Smckusick /*-
2*63270Smckusick  * Copyright (c) 1993 The Regents of the University of California.
3*63270Smckusick  * All rights reserved.
4*63270Smckusick  *
5*63270Smckusick  * This code is derived from software contributed to Berkeley by
6*63270Smckusick  * The Mach Operating System project at Carnegie-Mellon University.
7*63270Smckusick  *
8*63270Smckusick  * %sccs.include.redist.c%
9*63270Smckusick  *
10*63270Smckusick  *	@(#)alloc.c	7.1 (Berkeley) 06/11/93
11*63270Smckusick  *
12*63270Smckusick  *
13*63270Smckusick  * Copyright (c) 1989, 1990, 1991 Carnegie Mellon University
14*63270Smckusick  * All Rights Reserved.
15*63270Smckusick  *
16*63270Smckusick  * Author: Alessandro Forin
17*63270Smckusick  *
18*63270Smckusick  * Permission to use, copy, modify and distribute this software and its
19*63270Smckusick  * documentation is hereby granted, provided that both the copyright
20*63270Smckusick  * notice and this permission notice appear in all copies of the
21*63270Smckusick  * software, derivative works or modified versions, and any portions
22*63270Smckusick  * thereof, and that both notices appear in supporting documentation.
23*63270Smckusick  *
24*63270Smckusick  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
25*63270Smckusick  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR
26*63270Smckusick  * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
27*63270Smckusick  *
28*63270Smckusick  * Carnegie Mellon requests users of this software to return to
29*63270Smckusick  *
30*63270Smckusick  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
31*63270Smckusick  *  School of Computer Science
32*63270Smckusick  *  Carnegie Mellon University
33*63270Smckusick  *  Pittsburgh PA 15213-3890
34*63270Smckusick  *
35*63270Smckusick  * any improvements or extensions that they make and grant Carnegie the
36*63270Smckusick  * rights to redistribute these changes.
37*63270Smckusick  */
38*63270Smckusick 
39*63270Smckusick /*
40*63270Smckusick  *	Dynamic memory allocator
41*63270Smckusick  */
42*63270Smckusick struct fl {
43*63270Smckusick 	struct fl	*next;
44*63270Smckusick 	unsigned	size;
45*63270Smckusick } *freelist = (struct fl *)0;
46*63270Smckusick 
47*63270Smckusick extern char end[];
48*63270Smckusick static char *top = end;
49*63270Smckusick 
50*63270Smckusick void *
51*63270Smckusick alloc(size)
52*63270Smckusick 	unsigned size;
53*63270Smckusick {
54*63270Smckusick 	register struct fl *f = freelist, **prev;
55*63270Smckusick 
56*63270Smckusick 	prev = &freelist;
57*63270Smckusick 	while (f && f->size < size) {
58*63270Smckusick 		prev = &f->next;
59*63270Smckusick 		f = f->next;
60*63270Smckusick 	}
61*63270Smckusick 	if (f == (struct fl *)0) {
62*63270Smckusick 		f = (struct fl *)top;
63*63270Smckusick 		top += (size + 3) & ~3;
64*63270Smckusick 	} else
65*63270Smckusick 		*prev = f->next;
66*63270Smckusick 	return ((void *)f);
67*63270Smckusick }
68*63270Smckusick 
69*63270Smckusick void
70*63270Smckusick free(ptr, size)
71*63270Smckusick 	void *ptr;
72*63270Smckusick 	unsigned size;
73*63270Smckusick {
74*63270Smckusick 	register struct fl *f = (struct fl *)ptr;
75*63270Smckusick 
76*63270Smckusick 	f->size = (size + 3) & ~3;
77*63270Smckusick 	f->next = freelist;
78*63270Smckusick 	freelist = f;
79*63270Smckusick }
80