util_mem.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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, struct snd_util_memblk, list)
  29. /*
  30. * create a new memory manager
  31. */
  32. struct snd_util_memhdr *
  33. snd_util_memhdr_new(int memsize)
  34. {
  35. struct snd_util_memhdr *hdr;
  36. hdr = kzalloc(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(struct snd_util_memhdr *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. struct snd_util_memblk *
  62. __snd_util_mem_alloc(struct snd_util_memhdr *hdr, int size)
  63. {
  64. struct snd_util_memblk *blk;
  65. unsigned int 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. struct snd_util_memblk *
  93. __snd_util_memblk_new(struct snd_util_memhdr *hdr, unsigned int units,
  94. struct list_head *prev)
  95. {
  96. struct snd_util_memblk *blk;
  97. blk = kmalloc(sizeof(struct snd_util_memblk) + hdr->block_extra_size,
  98. GFP_KERNEL);
  99. if (blk == NULL)
  100. return NULL;
  101. if (! prev || prev == &hdr->block)
  102. blk->offset = 0;
  103. else {
  104. struct snd_util_memblk *p = get_memblk(prev);
  105. blk->offset = p->offset + p->size;
  106. }
  107. blk->size = units;
  108. list_add(&blk->list, prev);
  109. hdr->nblocks++;
  110. hdr->used += units;
  111. return blk;
  112. }
  113. /*
  114. * allocate a memory block (with mutex)
  115. */
  116. struct snd_util_memblk *
  117. snd_util_mem_alloc(struct snd_util_memhdr *hdr, int size)
  118. {
  119. struct snd_util_memblk *blk;
  120. down(&hdr->block_mutex);
  121. blk = __snd_util_mem_alloc(hdr, size);
  122. up(&hdr->block_mutex);
  123. return blk;
  124. }
  125. /*
  126. * remove the block from linked-list and free resource
  127. * (without mutex)
  128. */
  129. void
  130. __snd_util_mem_free(struct snd_util_memhdr *hdr, struct snd_util_memblk *blk)
  131. {
  132. list_del(&blk->list);
  133. hdr->nblocks--;
  134. hdr->used -= blk->size;
  135. kfree(blk);
  136. }
  137. /*
  138. * free a memory block (with mutex)
  139. */
  140. int snd_util_mem_free(struct snd_util_memhdr *hdr, struct snd_util_memblk *blk)
  141. {
  142. snd_assert(hdr && blk, return -EINVAL);
  143. down(&hdr->block_mutex);
  144. __snd_util_mem_free(hdr, blk);
  145. up(&hdr->block_mutex);
  146. return 0;
  147. }
  148. /*
  149. * return available memory size
  150. */
  151. int snd_util_mem_avail(struct snd_util_memhdr *hdr)
  152. {
  153. unsigned int size;
  154. down(&hdr->block_mutex);
  155. size = hdr->size - hdr->used;
  156. up(&hdr->block_mutex);
  157. return size;
  158. }
  159. EXPORT_SYMBOL(snd_util_memhdr_new);
  160. EXPORT_SYMBOL(snd_util_memhdr_free);
  161. EXPORT_SYMBOL(snd_util_mem_alloc);
  162. EXPORT_SYMBOL(snd_util_mem_free);
  163. EXPORT_SYMBOL(snd_util_mem_avail);
  164. EXPORT_SYMBOL(__snd_util_mem_alloc);
  165. EXPORT_SYMBOL(__snd_util_mem_free);
  166. EXPORT_SYMBOL(__snd_util_memblk_new);
  167. /*
  168. * INIT part
  169. */
  170. static int __init alsa_util_mem_init(void)
  171. {
  172. return 0;
  173. }
  174. static void __exit alsa_util_mem_exit(void)
  175. {
  176. }
  177. module_init(alsa_util_mem_init)
  178. module_exit(alsa_util_mem_exit)