1 /* $NetBSD: stalloc.c,v 1.1.1.1 1995/03/26 07:12:21 leo 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/queue.h> 39 40 #include <atari/atari/misc.h> 41 42 /* 43 * St-mem allocator. 44 */ 45 /* 46 * From atari_init.c 47 */ 48 extern u_long st_pool_size, st_pool_virt, st_pool_phys; 49 50 #define PHYS_ADDR(virt) ((u_long)(virt) - st_pool_virt + st_pool_phys) 51 52 static CIRCLEQ_HEAD(stlist, mem_node) st_list; 53 static CIRCLEQ_HEAD(freelist, mem_node) free_list; 54 static u_long stmem_total; /* total free. */ 55 56 void 57 init_stmem() 58 { 59 int s = splhigh (); 60 struct mem_node *mem; 61 62 stmem_total = st_pool_size - sizeof(*mem); 63 64 mem = (struct mem_node *)st_pool_virt; 65 mem->size = st_pool_size - sizeof(*mem); 66 67 CIRCLEQ_INIT(&st_list); 68 CIRCLEQ_INIT(&free_list); 69 70 CIRCLEQ_INSERT_HEAD(&st_list, mem, link); 71 CIRCLEQ_INSERT_HEAD(&free_list, mem, free_link); 72 splx(s); 73 } 74 75 void * 76 alloc_stmem(size, phys_addr) 77 u_long size; 78 void **phys_addr; 79 { 80 struct mem_node *mn, *new; 81 void *mem; 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. 94 */ 95 mn = free_list.cqh_first; 96 while (size > mn->size && mn != (void *)&free_list) 97 mn = mn->free_link.cqe_next; 98 99 if (mn == (void *)&free_list) { 100 printf("St-mem pool exhausted, binpatch 'st_pool_size'" 101 "to get more\n"); 102 return(NULL); 103 } 104 105 if ((mn->size - size) <= sizeof (*mn)) { 106 /* 107 * our allocation would not leave room 108 * for a new node in between. 109 */ 110 CIRCLEQ_REMOVE(&free_list, mn, free_link); 111 mn->free_link.cqe_next = NULL; 112 size = mn->size; /* increase size. (or same) */ 113 stmem_total -= mn->size; 114 splx(s); 115 *phys_addr = (void*)PHYS_ADDR(&mn[1]); 116 return ((void *)&mn[1]); 117 } 118 119 /* 120 * split the node's memory. 121 */ 122 new = mn; 123 new->size -= size + sizeof(struct mem_node); 124 mn = (struct mem_node *)(MNODES_MEM(new) + new->size); 125 mn->size = size; 126 127 /* 128 * add split node to node list 129 * and mark as not on free list 130 */ 131 CIRCLEQ_INSERT_AFTER(&st_list, new, mn, link); 132 mn->free_link.cqe_next = NULL; 133 134 stmem_total -= size + sizeof(struct mem_node); 135 splx(s); 136 *phys_addr = (void*)PHYS_ADDR(&mn[1]); 137 return ((void *)&mn[1]); 138 } 139 140 void 141 free_stmem(mem) 142 void *mem; 143 { 144 struct mem_node *mn, *next, *prev; 145 int s; 146 147 if (mem == NULL) 148 return; 149 150 s = splhigh(); 151 mn = (struct mem_node *)mem - 1; 152 next = mn->link.cqe_next; 153 prev = mn->link.cqe_prev; 154 155 /* 156 * check ahead of us. 157 */ 158 if (next->link.cqe_next != (void *)&st_list && 159 next->free_link.cqe_next) { 160 /* 161 * if next is: a valid node and a free node. ==> merge 162 */ 163 CIRCLEQ_INSERT_BEFORE(&free_list, next, mn, free_link); 164 CIRCLEQ_REMOVE(&st_list, next, link); 165 CIRCLEQ_REMOVE(&st_list, next, free_link); 166 stmem_total += mn->size + sizeof(struct mem_node); 167 mn->size += next->size + sizeof(struct mem_node); 168 } 169 if (prev->link.cqe_prev != (void *)&st_list && 170 prev->free_link.cqe_prev) { 171 /* 172 * if prev is: a valid node and a free node. ==> merge 173 */ 174 if (mn->free_link.cqe_next == NULL) 175 stmem_total += mn->size + sizeof(struct mem_node); 176 else { 177 /* already on free list */ 178 CIRCLEQ_REMOVE(&free_list, mn, free_link); 179 stmem_total += sizeof(struct mem_node); 180 } 181 CIRCLEQ_REMOVE(&st_list, mn, link); 182 prev->size += mn->size + sizeof(struct mem_node); 183 } else if (mn->free_link.cqe_next == NULL) { 184 /* 185 * we still are not on free list and we need to be. 186 * <-- | --> 187 */ 188 while (next->link.cqe_next != (void *)&st_list && 189 prev->link.cqe_prev != (void *)&st_list) { 190 if (next->free_link.cqe_next) { 191 CIRCLEQ_INSERT_BEFORE(&free_list, next, mn, 192 free_link); 193 break; 194 } 195 if (prev->free_link.cqe_next) { 196 CIRCLEQ_INSERT_AFTER(&free_list, prev, mn, 197 free_link); 198 break; 199 } 200 prev = prev->link.cqe_prev; 201 next = next->link.cqe_next; 202 } 203 if (mn->free_link.cqe_next == NULL) { 204 if (next->link.cqe_next == (void *)&st_list) { 205 /* 206 * we are not on list so we can add 207 * ourselves to the tail. (we walked to it.) 208 */ 209 CIRCLEQ_INSERT_TAIL(&free_list,mn,free_link); 210 } else { 211 CIRCLEQ_INSERT_HEAD(&free_list,mn,free_link); 212 } 213 } 214 stmem_total += mn->size;/* add our helpings to the pool. */ 215 } 216 splx(s); 217 } 218