summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author[email protected] <[email protected]>2012-01-30 12:22:57 +0000
committer[email protected] <[email protected]>2012-01-30 12:22:57 +0000
commit18888679cd2008b8a5fbc3082f2e562f63c767b3 (patch)
treeab0de2ba037aa57882a9692c70aae0a91049c82f
parentfcf01d1d3d08e3fed2af2f35d49defa21bfb09be (diff)
add alignment memory allocation.
git-svn-id: https://rt-thread.googlecode.com/svn/trunk@1928 bbd45198-f89e-11dd-88c7-29a3b14d5316
-rw-r--r--include/rtdef.h6
-rw-r--r--include/rtthread.h2
-rw-r--r--src/kservice.c58
3 files changed, 63 insertions, 3 deletions
diff --git a/include/rtdef.h b/include/rtdef.h
index 18055b8f90..0847db737d 100644
--- a/include/rtdef.h
+++ b/include/rtdef.h
@@ -30,9 +30,9 @@ extern "C" {
*/
/*@{*/
-/* RT-Thread version information */
+/* RT-Thread version information */
#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 */
/* RT-Thread basic data type definitions */
@@ -334,7 +334,7 @@ typedef struct rt_timer *rt_timer_t;
/**
* 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_CHANGE_PRIORITY 0x02 /**< Change thread priority. */
#define RT_THREAD_CTRL_INFO 0x03 /**< Get thread information. */
diff --git a/include/rtthread.h b/include/rtthread.h
index eb7e2560a5..1cf0532627 100644
--- a/include/rtthread.h
+++ b/include/rtthread.h
@@ -179,6 +179,8 @@ void *rt_malloc(rt_size_t nbytes);
void rt_free(void *ptr);
void *rt_realloc(void *ptr, rt_size_t nbytes);
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);
diff --git a/src/kservice.c b/src/kservice.c
index 14fb19b225..0a96de77c8 100644
--- a/src/kservice.c
+++ b/src/kservice.c
@@ -1042,6 +1042,64 @@ void rt_kprintf(const char *fmt, ...)
}
#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__)
#include <sys/types.h>
void *memcpy(void *dest, const void *src, size_t n) __attribute__((weak, alias("rt_memcpy")));