xref: /netbsd-src/sys/arch/atari/atari/stalloc.c (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1 /*	$NetBSD: stalloc.c,v 1.16 2014/01/03 07:14:20 mlelstv 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.16 2014/01/03 07:14:20 mlelstv 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 TAILQ_HEAD(stlist, mem_node) st_list;
57 static TAILQ_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 	TAILQ_INIT(&st_list);
72 	TAILQ_INIT(&free_list);
73 
74 	TAILQ_INSERT_HEAD(&st_list, mem, link);
75 	TAILQ_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 	TAILQ_FOREACH(mn, &free_list, free_link) {
98 		if (size <= mn->size) {
99 			if ((bfit != NULL) &&
100 			    (bfit->size < mn->size))
101 				continue;
102 			bfit = mn;
103 		}
104 	}
105 	if (bfit != NULL)
106 		mn = bfit;
107 	if (mn == NULL) {
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 		TAILQ_REMOVE(&free_list, mn, free_link);
120 		mn->type = MNODE_USED;
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 	TAILQ_INSERT_AFTER(&st_list, new, mn, link);
141 	mn->type = MNODE_USED;
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 = TAILQ_NEXT(mn, link);
161 	prev = TAILQ_PREV(mn, stlist, link);
162 
163 	/*
164 	 * check ahead of us.
165 	 */
166 	if (next->type == MNODE_FREE) {
167 		/*
168 		 * if next is: a valid node and a free node. ==> merge
169 		 */
170 		TAILQ_INSERT_BEFORE(next, mn, free_link);
171 		mn->type = MNODE_FREE;
172 		TAILQ_REMOVE(&st_list, next, link);
173 		TAILQ_REMOVE(&free_list, next, free_link);
174 		stmem_total += mn->size + sizeof(struct mem_node);
175 		mn->size += next->size + sizeof(struct mem_node);
176 	}
177 	if (prev->type == MNODE_FREE) {
178 		/*
179 		 * if prev is: a valid node and a free node. ==> merge
180 		 */
181 		if (mn->type != MNODE_FREE)
182 			stmem_total += mn->size + sizeof(struct mem_node);
183 		else {
184 			/* already on free list */
185 			TAILQ_REMOVE(&free_list, mn, free_link);
186 			mn->type = MNODE_USED;
187 			stmem_total += sizeof(struct mem_node);
188 		}
189 		TAILQ_REMOVE(&st_list, mn, link);
190 		prev->size += mn->size + sizeof(struct mem_node);
191 	} else if (mn->type != MNODE_FREE) {
192 		/*
193 		 * we still are not on free list and we need to be.
194 		 * <-- | -->
195 		 */
196 		while (next != NULL && prev != NULL) {
197 			if (next->type == MNODE_FREE) {
198 				TAILQ_INSERT_BEFORE(next, mn, free_link);
199 				mn->type = MNODE_FREE;
200 				break;
201 			}
202 			if (prev->type == MNODE_FREE) {
203 				TAILQ_INSERT_AFTER(&free_list, prev, mn,
204 				    free_link);
205 				mn->type = MNODE_FREE;
206 				break;
207 			}
208 			prev = TAILQ_PREV(prev, stlist, link);
209 			next = TAILQ_NEXT(next, link);
210 		}
211 		if (mn->type != MNODE_FREE) {
212 			if (next == NULL) {
213 				/*
214 				 * we are not on list so we can add
215 				 * ourselves to the tail. (we walked to it.)
216 				 */
217 				TAILQ_INSERT_TAIL(&free_list,mn,free_link);
218 			} else {
219 				TAILQ_INSERT_HEAD(&free_list,mn,free_link);
220 			}
221 				mn->type = MNODE_FREE;
222 		}
223 		stmem_total += mn->size;/* add our helpings to the pool. */
224 	}
225 	splx(s);
226 }
227