Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

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