malloc.h File Reference

Dynamic memory allocation. More...

#include <stdint.h>
#include <stdlib.h>

Go to the source code of this file.

Functions

 FILE_LICENCE (GPL2_OR_LATER)
void *__malloc alloc_memblock (size_t size, size_t align)
 Allocate a memory block.
void free_memblock (void *ptr, size_t size)
 Free a memory block.
void mpopulate (void *start, size_t len)
 Add memory to allocation pool.
void mdumpfree (void)
static void *__malloc malloc_dma (size_t size, size_t phys_align)
 Allocate memory for DMA.
static void free_dma (void *ptr, size_t size)
 Free memory allocated with malloc_dma().

Variables

size_t freemem
 Total amount of free memory.


Detailed Description

Dynamic memory allocation.

Definition in file malloc.h.


Function Documentation

FILE_LICENCE ( GPL2_OR_LATER   ) 

void* __malloc alloc_memblock ( size_t  size,
size_t  align 
)

Allocate a memory block.

Parameters:
size Requested size
align Physical alignment
Return values:
ptr Memory block, or NULL
Allocates a memory block physically aligned as requested. No guarantees are provided for the alignment of the virtual address.

align must be a power of two. size may not be zero.

Definition at line 100 of file malloc.c.

References DBG, freemem, memory_block::list, list_add, list_del, list_for_each_entry, MIN_MEMBLOCK_SIZE, NULL, memory_block::size, and virt_to_phys().

Referenced by malloc_dma(), mtnic_alloc_aligned(), and realloc().

00100                                                     {
00101         struct memory_block *block;
00102         size_t align_mask;
00103         size_t pre_size;
00104         ssize_t post_size;
00105         struct memory_block *pre;
00106         struct memory_block *post;
00107 
00108         /* Round up size to multiple of MIN_MEMBLOCK_SIZE and
00109          * calculate alignment mask.
00110          */
00111         size = ( size + MIN_MEMBLOCK_SIZE - 1 ) & ~( MIN_MEMBLOCK_SIZE - 1 );
00112         align_mask = ( align - 1 ) | ( MIN_MEMBLOCK_SIZE - 1 );
00113 
00114         DBG ( "Allocating %#zx (aligned %#zx)\n", size, align );
00115 
00116         /* Search through blocks for the first one with enough space */
00117         list_for_each_entry ( block, &free_blocks, list ) {
00118                 pre_size = ( - virt_to_phys ( block ) ) & align_mask;
00119                 post_size = block->size - pre_size - size;
00120                 if ( post_size >= 0 ) {
00121                         /* Split block into pre-block, block, and
00122                          * post-block.  After this split, the "pre"
00123                          * block is the one currently linked into the
00124                          * free list.
00125                          */
00126                         pre   = block;
00127                         block = ( ( ( void * ) pre   ) + pre_size );
00128                         post  = ( ( ( void * ) block ) + size     );
00129                         DBG ( "[%p,%p) -> [%p,%p) + [%p,%p)\n", pre,
00130                               ( ( ( void * ) pre ) + pre->size ), pre, block,
00131                               post, ( ( ( void * ) pre ) + pre->size ) );
00132                         /* If there is a "post" block, add it in to
00133                          * the free list.  Leak it if it is too small
00134                          * (which can happen only at the very end of
00135                          * the heap).
00136                          */
00137                         if ( ( size_t ) post_size >= MIN_MEMBLOCK_SIZE ) {
00138                                 post->size = post_size;
00139                                 list_add ( &post->list, &pre->list );
00140                         }
00141                         /* Shrink "pre" block, leaving the main block
00142                          * isolated and no longer part of the free
00143                          * list.
00144                          */
00145                         pre->size = pre_size;
00146                         /* If there is no "pre" block, remove it from
00147                          * the list.  Also remove it (i.e. leak it) if
00148                          * it is too small, which can happen only at
00149                          * the very start of the heap.
00150                          */
00151                         if ( pre_size < MIN_MEMBLOCK_SIZE )
00152                                 list_del ( &pre->list );
00153                         /* Update total free memory */
00154                         freemem -= size;
00155                         /* Return allocated block */
00156                         DBG ( "Allocated [%p,%p)\n", block,
00157                               ( ( ( void * ) block ) + size ) );
00158                         return block;
00159                 }
00160         }
00161 
00162         DBG ( "Failed to allocate %#zx (aligned %#zx)\n", size, align );
00163         return NULL;
00164 }

void free_memblock ( void *  ptr,
size_t  size 
)

Free a memory block.

Parameters:
ptr Memory allocated by alloc_memblock(), or NULL
size Size of the memory
If ptr is NULL, no action is taken.

Definition at line 174 of file malloc.c.

References DBG, freemem, memory_block::list, list_add_tail, list_del, list_for_each_entry, MIN_MEMBLOCK_SIZE, and memory_block::size.

Referenced by free_dma(), mpopulate(), mtnic_alloc_cq(), mtnic_alloc_resources(), mtnic_alloc_ring(), mtnic_close(), mtnic_disable(), mtnic_init_card(), mtnic_open(), and realloc().

