You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

malloc.c 7.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 2 of the
  7. * License, or any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. */
  18. #include <stddef.h>
  19. #include <stdint.h>
  20. #include <string.h>
  21. #include <strings.h>
  22. #include <io.h>
  23. #include <gpxe/list.h>
  24. #include <malloc.h>
  25. /** @file
  26. *
  27. * Dynamic memory allocation
  28. *
  29. */
  30. /** A free block of memory */
  31. struct memory_block {
  32. /** List of free blocks */
  33. struct list_head list;
  34. /** Size of this block */
  35. size_t size;
  36. };
  37. #define MIN_MEMBLOCK_SIZE \
  38. ( ( size_t ) ( 1 << ( fls ( sizeof ( struct memory_block ) - 1 ) ) ) )
  39. /** A block of allocated memory complete with size information */
  40. struct autosized_block {
  41. /** Size of this block */
  42. size_t size;
  43. /** Remaining data */
  44. char data[0];
  45. };
  46. /** List of free memory blocks */
  47. static LIST_HEAD ( free_blocks );
  48. /**
  49. * Allocate a memory block
  50. *
  51. * @v size Requested size
  52. * @v align Physical alignment
  53. * @ret ptr Memory block, or NULL
  54. *
  55. * Allocates a memory block @b physically aligned as requested. No
  56. * guarantees are provided for the alignment of the virtual address.
  57. *
  58. * @c align must be a power of two. @c size may not be zero.
  59. */
  60. void * alloc_memblock ( size_t size, size_t align ) {
  61. struct memory_block *block;
  62. size_t align_mask;
  63. size_t pre_size;
  64. ssize_t post_size;
  65. struct memory_block *pre;
  66. struct memory_block *post;
  67. /* Round up size to multiple of MIN_MEMBLOCK_SIZE and
  68. * calculate alignment mask.
  69. */
  70. size = ( size + MIN_MEMBLOCK_SIZE - 1 ) & ~( MIN_MEMBLOCK_SIZE - 1 );
  71. align_mask = ( align - 1 ) | ( MIN_MEMBLOCK_SIZE - 1 );
  72. DBG ( "Allocating %#zx (aligned %#zx)\n", size, align );
  73. /* Search through blocks for the first one with enough space */
  74. list_for_each_entry ( block, &free_blocks, list ) {
  75. pre_size = ( - virt_to_phys ( block ) ) & align_mask;
  76. post_size = block->size - pre_size - size;
  77. if ( post_size >= 0 ) {
  78. /* Split block into pre-block, block, and
  79. * post-block. After this split, the "pre"
  80. * block is the one currently linked into the
  81. * free list.
  82. */
  83. pre = block;
  84. block = ( ( ( void * ) pre ) + pre_size );
  85. post = ( ( ( void * ) block ) + size );
  86. DBG ( "[%p,%p) -> [%p,%p) + [%p,%p)\n", pre,
  87. ( ( ( void * ) pre ) + pre->size ), pre, block,
  88. post, ( ( ( void * ) pre ) + pre->size ) );
  89. /* If there is a "post" block, add it in to
  90. * the free list. Leak it if it is too small
  91. * (which can happen only at the very end of
  92. * the heap).
  93. */
  94. if ( ( size_t ) post_size >= MIN_MEMBLOCK_SIZE ) {
  95. post->size = post_size;
  96. list_add ( &post->list, &pre->list );
  97. }
  98. /* Shrink "pre" block, leaving the main block
  99. * isolated and no longer part of the free
  100. * list.
  101. */
  102. pre->size = pre_size;
  103. /* If there is no "pre" block, remove it from
  104. * the list. Also remove it (i.e. leak it) if
  105. * it is too small, which can happen only at
  106. * the very start of the heap.
  107. */
  108. if ( pre_size < MIN_MEMBLOCK_SIZE )
  109. list_del ( &pre->list );
  110. /* Zero allocated memory, for calloc() */
  111. memset ( block, 0, size );
  112. DBG ( "Allocated [%p,%p)\n", block,
  113. ( ( ( void * ) block ) + size ) );
  114. return block;
  115. }
  116. }
  117. DBG ( "Failed to allocate %#zx (aligned %#zx)\n", size, align );
  118. return NULL;
  119. }
  120. /**
  121. * Free a memory block
  122. *
  123. * @v ptr Memory allocated by alloc_memblock(), or NULL
  124. * @v size Size of the memory
  125. *
  126. * If @c ptr is NULL, no action is taken.
  127. */
  128. void free_memblock ( void *ptr, size_t size ) {
  129. struct memory_block *freeing;
  130. struct memory_block *block;
  131. ssize_t gap_before;
  132. ssize_t gap_after = -1;
  133. /* Allow for ptr==NULL */
  134. if ( ! ptr )
  135. return;
  136. /* Round up size to match actual size that alloc_memblock()
  137. * would have used.
  138. */
  139. size = ( size + MIN_MEMBLOCK_SIZE - 1 ) & ~( MIN_MEMBLOCK_SIZE - 1 );
  140. freeing = ptr;
  141. freeing->size = size;
  142. DBG ( "Freeing [%p,%p)\n", freeing, ( ( ( void * ) freeing ) + size ));
  143. /* Insert/merge into free list */
  144. list_for_each_entry ( block, &free_blocks, list ) {
  145. /* Calculate gaps before and after the "freeing" block */
  146. gap_before = ( ( ( void * ) freeing ) -
  147. ( ( ( void * ) block ) + block->size ) );
  148. gap_after = ( ( ( void * ) block ) -
  149. ( ( ( void * ) freeing ) + freeing->size ) );
  150. /* Merge with immediately preceding block, if possible */
  151. if ( gap_before == 0 ) {
  152. DBG ( "[%p,%p) + [%p,%p) -> [%p,%p)\n", block,
  153. ( ( ( void * ) block ) + block->size ), freeing,
  154. ( ( ( void * ) freeing ) + freeing->size ),block,
  155. ( ( ( void * ) freeing ) + freeing->size ) );
  156. block->size += size;
  157. list_del ( &block->list );
  158. freeing = block;
  159. }
  160. /* Stop processing as soon as we reach a following block */
  161. if ( gap_after >= 0 )
  162. break;
  163. }
  164. /* Insert before the immediately following block. If
  165. * possible, merge the following block into the "freeing"
  166. * block.
  167. */
  168. DBG ( "[%p,%p)\n", freeing, ( ( ( void * ) freeing ) + freeing->size));
  169. list_add_tail ( &freeing->list, &block->list );
  170. if ( gap_after == 0 ) {
  171. DBG ( "[%p,%p) + [%p,%p) -> [%p,%p)\n", freeing,
  172. ( ( ( void * ) freeing ) + freeing->size ), block,
  173. ( ( ( void * ) block ) + block->size ), freeing,
  174. ( ( ( void * ) block ) + block->size ) );
  175. freeing->size += block->size;
  176. list_del ( &block->list );
  177. }
  178. }
  179. /**
  180. * Allocate memory
  181. *
  182. * @v size Requested size
  183. * @ret ptr Memory, or NULL
  184. *
  185. * Allocates memory with no particular alignment requirement. @c ptr
  186. * will be aligned to at least a multiple of sizeof(void*).
  187. */
  188. void * malloc ( size_t size ) {
  189. size_t total_size;
  190. struct autosized_block *block;
  191. total_size = size + offsetof ( struct autosized_block, data );
  192. block = alloc_memblock ( total_size, 1 );
  193. if ( ! block )
  194. return NULL;
  195. block->size = total_size;
  196. return &block->data;
  197. }
  198. /**
  199. * Free memory
  200. *
  201. * @v size Memory allocated by malloc(), or NULL
  202. *
  203. * Memory allocated with malloc_dma() cannot be freed with free(); it
  204. * must be freed with free_dma() instead.
  205. *
  206. * If @c ptr is NULL, no action is taken.
  207. */
  208. void free ( void *ptr ) {
  209. struct autosized_block *block;
  210. if ( ptr ) {
  211. block = container_of ( ptr, struct autosized_block, data );
  212. free_memblock ( block, block->size );
  213. }
  214. }
  215. /**
  216. * Add memory to allocation pool
  217. *
  218. * @v start Start address
  219. * @v end End address
  220. *
  221. * Adds a block of memory [start,end) to the allocation pool. This is
  222. * a one-way operation; there is no way to reclaim this memory.
  223. *
  224. * @c start must be aligned to at least a multiple of sizeof(void*).
  225. */
  226. void mpopulate ( void *start, size_t len ) {
  227. /* Prevent free_memblock() from rounding up len beyond the end
  228. * of what we were actually given...
  229. */
  230. free_memblock ( start, ( len & ~( MIN_MEMBLOCK_SIZE - 1 ) ) );
  231. }
  232. #if 0
  233. #include <vsprintf.h>
  234. /**
  235. * Dump free block list
  236. *
  237. */
  238. void mdumpfree ( void ) {
  239. struct memory_block *block;
  240. printf ( "Free block list:\n" );
  241. list_for_each_entry ( block, &free_blocks, list ) {
  242. printf ( "[%p,%p] (size %#zx)\n", block,
  243. ( ( ( void * ) block ) + block->size ), block->size );
  244. }
  245. }
  246. #endif