xref: /netbsd/sys/arch/atari/atari/stalloc.c (revision 6550d01e)
1 /*	$NetBSD: stalloc.c,v 1.13 2010/04/13 11:22:22 tsutsui Exp $	*/
2 
3 /*
4  * Copyright (c) 1995 Leo Weppelman (Atari modifications)
5  * Copyright (c) 1994 Christian E. Hopps (allocator stuff)
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *	This product includes software developed by the University of
18  *	California, Berkeley and its contributors.
19  * 4. Neither the name of the University nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: stalloc.c,v 1.13 2010/04/13 11:22:22 tsutsui Exp $");
38 
39 #include <sys/types.h>
40 #include <sys/param.h>
41 #include <sys/systm.h>
42 #include <sys/queue.h>
43 
44 #include <atari/atari/stalloc.h>
45 
46 /*
47  * St-mem allocator.
48  */
49 /*
50  * From atari_init.c
51  */
52 extern u_long st_pool_size, st_pool_virt, st_pool_phys;
53 
54 #define	PHYS_ADDR(virt)	((u_long)(virt) - st_pool_virt + st_pool_phys)
55 
56 static CIRCLEQ_HEAD(stlist, mem_node) st_list;
57 static CIRCLEQ_HEAD(freelist, mem_node) free_list;
58 u_long   stmem_total;		/* total free.		*/
59 
60 void
61 init_stmem(void)
62 {
63 	int s = splhigh ();
64 	struct mem_node *mem;
65 
66 	stmem_total = st_pool_size - sizeof(*mem);
67 
68 	mem = (struct mem_node *)st_pool_virt;
69 	mem->size = st_pool_size - sizeof(*mem);
70 
71 	CIRCLEQ_INIT(&st_list);
72 	CIRCLEQ_INIT(&free_list);
73 
74 	CIRCLEQ_INSERT_HEAD(&st_list, mem, link);
75 	CIRCLEQ_INSERT_HEAD(&free_list, mem, free_link);
76 	splx(s);
77 }
78 
79 void *
80 alloc_stmem(u_long size, void **phys_addr)
81 {
82 	struct mem_node *mn, *new, *bfit;
83 	int		s;
84 
85 	if (size == 0)
86 		return NULL;
87 
88 	s = splhigh();
89 
90 	if (size & ~(ST_BLOCKMASK))
91 		size = (size & ST_BLOCKMASK) + ST_BLOCKSIZE;
92 
93 	/*
94 	 * walk list of available nodes, finding the best-fit.
95 	 */
96 	bfit = NULL;
97 	mn   = free_list.cqh_first;
98 	for (; mn != (void *)&free_list; mn = mn->free_link.cqe_next) {
99 		if (size <= mn->size) {
100 			if ((bfit != NULL) && (bfit->size < mn->size))
101 				continue;
102 			bfit = mn;
103 		}
104 	}
105 	if (bfit != NULL)
106 		mn = bfit;
107 	if (mn == (void *)&free_list) {
108 		printf("St-mem pool exhausted, binpatch 'st_pool_size'"
109 			"to get more\n");
110 		splx(s);
111 		return(NULL);
112 	}
113 
114 	if ((mn->size - size) <= sizeof (*mn)) {
115 		/*
116 		 * our allocation would not leave room
117 		 * for a new node in between.
118 		 */
119 		CIRCLEQ_REMOVE(&free_list, mn, free_link);
120 		mn->free_link.cqe_next = NULL;
121 		size = mn->size;	 /* increase size. (or same) */
122 		stmem_total -= mn->size;
123 		splx(s);
124 		*phys_addr = (void*)PHYS_ADDR(&mn[1]);
125 		return ((void *)&mn[1]);
126 	}
127 
128 	/*
129 	 * split the node's memory.
130 	 */
131 	new = mn;
132 	new->size -= size + sizeof(struct mem_node);
133 	mn = (struct mem_node *)(MNODES_MEM(new) + new->size);
134 	mn->size = size;
135 
136 	/*
137 	 * add split node to node list
138 	 * and mark as not on free list
139 	 */
140 	CIRCLEQ_INSERT_AFTER(&st_list, new, mn, link);
141 	mn->free_link.cqe_next = NULL;
142 
143 	stmem_total -= size + sizeof(struct mem_node);
144 	splx(s);
145 	*phys_addr = (void*)PHYS_ADDR(&mn[1]);
146 	return ((void *)&mn[1]);
147 }
148 
149 void
150 free_stmem(void *mem)
151 {
152 	struct mem_node *mn, *next, *prev;
153 	int		s;
154 
155 	if (mem == NULL)
156 		return;
157 
158 	s = splhigh();
159 	mn = (struct mem_node *)mem - 1;
160 	next = mn->link.cqe_next;
161 	prev = mn->link.cqe_prev;
162 
163 	/*
164 	 * check ahead of us.
165 	 */
166 	if (next != (void *)&st_list && next->free_link.cqe_next) {
167 		/*
168 		 * if next is: a valid node and a free node. ==> merge
169 		 */
170 		CIRCLEQ_INSERT_BEFORE(&free_list, next, mn, free_link);
171 		CIRCLEQ_REMOVE(&st_list, next, link);
172 		CIRCLEQ_REMOVE(&st_list, next, free_link);
173 		stmem_total += mn->size + sizeof(struct mem_node);
174 		mn->size += next->size + sizeof(struct mem_node);
175 	}
176 	if (prev != (void *)&st_list && prev->free_link.cqe_prev) {
177 		/*
178 		 * if prev is: a valid node and a free node. ==> merge
179 		 */
180 		if (mn->free_link.cqe_next == NULL)
181 			stmem_total += mn->size + sizeof(struct mem_node);
182 		else {
183 			/* already on free list */
184 			CIRCLEQ_REMOVE(&free_list, mn, free_link);
185 			stmem_total += sizeof(struct mem_node);
186 		}
187 		CIRCLEQ_REMOVE(&st_list, mn, link);
188 		prev->size += mn->size + sizeof(struct mem_node);
189 	} else if (mn->free_link.cqe_next == NULL) {
190 		/*
191 		 * we still are not on free list and we need to be.
192 		 * <-- | -->
193 		 */
194 		while (next != (void *)&st_list && prev != (void *)&st_list) {
195 			if (next->free_link.cqe_next) {
196 				CIRCLEQ_INSERT_BEFORE(&free_list, next, mn,
197 				    free_link);
198 				break;
199 			}
200 			if (prev->free_link.cqe_next) {
201 				CIRCLEQ_INSERT_AFTER(&free_list, prev, mn,
202 				    free_link);
203 				break;
204 			}
205 			prev = prev->link.cqe_prev;
206 			next = next->link.cqe_next;
207 		}
208 		if (mn->free_link.cqe_next == NULL) {
209 			if (next == (void *)&st_list) {
210 				/*
211 				 * we are not on list so we can add
212 				 * ourselves to the tail. (we walked to it.)
213 				 */
214 				CIRCLEQ_INSERT_TAIL(&free_list,mn,free_link);
215 			} else {
216 				CIRCLEQ_INSERT_HEAD(&free_list,mn,free_link);
217 			}
218 		}
219 		stmem_total += mn->size;/* add our helpings to the pool. */
220 	}
221 	splx(s);
222 }
223