util_mem.c 4.5 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 <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. mutex_init(&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. if (!hdr)
  51. 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. if (snd_BUG_ON(!hdr || size <= 0))
  69. 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 == &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. if (snd_BUG_ON(!hdr || !blk))
  144. return -EINVAL;
  145. mutex_lock(&hdr->block_mutex);
  146. __snd_util_mem_free(hdr, blk);
  147. mutex_unlock(&hdr->block_mutex);
  148. return 0;
  149. }
  150. /*
  151. * return available memory size
  152. */
  153. int snd_util_mem_avail(struct snd_util_memhdr *hdr)
  154. {
  155. unsigned int size;
  156. mutex_lock(&hdr->block_mutex);
  157. size = hdr->size - hdr->used;
  158. mutex_unlock(&hdr->block_mutex);
  159. return size;
  160. }
  161. EXPORT_SYMBOL(snd_util_memhdr_new);
  162. EXPORT_SYMBOL(snd_util_memhdr_free);
  163. EXPORT_SYMBOL(snd_util_mem_alloc);
  164. EXPORT_SYMBOL(snd_util_mem_free);
  165. EXPORT_SYMBOL(snd_util_mem_avail);
  166. EXPORT_SYMBOL(__snd_util_mem_alloc);
  167. EXPORT_SYMBOL(__snd_util_mem_free);
  168. EXPORT_SYMBOL(__snd_util_memblk_new);
  169. /*
  170. * INIT part
  171. */
  172. static int __init alsa_util_mem_init(void)
  173. {
  174. return 0;
  175. }
  176. static void __exit alsa_util_mem_exit(void)
  177. {
  178. }
  179. module_init(alsa_util_mem_init)
  180. module_exit(alsa_util_mem_exit)