1 /* $NetBSD: stalloc.c,v 1.9 2003/07/15 01:19:46 lukem 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.9 2003/07/15 01:19:46 lukem 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() 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(size, phys_addr) 81 u_long size; 82 void **phys_addr; 83 { 84 struct mem_node *mn, *new, *bfit; 85 int s; 86 87 if (size == 0) 88 return NULL; 89 90 s = splhigh(); 91 92 if (size & ~(ST_BLOCKMASK)) 93 size = (size & ST_BLOCKMASK) + ST_BLOCKSIZE; 94 95 /* 96 * walk list of available nodes, finding the best-fit. 97 */ 98 bfit = NULL; 99 mn = free_list.cqh_first; 100 for(; mn != (void *)&free_list; mn = mn->free_link.cqe_next) { 101 if(size <= mn->size) { 102 if((bfit != NULL) && (bfit->size < mn->size)) 103 continue; 104 bfit = mn; 105 } 106 } 107 if(bfit != NULL) 108 mn = bfit; 109 if (mn == (void *)&free_list) { 110 printf("St-mem pool exhausted, binpatch 'st_pool_size'" 111 "to get more\n"); 112 splx(s); 113 return(NULL); 114 } 115 116 if ((mn->size - size) <= sizeof (*mn)) { 117 /* 118 * our allocation would not leave room 119 * for a new node in between. 120 */ 121 CIRCLEQ_REMOVE(&free_list, mn, free_link); 122 mn->free_link.cqe_next = NULL; 123 size = mn->size; /* increase size. (or same) */ 124 stmem_total -= mn->size; 125 splx(s); 126 *phys_addr = (void*)PHYS_ADDR(&mn[1]); 127 return ((void *)&mn[1]); 128 } 129 130 /* 131 * split the node's memory. 132 */ 133 new = mn; 134 new->size -= size + sizeof(struct mem_node); 135 mn = (struct mem_node *)(MNODES_MEM(new) + new->size); 136 mn->size = size; 137 138 /* 139 * add split node to node list 140 * and mark as not on free list 141 */ 142 CIRCLEQ_INSERT_AFTER(&st_list, new, mn, link); 143 mn->free_link.cqe_next = NULL; 144 145 stmem_total -= size + sizeof(struct mem_node); 146 splx(s); 147 *phys_addr = (void*)PHYS_ADDR(&mn[1]); 148 return ((void *)&mn[1]); 149 } 150 151 void 152 free_stmem(mem) 153 void *mem; 154 { 155 struct mem_node *mn, *next, *prev; 156 int s; 157 158 if (mem == NULL) 159 return; 160 161 s = splhigh(); 162 mn = (struct mem_node *)mem - 1; 163 next = mn->link.cqe_next; 164 prev = mn->link.cqe_prev; 165 166 /* 167 * check ahead of us. 168 */ 169 if (next != (void *)&st_list && next->free_link.cqe_next) { 170 /* 171 * if next is: a valid node and a free node. ==> merge 172 */ 173 CIRCLEQ_INSERT_BEFORE(&free_list, next, mn, free_link); 174 CIRCLEQ_REMOVE(&st_list, next, link); 175 CIRCLEQ_REMOVE(&st_list, next, free_link); 176 stmem_total += mn->size + sizeof(struct mem_node); 177 mn->size += next->size + sizeof(struct mem_node); 178 } 179 if (prev != (void *)&st_list && prev->free_link.cqe_prev) { 180 /* 181 * if prev is: a valid node and a free node. ==> merge 182 */ 183 if (mn->free_link.cqe_next == NULL) 184 stmem_total += mn->size + sizeof(struct mem_node); 185 else { 186 /* already on free list */ 187 CIRCLEQ_REMOVE(&free_list, mn, free_link); 188 stmem_total += sizeof(struct mem_node); 189 } 190 CIRCLEQ_REMOVE(&st_list, mn, link); 191 prev->size += mn->size + sizeof(struct mem_node); 192 } else if (mn->free_link.cqe_next == NULL) { 193 /* 194 * we still are not on free list and we need to be. 195 * <-- | --> 196 */ 197 while (next != (void *)&st_list && prev != (void *)&st_list) { 198 if (next->free_link.cqe_next) { 199 CIRCLEQ_INSERT_BEFORE(&free_list, next, mn, 200 free_link); 201 break; 202 } 203 if (prev->free_link.cqe_next) { 204 CIRCLEQ_INSERT_AFTER(&free_list, prev, mn, 205 free_link); 206 break; 207 } 208 prev = prev->link.cqe_prev; 209 next = next->link.cqe_next; 210 } 211 if (mn->free_link.cqe_next == NULL) { 212 if (next == (void *)&st_list) { 213 /* 214 * we are not on list so we can add 215 * ourselves to the tail. (we walked to it.) 216 */ 217 CIRCLEQ_INSERT_TAIL(&free_list,mn,free_link); 218 } else { 219 CIRCLEQ_INSERT_HEAD(&free_list,mn,free_link); 220 } 221 } 222 stmem_total += mn->size;/* add our helpings to the pool. */ 223 } 224 splx(s); 225 } 226