Changeset 34 for GPL/trunk/alsa-kernel/synth/util_mem.c
- Timestamp:
- Dec 11, 2005, 5:57:39 PM (20 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
GPL/trunk/alsa-kernel/synth/util_mem.c
r33 r34 16 16 * You should have received a copy of the GNU General Public License 17 17 * along with this program; if not, write to the Free Software 18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 19 */ 20 21 #define SNDRV_MAIN_OBJECT_FILE 18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 */ 20 22 21 #include <sound/driver.h> 22 #include <linux/init.h> 23 #include <linux/slab.h> 24 #include <sound/core.h> 23 25 #include <sound/util_mem.h> 24 26 25 27 MODULE_AUTHOR("Takashi Iwai"); 26 27 #define get_memblk(p) list_entry(p, snd_util_memblk_t, list) 28 MODULE_DESCRIPTION("Generic memory management routines for soundcard memory allocation"); 29 MODULE_LICENSE("GPL"); 30 31 #define get_memblk(p) list_entry(p, struct snd_util_memblk, list) 28 32 29 33 /* 30 34 * create a new memory manager 31 35 */ 32 s nd_util_memhdr_t*33 34 { 35 snd_util_memhdr_t*hdr;36 37 hdr = kcalloc(1,sizeof(*hdr), GFP_KERNEL);38 39 40 41 42 43 44 36 struct snd_util_memhdr * 37 snd_util_memhdr_new(int memsize) 38 { 39 struct snd_util_memhdr *hdr; 40 41 hdr = kzalloc(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; 45 49 } 46 50 … … 48 52 * free a memory manager 49 53 */ 50 void snd_util_memhdr_free(s nd_util_memhdr_t*hdr)51 { 52 53 54 55 56 57 58 59 60 54 void snd_util_memhdr_free(struct snd_util_memhdr *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); 61 65 } 62 66 … … 64 68 * allocate a memory block (without mutex) 65 69 */ 66 snd_util_memblk_t * 67 __snd_util_mem_alloc(snd_util_memhdr_t *hdr, int size) 68 { 69 snd_util_memblk_t *blk; 70 snd_util_unit_t units, prev_offset; 71 struct list_head *p; 72 73 snd_assert(hdr != NULL, return NULL); 74 snd_assert(size > 0, return NULL); 75 /* word alignment */ 76 units = size; 77 if (units & 1) 78 units++; 79 if (units > hdr->size) 80 return NULL; 81 82 /* look for empty block */ 83 prev_offset = 0; 84 list_for_each(p, &hdr->block) { 85 blk = get_memblk(p); 86 if (blk->offset - prev_offset >= units) 87 goto __found; 88 prev_offset = blk->offset + blk->size; 89 } 90 if (hdr->size - prev_offset < units) 91 return NULL; 70 struct snd_util_memblk * 71 __snd_util_mem_alloc(struct snd_util_memhdr *hdr, int size) 72 { 73 struct snd_util_memblk *blk; 74 unsigned int 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; 92 97 93 98 __found: 94 99 return __snd_util_memblk_new(hdr, units, p->prev); 95 100 } 96 101 … … 100 105 * the block is linked next to prev 101 106 */ 102 snd_util_memblk_t * 103 __snd_util_memblk_new(snd_util_memhdr_t *hdr, snd_util_unit_t units, 104 struct list_head *prev) 105 { 106 snd_util_memblk_t *blk; 107 108 blk = kmalloc(sizeof(snd_util_memblk_t) + hdr->block_extra_size, GFP_KERNEL); 109 if (blk == NULL) 110 return NULL; 111 112 if (! prev || prev == &hdr->block) 113 blk->offset = 0; 114 else { 115 snd_util_memblk_t *p = get_memblk(prev); 116 blk->offset = p->offset + p->size; 117 } 118 blk->size = units; 119 list_add(&blk->list, prev); 120 hdr->nblocks++; 121 hdr->used += units; 122 return blk; 107 struct snd_util_memblk * 108 __snd_util_memblk_new(struct snd_util_memhdr *hdr, unsigned int units, 109 struct list_head *prev) 110 { 111 struct snd_util_memblk *blk; 112 113 blk = kmalloc(sizeof(struct snd_util_memblk) + hdr->block_extra_size, 114 GFP_KERNEL); 115 if (blk == NULL) 116 return NULL; 117 118 if (! prev || prev == &hdr->block) 119 blk->offset = 0; 120 else { 121 struct snd_util_memblk *p = get_memblk(prev); 122 blk->offset = p->offset + p->size; 123 } 124 blk->size = units; 125 list_add(&blk->list, prev); 126 hdr->nblocks++; 127 hdr->used += units; 128 return blk; 123 129 } 124 130 … … 127 133 * allocate a memory block (with mutex) 128 134 */ 129 s nd_util_memblk_t*130 snd_util_mem_alloc(s nd_util_memhdr_t*hdr, int size)131 { 132 snd_util_memblk_t*blk;133 134 135 136 135 struct snd_util_memblk * 136 snd_util_mem_alloc(struct snd_util_memhdr *hdr, int size) 137 { 138 struct snd_util_memblk *blk; 139 down(&hdr->block_mutex); 140 blk = __snd_util_mem_alloc(hdr, size); 141 up(&hdr->block_mutex); 142 return blk; 137 143 } 138 144 … … 143 149 */ 144 150 void 145 __snd_util_mem_free(s nd_util_memhdr_t *hdr, snd_util_memblk_t*blk)146 { 147 148 149 150 151 __snd_util_mem_free(struct snd_util_memhdr *hdr, struct snd_util_memblk *blk) 152 { 153 list_del(&blk->list); 154 hdr->nblocks--; 155 hdr->used -= blk->size; 156 kfree(blk); 151 157 } 152 158 … … 154 160 * free a memory block (with mutex) 155 161 */ 156 int snd_util_mem_free(s nd_util_memhdr_t *hdr, snd_util_memblk_t*blk)157 { 158 159 160 161 162 163 162 int snd_util_mem_free(struct snd_util_memhdr *hdr, struct snd_util_memblk *blk) 163 { 164 snd_assert(hdr && blk, return -EINVAL); 165 166 down(&hdr->block_mutex); 167 __snd_util_mem_free(hdr, blk); 168 up(&hdr->block_mutex); 169 return 0; 164 170 } 165 171 … … 167 173 * return available memory size 168 174 */ 169 int snd_util_mem_avail(s nd_util_memhdr_t*hdr)170 { 171 172 173 174 175 175 int snd_util_mem_avail(struct snd_util_memhdr *hdr) 176 { 177 unsigned int size; 178 down(&hdr->block_mutex); 179 size = hdr->size - hdr->used; 180 up(&hdr->block_mutex); 181 return size; 176 182 } 177 183 … … 192 198 static int __init alsa_util_mem_init(void) 193 199 { 194 200 return 0; 195 201 } 196 202
Note:
See TracChangeset
for help on using the changeset viewer.