1 /* 2 * Copyright (C) 2000 Takashi Iwai <tiwai@suse.de> 3 * 4 * Generic memory management routines for soundcard memory allocation 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 2 of the License, or 9 * (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 */ 20 21 #include <sound/driver.h> 22 #include <linux/init.h> 23 #include <linux/slab.h> 24 #include <sound/core.h> 25 #include <sound/util_mem.h> 26 27 MODULE_AUTHOR("Takashi Iwai"); 28 MODULE_DESCRIPTION("Generic memory management routines for soundcard memory allocation"); 29 MODULE_LICENSE("GPL"); 30 31 #define get_memblk(p) list_entry(p, snd_util_memblk_t, list) 32 33 /* 34 * create a new memory manager 35 */ 36 snd_util_memhdr_t * 37 snd_util_memhdr_new(int memsize) 38 { 39 snd_util_memhdr_t *hdr; 40 41 hdr = kcalloc(1, sizeof(*hdr), GFP_KERNEL); 42 if (hdr == NULL) 43 return NULL; 44 hdr->size = memsize; 45 init_MUTEX(&hdr->block_mutex); 46 INIT_LIST_HEAD(&hdr->block); 47 48 return hdr; 49 } 50 51 /* 52 * free a memory manager 53 */ 54 void snd_util_memhdr_free(snd_util_memhdr_t *hdr) 55 { 56 struct list_head *p; 57 58 snd_assert(hdr != NULL, return); 59 /* release all blocks */ 60 while ((p = hdr->block.next) != &hdr->block) { 61 list_del(p); 62 kfree(get_memblk(p)); 63 } 64 kfree(hdr); 65 } 66 67 /* 68 * allocate a memory block (without mutex) 69 */ 70 snd_util_memblk_t * 71 __snd_util_mem_alloc(snd_util_memhdr_t *hdr, int size) 72 { 73 snd_util_memblk_t *blk; 74 snd_util_unit_t units, prev_offset; 75 struct list_head *p; 76 77 snd_assert(hdr != NULL, return NULL); 78 snd_assert(size > 0, return NULL); 79 80 /* word alignment */ 81 units = size; 82 if (units & 1) 83 units++; 84 if (units > hdr->size) 85 return NULL; 86 87 /* look for empty block */ 88 prev_offset = 0; 89 list_for_each(p, &hdr->block) { 90 blk = get_memblk(p); 91 if (blk->offset - prev_offset >= units) 92 goto __found; 93 prev_offset = blk->offset + blk->size; 94 } 95 if (hdr->size - prev_offset < units) 96 return NULL; 97 98 __found: 99 return __snd_util_memblk_new(hdr, units, p->prev); 100 } 101 102 103 /* 104 * create a new memory block with the given size 105 * the block is linked next to prev 106 */ 107 snd_util_memblk_t * 108 __snd_util_memblk_new(snd_util_memhdr_t *hdr, snd_util_unit_t units, 109 struct list_head *prev) 110 { 111 snd_util_memblk_t *blk; 112 113 blk = kmalloc(sizeof(snd_util_memblk_t) + hdr->block_extra_size, GFP_KERNEL); 114 if (blk == NULL) 115 return NULL; 116 117 if (! prev || prev == &hdr->block) 118 blk->offset = 0; 119 else { 120 snd_util_memblk_t *p = get_memblk(prev); 121 blk->offset = p->offset + p->size; 122 } 123 blk->size = units; 124 list_add(&blk->list, prev); 125 hdr->nblocks++; 126 hdr->used += units; 127 return blk; 128 } 129 130 131 /* 132 * allocate a memory block (with mutex) 133 */ 134 snd_util_memblk_t * 135 snd_util_mem_alloc(snd_util_memhdr_t *hdr, int size) 136 { 137 snd_util_memblk_t *blk; 138 down(&hdr->block_mutex); 139 blk = __snd_util_mem_alloc(hdr, size); 140 up(&hdr->block_mutex); 141 return blk; 142 } 143 144 145 /* 146 * remove the block from linked-list and free resource 147 * (without mutex) 148 */ 149 void 150 __snd_util_mem_free(snd_util_memhdr_t *hdr, snd_util_memblk_t *blk) 151 { 152 list_del(&blk->list); 153 hdr->nblocks--; 154 hdr->used -= blk->size; 155 kfree(blk); 156 } 157 158 /* 159 * free a memory block (with mutex) 160 */ 161 int snd_util_mem_free(snd_util_memhdr_t *hdr, snd_util_memblk_t *blk) 162 { 163 snd_assert(hdr && blk, return -EINVAL); 164 165 down(&hdr->block_mutex); 166 __snd_util_mem_free(hdr, blk); 167 up(&hdr->block_mutex); 168 return 0; 169 } 170 171 /* 172 * return available memory size 173 */ 174 int snd_util_mem_avail(snd_util_memhdr_t *hdr) 175 { 176 unsigned int size; 177 down(&hdr->block_mutex); 178 size = hdr->size - hdr->used; 179 up(&hdr->block_mutex); 180 return size; 181 } 182 183 184 EXPORT_SYMBOL(snd_util_memhdr_new); 185 EXPORT_SYMBOL(snd_util_memhdr_free); 186 EXPORT_SYMBOL(snd_util_mem_alloc); 187 EXPORT_SYMBOL(snd_util_mem_free); 188 EXPORT_SYMBOL(snd_util_mem_avail); 189 EXPORT_SYMBOL(__snd_util_mem_alloc); 190 EXPORT_SYMBOL(__snd_util_mem_free); 191 EXPORT_SYMBOL(__snd_util_memblk_new); 192 193 /* 194 * INIT part 195 */ 196 197 static int __init alsa_util_mem_init(void) 198 { 199 return 0; 200 } 201 202 static void __exit alsa_util_mem_exit(void) 203 { 204 } 205 206 module_init(alsa_util_mem_init) 207 module_exit(alsa_util_mem_exit) 208