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