呵呵 不会告诉你怎么用~skr~
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
// config
#define mem_debug printf
//#define MD_MEM_STATS
//#define MD_DEBUG_MEM
void md_system_heap_init(void *begin_addr, void *end_addr);
void *md_malloc(size_t size);
void *md_realloc(void *rmem, size_t newsize);
void *md_calloc(size_t count, size_t size);
void md_free(void *rmem);
#ifdef MD_MEM_STATS
void md_mem_info(uint32_t *total, uint32_t *used, uint32_t *max_used);
void md_list_mem(void);
#endif
#define MD_ASSERT(EX)
//#define MD_ASSERT(EX) if (!(EX)) { while(1) }
#define MD_DEBUG_NOT_IN_INTERRUPT
#define MD_ALIGN_SIZE 4
#define MD_ALIGN(size, align) (((size) + (align) - 1) & ~((align) - 1))
#define MD_ALIGN_DOWN(size, align) ((size) & ~((align) - 1))
#define MD_HEAP_MAGIC 0x1ea0
struct md_heap_mem
{
/* magic and used flag */
uint16_t magic;
uint16_t used;
size_t next, prev;
};
/** pointer to the heap: for alignment, heap_ptr is now a pointer instead of an array */
static uint8_t *heap_ptr;
/** the last entry, always unused! */
static struct md_heap_mem *heap_end;
#define MIN_SIZE 12
#define MIN_SIZE_ALIGNED MD_ALIGN(MIN_SIZE, MD_ALIGN_SIZE)
#define SIZEOF_STRUCT_MEM MD_ALIGN(sizeof(struct md_heap_mem), MD_ALIGN_SIZE)
static struct md_heap_mem *md_mem_lfree; /* pointer to the lowest free block */
static size_t mem_size_aligned;
#ifdef MD_MEM_STATS
static size_t md_used_mem, md_max_mem;
#endif
static void plug_holes(struct md_heap_mem *mem)
{
struct md_heap_mem *nmem;
struct md_heap_mem *pmem;
MD_ASSERT((uint8_t *)mem >= heap_ptr);
MD_ASSERT((uint8_t *)mem < (uint8_t *)heap_end);
MD_ASSERT(mem->used == 0);
/* plug hole forward */
nmem = (struct md_heap_mem *)&heap_ptr[mem->next];
if (mem != nmem &&
nmem->used == 0 &&
(uint8_t *)nmem != (uint8_t *)heap_end)
{
/* if mem->next is unused and not end of heap_ptr,
* combine mem and mem->next
*/
if (md_mem_lfree == nmem)
{
md_mem_lfree = mem;
}
mem->next = nmem->next;
((struct md_heap_mem *)&heap_ptr[nmem->next])->prev = (uint8_t *)mem - heap_ptr;
}
/* plug hole backward */
pmem = (struct md_heap_mem *)&heap_ptr[mem->prev];
if (pmem != mem && pmem->used == 0)
{
/* if mem->prev is unused, combine mem and mem->prev */
if (md_mem_lfree == mem)
{
md_mem_lfree = pmem;
}
pmem->next = mem->next;
((struct md_heap_mem *)&heap_ptr[mem->next])->prev = (uint8_t *)pmem - heap_ptr;
}
}
/**
* @ingroup SystemInit
*
* This function will initialize system heap memory.
*
* @param begin_addr the beginning address of system heap memory.
* @param end_addr the end address of system heap memory.
*/
void md_system_heap_init(void *begin_addr, void *end_addr)
{
struct md_heap_mem *mem;
uint32_t begin_align = MD_ALIGN((uint32_t)begin_addr, MD_ALIGN_SIZE);
uint32_t end_align = MD_ALIGN_DOWN((uint32_t)end_addr, MD_ALIGN_SIZE);
MD_DEBUG_NOT_IN_INTERRUPT;
/* alignment addr */
if ((end_align > (2 * SIZEOF_STRUCT_MEM)) &&
((end_align - 2 * SIZEOF_STRUCT_MEM) >= begin_align)) {
/* calculate the aligned memory size */
mem_size_aligned = end_align - begin_align - 2 * SIZEOF_STRUCT_MEM;
} else {
mem_debug("mem init, error begin address 0x%x, and end address 0x%x\n",
(uint32_t)begin_addr, (uint32_t)end_addr);
return;
}
/* point to begin address of heap */
heap_ptr = (uint8_t *)begin_align;
#ifdef MD_DEBUG_MEM
mem_debug("mem init, heap begin address 0x%x, size %d\n", (uint32_t)heap_ptr, mem_size_aligned);
#endif
/* initialize the start of the heap */
mem = (struct md_heap_mem *)heap_ptr;
mem->magic = MD_HEAP_MAGIC;
mem->next = mem_size_aligned + SIZEOF_STRUCT_MEM;
mem->prev = 0;
mem->used = 0;
/* initialize the end of the heap */
heap_end = (struct md_heap_mem *)&heap_ptr[mem->next];
heap_end->magic = MD_HEAP_MAGIC;
heap_end->used = 1;
heap_end->next = mem_size_aligned + SIZEOF_STRUCT_MEM;
heap_end->prev = mem_size_aligned + SIZEOF_STRUCT_MEM;
/* initialize the lowest-free pointer to the start of the heap */
md_mem_lfree = (struct md_heap_mem *)heap_ptr;
}
/**
* @addtogroup MM
*/
/*@{*/
/**
* Allocate a block of memory with a minimum of 'size' bytes.
*
* @param size is the minimum size of the requested block in bytes.
*
* @return pointer to allocated memory or NULL if no free memory was found.
*/
void *md_malloc(size_t size)
{
size_t ptr, ptr2;
struct md_heap_mem *mem, *mem2;
MD_DEBUG_NOT_IN_INTERRUPT;
if (size == 0)
return NULL;
#ifdef MD_DEBUG_MEM
if (size != MD_ALIGN(size, MD_ALIGN_SIZE))
mem_debug("malloc size %d, but align to %d\n", size, MD_ALIGN(size, MD_ALIGN_SIZE));
else
mem_debug("malloc size %d\n", size);
#endif
/* alignment size */
size = MD_ALIGN(size, MD_ALIGN_SIZE);
if (size > mem_size_aligned) {
#ifdef MD_DEBUG_MEM
mem_debug("no memory\n");
#endif
return NULL;
}
/* every data block must be at least MIN_SIZE_ALIGNED long */
if (size < MIN_SIZE_ALIGNED)
size = MIN_SIZE_ALIGNED;
for (ptr = (uint8_t *)md_mem_lfree - heap_ptr;
ptr < mem_size_aligned - size;
ptr = ((struct md_heap_mem *)&heap_ptr[ptr])->next)
{
mem = (struct md_heap_mem *)&heap_ptr[ptr];
if ((!mem->used) && (mem->next - (ptr + SIZEOF_STRUCT_MEM)) >= size)
{
/* mem is not used and at least perfect fit is possible:
* mem->next - (ptr + SIZEOF_STRUCT_MEM) gives us the 'user data size' of mem */
if (mem->next - (ptr + SIZEOF_STRUCT_MEM) >=
(size + SIZEOF_STRUCT_MEM + MIN_SIZE_ALIGNED))
{
/* (in addition to the above, we test if another struct md_heap_mem (SIZEOF_STRUCT_MEM) containing
* at least MIN_SIZE_ALIGNED of data also fits in the 'user data space' of 'mem')
* -> split large block, create empty remainder,
* remainder must be large enough to contain MIN_SIZE_ALIGNED data: if
* mem->next - (ptr + (2*SIZEOF_STRUCT_MEM)) == size,
* struct md_heap_mem would fit in but no data between mem2 and mem2->next
* @todo we could leave out MIN_SIZE_ALIGNED. We would create an empty
* region that couldn't hold data, but when mem->next gets freed,
* the 2 regions would be combined, resulting in more free memory
*/
ptr2 = ptr + SIZEOF_STRUCT_MEM + size;
/* create mem2 struct */
mem2 = (struct md_heap_mem *)&heap_ptr[ptr2];
mem2->used = 0;
mem2->next = mem->next;
mem2->prev = ptr;
/* and insert it between mem and mem->next */
mem->next = ptr2;
mem->used = 1;
if (mem2->next != mem_size_aligned + SIZEOF_STRUCT_MEM)
{
((struct md_heap_mem *)&heap_ptr[mem2->next])->prev = ptr2;
}
#ifdef MD_MEM_STATS
md_used_mem += (size + SIZEOF_STRUCT_MEM);
if (md_max_mem < md_used_mem)
md_max_mem = md_used_mem;
#endif
}
else
{
/* (a mem2 struct does no fit into the user data space of mem and mem->next will always
* be used at this point: if not we have 2 unused structs in a row, plug_holes should have
* take care of this).
* -> near fit or excact fit: do not split, no mem2 creation
* also can't move mem->next directly behind mem, since mem->next
* will always be used at this point!
*/
mem->used = 1;
#ifdef MD_MEM_STATS
md_used_mem += mem->next - ((uint8_t*)mem - heap_ptr);
if (md_max_mem < md_used_mem)
md_max_mem = md_used_mem;
#endif
}
/* set memory block magic */
mem->magic = MD_HEAP_MAGIC;
if (mem == md_mem_lfree)
{
/* Find next free block after mem and update lowest free pointer */
while (md_mem_lfree->used && md_mem_lfree != heap_end)
md_mem_lfree = (struct md_heap_mem *)&heap_ptr[md_mem_lfree->next];
MD_ASSERT(((md_mem_lfree == heap_end) || (!md_mem_lfree->used)));
}
MD_ASSERT((uint32_t)mem + SIZEOF_STRUCT_MEM + size <= (uint32_t)heap_end);
MD_ASSERT((uint32_t)((uint8_t *)mem + SIZEOF_STRUCT_MEM) % MD_ALIGN_SIZE == 0);
MD_ASSERT((((uint32_t)mem) & (MD_ALIGN_SIZE-1)) == 0);
#ifdef MD_DEBUG_MEM
mem_debug("allocate memory at 0x%x, size: %d\n",
(uint32_t)((uint8_t *)mem + SIZEOF_STRUCT_MEM),
(uint32_t)(mem->next - ((uint8_t *)mem - heap_ptr)));
#endif
/* return the memory data except mem struct */
return (uint8_t *)mem + SIZEOF_STRUCT_MEM;
}
}
return NULL;
}
/**
* This function will change the previously allocated memory block.
*
* @param rmem pointer to memory allocated by md_malloc
* @param newsize the required new size
*
* @return the changed memory block address
*/
void *md_realloc(void *rmem, size_t newsize)
{
size_t size;
size_t ptr, ptr2;
struct md_heap_mem *mem, *mem2;
void *nmem;
MD_DEBUG_NOT_IN_INTERRUPT;
/* alignment size */
newsize = MD_ALIGN(newsize, MD_ALIGN_SIZE);
if (newsize > mem_size_aligned) {
#ifdef MD_DEBUG_MEM
mem_debug("realloc: out of memory\n");
#endif
return NULL;
}
/* allocate a new memory block */
if (rmem == NULL)
return md_malloc(newsize);
if ((uint8_t *)rmem < (uint8_t *)heap_ptr ||
(uint8_t *)rmem >= (uint8_t *)heap_end)
{
/* illegal memory */
return rmem;
}
mem = (struct md_heap_mem *)((uint8_t *)rmem - SIZEOF_STRUCT_MEM);
ptr = (uint8_t *)mem - heap_ptr;
size = mem->next - ptr - SIZEOF_STRUCT_MEM;
if (size == newsize)
{
/* the size is the same as */
return rmem;
}
if (newsize + SIZEOF_STRUCT_MEM + MIN_SIZE < size)
{
/* split memory block */
#ifdef MD_MEM_STATS
md_used_mem -= (size - newsize);
#endif
ptr2 = ptr + SIZEOF_STRUCT_MEM + newsize;
mem2 = (struct md_heap_mem *)&heap_ptr[ptr2];
mem2->magic= MD_HEAP_MAGIC;
mem2->used = 0;
mem2->next = mem->next;
mem2->prev = ptr;
mem->next = ptr2;
if (mem2->next != mem_size_aligned + SIZEOF_STRUCT_MEM)
{
((struct md_heap_mem *)&heap_ptr[mem2->next])->prev = ptr2;
}
plug_holes(mem2);
return rmem;
}
/* expand memory */
nmem = md_malloc(newsize);
if (nmem != NULL) /* check memory */
{
memcpy(nmem, rmem, size < newsize ? size : newsize);
md_free(rmem);
}
return nmem;
}
/**
* This function will contiguously allocate enough space for count objects
* that are size bytes of memory each and returns a pointer to the allocated
* memory.
*
* The allocated memory is filled with bytes of value zero.
*
* @param count number of objects to allocate
* @param size size of the objects to allocate
*
* @return pointer to allocated memory / NULL pointer if there is an error
*/
void *md_calloc(size_t count, size_t size)
{
void *p;
MD_DEBUG_NOT_IN_INTERRUPT;
/* allocate 'count' objects of size 'size' */
p = md_malloc(count * size);
/* zero the memory */
if (p)
memset(p, 0, count * size);
return p;
}
/**
* This function will release the previously allocated memory block by
* md_malloc. The released memory block is taken back to system heap.
*
* @param rmem the address of memory which will be released
*/
void md_free(void *rmem)
{
struct md_heap_mem *mem;
MD_DEBUG_NOT_IN_INTERRUPT;
if (rmem == NULL)
return;
MD_ASSERT((((uint32_t)rmem) & (MD_ALIGN_SIZE-1)) == 0);
MD_ASSERT((uint8_t *)rmem >= (uint8_t *)heap_ptr &&
(uint8_t *)rmem < (uint8_t *)heap_end);
if ((uint8_t *)rmem < (uint8_t *)heap_ptr ||
(uint8_t *)rmem >= (uint8_t *)heap_end)
{
#ifdef MD_DEBUG_MEM
mem_debug("illegal memory\n");
#endif
return;
}
/* Get the corresponding struct md_heap_mem ... */
mem = (struct md_heap_mem *)((uint8_t *)rmem - SIZEOF_STRUCT_MEM);
#ifdef MD_DEBUG_MEM
mem_debug("release memory 0x%x, size: %d\n", (uint32_t)rmem, (uint32_t)(mem->next - ((uint8_t *)mem - heap_ptr)));
#endif
/* ... which has to be in a used state ... */
MD_ASSERT(mem->used);
MD_ASSERT(mem->magic == MD_HEAP_MAGIC);
/* ... and is now unused. */
mem->used = 0;
mem->magic = 0;
if (mem < md_mem_lfree) {
/* the newly freed struct is now the lowest */
md_mem_lfree = mem;
}
#ifdef MD_MEM_STATS
md_used_mem -= (mem->next - ((uint8_t*)mem - heap_ptr));
#endif
/* finally, see if prev or next are free also */
plug_holes(mem);
}
#ifdef MD_MEM_STATS
void md_mem_info(uint32_t *total, uint32_t *used, uint32_t *max_used)
{
if (total != NULL)
*total = mem_size_aligned;
if (used != NULL)
*used = md_used_mem;
if (max_used != NULL)
*max_used = md_max_mem;
}
void md_list_mem(void)
{
mem_debug("total memory: %d\n", mem_size_aligned);
mem_debug("used memory : %d\n", md_used_mem);
mem_debug("maximum allocated memory: %d\n", md_max_mem);
}
#endif