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.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. FILE_LICENCE ( GPL2_OR_LATER );
  19. #include <stddef.h>
  20. #include <stdint.h>
  21. #include <string.h>
  22. #include <strings.h>
  23. #include <gpxe/io.h>
  24. #include <gpxe/list.h>
  25. #include <gpxe/init.h>
  26. #include <gpxe/malloc.h>
  27. /** @file
  28. *
  29. * Dynamic memory allocation
  30. *
  31. */
  32. /** A free block of memory */
  33. struct memory_block {
  34. /** List of free blocks */
  35. struct list_head list;
  36. /** Size of this block */
  37. size_t size;
  38. };
  39. #define MIN_MEMBLOCK_SIZE \
  40. ( ( size_t ) ( 1 << ( fls ( sizeof ( struct memory_block ) - 1 ) ) ) )
  41. /** A block of allocated memory complete with size information */
  42. struct autosized_block {
  43. /** Size of this block */
  44. size_t size;
  45. /** Remaining data */
  46. char data[0];
  47. };
  48. /**
  49. * Address for zero-length memory blocks
  50. *
  51. * @c malloc(0) or @c realloc(ptr,0) will return the special value @c
  52. * NOWHERE. Calling @c free(NOWHERE) will have no effect.
  53. *
  54. * This is consistent with the ANSI C standards, which state that
  55. * "either NULL or a pointer suitable to be passed to free()" must be
  56. * returned in these cases. Using a special non-NULL value means that
  57. * the caller can take a NULL return value to indicate failure,
  58. * without first having to check for a requested size of zero.
  59. *
  60. * Code outside of malloc.c do not ever need to refer to the actual
  61. * value of @c NOWHERE; this is an internal definition.
  62. */
  63. #define NOWHERE ( ( void * ) ~( ( intptr_t ) 0 ) )
  64. /** List of free memory blocks */
  65. static LIST_HEAD ( free_blocks );
  66. /** Total amount of free memory */
  67. size_t freemem;
  68. /**
  69. * Heap size
  70. *
  71. * Currently fixed at 128kB.
  72. */
  73. #define HEAP_SIZE ( 128 * 1024 )
  74. /** The heap itself */
  75. static char heap[HEAP_SIZE] __attribute__ (( aligned ( __alignof__(void *) )));
  76. /**
  77. * Allocate a memory block
  78. *
  79. * @v size Requested size
  80. * @v align Physical alignment
  81. * @ret ptr Memory block, or NULL
  82. *
  83. * Allocates a memory block @b physically aligned as requested. No
  84. * guarantees are provided for the alignment of the virtual address.
  85. *
  86. * @c align must be a power of two. @c size may not be zero.
  87. */
  88. void * alloc_memblock ( size_t size, size_t align ) {
  89. struct memory_block *block;
  90. size_t align_mask;
  91. size_t pre_size;
  92. ssize_t post_size;
  93. struct memory_block *pre;
  94. struct memory_block *post;
  95. /* Round up size to multiple of MIN_MEMBLOCK_SIZE and
  96. * calculate alignment mask.
  97. */
  98. size = ( size + MIN_MEMBLOCK_SIZE - 1 ) & ~( MIN_MEMBLOCK_SIZE - 1 );
  99. align_mask = ( align - 1 ) | ( MIN_MEMBLOCK_SIZE - 1 );
  100. DBG ( "Allocating %#zx (aligned %#zx)\n", size, align );
  101. /* Search through blocks for the first one with enough space */
  102. list_for_each_entry ( block, &free_blocks, list ) {
  103. pre_size = ( - virt_to_phys ( block ) ) & align_mask;
  104. post_size = block->size - pre_size - size;
  105. if ( post_size >= 0 ) {
  106. /* Split block into pre-block, block, and
  107. * post-block. After this split, the "pre"
  108. * block is the one currently linked into the
  109. * free list.
  110. */
  111. pre = block;
  112. block = ( ( ( void * ) pre ) + pre_size );
  113. post = ( ( ( void * ) block ) + size );
  114. DBG ( "[%p,%p) -> [%p,%p) + [%p,%p)\n", pre,
  115. ( ( ( void * ) pre ) + pre->size ), pre, block,
  116. post, ( ( ( void * ) pre ) + pre->size ) );
  117. /* If there is a "post" block, add it in to
  118. * the free list. Leak it if it is too small
  119. * (which can happen only at the very end of
  120. * the heap).
  121. */
  122. if ( ( size_t ) post_size >= MIN_MEMBLOCK_SIZE ) {
  123. post->size = post_size;
  124. list_add ( &post->list, &pre->list );
  125. }
  126. /* Shrink "pre" block, leaving the main block
  127. * isolated and no longer part of the free
  128. * list.
  129. */
  130. pre->size = pre_size;
  131. /* If there is no "pre" block, remove it from
  132. * the list. Also remove it (i.e. leak it) if
  133. * it is too small, which can happen only at
  134. * the very start of the heap.
  135. */
  136. if ( pre_size < MIN_MEMBLOCK_SIZE )
  137. list_del ( &pre->list );
  138. /* Update total free memory */
  139. freemem -= size;
  140. /* Return allocated block */
  141. DBG ( "Allocated [%p,%p)\n", block,
  142. ( ( ( void * ) block ) + size ) );
  143. return block;
  144. }
  145. }
  146. DBG ( "Failed to allocate %#zx (aligned %#zx)\n", size, align );
  147. return NULL;
  148. }
  149. /**
  150. * Free a memory block
  151. *
  152. * @v ptr Memory allocated by alloc_memblock(), or NULL
  153. * @v size Size of the memory
  154. *
  155. * If @c ptr is NULL, no action is taken.
  156. */
  157. void free_memblock ( void *ptr, size_t size ) {
  158. struct memory_block *freeing;
  159. struct memory_block *block;
  160. ssize_t gap_before;
  161. ssize_t gap_after = -1;
  162. /* Allow for ptr==NULL */
  163. if ( ! ptr )
  164. return;
  165. /* Round up size to match actual size that alloc_memblock()
  166. * would have used.
  167. */
  168. size = ( size + MIN_MEMBLOCK_SIZE - 1 ) & ~( MIN_MEMBLOCK_SIZE - 1 );
  169. freeing = ptr;
  170. freeing->size = size;
  171. DBG ( "Freeing [%p,%p)\n", freeing, ( ( ( void * ) freeing ) + size ));
  172. /* Insert/merge into free list */
  173. list_for_each_entry ( block, &free_blocks, list ) {
  174. /* Calculate gaps before and after the "freeing" block */
  175. gap_before = ( ( ( void * ) freeing ) -
  176. ( ( ( void * ) block ) + block->size ) );
  177. gap_after = ( ( ( void * ) block ) -
  178. ( ( ( void * ) freeing ) + freeing->size ) );
  179. /* Merge with immediately preceding block, if possible */
  180. if ( gap_before == 0 ) {
  181. DBG ( "[%p,%p) + [%p,%p) -> [%p,%p)\n", block,
  182. ( ( ( void * ) block ) + block->size ), freeing,
  183. ( ( ( void * ) freeing ) + freeing->size ),block,
  184. ( ( ( void * ) freeing ) + freeing->size ) );
  185. block->size += size;
  186. list_del ( &block->list );
  187. freeing = block;
  188. }
  189. /* Stop processing as soon as we reach a following block */
  190. if ( gap_after >= 0 )
  191. break;
  192. }
  193. /* Insert before the immediately following block. If
  194. * possible, merge the following block into the "freeing"
  195. * block.
  196. */
  197. DBG ( "[%p,%p)\n", freeing, ( ( ( void * ) freeing ) + freeing->size));
  198. list_add_tail ( &freeing->list, &block->list );
  199. if ( gap_after == 0 ) {
  200. DBG ( "[%p,%p) + [%p,%p) -> [%p,%p)\n", freeing,
  201. ( ( ( void * ) freeing ) + freeing->size ), block,
  202. ( ( ( void * ) block ) + block->size ), freeing,
  203. ( ( ( void * ) block ) + block->size ) );
  204. freeing->size += block->size;
  205. list_del ( &block->list );
  206. }
  207. /* Update free memory counter */
  208. freemem += size;
  209. }
  210. /**
  211. * Reallocate memory
  212. *
  213. * @v old_ptr Memory previously allocated by malloc(), or NULL
  214. * @v new_size Requested size
  215. * @ret new_ptr Allocated memory, or NULL
  216. *
  217. * Allocates memory with no particular alignment requirement. @c
  218. * new_ptr will be aligned to at least a multiple of sizeof(void*).
  219. * If @c old_ptr is non-NULL, then the contents of the newly allocated
  220. * memory will be the same as the contents of the previously allocated
  221. * memory, up to the minimum of the old and new sizes. The old memory
  222. * will be freed.
  223. *
  224. * If allocation fails the previously allocated block is left
  225. * untouched and NULL is returned.
  226. *
  227. * Calling realloc() with a new size of zero is a valid way to free a
  228. * memory block.
  229. */
  230. void * realloc ( void *old_ptr, size_t new_size ) {
  231. struct autosized_block *old_block;
  232. struct autosized_block *new_block;
  233. size_t old_total_size;
  234. size_t new_total_size;
  235. size_t old_size;
  236. void *new_ptr = NOWHERE;
  237. /* Allocate new memory if necessary. If allocation fails,
  238. * return without touching the old block.
  239. */
  240. if ( new_size ) {
  241. new_total_size = ( new_size +
  242. offsetof ( struct autosized_block, data ) );
  243. new_block = alloc_memblock ( new_total_size, 1 );
  244. if ( ! new_block )
  245. return NULL;
  246. new_block->size = new_total_size;
  247. new_ptr = &new_block->data;
  248. }
  249. /* Copy across relevant part of the old data region (if any),
  250. * then free it. Note that at this point either (a) new_ptr
  251. * is valid, or (b) new_size is 0; either way, the memcpy() is
  252. * valid.
  253. */
  254. if ( old_ptr && ( old_ptr != NOWHERE ) ) {
  255. old_block = container_of ( old_ptr, struct autosized_block,
  256. data );
  257. old_total_size = old_block->size;
  258. old_size = ( old_total_size -
  259. offsetof ( struct autosized_block, data ) );
  260. memcpy ( new_ptr, old_ptr,
  261. ( ( old_size < new_size ) ? old_size : new_size ) );
  262. free_memblock ( old_block, old_total_size );
  263. }
  264. return new_ptr;
  265. }
  266. /**
  267. * Allocate memory
  268. *
  269. * @v size Requested size
  270. * @ret ptr Memory, or NULL
  271. *
  272. * Allocates memory with no particular alignment requirement. @c ptr
  273. * will be aligned to at least a multiple of sizeof(void*).
  274. */
  275. void * malloc ( size_t size ) {
  276. return realloc ( NULL, size );
  277. }
  278. /**
  279. * Free memory
  280. *
  281. * @v ptr Memory allocated by malloc(), or NULL
  282. *
  283. * Memory allocated with malloc_dma() cannot be freed with free(); it
  284. * must be freed with free_dma() instead.
  285. *
  286. * If @c ptr is NULL, no action is taken.
  287. */
  288. void free ( void *ptr ) {
  289. realloc ( ptr, 0 );
  290. }
  291. /**
  292. * Allocate cleared memory
  293. *
  294. * @v size Requested size
  295. * @ret ptr Allocated memory
  296. *
  297. * Allocate memory as per malloc(), and zero it.
  298. *
  299. * This function name is non-standard, but pretty intuitive.
  300. * zalloc(size) is always equivalent to calloc(1,size)
  301. */
  302. void * zalloc ( size_t size ) {
  303. void *data;
  304. data = malloc ( size );
  305. if ( data )
  306. memset ( data, 0, size );
  307. return data;
  308. }
  309. /**
  310. * Add memory to allocation pool
  311. *
  312. * @v start Start address
  313. * @v end End address
  314. *
  315. * Adds a block of memory [start,end) to the allocation pool. This is
  316. * a one-way operation; there is no way to reclaim this memory.
  317. *
  318. * @c start must be aligned to at least a multiple of sizeof(void*).
  319. */
  320. void mpopulate ( void *start, size_t len ) {
  321. /* Prevent free_memblock() from rounding up len beyond the end
  322. * of what we were actually given...
  323. */
  324. free_memblock ( start, ( len & ~( MIN_MEMBLOCK_SIZE - 1 ) ) );
  325. }
  326. /**
  327. * Initialise the heap
  328. *
  329. */
  330. static void init_heap ( void ) {
  331. mpopulate ( heap, sizeof ( heap ) );
  332. }
  333. /** Memory allocator initialisation function */
  334. struct init_fn heap_init_fn __init_fn ( INIT_EARLY ) = {
  335. .initialise = init_heap,
  336. };
  337. #if 0
  338. #include <stdio.h>
  339. /**
  340. * Dump free block list
  341. *
  342. */
  343. void mdumpfree ( void ) {
  344. struct memory_block *block;
  345. printf ( "Free block list:\n" );
  346. list_for_each_entry ( block, &free_blocks, list ) {
  347. printf ( "[%p,%p] (size %#zx)\n", block,
  348. ( ( ( void * ) block ) + block->size ), block->size );
  349. }
  350. }
  351. #endif