util_mem.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2000 Takashi Iwai <tiwai@suse.de>
  4. *
  5. * Generic memory management routines for soundcard memory allocation
  6. */
  7. #include <linux/mutex.h>
  8. #include <linux/init.h>
  9. #include <linux/slab.h>
  10. #include <linux/module.h>
  11. #include <sound/core.h>
  12. #include <sound/util_mem.h>
  13. MODULE_AUTHOR("Takashi Iwai");
  14. MODULE_DESCRIPTION("Generic memory management routines for soundcard memory allocation");
  15. MODULE_LICENSE("GPL");
  16. #define get_memblk(p) list_entry(p, struct snd_util_memblk, list)
  17. /*
  18. * create a new memory manager
  19. */
  20. struct snd_util_memhdr *
  21. snd_util_memhdr_new(int memsize)
  22. {
  23. struct snd_util_memhdr *hdr;
  24. hdr = kzalloc(sizeof(*hdr), GFP_KERNEL);
  25. if (hdr == NULL)
  26. return NULL;
  27. hdr->size = memsize;
  28. mutex_init(&hdr->block_mutex);
  29. INIT_LIST_HEAD(&hdr->block);
  30. return hdr;
  31. }
  32. /*
  33. * free a memory manager
  34. */
  35. void snd_util_memhdr_free(struct snd_util_memhdr *hdr)
  36. {
  37. struct list_head *p;
  38. if (!hdr)
  39. return;
  40. /* release all blocks */
  41. while ((p = hdr->block.next) != &hdr->block) {
  42. list_del(p);
  43. kfree(get_memblk(p));
  44. }
  45. kfree(hdr);
  46. }
  47. /*
  48. * allocate a memory block (without mutex)
  49. */
  50. struct snd_util_memblk *
  51. __snd_util_mem_alloc(struct snd_util_memhdr *hdr, int size)
  52. {
  53. struct snd_util_memblk *blk;
  54. unsigned int units, prev_offset;
  55. struct list_head *p;
  56. if (snd_BUG_ON(!hdr || size <= 0))
  57. return NULL;
  58. /* word alignment */
  59. units = size;
  60. if (units & 1)
  61. units++;
  62. if (units > hdr->size)
  63. return NULL;
  64. /* look for empty block */
  65. prev_offset = 0;
  66. list_for_each(p, &hdr->block) {
  67. blk = get_memblk(p);
  68. if (blk->offset - prev_offset >= units)
  69. goto __found;
  70. prev_offset = blk->offset + blk->size;
  71. }
  72. if (hdr->size - prev_offset < units)
  73. return NULL;
  74. __found:
  75. return __snd_util_memblk_new(hdr, units, p->prev);
  76. }
  77. /*
  78. * create a new memory block with the given size
  79. * the block is linked next to prev
  80. */
  81. struct snd_util_memblk *
  82. __snd_util_memblk_new(struct snd_util_memhdr *hdr, unsigned int units,
  83. struct list_head *prev)
  84. {
  85. struct snd_util_memblk *blk;
  86. blk = kmalloc(sizeof(struct snd_util_memblk) + hdr->block_extra_size,
  87. GFP_KERNEL);
  88. if (blk == NULL)
  89. return NULL;
  90. if (prev == &hdr->block)
  91. blk->offset = 0;
  92. else {
  93. struct snd_util_memblk *p = get_memblk(prev);
  94. blk->offset = p->offset + p->size;
  95. }
  96. blk->size = units;
  97. list_add(&blk->list, prev);
  98. hdr->nblocks++;
  99. hdr->used += units;
  100. return blk;
  101. }
  102. /*
  103. * allocate a memory block (with mutex)
  104. */
  105. struct snd_util_memblk *
  106. snd_util_mem_alloc(struct snd_util_memhdr *hdr, int size)
  107. {
  108. struct snd_util_memblk *blk;
  109. mutex_lock(&hdr->block_mutex);
  110. blk = __snd_util_mem_alloc(hdr, size);
  111. mutex_unlock(&hdr->block_mutex);
  112. return blk;
  113. }
  114. /*
  115. * remove the block from linked-list and free resource
  116. * (without mutex)
  117. */
  118. void
  119. __snd_util_mem_free(struct snd_util_memhdr *hdr, struct snd_util_memblk *blk)
  120. {
  121. list_del(&blk->list);
  122. hdr->nblocks--;
  123. hdr->used -= blk->size;
  124. kfree(blk);
  125. }
  126. /*
  127. * free a memory block (with mutex)
  128. */
  129. int snd_util_mem_free(struct snd_util_memhdr *hdr, struct snd_util_memblk *blk)
  130. {
  131. if (snd_BUG_ON(!hdr || !blk))
  132. return -EINVAL;
  133. mutex_lock(&hdr->block_mutex);
  134. __snd_util_mem_free(hdr, blk);
  135. mutex_unlock(&hdr->block_mutex);
  136. return 0;
  137. }
  138. /*
  139. * return available memory size
  140. */
  141. int snd_util_mem_avail(struct snd_util_memhdr *hdr)
  142. {
  143. unsigned int size;
  144. mutex_lock(&hdr->block_mutex);
  145. size = hdr->size - hdr->used;
  146. mutex_unlock(&hdr->block_mutex);
  147. return size;
  148. }
  149. EXPORT_SYMBOL(snd_util_memhdr_new);
  150. EXPORT_SYMBOL(snd_util_memhdr_free);
  151. EXPORT_SYMBOL(snd_util_mem_alloc);
  152. EXPORT_SYMBOL(snd_util_mem_free);
  153. EXPORT_SYMBOL(snd_util_mem_avail);
  154. EXPORT_SYMBOL(__snd_util_mem_alloc);
  155. EXPORT_SYMBOL(__snd_util_mem_free);
  156. EXPORT_SYMBOL(__snd_util_memblk_new);