mm_granalloc.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /****************************************************************************
  2. * mm/mm_gran/mm_granalloc.c
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one or more
  5. * contributor license agreements. See the NOTICE file distributed with
  6. * this work for additional information regarding copyright ownership. The
  7. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance with the
  9. * License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. * License for the specific language governing permissions and limitations
  17. * under the License.
  18. *
  19. ****************************************************************************/
  20. /****************************************************************************
  21. * Included Files
  22. ****************************************************************************/
  23. #include <nuttx/config.h>
  24. #include <assert.h>
  25. #include <nuttx/mm/gran.h>
  26. #include "mm_gran/mm_gran.h"
  27. #ifdef CONFIG_GRAN
  28. /****************************************************************************
  29. * Public Functions
  30. ****************************************************************************/
  31. /****************************************************************************
  32. * Name: gran_alloc
  33. *
  34. * Description:
  35. * Allocate memory from the granule heap.
  36. *
  37. * NOTE: The current implementation also restricts the maximum allocation
  38. * size to 32 granules. That restriction could be eliminated with some
  39. * additional coding effort.
  40. *
  41. * Input Parameters:
  42. * handle - The handle previously returned by gran_initialize
  43. * size - The size of the memory region to allocate.
  44. *
  45. * Returned Value:
  46. * On success, a non-NULL pointer to the allocated memory is returned;
  47. * NULL is returned on failure.
  48. *
  49. ****************************************************************************/
  50. FAR void *gran_alloc(GRAN_HANDLE handle, size_t size)
  51. {
  52. FAR struct gran_s *priv = (FAR struct gran_s *)handle;
  53. unsigned int ngranules;
  54. size_t tmpmask;
  55. uintptr_t alloc;
  56. uint32_t curr;
  57. uint32_t next;
  58. uint32_t mask;
  59. int granidx;
  60. int gatidx;
  61. int bitidx;
  62. int shift;
  63. int ret;
  64. DEBUGASSERT(priv != NULL && size <= 32 * (1 << priv->log2gran));
  65. if (priv != NULL && size > 0)
  66. {
  67. /* Get exclusive access to the GAT */
  68. ret = gran_enter_critical(priv);
  69. if (ret < 0)
  70. {
  71. return NULL;
  72. }
  73. /* How many contiguous granules we we need to find? */
  74. tmpmask = (1 << priv->log2gran) - 1;
  75. ngranules = (size + tmpmask) >> priv->log2gran;
  76. /* Then create mask for that number of granules */
  77. DEBUGASSERT(ngranules <= 32);
  78. mask = 0xffffffff >> (32 - ngranules);
  79. /* Now search the granule allocation table for that number of contiguous */
  80. for (granidx = 0; granidx < priv->ngranules; granidx += 32)
  81. {
  82. /* Get the GAT index associated with the granule table entry */
  83. gatidx = granidx >> 5;
  84. curr = priv->gat[gatidx];
  85. /* Handle the case where there are no free granules in the entry */
  86. if (curr == 0xffffffff)
  87. {
  88. continue;
  89. }
  90. /* Get the next entry from the GAT to support a 64 bit shift */
  91. if (granidx < priv->ngranules)
  92. {
  93. next = priv->gat[gatidx + 1];
  94. }
  95. /* Use all ones when are at the last entry in the GAT (meaning
  96. * nothing can be allocated.
  97. */
  98. else
  99. {
  100. next = 0xffffffff;
  101. }
  102. /* Search through the allocations in the 'curr' GAT entry
  103. * to see if we can satisfy the allocation starting in that
  104. * entry.
  105. *
  106. * This loop continues until either all of the bits have been
  107. * examined (bitidx >= 32), or until there are insufficient
  108. * granules left to satisfy the allocation.
  109. */
  110. alloc = priv->heapstart + (granidx << priv->log2gran);
  111. for (bitidx = 0;
  112. bitidx < 32 &&
  113. (granidx + bitidx + ngranules) <= priv->ngranules;
  114. )
  115. {
  116. /* Break out if there are no further free bits in 'curr'.
  117. * All of the zero bits might have gotten shifted out.
  118. */
  119. if (curr == 0xffffffff)
  120. {
  121. break;
  122. }
  123. /* Check for the first zero bit in the lower or upper 16-bits.
  124. * From the test above, we know that at least one of the 32-
  125. * bits in 'curr' is zero.
  126. */
  127. else if ((curr & 0x0000ffff) == 0x0000ffff)
  128. {
  129. /* Not in the lower 16 bits. The first free bit must be
  130. * in the upper 16 bits.
  131. */
  132. shift = 16;
  133. }
  134. /* We know that the first free bit is now within the lower 16
  135. * bits of 'curr'. Is it in the upper or lower byte?
  136. */
  137. else if ((curr & 0x0000ff) == 0x000000ff)
  138. {
  139. /* Not in the lower 8 bits. The first free bit must be in
  140. * the upper 8 bits.
  141. */
  142. shift = 8;
  143. }
  144. /* We know that the first free bit is now within the lower 4
  145. * bits of 'curr'. Is it in the upper or lower nibble?
  146. */
  147. else if ((curr & 0x00000f) == 0x0000000f)
  148. {
  149. /* Not in the lower 4 bits. The first free bit must be in
  150. * the upper 4 bits.
  151. */
  152. shift = 4;
  153. }
  154. /* We know that the first free bit is now within the lower 4
  155. * bits of 'curr'. Is it in the upper or lower pair?
  156. */
  157. else if ((curr & 0x000003) == 0x00000003)
  158. {
  159. /* Not in the lower 2 bits. The first free bit must be in
  160. * the upper 2 bits.
  161. */
  162. shift = 2;
  163. }
  164. /* We know that the first free bit is now within the lower 4
  165. * bits of 'curr'. Check if we have the allocation at this
  166. * bit position.
  167. */
  168. else if ((curr & mask) == 0)
  169. {
  170. /* Yes.. mark these granules allocated */
  171. gran_mark_allocated(priv, alloc, ngranules);
  172. /* And return the allocation address */
  173. gran_leave_critical(priv);
  174. return (FAR void *)alloc;
  175. }
  176. /* The free allocation does not start at this position */
  177. else
  178. {
  179. shift = 1;
  180. }
  181. /* Set up for the next time through the loop. Perform a 64
  182. * bit shift to move to the next gran position and increment
  183. * to the next candidate allocation address.
  184. */
  185. alloc += (shift << priv->log2gran);
  186. curr = (curr >> shift) | (next << (32 - shift));
  187. next >>= shift;
  188. bitidx += shift;
  189. }
  190. }
  191. gran_leave_critical(priv);
  192. }
  193. return NULL;
  194. }
  195. #endif /* CONFIG_GRAN */