00174                                               {
00175         struct memory_block *freeing;
00176         struct memory_block *block;
00177         ssize_t gap_before;
00178         ssize_t gap_after = -1;
00179 
00180         /* Allow for ptr==NULL */
00181         if ( ! ptr )
00182                 return;
00183 
00184         /* Round up size to match actual size that alloc_memblock()
00185          * would have used.
00186          */
00187         size = ( size + MIN_MEMBLOCK_SIZE - 1 ) & ~( MIN_MEMBLOCK_SIZE - 1 );
00188         freeing = ptr;
00189         freeing->size = size;
00190         DBG ( "Freeing [%p,%p)\n", freeing, ( ( ( void * ) freeing ) + size ));
00191 
00192         /* Insert/merge into free list */
00193         list_for_each_entry ( block, &free_blocks, list ) {
00194                 /* Calculate gaps before and after the "freeing" block */
00195                 gap_before = ( ( ( void * ) freeing ) - 
00196                                ( ( ( void * ) block ) + block->size ) );
00197                 gap_after = ( ( ( void * ) block ) - 
00198                               ( ( ( void * ) freeing ) + freeing->size ) );
00199                 /* Merge with immediately preceding block, if possible */
00200                 if ( gap_before == 0 ) {
00201                         DBG ( "[%p,%p) + [%p,%p) -> [%p,%p)\n", block,
00202                               ( ( ( void * ) block ) + block->size ), freeing,
00203                               ( ( ( void * ) freeing ) + freeing->size ),block,
00204                               ( ( ( void * ) freeing ) + freeing->size ) );
00205                         block->size += size;
00206                         list_del ( &block->list );
00207                         freeing = block;
00208                 }
00209                 /* Stop processing as soon as we reach a following block */
00210                 if ( gap_after >= 0 )
00211                         break;
00212         }
00213 
00214         /* Insert before the immediately following block.  If
00215          * possible, merge the following block into the "freeing"
00216          * block.
00217          */
00218         DBG ( "[%p,%p)\n", freeing, ( ( ( void * ) freeing ) + freeing->size));
00219         list_add_tail ( &freeing->list, &block->list );
00220         if ( gap_after == 0 ) {
00221                 DBG ( "[%p,%p) + [%p,%p) -> [%p,%p)\n", freeing,
00222                       ( ( ( void * ) freeing ) + freeing->size ), block,
00223                       ( ( ( void * ) block ) + block->size ), freeing,
00224                       ( ( ( void * ) block ) + block->size ) );
00225                 freeing->size += block->size;
00226                 list_del ( &block->list );
00227         }
00228 
00229         /* Update free memory counter */
00230         freemem += size;
00231 }

void mpopulate ( void *  start,
size_t  len 
)

Add memory to allocation pool.

Parameters:
start Start address
end End address
Adds a block of memory [start,end) to the allocation pool. This is a one-way operation; there is no way to reclaim this memory.

start must be aligned to at least a multiple of sizeof(void*).

Definition at line 351 of file malloc.c.

References free_memblock(), and MIN_MEMBLOCK_SIZE.

Referenced by init_heap().

00351                                            {
00352         /* Prevent free_memblock() from rounding up len beyond the end
00353          * of what we were actually given...
00354          */
00355         free_memblock ( start, ( len & ~( MIN_MEMBLOCK_SIZE - 1 ) ) );
00356 }

void mdumpfree ( void   ) 

static void* __malloc malloc_dma ( size_t  size,
size_t  phys_align 
) [inline, static]

static void free_dma ( void *  ptr,
size_t  size 
) [inline, static]

Free memory allocated with malloc_dma().

Parameters:
ptr Memory allocated by malloc_dma(), or NULL
size Size of memory, as passed to malloc_dma()
Memory allocated with malloc_dma() can only be freed with free_dma(); it cannot be freed with the standard free().

If ptr is NULL, no action is taken.

Definition at line 55 of file malloc.h.

References free_memblock().

Referenced by __vxge_hw_fifo_delete(), __vxge_hw_ring_delete(), a3c90x_free_rx_ring(), a3c90x_free_tx_ring(), arbel_create_cq(), arbel_create_eq(), arbel_create_qp(), arbel_destroy_cq(), arbel_destroy_eq(), arbel_destroy_qp(), arbel_probe(), arbel_remove(), ath5k_desc_alloc(), ath5k_desc_free(), atl1e_free_ring_resources(), b44_free_rx_ring(), b44_free_tx_ring(), e1000_free_rx_resources(), e1000_free_tx_resources(), e1000e_free_rx_resources(), e1000e_free_tx_resources(), falcon_free_special_buffer(), free_iob(), hermon_create_cq(), hermon_create_eq(), hermon_create_qp(), hermon_destroy_cq(), hermon_destroy_eq(), hermon_destroy_qp(), hermon_probe(), hermon_remove(), ifec_free(), ifec_net_open(), igb_free_rx_resources(), igb_free_tx_resources(), linda_create_recv_wq(), linda_destroy_recv_wq(), linda_fini_send(), linda_init_send(), myri10ge_net_close(), myri10ge_net_open(), phantom_close(), phantom_create_rx_ctx(), phantom_create_tx_ctx(), phantom_open(), rtl8169_free_rx_resources(), rtl8169_free_tx_resources(), rtl818x_free_rx_ring(), rtl818x_free_tx_ring(), sis190_free(), skge_free(), sky2_free_rings(), sky2_probe(), and sky2_remove().

00055                                                        {
00056         free_memblock ( ptr, size );
00057 }


Variable Documentation

Total amount of free memory.

Definition at line 76 of file malloc.c.

Referenced by alloc_memblock(), free_memblock(), and tcp_xmit().


Generated on Tue Apr 6 20:01:51 2010 for gPXE by  doxygen 1.5.7.1