util_mem.c 4.4 KB

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