util_mem.c 4.6 KB

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