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 9.7KB

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