util_mem.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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_obj(*hdr);
  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. guard(mutex)(&hdr->block_mutex);
  109. return __snd_util_mem_alloc(hdr, size);
  110. }
  111. /*
  112. * remove the block from linked-list and free resource
  113. * (without mutex)
  114. */
  115. void
  116. __snd_util_mem_free(struct snd_util_memhdr *hdr, struct snd_util_memblk *blk)
  117. {
  118. list_del(&blk->list);
  119. hdr->nblocks--;
  120. hdr->used -= blk->size;
  121. kfree(blk);
  122. }
  123. /*
  124. * free a memory block (with mutex)
  125. */
  126. int snd_util_mem_free(struct snd_util_memhdr *hdr, struct snd_util_memblk *blk)
  127. {
  128. if (snd_BUG_ON(!hdr || !blk))
  129. return -EINVAL;
  130. guard(mutex)(&hdr->block_mutex);
  131. __snd_util_mem_free(hdr, blk);
  132. return 0;
  133. }
  134. /*
  135. * return available memory size
  136. */
  137. int snd_util_mem_avail(struct snd_util_memhdr *hdr)
  138. {
  139. guard(mutex)(&hdr->block_mutex);
  140. return hdr->size - hdr->used;
  141. }
  142. EXPORT_SYMBOL(snd_util_memhdr_new);
  143. EXPORT_SYMBOL(snd_util_memhdr_free);
  144. EXPORT_SYMBOL(snd_util_mem_alloc);
  145. EXPORT_SYMBOL(snd_util_mem_free);
  146. EXPORT_SYMBOL(snd_util_mem_avail);
  147. EXPORT_SYMBOL(__snd_util_mem_alloc);
  148. EXPORT_SYMBOL(__snd_util_mem_free);
  149. EXPORT_SYMBOL(__snd_util_memblk_new);