membag.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. /**
  2. * \file
  3. *
  4. * \brief Memory bag allocator
  5. *
  6. * Copyright (C) 2012-2015 Atmel Corporation. All rights reserved.
  7. *
  8. * \asf_license_start
  9. *
  10. * \page License
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. *
  15. * 1. Redistributions of source code must retain the above copyright notice,
  16. * this list of conditions and the following disclaimer.
  17. *
  18. * 2. Redistributions in binary form must reproduce the above copyright notice,
  19. * this list of conditions and the following disclaimer in the documentation
  20. * and/or other materials provided with the distribution.
  21. *
  22. * 3. The name of Atmel may not be used to endorse or promote products derived
  23. * from this software without specific prior written permission.
  24. *
  25. * 4. This software may only be redistributed and used in connection with an
  26. * Atmel microcontroller product.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
  29. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  30. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  31. * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
  32. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  34. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  36. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  37. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  38. * POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. * \asf_license_stop
  41. *
  42. */
  43. /*
  44. * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
  45. */
  46. #include <compiler.h>
  47. #include <membag.h>
  48. #include "conf_membag.h"
  49. /** \internal
  50. *
  51. * Retrieves the number of elements in a statically declared array.
  52. */
  53. #define ARRAY_LEN(a) (sizeof(a) / sizeof((a)[0]))
  54. /**
  55. * Static address space which is split up into usable chunks by membag.
  56. * For configuration details, see \ref membag_list.
  57. */
  58. static uint8_t membag_pool[CONF_MEMBAG_POOL_SIZE];
  59. /**
  60. * Internal structure used by membag to keep track of memory,
  61. * with maximum 32 blocks per membag.
  62. */
  63. struct membag {
  64. /*! Number of bytes per block in this bag. */
  65. size_t block_size;
  66. /*! Total number of blocks. */
  67. size_t num_blocks;
  68. /*! Pointer to start of this bag. */
  69. uintptr_t start;
  70. /*! Pointer to end of this bag. */
  71. uintptr_t end;
  72. /*! 32-bit integer used to keep track of allocations. */
  73. uint32_t allocated;
  74. /*! Counter for number of free blocks. */
  75. uint8_t blocks_free;
  76. };
  77. /**
  78. * Array of available membags, provided by the user in the applications
  79. * conf_membag.h header file. Example:
  80. *
  81. * \code
  82. #define CONF_MEMBAG_ARRAY \
  83. MEMBAG(32, 4), \
  84. MEMBAG(16, 2),
  85. #define CONF_MEMBAG_POOL_SIZE \
  86. MEMBAG_SIZE(32, 4) + \
  87. MEMBAG_SIZE(16, 2)
  88. \endcode
  89. *
  90. */
  91. static struct membag membag_list[] = {
  92. CONF_MEMBAG_ARRAY
  93. };
  94. /**
  95. * \brief Initialize the membag system.
  96. *
  97. * This function sets up the membags, allocates memory from the memory pool, and
  98. * initializes them. Any existing allocations are destroyed and all memory pools
  99. * reset to their initial states.
  100. */
  101. void membag_init(void)
  102. {
  103. uint8_t i;
  104. uintptr_t poolptr;
  105. poolptr = (uintptr_t)membag_pool;
  106. for (i = 0; i < ARRAY_LEN(membag_list); i++) {
  107. Assert(membag_list[i].block_size > 0);
  108. Assert(membag_list[i].num_blocks > 0);
  109. Assert(membag_list[i].num_blocks <= 32);
  110. membag_list[i].start = poolptr;
  111. poolptr += (membag_list[i].block_size *
  112. membag_list[i].num_blocks);
  113. membag_list[i].end = poolptr;
  114. membag_list[i].blocks_free = membag_list[i].num_blocks;
  115. /* Mark all blocks as free. */
  116. membag_list[i].allocated = 0;
  117. }
  118. }
  119. /**
  120. * \brief Determine the total remaining free memory from all membags.
  121. *
  122. * \return Sum of all free memory, in bytes.
  123. */
  124. size_t membag_get_total_free(void)
  125. {
  126. uint8_t i;
  127. size_t total_free = 0;
  128. for (i = 0; i < ARRAY_LEN(membag_list); i++) {
  129. total_free += membag_list[i].blocks_free *
  130. membag_list[i].block_size;
  131. }
  132. return total_free;
  133. }
  134. /**
  135. * \brief Determine the total memory from all membags.
  136. *
  137. * \return Sum of all blocks in all bags, in bytes.
  138. */
  139. size_t membag_get_total(void)
  140. {
  141. uint8_t i;
  142. size_t total = 0;
  143. for (i = 0; i < ARRAY_LEN(membag_list); i++) {
  144. total += membag_list[i].num_blocks * membag_list[i].block_size;
  145. }
  146. return total;
  147. }
  148. /**
  149. * \brief Determine the smallest available block size.
  150. *
  151. * Calculates the smallest block which can be allocated by the Membag allocator
  152. * if requested. Allocations larger than this amount are not guaranteed to
  153. * complete successfully.
  154. *
  155. * \return Size of the smallest available block, in bytes.
  156. */
  157. size_t membag_get_smallest_free_block_size(void)
  158. {
  159. uint8_t i;
  160. struct membag *smallest_bag = NULL;
  161. for (i = 0; i < ARRAY_LEN(membag_list); i++) {
  162. if (membag_list[i].blocks_free == 0) {
  163. continue;
  164. }
  165. if (!smallest_bag ||
  166. (smallest_bag->block_size > membag_list[i].block_size)) {
  167. smallest_bag = &membag_list[i];
  168. }
  169. }
  170. if (smallest_bag) {
  171. return smallest_bag->block_size;
  172. }
  173. return 0;
  174. }
  175. /**
  176. * \brief Determine the largest available block size.
  177. *
  178. * Calculates the largest block which can be allocated by the Membag allocator
  179. * if requested. Allocations larger than this amount are guaranteed to fail.
  180. *
  181. * \return Size of the largest available block, in bytes.
  182. */
  183. size_t membag_get_largest_free_block_size(void)
  184. {
  185. uint8_t i;
  186. struct membag *largest_bag = NULL;
  187. for (i = 0; i < ARRAY_LEN(membag_list); i++) {
  188. if (membag_list[i].blocks_free == 0) {
  189. continue;
  190. }
  191. if (!largest_bag ||
  192. (largest_bag->block_size < membag_list[i].block_size)) {
  193. largest_bag = &membag_list[i];
  194. }
  195. }
  196. if (largest_bag) {
  197. return largest_bag->block_size;
  198. }
  199. return 0;
  200. }
  201. /**
  202. * \brief Allocate a memory block via a block from the Membag pool
  203. *
  204. * Allocates memory to the user from one of the available Membag pools. Each
  205. * Membag pool is examined in sequence, and the first free block of sufficient
  206. * size (if any) is chosen for the allocation. Allocated blocks persist until
  207. * either the Membag module is re-initialized, or an allocation block is freed
  208. * via \ref membag_free().
  209. *
  210. * \note The execution cycle time for this function is not deterministic; each
  211. * allocation request may take a variable amount of cycles to complete.
  212. *
  213. * \param size Size of memory block requested, in bytes
  214. *
  215. * \return Pointer to the start of an allocated block if one was found in the
  216. * Membag pool, NULL if no suitable block was found.
  217. */
  218. void *membag_alloc(const size_t size)
  219. {
  220. uint8_t i;
  221. struct membag *smallest_bag = NULL;
  222. uintptr_t p;
  223. /* Find the smallest available block size big enough for the requested
  224. * memory chunk size. */
  225. for (i = 0; i < ARRAY_LEN(membag_list); i++) {
  226. if (membag_list[i].blocks_free == 0) {
  227. continue;
  228. }
  229. if (membag_list[i].block_size >= size) {
  230. if (!smallest_bag ||
  231. (smallest_bag->block_size > membag_list[i].block_size)) {
  232. smallest_bag = &membag_list[i];
  233. }
  234. }
  235. }
  236. /* We return the first available block in the bag that has one, and if
  237. * there is none, we return NULL.
  238. */
  239. if (smallest_bag) {
  240. /* We know that there is a free block within the membag's
  241. * memory, and we simply return the first one available.
  242. */
  243. p = smallest_bag->start;
  244. for (i = 0; i < smallest_bag->num_blocks; i++) {
  245. /* Check the allocation byte to see whether the block is
  246. * in use. */
  247. if (!(smallest_bag->allocated & ((uint32_t)1 << i))) {
  248. /* It is free, set it to used. */
  249. smallest_bag->allocated |= ((uint32_t)1 << i);
  250. smallest_bag->blocks_free--;
  251. return (void *)(p);
  252. }
  253. p += smallest_bag->block_size;
  254. }
  255. }
  256. /* There is no available memory. Return NULL. */
  257. return NULL;
  258. }
  259. /**
  260. * \brief Free a previously allocated memory block from the Membag pool
  261. *
  262. * This function frees memory which has been allocated previously via a
  263. * successful call to \ref membag_alloc(). Once deallocated, the given pointer
  264. * is no longer valid and should not be used in the user application unless
  265. * re-allocated.
  266. *
  267. * \note The execution cycle time for this function is not deterministic; each
  268. * allocation request may take a variable amount of cycles to complete.
  269. *
  270. * \param ptr Pointer to an allocated memory block to free
  271. */
  272. void membag_free(const void *ptr)
  273. {
  274. uint8_t i;
  275. uintptr_t p = (uintptr_t)ptr;
  276. uint8_t block_index;
  277. for (i = 0; i < ARRAY_LEN(membag_list); i++) {
  278. if (p >= membag_list[i].start && p < membag_list[i].end) {
  279. block_index = (p - membag_list[i].start) / membag_list[i].block_size;
  280. /* Mark the memory as free. */
  281. membag_list[i].allocated &= ~((uint32_t)1 << block_index);
  282. membag_list[i].blocks_free++;
  283. return;
  284. }
  285. }
  286. }