util_mem.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  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. #include <linux/mutex.h>
  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. MODULE_AUTHOR("Takashi Iwai");
  27. MODULE_DESCRIPTION("Generic memory management routines for soundcard memory allocation");
  28. MODULE_LICENSE("GPL");
  29. #define get_memblk(p) list_entry(p, struct snd_util_memblk, list)
  30. /*
  31. * create a new memory manager
  32. */
  33. struct snd_util_memhdr *
  34. snd_util_memhdr_new(int memsize)
  35. {
  36. struct snd_util_memhdr *hdr;
  37. hdr = kzalloc(sizeof(*hdr), GFP_KERNEL);
  38. if (hdr == NULL)
  39. return NULL;
  40. hdr->size = memsize;
  41. mutex_init(&hdr->block_mutex);
  42. INIT_LIST_HEAD(&hdr->block);
  43. return hdr;
  44. }
  45. /*
  46. * free a memory manager
  47. */
  48. void snd_util_memhdr_free(struct snd_util_memhdr *hdr)
  49. {
  50. struct list_head *p;
  51. snd_assert(hdr != NULL, return);
  52. /* release all blocks */
  53. while ((p = hdr->block.next) != &hdr->block) {
  54. list_del(p);
  55. kfree(get_memblk(p));
  56. }
  57. kfree(hdr);
  58. }
  59. /*
  60. * allocate a memory block (without mutex)
  61. */
  62. struct snd_util_memblk *
  63. __snd_util_mem_alloc(struct snd_util_memhdr *hdr, int size)
  64. {
  65. struct snd_util_memblk *blk;
  66. unsigned int units, prev_offset;
  67. struct list_head *p;
  68. snd_assert(hdr != NULL, return NULL);
  69. snd_assert(size > 0, return NULL);
  70. /* word alignment */
  71. units = size;
  72. if (units & 1)
  73. units++;
  74. if (units > hdr->size)
  75. return NULL;
  76. /* look for empty block */
  77. prev_offset = 0;
  78. list_for_each(p, &hdr->block) {
  79. blk = get_memblk(p);
  80. if (blk->offset - prev_offset >= units)
  81. goto __found;
  82. prev_offset = blk->offset + blk->size;
  83. }
  84. if (hdr->size - prev_offset < units)
  85. return NULL;
  86. __found:
  87. return __snd_util_memblk_new(hdr, units, p->prev);
  88. }
  89. /*
  90. * create a new memory block with the given size
  91. * the block is linked next to prev
  92. */
  93. struct snd_util_memblk *
  94. __snd_util_memblk_new(struct snd_util_memhdr *hdr, unsigned int units,
  95. struct list_head *prev)
  96. {
  97. struct snd_util_memblk *blk;
  98. blk = kmalloc(sizeof(struct snd_util_memblk) + hdr->block_extra_size,
  99. GFP_KERNEL);
  100. if (blk == NULL)
  101. return NULL;
  102. if (! prev || prev == &hdr->block)
  103. blk->offset = 0;
  104. else {
  105. struct snd_util_memblk *p = get_memblk(prev);
  106. blk->offset = p->offset + p->size;
  107. }
  108. blk->size = units;
  109. list_add(&blk->list, prev);
  110. hdr->nblocks++;
  111. hdr->used += units;
  112. return blk;
  113. }
  114. /*
  115. * allocate a memory block (with mutex)
  116. */
  117. struct snd_util_memblk *
  118. snd_util_mem_alloc(struct snd_util_memhdr *hdr, int size)
  119. {
  120. struct snd_util_memblk *blk;
  121. mutex_lock(&hdr->block_mutex);
  122. blk = __snd_util_mem_alloc(hdr, size);
  123. mutex_unlock(&hdr->block_mutex);
  124. return blk;
  125. }
  126. /*
  127. * remove the block from linked-list and free resource
  128. * (without mutex)
  129. */
  130. void
  131. __snd_util_mem_free(struct snd_util_memhdr *hdr, struct snd_util_memblk *blk)
  132. {
  133. list_del(&blk->list);
  134. hdr->nblocks--;
  135. hdr->used -= blk->size;
  136. kfree(blk);
  137. }
  138. /*
  139. * free a memory block (with mutex)
  140. */
  141. int snd_util_mem_free(struct snd_util_memhdr *hdr, struct snd_util_memblk *blk)
  142. {
  143. snd_assert(hdr && blk, return -EINVAL);
  144. mutex_lock(&hdr->block_mutex);
  145. __snd_util_mem_free(hdr, blk);
  146. mutex_unlock(&hdr->block_mutex);
  147. return 0;
  148. }
  149. /*
  150. * return available memory size
  151. */
  152. int snd_util_mem_avail(struct snd_util_memhdr *hdr)
  153. {
  154. unsigned int size;
  155. mutex_lock(&hdr->block_mutex);
  156. size = hdr->size - hdr->used;
  157. mutex_unlock(&hdr->block_mutex);
  158. return size;
  159. }
  160. EXPORT_SYMBOL(snd_util_memhdr_new);
  161. EXPORT_SYMBOL(snd_util_memhdr_free);
  162. EXPORT_SYMBOL(snd_util_mem_alloc);
  163. EXPORT_SYMBOL(snd_util_mem_free);
  164. EXPORT_SYMBOL(snd_util_mem_avail);
  165. EXPORT_SYMBOL(__snd_util_mem_alloc);
  166. EXPORT_SYMBOL(__snd_util_mem_free);
  167. EXPORT_SYMBOL(__snd_util_memblk_new);
  168. /*
  169. * INIT part
  170. */
  171. static int __init alsa_util_mem_init(void)
  172. {
  173. return 0;
  174. }
  175. static void __exit alsa_util_mem_exit(void)
  176. {
  177. }
  178. module_init(alsa_util_mem_init)
  179. module_exit(alsa_util_mem_exit)