1 /* $NetBSD: stalloc.c,v 1.8 1996/10/13 04:10:43 christos 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/types.h> 37 #include <sys/param.h> 38 #include <sys/systm.h> 39 #include <sys/queue.h> 40 41 #include <atari/atari/stalloc.h> 42 43 /* 44 * St-mem allocator. 45 */ 46 /* 47 * From atari_init.c 48 */ 49 extern u_long st_pool_size, st_pool_virt, st_pool_phys; 50 51 #define PHYS_ADDR(virt) ((u_long)(virt) - st_pool_virt + st_pool_phys) 52 53 static CIRCLEQ_HEAD(stlist, mem_node) st_list; 54 static CIRCLEQ_HEAD(freelist, mem_node) free_list; 55 u_long stmem_total; /* total free. */ 56 57 void 58 init_stmem() 59 { 60 int s = splhigh (); 61 struct mem_node *mem; 62 63 stmem_total = st_pool_size - sizeof(*mem); 64 65 mem = (struct mem_node *)st_pool_virt; 66 mem->size = st_pool_size - sizeof(*mem); 67 68 CIRCLEQ_INIT(&st_list); 69 CIRCLEQ_INIT(&free_list); 70 71 CIRCLEQ_INSERT_HEAD(&st_list, mem, link); 72 CIRCLEQ_INSERT_HEAD(&free_list, mem, free_link); 73 splx(s); 74 } 75 76 void * 77 alloc_stmem(size, phys_addr) 78 u_long size; 79 void **phys_addr; 80 { 81 struct mem_node *mn, *new, *bfit; 82 int s; 83 84 if (size == 0) 85 return NULL; 86 87 s = splhigh(); 88 89 if (size & ~(ST_BLOCKMASK)) 90 size = (size & ST_BLOCKMASK) + ST_BLOCKSIZE; 91 92 /* 93 * walk list of available nodes, finding the best-fit. 94 */ 95 bfit = NULL; 96 mn = free_list.cqh_first; 97 for(; mn != (void *)&free_list; mn = mn->free_link.cqe_next) { 98 if(size <= mn->size) { 99 if((bfit != NULL) && (bfit->size < mn->size)) 100 continue; 101 bfit = mn; 102 } 103 } 104 if(bfit != NULL) 105 mn = bfit; 106 if (mn == (void *)&free_list) { 107 printf("St-mem pool exhausted, binpatch 'st_pool_size'" 108 "to get more\n"); 109 splx(s); 110 return(NULL); 111 } 112 113 if ((mn->size - size) <= sizeof (*mn)) { 114 /* 115 * our allocation would not leave room 116 * for a new node in between. 117 */ 118 CIRCLEQ_REMOVE(&free_list, mn, free_link); 119 mn->free_link.cqe_next = NULL; 120 size = mn->size; /* increase size. (or same) */ 121 stmem_total -= mn->size; 122 splx(s); 123 *phys_addr = (void*)PHYS_ADDR(&mn[1]); 124 return ((void *)&mn[1]); 125 } 126 127 /* 128 * split the node's memory. 129 */ 130 new = mn; 131 new->size -= size + sizeof(struct mem_node); 132 mn = (struct mem_node *)(MNODES_MEM(new) + new->size); 133 mn->size = size; 134 135 /* 136 * add split node to node list 137 * and mark as not on free list 138 */ 139 CIRCLEQ_INSERT_AFTER(&st_list, new, mn, link); 140 mn->free_link.cqe_next = NULL; 141 142 stmem_total -= size + sizeof(struct mem_node); 143 splx(s); 144 *phys_addr = (void*)PHYS_ADDR(&mn[1]); 145 return ((void *)&mn[1]); 146 } 147 148 void 149 free_stmem(mem) 150 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