Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

malloc.c 10KB

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