add alignment memory allocation.

git-svn-id: https://rt-thread.googlecode.com/svn/trunk@1928 bbd45198-f89e-11dd-88c7-29a3b14d5316
This commit is contained in:
bernard.xiong@gmail.com 2012-01-30 12:22:57 +00:00
parent fcf01d1d3d
commit 18888679cd
3 changed files with 63 additions and 3 deletions

View File

@ -30,9 +30,9 @@ extern "C" {
*/ */
/*@{*/ /*@{*/
/* RT-Thread version information */ /* RT-Thread version information */
#define RT_VERSION 1L /**< major version number */ #define RT_VERSION 1L /**< major version number */
#define RT_SUBVERSION 0L /**< minor version number */ #define RT_SUBVERSION 1L /**< minor version number */
#define RT_REVISION 0L /**< revise version number */ #define RT_REVISION 0L /**< revise version number */
/* RT-Thread basic data type definitions */ /* RT-Thread basic data type definitions */
@ -334,7 +334,7 @@ typedef struct rt_timer *rt_timer_t;
/** /**
* thread control command definitions * thread control command definitions
*/ */
#define RT_THREAD_CTRL_STARTUP 0x00 /**< Starup thread. */ #define RT_THREAD_CTRL_STARTUP 0x00 /**< Startup thread. */
#define RT_THREAD_CTRL_CLOSE 0x01 /**< Close thread. */ #define RT_THREAD_CTRL_CLOSE 0x01 /**< Close thread. */
#define RT_THREAD_CTRL_CHANGE_PRIORITY 0x02 /**< Change thread priority. */ #define RT_THREAD_CTRL_CHANGE_PRIORITY 0x02 /**< Change thread priority. */
#define RT_THREAD_CTRL_INFO 0x03 /**< Get thread information. */ #define RT_THREAD_CTRL_INFO 0x03 /**< Get thread information. */

View File

@ -179,6 +179,8 @@ void *rt_malloc(rt_size_t nbytes);
void rt_free(void *ptr); void rt_free(void *ptr);
void *rt_realloc(void *ptr, rt_size_t nbytes); void *rt_realloc(void *ptr, rt_size_t nbytes);
void *rt_calloc(rt_size_t count, rt_size_t size); void *rt_calloc(rt_size_t count, rt_size_t size);
void* rt_malloc_align(rt_size_t size, rt_size_t align);
void rt_free_align(void* ptr);
void rt_memory_info(rt_uint32_t *total, rt_uint32_t *used, rt_uint32_t *max_used); void rt_memory_info(rt_uint32_t *total, rt_uint32_t *used, rt_uint32_t *max_used);

View File

@ -1042,6 +1042,64 @@ void rt_kprintf(const char *fmt, ...)
} }
#endif #endif
#ifdef RT_USING_HEAP
/**
* This function allocates a memory block, which address is aligned to the
* specified alignment size.
*
* @param size the allocated memory block size
* @param align the alignment size
*
* @return the allocated memory block on successful, otherwise returns RT_NULL
*/
void* rt_malloc_align(rt_size_t size, rt_size_t align)
{
void *align_ptr;
void *ptr;
rt_size_t align_size;
/* align the alignment size to 4 byte */
align = ((align + 0x03) & ~0x03);
/* get total aligned size */
align_size = ((size + 0x03) & ~0x03) + align;
/* allocate memory block from heap */
ptr = rt_malloc(align_size);
if (ptr != RT_NULL)
{
if (((rt_uint32_t)ptr & (align - 1)) == 0) /* the allocated memory block is aligned */
{
align_ptr = (void*) ((rt_uint32_t)ptr + align);
}
else
{
align_ptr = (void*) (((rt_uint32_t)ptr + (align - 1)) & ~(align - 1));
}
/* set the pointer before alignment pointer to the real pointer */
*((rt_uint32_t*)((rt_uint32_t)align_ptr - sizeof(void*))) = (rt_uint32_t)ptr;
ptr = align_ptr;
}
return ptr;
}
/**
* This function release the memory block, which is allocated by rt_malloc_align
* function and address is aligned.
*
* @param ptr the memory block pointer
*/
void rt_free_align(void* ptr)
{
void* real_ptr;
real_ptr = (void*)*(rt_uint32_t*)((rt_uint32_t)ptr - sizeof(void*));
rt_free(real_ptr);
}
#endif
#if !defined (RT_USING_NEWLIB) && defined (RT_USING_MINILIBC) && defined (__GNUC__) #if !defined (RT_USING_NEWLIB) && defined (RT_USING_MINILIBC) && defined (__GNUC__)
#include <sys/types.h> #include <sys/types.h>
void *memcpy(void *dest, const void *src, size_t n) __attribute__((weak, alias("rt_memcpy"))); void *memcpy(void *dest, const void *src, size_t n) __attribute__((weak, alias("rt_memcpy")));