cleanup kernel code

git-svn-id: https://rt-thread.googlecode.com/svn/trunk@1715 bbd45198-f89e-11dd-88c7-29a3b14d5316
This commit is contained in:
dzzxzz 2011-09-21 03:56:42 +00:00
parent fea7d02bfd
commit c0f80bdf9a
17 changed files with 1012 additions and 1009 deletions

View File

@ -1,7 +1,7 @@
/* /*
* File : clock.c * File : clock.c
* This file is part of RT-Thread RTOS * This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team * COPYRIGHT (C) 2006 - 2011, RT-Thread Development Team
* *
* The license and distribution terms for this file may be * The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at * found in the file LICENSE in this distribution or at
@ -31,7 +31,7 @@ extern void rt_timer_switch(void);
* @ingroup SystemInit * @ingroup SystemInit
* *
*/ */
void rt_system_tick_init() void rt_system_tick_init(void)
{ {
rt_tick = 0; rt_tick = 0;
} }
@ -47,7 +47,7 @@ void rt_system_tick_init()
* *
* @return current tick * @return current tick
*/ */
rt_tick_t rt_tick_get() rt_tick_t rt_tick_get(void)
{ {
/* return the global tick */ /* return the global tick */
return rt_tick; return rt_tick;
@ -70,9 +70,9 @@ void rt_tick_set(rt_tick_t tick)
* This function will notify kernel there is one tick passed. Normally, * This function will notify kernel there is one tick passed. Normally,
* this function is invoked by clock ISR. * this function is invoked by clock ISR.
*/ */
void rt_tick_increase() void rt_tick_increase(void)
{ {
struct rt_thread* thread; struct rt_thread *thread;
/* increase the global tick */ /* increase the global tick */
++ rt_tick; ++ rt_tick;
@ -104,7 +104,7 @@ void rt_tick_increase()
rt_tick_t rt_tick_from_millisecond(rt_uint32_t ms) rt_tick_t rt_tick_from_millisecond(rt_uint32_t ms)
{ {
/* return the calculated tick */ /* return the calculated tick */
return (RT_TICK_PER_SECOND * ms+999) / 1000; return (RT_TICK_PER_SECOND * ms + 999) / 1000;
} }
/*@}*/ /*@}*/

View File

@ -1,7 +1,7 @@
/* /*
* File : device.c * File : device.c
* This file is part of RT-Thread RTOS * This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team * COPYRIGHT (C) 2006 - 2011, RT-Thread Development Team
* *
* The license and distribution terms for this file may be * The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at * found in the file LICENSE in this distribution or at
@ -27,7 +27,7 @@
* *
* @return the error code, RT_EOK on initialization successfully. * @return the error code, RT_EOK on initialization successfully.
*/ */
rt_err_t rt_device_register(rt_device_t dev, const char* name, rt_uint16_t flags) rt_err_t rt_device_register(rt_device_t dev, const char *name, rt_uint16_t flags)
{ {
if (dev == RT_NULL) return -RT_ERROR; if (dev == RT_NULL) return -RT_ERROR;
@ -58,10 +58,10 @@ rt_err_t rt_device_unregister(rt_device_t dev)
* *
* @return the error code, RT_EOK on successfully. * @return the error code, RT_EOK on successfully.
*/ */
rt_err_t rt_device_init_all() rt_err_t rt_device_init_all(void)
{ {
struct rt_device* device; struct rt_device *device;
struct rt_list_node* node; struct rt_list_node *node;
struct rt_object_information *information; struct rt_object_information *information;
register rt_err_t result; register rt_err_t result;
@ -73,7 +73,7 @@ rt_err_t rt_device_init_all()
for (node = information->object_list.next; node != &(information->object_list); node = node->next) for (node = information->object_list.next; node != &(information->object_list); node = node->next)
{ {
rt_err_t (*init)(rt_device_t dev); rt_err_t (*init)(rt_device_t dev);
device = (struct rt_device*)rt_list_entry(node, struct rt_object, list); device = (struct rt_device *)rt_list_entry(node, struct rt_object, list);
/* get device init handler */ /* get device init handler */
init = device->init; init = device->init;
@ -102,10 +102,10 @@ rt_err_t rt_device_init_all()
* *
* @return the registered device driver on successful, or RT_NULL on failure. * @return the registered device driver on successful, or RT_NULL on failure.
*/ */
rt_device_t rt_device_find(const char* name) rt_device_t rt_device_find(const char *name)
{ {
struct rt_object* object; struct rt_object *object;
struct rt_list_node* node; struct rt_list_node *node;
struct rt_object_information *information; struct rt_object_information *information;
extern struct rt_object_information rt_object_container[]; extern struct rt_object_information rt_object_container[];
@ -182,7 +182,7 @@ rt_err_t rt_device_init(rt_device_t dev)
rt_err_t rt_device_open(rt_device_t dev, rt_uint16_t oflag) rt_err_t rt_device_open(rt_device_t dev, rt_uint16_t oflag)
{ {
rt_err_t result; rt_err_t result;
rt_err_t (*open) (rt_device_t dev, rt_uint16_t oflag); rt_err_t (*open)(rt_device_t dev, rt_uint16_t oflag);
RT_ASSERT(dev != RT_NULL); RT_ASSERT(dev != RT_NULL);
@ -205,8 +205,7 @@ rt_err_t rt_device_open(rt_device_t dev, rt_uint16_t oflag)
} }
/* device is a stand alone device and opened */ /* device is a stand alone device and opened */
if ((dev->flag & RT_DEVICE_FLAG_STANDALONE) && if ((dev->flag & RT_DEVICE_FLAG_STANDALONE) && (dev->open_flag & RT_DEVICE_OFLAG_OPEN))
(dev->open_flag & RT_DEVICE_OFLAG_OPEN))
return -RT_EBUSY; return -RT_EBUSY;
/* call device open interface */ /* call device open interface */
@ -273,9 +272,9 @@ rt_err_t rt_device_close(rt_device_t dev)
* *
* @note since 0.4.0, the unit of size/pos is a block for block device. * @note since 0.4.0, the unit of size/pos is a block for block device.
*/ */
rt_size_t rt_device_read (rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size) rt_size_t rt_device_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
{ {
rt_size_t (*read)(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size); rt_size_t (*read)(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size);
RT_ASSERT(dev != RT_NULL); RT_ASSERT(dev != RT_NULL);
@ -303,9 +302,9 @@ rt_size_t rt_device_read (rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t
* *
* @note since 0.4.0, the unit of size/pos is a block for block device. * @note since 0.4.0, the unit of size/pos is a block for block device.
*/ */
rt_size_t rt_device_write(rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size) rt_size_t rt_device_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
{ {
rt_size_t (*write)(rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size); rt_size_t (*write)(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size);
RT_ASSERT(dev != RT_NULL); RT_ASSERT(dev != RT_NULL);
@ -330,9 +329,9 @@ rt_size_t rt_device_write(rt_device_t dev, rt_off_t pos, const void* buffer, rt_
* *
* @return the result * @return the result
*/ */
rt_err_t rt_device_control(rt_device_t dev, rt_uint8_t cmd, void* arg) rt_err_t rt_device_control(rt_device_t dev, rt_uint8_t cmd, void *arg)
{ {
rt_err_t (*control)(rt_device_t dev, rt_uint8_t cmd, void* arg); rt_err_t (*control)(rt_device_t dev, rt_uint8_t cmd, void *arg);
RT_ASSERT(dev != RT_NULL); RT_ASSERT(dev != RT_NULL);
@ -355,7 +354,7 @@ rt_err_t rt_device_control(rt_device_t dev, rt_uint8_t cmd, void* arg)
* *
* @return RT_EOK * @return RT_EOK
*/ */
rt_err_t rt_device_set_rx_indicate(rt_device_t dev, rt_err_t (*rx_ind )(rt_device_t dev, rt_size_t size)) rt_err_t rt_device_set_rx_indicate(rt_device_t dev, rt_err_t (*rx_ind)(rt_device_t dev, rt_size_t size))
{ {
RT_ASSERT(dev != RT_NULL); RT_ASSERT(dev != RT_NULL);

View File

@ -134,7 +134,7 @@ void rt_thread_idle_excute(void)
if ((module->module_thread == RT_NULL) && if ((module->module_thread == RT_NULL) &&
rt_list_isempty(&module->module_object[RT_Object_Class_Thread].object_list) ) rt_list_isempty(&module->module_object[RT_Object_Class_Thread].object_list) )
{ {
module->nref--; module->nref --;
} }
} }
@ -144,7 +144,7 @@ void rt_thread_idle_excute(void)
} }
} }
static void rt_thread_idle_entry(void* parameter) static void rt_thread_idle_entry(void *parameter)
{ {
while (1) while (1)
{ {

138
src/ipc.c
View File

@ -42,9 +42,9 @@
#include "kservice.h" #include "kservice.h"
#ifdef RT_USING_HOOK #ifdef RT_USING_HOOK
extern void (*rt_object_trytake_hook)(struct rt_object* object); extern void (*rt_object_trytake_hook)(struct rt_object *object);
extern void (*rt_object_take_hook)(struct rt_object* object); extern void (*rt_object_take_hook)(struct rt_object *object);
extern void (*rt_object_put_hook)(struct rt_object* object); extern void (*rt_object_put_hook)(struct rt_object *object);
#endif #endif
/** /**
@ -90,8 +90,8 @@ rt_inline rt_err_t rt_ipc_list_suspend(rt_list_t *list, struct rt_thread *thread
case RT_IPC_FLAG_PRIO: case RT_IPC_FLAG_PRIO:
{ {
struct rt_list_node* n; struct rt_list_node *n;
struct rt_thread* sthread; struct rt_thread *sthread;
/* find a suitable position */ /* find a suitable position */
for (n = list->next; n != list; n = n->next) for (n = list->next; n != list; n = n->next)
@ -126,9 +126,9 @@ rt_inline rt_err_t rt_ipc_list_suspend(rt_list_t *list, struct rt_thread *thread
* *
* @return the operation status, RT_EOK on successful * @return the operation status, RT_EOK on successful
*/ */
rt_inline rt_err_t rt_ipc_list_resume(rt_list_t* list) rt_inline rt_err_t rt_ipc_list_resume(rt_list_t *list)
{ {
struct rt_thread* thread; struct rt_thread *thread;
/* get thread entry */ /* get thread entry */
thread = rt_list_entry(list->next, struct rt_thread, tlist); thread = rt_list_entry(list->next, struct rt_thread, tlist);
@ -149,9 +149,9 @@ rt_inline rt_err_t rt_ipc_list_resume(rt_list_t* list)
* *
* @return the operation status, RT_EOK on successful * @return the operation status, RT_EOK on successful
*/ */
rt_inline rt_err_t rt_ipc_list_resume_all(rt_list_t* list) rt_inline rt_err_t rt_ipc_list_resume_all(rt_list_t *list)
{ {
struct rt_thread* thread; struct rt_thread *thread;
register rt_ubase_t temp; register rt_ubase_t temp;
/* wakeup all suspend threads */ /* wakeup all suspend threads */
@ -191,7 +191,7 @@ rt_inline rt_err_t rt_ipc_list_resume_all(rt_list_t* list)
* *
* @return the operation status, RT_EOK on successful * @return the operation status, RT_EOK on successful
*/ */
rt_err_t rt_sem_init(rt_sem_t sem, const char* name, rt_uint32_t value, rt_uint8_t flag) rt_err_t rt_sem_init(rt_sem_t sem, const char *name, rt_uint32_t value, rt_uint8_t flag)
{ {
RT_ASSERT(sem != RT_NULL); RT_ASSERT(sem != RT_NULL);
@ -244,7 +244,7 @@ rt_err_t rt_sem_detach(rt_sem_t sem)
* *
* @see rt_sem_init * @see rt_sem_init
*/ */
rt_sem_t rt_sem_create(const char* name, rt_uint32_t value, rt_uint8_t flag) rt_sem_t rt_sem_create(const char *name, rt_uint32_t value, rt_uint8_t flag)
{ {
rt_sem_t sem; rt_sem_t sem;
@ -303,7 +303,7 @@ rt_err_t rt_sem_delete(rt_sem_t sem)
rt_err_t rt_sem_take(rt_sem_t sem, rt_int32_t time) rt_err_t rt_sem_take(rt_sem_t sem, rt_int32_t time)
{ {
register rt_base_t temp; register rt_base_t temp;
struct rt_thread* thread; struct rt_thread *thread;
RT_ASSERT(sem != RT_NULL); RT_ASSERT(sem != RT_NULL);
@ -314,7 +314,7 @@ rt_err_t rt_sem_take(rt_sem_t sem, rt_int32_t time)
RT_DEBUG_LOG(RT_DEBUG_IPC, RT_DEBUG_LOG(RT_DEBUG_IPC,
("thread %s take sem:%s, which value is: %d\n", rt_thread_self()->name, ("thread %s take sem:%s, which value is: %d\n", rt_thread_self()->name,
((struct rt_object*)sem)->name, sem->value)); ((struct rt_object *)sem)->name, sem->value));
if (sem->value > 0) if (sem->value > 0)
{ {
@ -344,8 +344,7 @@ rt_err_t rt_sem_take(rt_sem_t sem, rt_int32_t time)
/* reset thread error number */ /* reset thread error number */
thread->error = RT_EOK; thread->error = RT_EOK;
RT_DEBUG_LOG(RT_DEBUG_IPC, RT_DEBUG_LOG(RT_DEBUG_IPC, ("sem take: suspend thread - %s\n", thread->name));
("sem take: suspend thread - %s\n", thread->name));
/* suspend thread */ /* suspend thread */
rt_ipc_list_suspend(&(sem->parent.suspend_thread), rt_ipc_list_suspend(&(sem->parent.suspend_thread),
@ -413,7 +412,7 @@ rt_err_t rt_sem_release(rt_sem_t sem)
RT_DEBUG_LOG(RT_DEBUG_IPC, RT_DEBUG_LOG(RT_DEBUG_IPC,
("thread %s releases sem:%s, which value is: %d\n", rt_thread_self()->name, ("thread %s releases sem:%s, which value is: %d\n", rt_thread_self()->name,
((struct rt_object*)sem)->name, sem->value)); ((struct rt_object *)sem)->name, sem->value));
if (!rt_list_isempty(&sem->parent.suspend_thread)) if (!rt_list_isempty(&sem->parent.suspend_thread))
@ -442,7 +441,7 @@ rt_err_t rt_sem_release(rt_sem_t sem)
* *
* @return the error code * @return the error code
*/ */
rt_err_t rt_sem_control(rt_sem_t sem, rt_uint8_t cmd, void* arg) rt_err_t rt_sem_control(rt_sem_t sem, rt_uint8_t cmd, void *arg)
{ {
rt_ubase_t level; rt_ubase_t level;
RT_ASSERT(sem != RT_NULL); RT_ASSERT(sem != RT_NULL);
@ -483,7 +482,7 @@ rt_err_t rt_sem_control(rt_sem_t sem, rt_uint8_t cmd, void* arg)
* *
* @return the operation status, RT_EOK on successful * @return the operation status, RT_EOK on successful
*/ */
rt_err_t rt_mutex_init(rt_mutex_t mutex, const char* name, rt_uint8_t flag) rt_err_t rt_mutex_init(rt_mutex_t mutex, const char *name, rt_uint8_t flag)
{ {
RT_ASSERT(mutex != RT_NULL); RT_ASSERT(mutex != RT_NULL);
@ -537,9 +536,9 @@ rt_err_t rt_mutex_detach(rt_mutex_t mutex)
* *
* @see rt_mutex_init * @see rt_mutex_init
*/ */
rt_mutex_t rt_mutex_create(const char* name, rt_uint8_t flag) rt_mutex_t rt_mutex_create(const char *name, rt_uint8_t flag)
{ {
struct rt_mutex* mutex; struct rt_mutex *mutex;
RT_DEBUG_NOT_IN_INTERRUPT; RT_DEBUG_NOT_IN_INTERRUPT;
@ -598,7 +597,7 @@ rt_err_t rt_mutex_delete(rt_mutex_t mutex)
rt_err_t rt_mutex_take(rt_mutex_t mutex, rt_int32_t time) rt_err_t rt_mutex_take(rt_mutex_t mutex, rt_int32_t time)
{ {
register rt_base_t temp; register rt_base_t temp;
struct rt_thread* thread; struct rt_thread *thread;
/* this function must not be used in interrupt even if time = 0 */ /* this function must not be used in interrupt even if time = 0 */
RT_DEBUG_NOT_IN_INTERRUPT; RT_DEBUG_NOT_IN_INTERRUPT;
@ -722,7 +721,7 @@ rt_err_t rt_mutex_take(rt_mutex_t mutex, rt_int32_t time)
rt_err_t rt_mutex_release(rt_mutex_t mutex) rt_err_t rt_mutex_release(rt_mutex_t mutex)
{ {
register rt_base_t temp; register rt_base_t temp;
struct rt_thread* thread; struct rt_thread *thread;
rt_bool_t need_schedule; rt_bool_t need_schedule;
need_schedule = RT_FALSE; need_schedule = RT_FALSE;
@ -768,8 +767,7 @@ rt_err_t rt_mutex_release(rt_mutex_t mutex)
/* get suspended thread */ /* get suspended thread */
thread = rt_list_entry(mutex->parent.suspend_thread.next, struct rt_thread, tlist); thread = rt_list_entry(mutex->parent.suspend_thread.next, struct rt_thread, tlist);
RT_DEBUG_LOG(RT_DEBUG_IPC, RT_DEBUG_LOG(RT_DEBUG_IPC, ("mutex_release: resume thread: %s\n", thread->name));
("mutex_release: resume thread: %s\n", thread->name));
/* set new owner and priority */ /* set new owner and priority */
mutex->owner = thread; mutex->owner = thread;
@ -810,7 +808,7 @@ rt_err_t rt_mutex_release(rt_mutex_t mutex)
* *
* @return the error code * @return the error code
*/ */
rt_err_t rt_mutex_control(rt_mutex_t mutex, rt_uint8_t cmd, void* arg) rt_err_t rt_mutex_control(rt_mutex_t mutex, rt_uint8_t cmd, void *arg)
{ {
return -RT_ERROR; return -RT_ERROR;
} }
@ -827,7 +825,7 @@ rt_err_t rt_mutex_control(rt_mutex_t mutex, rt_uint8_t cmd, void* arg)
* *
* @return the operation status, RT_EOK on successful * @return the operation status, RT_EOK on successful
*/ */
rt_err_t rt_event_init(rt_event_t event, const char* name, rt_uint8_t flag) rt_err_t rt_event_init(rt_event_t event, const char *name, rt_uint8_t flag)
{ {
RT_ASSERT(event != RT_NULL); RT_ASSERT(event != RT_NULL);
@ -876,7 +874,7 @@ rt_err_t rt_event_detach(rt_event_t event)
* *
* @return the created event, RT_NULL on error happen * @return the created event, RT_NULL on error happen
*/ */
rt_event_t rt_event_create(const char* name, rt_uint8_t flag) rt_event_t rt_event_create(const char *name, rt_uint8_t flag)
{ {
rt_event_t event; rt_event_t event;
@ -933,8 +931,8 @@ rt_err_t rt_event_delete(rt_event_t event)
*/ */
rt_err_t rt_event_send(rt_event_t event, rt_uint32_t set) rt_err_t rt_event_send(rt_event_t event, rt_uint32_t set)
{ {
struct rt_list_node* n; struct rt_list_node *n;
struct rt_thread* thread; struct rt_thread *thread;
register rt_ubase_t level; register rt_ubase_t level;
register rt_base_t status; register rt_base_t status;
rt_bool_t need_schedule; rt_bool_t need_schedule;
@ -1023,9 +1021,9 @@ rt_err_t rt_event_send(rt_event_t event, rt_uint32_t set)
* *
* @return the error code * @return the error code
*/ */
rt_err_t rt_event_recv(rt_event_t event, rt_uint32_t set, rt_uint8_t option, rt_int32_t timeout, rt_uint32_t* recved) rt_err_t rt_event_recv(rt_event_t event, rt_uint32_t set, rt_uint8_t option, rt_int32_t timeout, rt_uint32_t *recved)
{ {
struct rt_thread* thread; struct rt_thread *thread;
register rt_ubase_t level; register rt_ubase_t level;
register rt_base_t status; register rt_base_t status;
@ -1077,8 +1075,7 @@ rt_err_t rt_event_recv(rt_event_t event, rt_uint32_t set, rt_uint8_t option, rt_
thread->event_info = option; thread->event_info = option;
/* put thread to suspended thread list */ /* put thread to suspended thread list */
rt_ipc_list_suspend(&(event->parent.suspend_thread), rt_ipc_list_suspend(&(event->parent.suspend_thread), thread, event->parent.parent.flag);
thread, event->parent.parent.flag);
/* if there is a waiting timeout, active thread timer */ /* if there is a waiting timeout, active thread timer */
if (timeout > 0) if (timeout > 0)
@ -1124,7 +1121,7 @@ rt_err_t rt_event_recv(rt_event_t event, rt_uint32_t set, rt_uint8_t option, rt_
* *
* @return the error code * @return the error code
*/ */
rt_err_t rt_event_control(rt_event_t event, rt_uint8_t cmd, void* arg) rt_err_t rt_event_control(rt_event_t event, rt_uint8_t cmd, void *arg)
{ {
rt_ubase_t level; rt_ubase_t level;
RT_ASSERT(event != RT_NULL); RT_ASSERT(event != RT_NULL);
@ -1163,7 +1160,7 @@ rt_err_t rt_event_control(rt_event_t event, rt_uint8_t cmd, void* arg)
* *
* @return the operation status, RT_EOK on successful * @return the operation status, RT_EOK on successful
*/ */
rt_err_t rt_mb_init(rt_mailbox_t mb, const char* name, void* msgpool, rt_size_t size, rt_uint8_t flag) rt_err_t rt_mb_init(rt_mailbox_t mb, const char *name, void *msgpool, rt_size_t size, rt_uint8_t flag)
{ {
RT_ASSERT(mb != RT_NULL); RT_ASSERT(mb != RT_NULL);
@ -1222,7 +1219,7 @@ rt_err_t rt_mb_detach(rt_mailbox_t mb)
* *
* @return the created mailbox, RT_NULL on error happen * @return the created mailbox, RT_NULL on error happen
*/ */
rt_mailbox_t rt_mb_create(const char* name, rt_size_t size, rt_uint8_t flag) rt_mailbox_t rt_mb_create(const char *name, rt_size_t size, rt_uint8_t flag)
{ {
rt_mailbox_t mb; rt_mailbox_t mb;
@ -1307,7 +1304,7 @@ rt_err_t rt_mb_delete(rt_mailbox_t mb)
*/ */
rt_err_t rt_mb_send_wait(rt_mailbox_t mb, rt_uint32_t value, rt_int32_t timeout) rt_err_t rt_mb_send_wait(rt_mailbox_t mb, rt_uint32_t value, rt_int32_t timeout)
{ {
struct rt_thread* thread; struct rt_thread *thread;
register rt_ubase_t temp; register rt_ubase_t temp;
rt_uint32_t tick_delta; rt_uint32_t tick_delta;
@ -1347,8 +1344,7 @@ rt_err_t rt_mb_send_wait(rt_mailbox_t mb, rt_uint32_t value, rt_int32_t timeout)
RT_DEBUG_NOT_IN_INTERRUPT; RT_DEBUG_NOT_IN_INTERRUPT;
/* suspend current thread */ /* suspend current thread */
rt_ipc_list_suspend(&(mb->suspend_sender_thread), rt_ipc_list_suspend(&(mb->suspend_sender_thread), thread, mb->parent.parent.flag);
thread, mb->parent.parent.flag);
/* has waiting time, start thread timer */ /* has waiting time, start thread timer */
if (timeout > 0) if (timeout > 0)
@ -1440,9 +1436,9 @@ rt_err_t rt_mb_send(rt_mailbox_t mb, rt_uint32_t value)
* *
* @return the error code * @return the error code
*/ */
rt_err_t rt_mb_recv(rt_mailbox_t mb, rt_uint32_t* value, rt_int32_t timeout) rt_err_t rt_mb_recv(rt_mailbox_t mb, rt_uint32_t *value, rt_int32_t timeout)
{ {
struct rt_thread* thread; struct rt_thread *thread;
register rt_ubase_t temp; register rt_ubase_t temp;
rt_uint32_t tick_delta; rt_uint32_t tick_delta;
@ -1484,8 +1480,7 @@ rt_err_t rt_mb_recv(rt_mailbox_t mb, rt_uint32_t* value, rt_int32_t timeout)
RT_DEBUG_NOT_IN_INTERRUPT; RT_DEBUG_NOT_IN_INTERRUPT;
/* suspend current thread */ /* suspend current thread */
rt_ipc_list_suspend(&(mb->parent.suspend_thread), rt_ipc_list_suspend(&(mb->parent.suspend_thread), thread, mb->parent.parent.flag);
thread, mb->parent.parent.flag);
/* has waiting time, start thread timer */ /* has waiting time, start thread timer */
if (timeout > 0) if (timeout > 0)
@ -1530,7 +1525,7 @@ rt_err_t rt_mb_recv(rt_mailbox_t mb, rt_uint32_t* value, rt_int32_t timeout)
*value = mb->msg_pool[mb->out_offset]; *value = mb->msg_pool[mb->out_offset];
/* increase output offset */ /* increase output offset */
++mb->out_offset; ++ mb->out_offset;
if (mb->out_offset >= mb->size) mb->out_offset = 0; if (mb->out_offset >= mb->size) mb->out_offset = 0;
/* decrease message entry */ /* decrease message entry */
mb->entry --; mb->entry --;
@ -1567,7 +1562,7 @@ rt_err_t rt_mb_recv(rt_mailbox_t mb, rt_uint32_t* value, rt_int32_t timeout)
* *
* @return the error code * @return the error code
*/ */
rt_err_t rt_mb_control(rt_mailbox_t mb, rt_uint8_t cmd, void* arg) rt_err_t rt_mb_control(rt_mailbox_t mb, rt_uint8_t cmd, void *arg)
{ {
rt_ubase_t level; rt_ubase_t level;
RT_ASSERT(mb != RT_NULL); RT_ASSERT(mb != RT_NULL);
@ -1601,7 +1596,7 @@ rt_err_t rt_mb_control(rt_mailbox_t mb, rt_uint8_t cmd, void* arg)
#ifdef RT_USING_MESSAGEQUEUE #ifdef RT_USING_MESSAGEQUEUE
struct rt_mq_message struct rt_mq_message
{ {
struct rt_mq_message* next; struct rt_mq_message *next;
}; };
/** /**
@ -1617,9 +1612,9 @@ struct rt_mq_message
* *
* @return the operation status, RT_EOK on successful * @return the operation status, RT_EOK on successful
*/ */
rt_err_t rt_mq_init(rt_mq_t mq, const char* name, void *msgpool, rt_size_t msg_size, rt_size_t pool_size, rt_uint8_t flag) rt_err_t rt_mq_init(rt_mq_t mq, const char *name, void *msgpool, rt_size_t msg_size, rt_size_t pool_size, rt_uint8_t flag)
{ {
struct rt_mq_message* head; struct rt_mq_message *head;
register rt_base_t temp; register rt_base_t temp;
/* parameter check */ /* parameter check */
@ -1649,7 +1644,7 @@ rt_err_t rt_mq_init(rt_mq_t mq, const char* name, void *msgpool, rt_size_t msg_s
mq->msg_queue_free = RT_NULL; mq->msg_queue_free = RT_NULL;
for (temp = 0; temp < mq->max_msgs; temp ++) for (temp = 0; temp < mq->max_msgs; temp ++)
{ {
head = (struct rt_mq_message*)((rt_uint8_t*)mq->msg_pool + head = (struct rt_mq_message *)((rt_uint8_t *)mq->msg_pool +
temp * (mq->msg_size + sizeof(struct rt_mq_message))); temp * (mq->msg_size + sizeof(struct rt_mq_message)));
head->next = mq->msg_queue_free; head->next = mq->msg_queue_free;
mq->msg_queue_free = head; mq->msg_queue_free = head;
@ -1693,10 +1688,10 @@ rt_err_t rt_mq_detach(rt_mq_t mq)
* *
* @return the created message queue, RT_NULL on error happen * @return the created message queue, RT_NULL on error happen
*/ */
rt_mq_t rt_mq_create(const char* name, rt_size_t msg_size, rt_size_t max_msgs, rt_uint8_t flag) rt_mq_t rt_mq_create(const char *name, rt_size_t msg_size, rt_size_t max_msgs, rt_uint8_t flag)
{ {
struct rt_messagequeue* mq; struct rt_messagequeue *mq;
struct rt_mq_message* head; struct rt_mq_message *head;
register rt_base_t temp; register rt_base_t temp;
RT_DEBUG_NOT_IN_INTERRUPT; RT_DEBUG_NOT_IN_INTERRUPT;
@ -1789,10 +1784,10 @@ rt_err_t rt_mq_delete(rt_mq_t mq)
* *
* @return the error code * @return the error code
*/ */
rt_err_t rt_mq_send(rt_mq_t mq, void* buffer, rt_size_t size) rt_err_t rt_mq_send(rt_mq_t mq, void *buffer, rt_size_t size)
{ {
register rt_ubase_t temp; register rt_ubase_t temp;
struct rt_mq_message* msg; struct rt_mq_message *msg;
/* greater than one message size */ /* greater than one message size */
if (size > mq->msg_size) return -RT_ERROR; if (size > mq->msg_size) return -RT_ERROR;
@ -1829,7 +1824,7 @@ rt_err_t rt_mq_send(rt_mq_t mq, void* buffer, rt_size_t size)
if (mq->msg_queue_tail != RT_NULL) if (mq->msg_queue_tail != RT_NULL)
{ {
/* if the tail exists, */ /* if the tail exists, */
((struct rt_mq_message*)mq->msg_queue_tail)->next = msg; ((struct rt_mq_message *)mq->msg_queue_tail)->next = msg;
} }
/* set new tail */ /* set new tail */
@ -1869,10 +1864,10 @@ rt_err_t rt_mq_send(rt_mq_t mq, void* buffer, rt_size_t size)
* *
* @return the error code * @return the error code
*/ */
rt_err_t rt_mq_urgent(rt_mq_t mq, void* buffer, rt_size_t size) rt_err_t rt_mq_urgent(rt_mq_t mq, void *buffer, rt_size_t size)
{ {
register rt_ubase_t temp; register rt_ubase_t temp;
struct rt_mq_message* msg; struct rt_mq_message *msg;
/* greater than one message size */ /* greater than one message size */
if (size > mq->msg_size) return -RT_ERROR; if (size > mq->msg_size) return -RT_ERROR;
@ -1883,7 +1878,7 @@ rt_err_t rt_mq_urgent(rt_mq_t mq, void* buffer, rt_size_t size)
temp = rt_hw_interrupt_disable(); temp = rt_hw_interrupt_disable();
/* get a free list, there must be an empty item */ /* get a free list, there must be an empty item */
msg = (struct rt_mq_message*)mq->msg_queue_free; msg = (struct rt_mq_message *)mq->msg_queue_free;
/* message queue is full */ /* message queue is full */
if (msg == RT_NULL) if (msg == RT_NULL)
{ {
@ -1943,11 +1938,11 @@ rt_err_t rt_mq_urgent(rt_mq_t mq, void* buffer, rt_size_t size)
* *
* @return the error code * @return the error code
*/ */
rt_err_t rt_mq_recv(rt_mq_t mq, void* buffer, rt_size_t size, rt_int32_t timeout) rt_err_t rt_mq_recv(rt_mq_t mq, void *buffer, rt_size_t size, rt_int32_t timeout)
{ {
struct rt_thread* thread; struct rt_thread *thread;
register rt_ubase_t temp; register rt_ubase_t temp;
struct rt_mq_message* msg; struct rt_mq_message *msg;
rt_uint32_t tick_delta; rt_uint32_t tick_delta;
/* initialize delta tick */ /* initialize delta tick */
@ -1985,8 +1980,7 @@ rt_err_t rt_mq_recv(rt_mq_t mq, void* buffer, rt_size_t size, rt_int32_t timeout
} }
/* suspend current thread */ /* suspend current thread */
rt_ipc_list_suspend(&(mq->parent.suspend_thread), rt_ipc_list_suspend(&(mq->parent.suspend_thread), thread, mq->parent.parent.flag);
thread, mq->parent.parent.flag);
/* has waiting time, start thread timer */ /* has waiting time, start thread timer */
if (timeout > 0) if (timeout > 0)
@ -1994,8 +1988,7 @@ rt_err_t rt_mq_recv(rt_mq_t mq, void* buffer, rt_size_t size, rt_int32_t timeout
/* get the start tick of timer */ /* get the start tick of timer */
tick_delta = rt_tick_get(); tick_delta = rt_tick_get();
RT_DEBUG_LOG(RT_DEBUG_IPC, RT_DEBUG_LOG(RT_DEBUG_IPC, ("set thread:%s to timer list\n", thread->name));
("set thread:%s to timer list\n", thread->name));
/* reset the timeout of thread timer and start it */ /* reset the timeout of thread timer and start it */
rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, &timeout); rt_timer_control(&(thread->thread_timer), RT_TIMER_CTRL_SET_TIME, &timeout);
@ -2028,7 +2021,7 @@ rt_err_t rt_mq_recv(rt_mq_t mq, void* buffer, rt_size_t size, rt_int32_t timeout
} }
/* get message from queue */ /* get message from queue */
msg = (struct rt_mq_message*) mq->msg_queue_head; msg = (struct rt_mq_message *)mq->msg_queue_head;
/* move message queue head */ /* move message queue head */
mq->msg_queue_head = msg->next; mq->msg_queue_head = msg->next;
@ -2042,13 +2035,12 @@ rt_err_t rt_mq_recv(rt_mq_t mq, void* buffer, rt_size_t size, rt_int32_t timeout
rt_hw_interrupt_enable(temp); rt_hw_interrupt_enable(temp);
/* copy message */ /* copy message */
rt_memcpy(buffer, msg + 1, rt_memcpy(buffer, msg + 1, size > mq->msg_size ? mq->msg_size : size);
size > mq->msg_size? mq->msg_size : size);
/* disable interrupt */ /* disable interrupt */
temp = rt_hw_interrupt_disable(); temp = rt_hw_interrupt_disable();
/* put message to free list */ /* put message to free list */
msg->next = (struct rt_mq_message*)mq->msg_queue_free; msg->next = (struct rt_mq_message *)mq->msg_queue_free;
mq->msg_queue_free = msg; mq->msg_queue_free = msg;
/* enable interrupt */ /* enable interrupt */
rt_hw_interrupt_enable(temp); rt_hw_interrupt_enable(temp);
@ -2067,10 +2059,10 @@ rt_err_t rt_mq_recv(rt_mq_t mq, void* buffer, rt_size_t size, rt_int32_t timeout
* *
* @return the error code * @return the error code
*/ */
rt_err_t rt_mq_control(rt_mq_t mq, rt_uint8_t cmd, void* arg) rt_err_t rt_mq_control(rt_mq_t mq, rt_uint8_t cmd, void *arg)
{ {
rt_ubase_t level; rt_ubase_t level;
struct rt_mq_message* msg; struct rt_mq_message *msg;
RT_ASSERT(mq != RT_NULL); RT_ASSERT(mq != RT_NULL);
@ -2086,7 +2078,7 @@ rt_err_t rt_mq_control(rt_mq_t mq, rt_uint8_t cmd, void* arg)
while (mq->msg_queue_head != RT_NULL) while (mq->msg_queue_head != RT_NULL)
{ {
/* get message from queue */ /* get message from queue */
msg = (struct rt_mq_message*) mq->msg_queue_head; msg = (struct rt_mq_message *)mq->msg_queue_head;
/* move message queue head */ /* move message queue head */
mq->msg_queue_head = msg->next; mq->msg_queue_head = msg->next;
@ -2094,7 +2086,7 @@ rt_err_t rt_mq_control(rt_mq_t mq, rt_uint8_t cmd, void* arg)
if (mq->msg_queue_tail == msg) mq->msg_queue_tail = RT_NULL; if (mq->msg_queue_tail == msg) mq->msg_queue_tail = RT_NULL;
/* put message to free list */ /* put message to free list */
msg->next = (struct rt_mq_message*)mq->msg_queue_free; msg->next = (struct rt_mq_message *)mq->msg_queue_free;
mq->msg_queue_free = msg; mq->msg_queue_free = msg;
} }

View File

@ -1,7 +1,7 @@
/* /*
* File : irq.c * File : irq.c
* This file is part of RT-Thread RTOS * This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team * COPYRIGHT (C) 2006 - 2011, RT-Thread Development Team
* *
* The license and distribution terms for this file may be * The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at * found in the file LICENSE in this distribution or at
@ -33,11 +33,11 @@ volatile rt_uint8_t rt_interrupt_nest;
* *
* @see rt_interrupt_leave * @see rt_interrupt_leave
*/ */
void rt_interrupt_enter() void rt_interrupt_enter(void)
{ {
rt_base_t level; rt_base_t level;
RT_DEBUG_LOG(RT_DEBUG_IRQ,("irq comming..., irq nest:%d\n", rt_interrupt_nest)); RT_DEBUG_LOG(RT_DEBUG_IRQ, ("irq comming..., irq nest:%d\n", rt_interrupt_nest));
level = rt_hw_interrupt_disable(); level = rt_hw_interrupt_disable();
rt_interrupt_nest ++; rt_interrupt_nest ++;
@ -51,11 +51,11 @@ void rt_interrupt_enter()
* *
* @see rt_interrupt_enter * @see rt_interrupt_enter
*/ */
void rt_interrupt_leave() void rt_interrupt_leave(void)
{ {
rt_base_t level; rt_base_t level;
RT_DEBUG_LOG(RT_DEBUG_IRQ,("irq leave, irq nest:%d\n", rt_interrupt_nest)); RT_DEBUG_LOG(RT_DEBUG_IRQ, ("irq leave, irq nest:%d\n", rt_interrupt_nest));
level = rt_hw_interrupt_disable(); level = rt_hw_interrupt_disable();
rt_interrupt_nest --; rt_interrupt_nest --;

View File

@ -1,7 +1,7 @@
/* /*
* File : kservice.c * File : kservice.c
* This file is part of RT-Thread RTOS * This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team * COPYRIGHT (C) 2006 - 2011, RT-Thread Development Team
* *
* The license and distribution terms for this file may be * The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at * found in the file LICENSE in this distribution or at
@ -26,7 +26,7 @@
/*@{*/ /*@{*/
#ifndef RT_USING_NEWLIB #ifndef RT_USING_NEWLIB
/* global errno in RT-Thread*/ /* global errno in RT-Thread */
static volatile int _errno; static volatile int _errno;
#else #else
#include <errno.h> #include <errno.h>
@ -44,7 +44,7 @@ rt_err_t rt_get_errno(void)
{ {
rt_thread_t tid; rt_thread_t tid;
if(rt_interrupt_get_nest() != 0) if (rt_interrupt_get_nest() != 0)
{ {
/* it's in interrupt context */ /* it's in interrupt context */
return _errno; return _errno;
@ -65,7 +65,7 @@ void rt_set_errno(rt_err_t error)
{ {
rt_thread_t tid; rt_thread_t tid;
if(rt_interrupt_get_nest() != 0) if (rt_interrupt_get_nest() != 0)
{ {
/* it's in interrupt context */ /* it's in interrupt context */
_errno = error; _errno = error;
@ -105,10 +105,10 @@ int *_rt_errno(void)
* @return the address of source memory * @return the address of source memory
* *
*/ */
void *rt_memset(void * s, int c, rt_ubase_t count) void *rt_memset(void *s, int c, rt_ubase_t count)
{ {
#ifdef RT_TINY_SIZE #ifdef RT_TINY_SIZE
char *xs = (char *) s; char *xs = (char *)s;
while (count--) while (count--)
*xs++ = c; *xs++ = c;
@ -125,10 +125,10 @@ void *rt_memset(void * s, int c, rt_ubase_t count)
rt_uint32_t *aligned_addr; rt_uint32_t *aligned_addr;
rt_uint32_t d = c & 0xff; rt_uint32_t d = c & 0xff;
if (!TOO_SMALL (count) && !UNALIGNED (s)) if (!TOO_SMALL(count) && !UNALIGNED(s))
{ {
/* If we get this far, we know that n is large and m is word-aligned. */ /* If we get this far, we know that n is large and m is word-aligned. */
aligned_addr = (rt_uint32_t*)s; aligned_addr = (rt_uint32_t *)s;
/* Store D into each char sized location in BUFFER so that /* Store D into each char sized location in BUFFER so that
* we can set large blocks quickly. * we can set large blocks quickly.
@ -161,7 +161,7 @@ void *rt_memset(void * s, int c, rt_ubase_t count)
} }
/* Pick up the remainder with a bytewise loop. */ /* Pick up the remainder with a bytewise loop. */
m = (char*)aligned_addr; m = (char *)aligned_addr;
} }
while (count--) while (count--)
@ -188,10 +188,10 @@ void *rt_memset(void * s, int c, rt_ubase_t count)
* @return the address of destination memory * @return the address of destination memory
* *
*/ */
void *rt_memcpy(void * dst, const void *src, rt_ubase_t count) void *rt_memcpy(void *dst, const void *src, rt_ubase_t count)
{ {
#ifdef RT_TINY_SIZE #ifdef RT_TINY_SIZE
char *tmp = (char *) dst, *s = (char *) src; char *tmp = (char *)dst, *s = (char *)src;
while (count--) while (count--)
*tmp++ = *s++; *tmp++ = *s++;
@ -200,23 +200,23 @@ void *rt_memcpy(void * dst, const void *src, rt_ubase_t count)
#else #else
#define UNALIGNED(X, Y) \ #define UNALIGNED(X, Y) \
(((rt_int32_t)X & (sizeof (rt_int32_t) - 1)) | ((rt_int32_t)Y & (sizeof (rt_int32_t) - 1))) (((rt_int32_t)X & (sizeof(rt_int32_t) - 1)) | ((rt_int32_t)Y & (sizeof(rt_int32_t) - 1)))
#define BIGBLOCKSIZE (sizeof (rt_int32_t) << 2) #define BIGBLOCKSIZE (sizeof(rt_int32_t) << 2)
#define LITTLEBLOCKSIZE (sizeof (rt_int32_t)) #define LITTLEBLOCKSIZE (sizeof(rt_int32_t))
#define TOO_SMALL(LEN) ((LEN) < BIGBLOCKSIZE) #define TOO_SMALL(LEN) ((LEN) < BIGBLOCKSIZE)
char *dst_ptr = (char*)dst; char *dst_ptr = (char *)dst;
char *src_ptr = (char*)src; char *src_ptr = (char *)src;
rt_int32_t *aligned_dst; rt_int32_t *aligned_dst;
rt_int32_t *aligned_src; rt_int32_t *aligned_src;
int len = count; int len = count;
/* If the size is small, or either SRC or DST is unaligned, /* If the size is small, or either SRC or DST is unaligned,
then punt into the byte copy loop. This should be rare. */ then punt into the byte copy loop. This should be rare. */
if (!TOO_SMALL(len) && !UNALIGNED (src_ptr, dst_ptr)) if (!TOO_SMALL(len) && !UNALIGNED(src_ptr, dst_ptr))
{ {
aligned_dst = (rt_int32_t*)dst_ptr; aligned_dst = (rt_int32_t *)dst_ptr;
aligned_src = (rt_int32_t*)src_ptr; aligned_src = (rt_int32_t *)src_ptr;
/* Copy 4X long words at a time if possible. */ /* Copy 4X long words at a time if possible. */
while (len >= BIGBLOCKSIZE) while (len >= BIGBLOCKSIZE)
@ -236,8 +236,8 @@ void *rt_memcpy(void * dst, const void *src, rt_ubase_t count)
} }
/* Pick up any residual with a byte copier. */ /* Pick up any residual with a byte copier. */
dst_ptr = (char*)aligned_dst; dst_ptr = (char *)aligned_dst;
src_ptr = (char*)aligned_src; src_ptr = (char *)aligned_src;
} }
while (len--) while (len--)
@ -262,14 +262,14 @@ void *rt_memcpy(void * dst, const void *src, rt_ubase_t count)
* @return the address of destination memory * @return the address of destination memory
* *
*/ */
void* rt_memmove(void *dest, const void *src, rt_ubase_t n) void *rt_memmove(void *dest, const void *src, rt_ubase_t n)
{ {
char *tmp = (char *) dest, *s = (char *) src; char *tmp = (char *)dest, *s = (char *)src;
if (s < tmp && tmp < s + n) if (s < tmp && tmp < s + n)
{ {
tmp+=n; tmp += n;
s+=n; s += n;
while (n--) while (n--)
*tmp-- = *s--; *tmp-- = *s--;
@ -284,17 +284,20 @@ void* rt_memmove(void *dest, const void *src, rt_ubase_t n)
} }
/** /**
* memcmp - Compare two areas of memory * This function will compare two areas of memory
* @param cs: One area of memory *
* @param ct: Another area of memory * @param cs one area of memory
* @param count: The size of the area. * @param ct znother area of memory
* @param count the size of the area
*
* @return the result
*/ */
rt_int32_t rt_memcmp(const void * cs,const void * ct, rt_ubase_t count) rt_int32_t rt_memcmp(const void *cs, const void *ct, rt_ubase_t count)
{ {
const unsigned char *su1, *su2; const unsigned char *su1, *su2;
int res = 0; int res = 0;
for( su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--) for (su1 = cs, su2 = ct; 0 < count; ++su1, ++su2, count--)
if ((res = *su1 - *su2) != 0) if ((res = *su1 - *su2) != 0)
break; break;
return res; return res;
@ -308,20 +311,20 @@ rt_int32_t rt_memcmp(const void * cs,const void * ct, rt_ubase_t count)
* *
* @return the first occurrence of a s2 in s1, or RT_NULL if no found. * @return the first occurrence of a s2 in s1, or RT_NULL if no found.
*/ */
char * rt_strstr(const char * s1,const char * s2) char *rt_strstr(const char *s1, const char *s2)
{ {
int l1, l2; int l1, l2;
l2 = rt_strlen(s2); l2 = rt_strlen(s2);
if (!l2) if (!l2)
return (char *) s1; return (char *)s1;
l1 = rt_strlen(s1); l1 = rt_strlen(s1);
while (l1 >= l2) while (l1 >= l2)
{ {
l1--; l1 --;
if (!rt_memcmp(s1,s2,l2)) if (!rt_memcmp(s1, s2, l2))
return (char *) s1; return (char *)s1;
s1++; s1 ++;
} }
return RT_NULL; return RT_NULL;
} }
@ -363,9 +366,9 @@ rt_uint32_t rt_strcasecmp(const char *a, const char *b)
*/ */
char *rt_strncpy(char *dest, const char *src, rt_ubase_t n) char *rt_strncpy(char *dest, const char *src, rt_ubase_t n)
{ {
char *tmp = (char *) dest, *s = (char *) src; char *tmp = (char *)dest, *s = (char *)src;
while(n--) while (n--)
*tmp++ = *s++; *tmp++ = *s++;
return dest; return dest;
@ -380,7 +383,7 @@ char *rt_strncpy(char *dest, const char *src, rt_ubase_t n)
* *
* @return the result * @return the result
*/ */
rt_ubase_t rt_strncmp(const char * cs, const char * ct, rt_ubase_t count) rt_ubase_t rt_strncmp(const char *cs, const char *ct, rt_ubase_t count)
{ {
register signed char __res = 0; register signed char __res = 0;
@ -388,7 +391,7 @@ rt_ubase_t rt_strncmp(const char * cs, const char * ct, rt_ubase_t count)
{ {
if ((__res = *cs - *ct++) != 0 || !*cs++) if ((__res = *cs - *ct++) != 0 || !*cs++)
break; break;
count--; count --;
} }
return __res; return __res;
@ -402,7 +405,7 @@ rt_ubase_t rt_strncmp(const char * cs, const char * ct, rt_ubase_t count)
* *
* @return the result * @return the result
*/ */
rt_ubase_t rt_strcmp (const char *cs, const char *ct) rt_ubase_t rt_strcmp(const char *cs, const char *ct)
{ {
while (*cs && *cs == *ct) while (*cs && *cs == *ct)
cs++, ct++; cs++, ct++;
@ -440,7 +443,7 @@ char *rt_strdup(const char *s)
rt_size_t len = rt_strlen(s) + 1; rt_size_t len = rt_strlen(s) + 1;
char *tmp = (char *)rt_malloc(len); char *tmp = (char *)rt_malloc(len);
if(!tmp) return RT_NULL; if (!tmp) return RT_NULL;
rt_memcpy(tmp, s, len); rt_memcpy(tmp, s, len);
return tmp; return tmp;
@ -450,7 +453,7 @@ char *rt_strdup(const char *s)
/** /**
* This function will show the version of rt-thread rtos * This function will show the version of rt-thread rtos
*/ */
void rt_show_version() void rt_show_version(void)
{ {
rt_kprintf("\n \\ | /\n"); rt_kprintf("\n \\ | /\n");
rt_kprintf("- RT - Thread Operating System\n"); rt_kprintf("- RT - Thread Operating System\n");
@ -497,9 +500,9 @@ rt_inline int skip_atoi(const char **s)
#define LARGE (1 << 6) /* use 'ABCDEF' instead of 'abcdef' */ #define LARGE (1 << 6) /* use 'ABCDEF' instead of 'abcdef' */
#ifdef RT_PRINTF_PRECISION #ifdef RT_PRINTF_PRECISION
static char *print_number(char * buf, char * end, long num, int base, int s, int precision, int type) static char *print_number(char *buf, char *end, long num, int base, int s, int precision, int type)
#else #else
static char *print_number(char * buf, char * end, long num, int base, int s, int type) static char *print_number(char *buf, char *end, long num, int base, int s, int type)
#endif #endif
{ {
char c, sign; char c, sign;
@ -558,10 +561,10 @@ static char *print_number(char * buf, char * end, long num, int base, int s, int
if (!(type&(ZEROPAD | LEFT))) if (!(type&(ZEROPAD | LEFT)))
{ {
while(size-->0) while (size-->0)
{ {
if (buf <= end) *buf = ' '; if (buf <= end) *buf = ' ';
++buf; ++ buf;
} }
} }
@ -570,9 +573,9 @@ static char *print_number(char * buf, char * end, long num, int base, int s, int
if (buf <= end) if (buf <= end)
{ {
*buf = sign; *buf = sign;
--size; -- size;
} }
++buf; ++ buf;
} }
#ifdef RT_PRINTF_SPECIAL #ifdef RT_PRINTF_SPECIAL
@ -581,17 +584,17 @@ static char *print_number(char * buf, char * end, long num, int base, int s, int
if (base==8) if (base==8)
{ {
if (buf <= end) *buf = '0'; if (buf <= end) *buf = '0';
++buf; ++ buf;
} }
else if (base==16) else if (base == 16)
{ {
if (buf <= end) *buf = '0'; if (buf <= end) *buf = '0';
++buf; ++ buf;
if (buf <= end) if (buf <= end)
{ {
*buf = type & LARGE? 'X' : 'x'; *buf = type & LARGE? 'X' : 'x';
} }
++buf; ++ buf;
} }
} }
#endif #endif
@ -602,7 +605,7 @@ static char *print_number(char * buf, char * end, long num, int base, int s, int
while (size-- > 0) while (size-- > 0)
{ {
if (buf <= end) *buf = c; if (buf <= end) *buf = c;
++buf; ++ buf;
} }
} }
@ -610,7 +613,7 @@ static char *print_number(char * buf, char * end, long num, int base, int s, int
while (i < precision--) while (i < precision--)
{ {
if (buf <= end) *buf = '0'; if (buf <= end) *buf = '0';
++buf; ++ buf;
} }
#endif #endif
@ -618,13 +621,13 @@ static char *print_number(char * buf, char * end, long num, int base, int s, int
while (i-- > 0) while (i-- > 0)
{ {
if (buf <= end) *buf = tmp[i]; if (buf <= end) *buf = tmp[i];
++buf; ++ buf;
} }
while (size-- > 0) while (size-- > 0)
{ {
if (buf <= end) *buf = ' '; if (buf <= end) *buf = ' ';
++buf; ++ buf;
} }
return buf; return buf;
@ -665,17 +668,17 @@ static rt_int32_t vsnprintf(char *buf, rt_size_t size, const char *fmt, va_list
if (*fmt != '%') if (*fmt != '%')
{ {
if (str <= end) *str = *fmt; if (str <= end) *str = *fmt;
++str; ++ str;
continue; continue;
} }
/* process flags */ /* process flags */
flags = 0; flags = 0;
while(1) while (1)
{ {
/* skips the first '%' also */ /* skips the first '%' also */
++fmt; ++ fmt;
if (*fmt == '-') flags |= LEFT; if (*fmt == '-') flags |= LEFT;
else if (*fmt == '+') flags |= PLUS; else if (*fmt == '+') flags |= PLUS;
else if (*fmt == ' ') flags |= SPACE; else if (*fmt == ' ') flags |= SPACE;
@ -689,7 +692,7 @@ static rt_int32_t vsnprintf(char *buf, rt_size_t size, const char *fmt, va_list
if (isdigit(*fmt)) field_width = skip_atoi(&fmt); if (isdigit(*fmt)) field_width = skip_atoi(&fmt);
else if (*fmt == '*') else if (*fmt == '*')
{ {
++fmt; ++ fmt;
/* it's the next argument */ /* it's the next argument */
field_width = va_arg(args, int); field_width = va_arg(args, int);
if (field_width < 0) if (field_width < 0)
@ -704,11 +707,11 @@ static rt_int32_t vsnprintf(char *buf, rt_size_t size, const char *fmt, va_list
precision = -1; precision = -1;
if (*fmt == '.') if (*fmt == '.')
{ {
++fmt; ++ fmt;
if (isdigit(*fmt)) precision = skip_atoi(&fmt); if (isdigit(*fmt)) precision = skip_atoi(&fmt);
else if (*fmt == '*') else if (*fmt == '*')
{ {
++fmt; ++ fmt;
/* it's the next argument */ /* it's the next argument */
precision = va_arg(args, int); precision = va_arg(args, int);
} }
@ -724,12 +727,12 @@ static rt_int32_t vsnprintf(char *buf, rt_size_t size, const char *fmt, va_list
) )
{ {
qualifier = *fmt; qualifier = *fmt;
++fmt; ++ fmt;
#ifdef RT_PRINTF_LONGLONG #ifdef RT_PRINTF_LONGLONG
if (qualifier == 'l' && *fmt == 'l') if (qualifier == 'l' && *fmt == 'l')
{ {
qualifier = 'L'; qualifier = 'L';
++fmt; ++ fmt;
} }
#endif #endif
} }
@ -745,20 +748,20 @@ static rt_int32_t vsnprintf(char *buf, rt_size_t size, const char *fmt, va_list
while (--field_width > 0) while (--field_width > 0)
{ {
if (str <= end) *str = ' '; if (str <= end) *str = ' ';
++str; ++ str;
} }
} }
/* get character */ /* get character */
c = (rt_uint8_t) va_arg(args, int); c = (rt_uint8_t)va_arg(args, int);
if (str <= end) *str = c; if (str <= end) *str = c;
++str; ++ str;
/* put width */ /* put width */
while (--field_width > 0) while (--field_width > 0)
{ {
if (str <= end) *str = ' '; if (str <= end) *str = ' ';
++str; ++ str;
} }
continue; continue;
@ -776,21 +779,21 @@ static rt_int32_t vsnprintf(char *buf, rt_size_t size, const char *fmt, va_list
while (len < field_width--) while (len < field_width--)
{ {
if (str <= end) *str = ' '; if (str <= end) *str = ' ';
++str; ++ str;
} }
} }
for (i = 0; i < len; ++i) for (i = 0; i < len; ++i)
{ {
if (str <= end) *str = *s; if (str <= end) *str = *s;
++str; ++ str;
++s; ++ s;
} }
while (len < field_width--) while (len < field_width--)
{ {
if (str <= end) *str = ' '; if (str <= end) *str = ' ';
++str; ++ str;
} }
continue; continue;
@ -802,18 +805,18 @@ static rt_int32_t vsnprintf(char *buf, rt_size_t size, const char *fmt, va_list
} }
#ifdef RT_PRINTF_PRECISION #ifdef RT_PRINTF_PRECISION
str = print_number(str, end, str = print_number(str, end,
(long) va_arg(args, void *), (long)va_arg(args, void *),
16, field_width, precision, flags); 16, field_width, precision, flags);
#else #else
str = print_number(str, end, str = print_number(str, end,
(long) va_arg(args, void *), (long)va_arg(args, void *),
16, field_width, flags); 16, field_width, flags);
#endif #endif
continue; continue;
case '%': case '%':
if (str <= end) *str = '%'; if (str <= end) *str = '%';
++str; ++ str;
continue; continue;
/* integer number formats - set up the flags and "break" */ /* integer number formats - set up the flags and "break" */
@ -835,16 +838,16 @@ static rt_int32_t vsnprintf(char *buf, rt_size_t size, const char *fmt, va_list
default: default:
if (str <= end) *str = '%'; if (str <= end) *str = '%';
++str; ++ str;
if (*fmt) if (*fmt)
{ {
if (str <= end) *str = *fmt; if (str <= end) *str = *fmt;
++str; ++ str;
} }
else else
{ {
--fmt; -- fmt;
} }
continue; continue;
} }
@ -857,17 +860,17 @@ static rt_int32_t vsnprintf(char *buf, rt_size_t size, const char *fmt, va_list
#endif #endif
{ {
num = va_arg(args, rt_uint32_t); num = va_arg(args, rt_uint32_t);
if (flags & SIGN) num = (rt_int32_t) num; if (flags & SIGN) num = (rt_int32_t)num;
} }
else if (qualifier == 'h') else if (qualifier == 'h')
{ {
num = (rt_uint16_t) va_arg(args, rt_int32_t); num = (rt_uint16_t)va_arg(args, rt_int32_t);
if (flags & SIGN) num = (rt_int16_t) num; if (flags & SIGN) num = (rt_int16_t)num;
} }
else else
{ {
num = va_arg(args, rt_uint32_t); num = va_arg(args, rt_uint32_t);
if (flags & SIGN) num = (rt_int32_t) num; if (flags & SIGN) num = (rt_int32_t)num;
} }
#ifdef RT_PRINTF_PRECISION #ifdef RT_PRINTF_PRECISION
str = print_number(str, end, num, base, field_width, precision, flags); str = print_number(str, end, num, base, field_width, precision, flags);
@ -922,14 +925,14 @@ rt_int32_t rt_vsprintf(char *buf, const char *format, va_list arg_ptr)
* @param buf the buffer to save formatted string * @param buf the buffer to save formatted string
* @param format the format * @param format the format
*/ */
rt_int32_t rt_sprintf(char *buf ,const char *format,...) rt_int32_t rt_sprintf(char *buf, const char *format, ...)
{ {
rt_int32_t n; rt_int32_t n;
va_list arg_ptr; va_list arg_ptr;
va_start(arg_ptr, format); va_start(arg_ptr, format);
n = rt_vsprintf(buf ,format,arg_ptr); n = rt_vsprintf(buf ,format, arg_ptr);
va_end (arg_ptr); va_end(arg_ptr);
return n; return n;
} }
@ -946,7 +949,7 @@ rt_int32_t rt_sprintf(char *buf ,const char *format,...)
* *
* @return the old console device handler * @return the old console device handler
*/ */
rt_device_t rt_console_set_device(const char* name) rt_device_t rt_console_set_device(const char *name)
{ {
rt_device_t new, old; rt_device_t new, old;
@ -973,15 +976,15 @@ rt_device_t rt_console_set_device(const char* name)
#endif #endif
#if defined(__GNUC__) #if defined(__GNUC__)
void rt_hw_console_output(const char* str) __attribute__((weak)); void rt_hw_console_output(const char *str) __attribute__((weak));
void rt_hw_console_output(const char* str) void rt_hw_console_output(const char *str)
#elif defined(__CC_ARM) #elif defined(__CC_ARM)
__weak void rt_hw_console_output(const char* str) __weak void rt_hw_console_output(const char *str)
#elif defined(__IAR_SYSTEMS_ICC__) #elif defined(__IAR_SYSTEMS_ICC__)
#if __VER__ > 540 #if __VER__ > 540
__weak __weak
#endif #endif
void rt_hw_console_output(const char* str) void rt_hw_console_output(const char *str)
#endif #endif
{ {
/* empty console output */ /* empty console output */
@ -1023,9 +1026,9 @@ void rt_kprintf(const char *fmt, ...)
#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")));
void* memset(void *s, int c, size_t n) __attribute__((weak, alias("rt_memset"))); void *memset(void *s, int c, size_t n) __attribute__((weak, alias("rt_memset")));
void* memmove(void *dest, const void *src, size_t n) __attribute__((weak, alias("rt_memmove"))); void *memmove(void *dest, const void *src, size_t n) __attribute__((weak, alias("rt_memmove")));
int memcmp(const void *s1, const void *s2, size_t n) __attribute__((weak, alias("rt_memcmp"))); int memcmp(const void *s1, const void *s2, size_t n) __attribute__((weak, alias("rt_memcmp")));
size_t strlen(const char *s) __attribute__((weak, alias("rt_strlen"))); size_t strlen(const char *s) __attribute__((weak, alias("rt_strlen")));
@ -1037,7 +1040,7 @@ int strncmp(const char *cs, const char *ct, size_t count) __attribute__((weak, a
char *strdup(const char *s) __attribute__((weak, alias("rt_strdup"))); char *strdup(const char *s) __attribute__((weak, alias("rt_strdup")));
#endif #endif
int sprintf(char * buf,const char * format,...) __attribute__((weak, alias("rt_sprintf"))); int sprintf(char *buf, const char *format, ...) __attribute__((weak, alias("rt_sprintf")));
int snprintf(char *buf, rt_size_t size, const char *fmt, ...) __attribute__((weak, alias("rt_snprintf"))); int snprintf(char *buf, rt_size_t size, const char *fmt, ...) __attribute__((weak, alias("rt_snprintf")));
int vsprintf(char *buf, const char *format, va_list arg_ptr) __attribute__((weak, alias("rt_vsprintf"))); int vsprintf(char *buf, const char *format, va_list arg_ptr) __attribute__((weak, alias("rt_vsprintf")));

View File

@ -1,7 +1,7 @@
/* /*
* File : kservice.h * File : kservice.h
* This file is part of RT-Thread RTOS * This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team * COPYRIGHT (C) 2006 - 2011, RT-Thread Development Team
* *
* The license and distribution terms for this file may be * The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at * found in the file LICENSE in this distribution or at

View File

@ -1,7 +1,7 @@
/* /*
* File : mem.c * File : mem.c
* This file is part of RT-Thread RTOS * This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2008 - 2009, RT-Thread Development Team * COPYRIGHT (C) 2008 - 2011, RT-Thread Development Team
* *
* The license and distribution terms for this file may be * The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at * found in the file LICENSE in this distribution or at
@ -165,7 +165,7 @@ static void plug_holes(struct heap_mem *mem)
* @param end_addr the end address of system page * @param end_addr the end address of system page
* *
*/ */
void rt_system_heap_init(void* begin_addr, void* end_addr) void rt_system_heap_init(void *begin_addr, void *end_addr)
{ {
struct heap_mem *mem; struct heap_mem *mem;
rt_uint32_t begin_align = RT_ALIGN((rt_uint32_t)begin_addr, RT_ALIGN_SIZE); rt_uint32_t begin_align = RT_ALIGN((rt_uint32_t)begin_addr, RT_ALIGN_SIZE);
@ -174,12 +174,14 @@ void rt_system_heap_init(void* begin_addr, void* end_addr)
RT_DEBUG_NOT_IN_INTERRUPT; RT_DEBUG_NOT_IN_INTERRUPT;
/* alignment addr */ /* alignment addr */
if((end_align > (2 * SIZEOF_STRUCT_MEM) ) && if ((end_align > (2 * SIZEOF_STRUCT_MEM)) &&
((end_align - 2 * SIZEOF_STRUCT_MEM) >= begin_align )) { ((end_align - 2 * SIZEOF_STRUCT_MEM) >= begin_align))
{
/* calculate the aligned memory size */ /* calculate the aligned memory size */
mem_size_aligned = end_align - begin_align - 2 * SIZEOF_STRUCT_MEM; mem_size_aligned = end_align - begin_align - 2 * SIZEOF_STRUCT_MEM;
} }
else { else
{
rt_kprintf("mem init, error begin address 0x%x, and end address 0x%x\n", (rt_uint32_t)begin_addr, (rt_uint32_t)end_addr); rt_kprintf("mem init, error begin address 0x%x, and end address 0x%x\n", (rt_uint32_t)begin_addr, (rt_uint32_t)end_addr);
return; return;
} }
@ -242,13 +244,13 @@ void *rt_malloc(rt_size_t size)
if (size > mem_size_aligned) if (size > mem_size_aligned)
{ {
RT_DEBUG_LOG(RT_DEBUG_MEM,("no memory\n")); RT_DEBUG_LOG(RT_DEBUG_MEM, ("no memory\n"));
return RT_NULL; return RT_NULL;
} }
/* every data block must be at least MIN_SIZE_ALIGNED long */ /* every data block must be at least MIN_SIZE_ALIGNED long */
if(size < MIN_SIZE_ALIGNED) size = MIN_SIZE_ALIGNED; if (size < MIN_SIZE_ALIGNED) size = MIN_SIZE_ALIGNED;
/* take memory semaphore */ /* take memory semaphore */
rt_sem_take(&heap_sem, RT_WAITING_FOREVER); rt_sem_take(&heap_sem, RT_WAITING_FOREVER);
@ -258,8 +260,7 @@ void *rt_malloc(rt_size_t size)
{ {
mem = (struct heap_mem *)&heap_ptr[ptr]; mem = (struct heap_mem *)&heap_ptr[ptr];
if ((!mem->used) && if ((!mem->used) && (mem->next - (ptr + SIZEOF_STRUCT_MEM)) >= size)
(mem->next - (ptr + SIZEOF_STRUCT_MEM)) >= size)
{ {
/* mem is not used and at least perfect fit is possible: /* 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 */ * mem->next - (ptr + SIZEOF_STRUCT_MEM) gives us the 'user data size' of mem */
@ -328,9 +329,9 @@ void *rt_malloc(rt_size_t size)
RT_ASSERT((rt_uint32_t)((rt_uint8_t *)mem + SIZEOF_STRUCT_MEM) % RT_ALIGN_SIZE == 0); RT_ASSERT((rt_uint32_t)((rt_uint8_t *)mem + SIZEOF_STRUCT_MEM) % RT_ALIGN_SIZE == 0);
RT_ASSERT((((rt_uint32_t)mem) & (RT_ALIGN_SIZE-1)) == 0); RT_ASSERT((((rt_uint32_t)mem) & (RT_ALIGN_SIZE-1)) == 0);
RT_DEBUG_LOG(RT_DEBUG_MEM,("allocate memory at 0x%x, size: %d\n", RT_DEBUG_LOG(RT_DEBUG_MEM, ("allocate memory at 0x%x, size: %d\n",
(rt_uint32_t)((rt_uint8_t*)mem + SIZEOF_STRUCT_MEM), (rt_uint32_t)((rt_uint8_t *)mem + SIZEOF_STRUCT_MEM),
(rt_uint32_t)(mem->next - ((rt_uint8_t*)mem - heap_ptr)))); (rt_uint32_t)(mem->next - ((rt_uint8_t *)mem - heap_ptr))));
RT_OBJECT_HOOK_CALL(rt_malloc_hook, (((void*)((rt_uint8_t *)mem + SIZEOF_STRUCT_MEM)), size)); RT_OBJECT_HOOK_CALL(rt_malloc_hook, (((void*)((rt_uint8_t *)mem + SIZEOF_STRUCT_MEM)), size));
/* return the memory data except mem struct */ /* return the memory data except mem struct */
@ -355,7 +356,7 @@ void *rt_realloc(void *rmem, rt_size_t newsize)
rt_size_t size; rt_size_t size;
rt_size_t ptr, ptr2; rt_size_t ptr, ptr2;
struct heap_mem *mem, *mem2; struct heap_mem *mem, *mem2;
void* nmem; void *nmem;
RT_DEBUG_NOT_IN_INTERRUPT; RT_DEBUG_NOT_IN_INTERRUPT;
@ -363,7 +364,7 @@ void *rt_realloc(void *rmem, rt_size_t newsize)
newsize = RT_ALIGN(newsize, RT_ALIGN_SIZE); newsize = RT_ALIGN(newsize, RT_ALIGN_SIZE);
if (newsize > mem_size_aligned) if (newsize > mem_size_aligned)
{ {
RT_DEBUG_LOG(RT_DEBUG_MEM,("realloc: out of memory\n")); RT_DEBUG_LOG(RT_DEBUG_MEM, ("realloc: out of memory\n"));
return RT_NULL; return RT_NULL;
} }
@ -478,7 +479,7 @@ void rt_free(void *rmem)
if ((rt_uint8_t *)rmem < (rt_uint8_t *)heap_ptr || (rt_uint8_t *)rmem >= (rt_uint8_t *)heap_end) if ((rt_uint8_t *)rmem < (rt_uint8_t *)heap_ptr || (rt_uint8_t *)rmem >= (rt_uint8_t *)heap_end)
{ {
RT_DEBUG_LOG(RT_DEBUG_MEM,("illegal memory\n")); RT_DEBUG_LOG(RT_DEBUG_MEM, ("illegal memory\n"));
return; return;
} }
@ -486,9 +487,9 @@ void rt_free(void *rmem)
/* Get the corresponding struct heap_mem ... */ /* Get the corresponding struct heap_mem ... */
mem = (struct heap_mem *)((rt_uint8_t *)rmem - SIZEOF_STRUCT_MEM); mem = (struct heap_mem *)((rt_uint8_t *)rmem - SIZEOF_STRUCT_MEM);
RT_DEBUG_LOG(RT_DEBUG_MEM,("release memory 0x%x, size: %d\n", RT_DEBUG_LOG(RT_DEBUG_MEM, ("release memory 0x%x, size: %d\n",
(rt_uint32_t)rmem, (rt_uint32_t)rmem,
(rt_uint32_t)(mem->next - ((rt_uint8_t*)mem - heap_ptr)))); (rt_uint32_t)(mem->next - ((rt_uint8_t *)mem - heap_ptr))));
/* protect the heap from concurrent access */ /* protect the heap from concurrent access */
@ -517,9 +518,7 @@ void rt_free(void *rmem)
} }
#ifdef RT_MEM_STATS #ifdef RT_MEM_STATS
void rt_memory_info(rt_uint32_t *total, void rt_memory_info(rt_uint32_t *total, rt_uint32_t *used, rt_uint32_t *max_used)
rt_uint32_t *used,
rt_uint32_t *max_used)
{ {
if (total != RT_NULL) *total = mem_size_aligned; if (total != RT_NULL) *total = mem_size_aligned;
if (used != RT_NULL) *used = used_mem; if (used != RT_NULL) *used = used_mem;
@ -528,7 +527,7 @@ void rt_memory_info(rt_uint32_t *total,
#ifdef RT_USING_FINSH #ifdef RT_USING_FINSH
#include <finsh.h> #include <finsh.h>
void list_mem() void list_mem(void)
{ {
rt_kprintf("total memory: %d\n", mem_size_aligned); rt_kprintf("total memory: %d\n", mem_size_aligned);
rt_kprintf("used memory : %d\n", used_mem); rt_kprintf("used memory : %d\n", used_mem);

View File

@ -1,7 +1,7 @@
/* /*
* File : mempool.c * File : mempool.c
* This file is part of RT-Thread RTOS * This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team * COPYRIGHT (C) 2006 - 2011, RT-Thread Development Team
* *
* The license and distribution terms for this file may be * The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at * found in the file LICENSE in this distribution or at
@ -27,8 +27,8 @@
#ifdef RT_USING_MEMPOOL #ifdef RT_USING_MEMPOOL
#ifdef RT_USING_HOOK #ifdef RT_USING_HOOK
static void (*rt_mp_alloc_hook)(struct rt_mempool* mp, void *block); static void (*rt_mp_alloc_hook)(struct rt_mempool *mp, void *block);
static void (*rt_mp_free_hook)(struct rt_mempool* mp, void *block); static void (*rt_mp_free_hook)(struct rt_mempool *mp, void *block);
/** /**
* @addtogroup Hook * @addtogroup Hook
@ -41,7 +41,7 @@ static void (*rt_mp_free_hook)(struct rt_mempool* mp, void *block);
* *
* @param hook the hook function * @param hook the hook function
*/ */
void rt_mp_alloc_sethook(void (*hook)(struct rt_mempool* mp, void *block)) void rt_mp_alloc_sethook(void (*hook)(struct rt_mempool *mp, void *block))
{ {
rt_mp_alloc_hook = hook; rt_mp_alloc_hook = hook;
} }
@ -52,7 +52,7 @@ void rt_mp_alloc_sethook(void (*hook)(struct rt_mempool* mp, void *block))
* *
* @param hook the hook function * @param hook the hook function
*/ */
void rt_mp_free_sethook(void (*hook)(struct rt_mempool* mp, void *block)) void rt_mp_free_sethook(void (*hook)(struct rt_mempool *mp, void *block))
{ {
rt_mp_free_hook = hook; rt_mp_free_hook = hook;
} }
@ -79,7 +79,7 @@ void rt_mp_free_sethook(void (*hook)(struct rt_mempool* mp, void *block))
* @return RT_EOK * @return RT_EOK
* *
*/ */
rt_err_t rt_mp_init(struct rt_mempool* mp, const char* name, void *start, rt_size_t size, rt_size_t block_size) rt_err_t rt_mp_init(struct rt_mempool *mp, const char *name, void *start, rt_size_t size, rt_size_t block_size)
{ {
rt_uint8_t *block_ptr; rt_uint8_t *block_ptr;
register rt_base_t offset; register rt_base_t offset;
@ -97,7 +97,7 @@ rt_err_t rt_mp_init(struct rt_mempool* mp, const char* name, void *start, rt_siz
mp->block_size = block_size; mp->block_size = block_size;
/* align to align size byte */ /* align to align size byte */
mp->block_total_count = mp->size / (mp->block_size + sizeof(rt_uint8_t*)); mp->block_total_count = mp->size / (mp->block_size + sizeof(rt_uint8_t *));
mp->block_free_count = mp->block_total_count; mp->block_free_count = mp->block_total_count;
/* init suspended thread list */ /* init suspended thread list */
@ -105,14 +105,14 @@ rt_err_t rt_mp_init(struct rt_mempool* mp, const char* name, void *start, rt_siz
mp->suspend_thread_count = 0; mp->suspend_thread_count = 0;
/* init free block list */ /* init free block list */
block_ptr = (rt_uint8_t*) mp->start_address; block_ptr = (rt_uint8_t *)mp->start_address;
for (offset = 0; offset < mp->block_total_count; offset ++) for (offset = 0; offset < mp->block_total_count; offset ++)
{ {
*(rt_uint8_t**)(block_ptr + offset * (block_size + sizeof(rt_uint8_t*))) *(rt_uint8_t **)(block_ptr + offset * (block_size + sizeof(rt_uint8_t *)))
= (rt_uint8_t*)(block_ptr + (offset + 1) * (block_size + sizeof(rt_uint8_t*))); = (rt_uint8_t *)(block_ptr + (offset + 1) * (block_size + sizeof(rt_uint8_t *)));
} }
*(rt_uint8_t**)(block_ptr + (offset - 1) * (block_size + sizeof(rt_uint8_t*))) = RT_NULL; *(rt_uint8_t **)(block_ptr + (offset - 1) * (block_size + sizeof(rt_uint8_t *))) = RT_NULL;
mp->block_list = block_ptr; mp->block_list = block_ptr;
@ -126,9 +126,9 @@ rt_err_t rt_mp_init(struct rt_mempool* mp, const char* name, void *start, rt_siz
* *
* @return RT_EOK * @return RT_EOK
*/ */
rt_err_t rt_mp_detach(struct rt_mempool* mp) rt_err_t rt_mp_detach(struct rt_mempool *mp)
{ {
struct rt_thread* thread; struct rt_thread *thread;
register rt_ubase_t temp; register rt_ubase_t temp;
/* parameter check */ /* parameter check */
@ -176,24 +176,24 @@ rt_err_t rt_mp_detach(struct rt_mempool* mp)
* @return the created mempool object * @return the created mempool object
* *
*/ */
rt_mp_t rt_mp_create(const char* name, rt_size_t block_count, rt_size_t block_size) rt_mp_t rt_mp_create(const char *name, rt_size_t block_count, rt_size_t block_size)
{ {
rt_uint8_t *block_ptr; rt_uint8_t *block_ptr;
struct rt_mempool* mp; struct rt_mempool *mp;
register rt_base_t offset; register rt_base_t offset;
RT_DEBUG_NOT_IN_INTERRUPT; RT_DEBUG_NOT_IN_INTERRUPT;
/* allocate object */ /* allocate object */
mp = (struct rt_mempool*)rt_object_allocate(RT_Object_Class_MemPool, name); mp = (struct rt_mempool *)rt_object_allocate(RT_Object_Class_MemPool, name);
if (mp == RT_NULL) return RT_NULL; /* allocate object failed */ if (mp == RT_NULL) return RT_NULL; /* allocate object failed */
/* init memory pool */ /* init memory pool */
mp->block_size = RT_ALIGN(block_size, RT_ALIGN_SIZE); mp->block_size = RT_ALIGN(block_size, RT_ALIGN_SIZE);
mp->size = (block_size + sizeof(rt_uint8_t*))* block_count; mp->size = (block_size + sizeof(rt_uint8_t *)) * block_count;
/* allocate memory */ /* allocate memory */
mp->start_address = rt_malloc((block_size + sizeof(rt_uint8_t*))* block_count); mp->start_address = rt_malloc((block_size + sizeof(rt_uint8_t *)) * block_count);
if (mp->start_address == RT_NULL) if (mp->start_address == RT_NULL)
{ {
/* no memory, delete memory pool object */ /* no memory, delete memory pool object */
@ -210,14 +210,14 @@ rt_mp_t rt_mp_create(const char* name, rt_size_t block_count, rt_size_t block_si
mp->suspend_thread_count = 0; mp->suspend_thread_count = 0;
/* init free block list */ /* init free block list */
block_ptr = (rt_uint8_t*) mp->start_address; block_ptr = (rt_uint8_t *)mp->start_address;
for (offset = 0; offset < mp->block_total_count; offset ++) for (offset = 0; offset < mp->block_total_count; offset ++)
{ {
*(rt_uint8_t**)(block_ptr + offset * (block_size + sizeof(rt_uint8_t*))) *(rt_uint8_t **)(block_ptr + offset * (block_size + sizeof(rt_uint8_t *)))
= block_ptr + (offset + 1) * (block_size + sizeof(rt_uint8_t*)); = block_ptr + (offset + 1) * (block_size + sizeof(rt_uint8_t *));
} }
*(rt_uint8_t**)(block_ptr + (offset - 1) * (block_size + sizeof(rt_uint8_t*))) = RT_NULL; *(rt_uint8_t **)(block_ptr + (offset - 1) * (block_size + sizeof(rt_uint8_t *))) = RT_NULL;
mp->block_list = block_ptr; mp->block_list = block_ptr;
@ -234,7 +234,7 @@ rt_mp_t rt_mp_create(const char* name, rt_size_t block_count, rt_size_t block_si
*/ */
rt_err_t rt_mp_delete(rt_mp_t mp) rt_err_t rt_mp_delete(rt_mp_t mp)
{ {
struct rt_thread* thread; struct rt_thread *thread;
register rt_ubase_t temp; register rt_ubase_t temp;
RT_DEBUG_NOT_IN_INTERRUPT; RT_DEBUG_NOT_IN_INTERRUPT;
@ -269,7 +269,7 @@ rt_err_t rt_mp_delete(rt_mp_t mp)
#if defined(RT_USING_MODULE) && defined(RT_USING_SLAB) #if defined(RT_USING_MODULE) && defined(RT_USING_SLAB)
/* the mp object belongs to an application module */ /* the mp object belongs to an application module */
if(mp->parent.flag & RT_OBJECT_FLAG_MODULE) if (mp->parent.flag & RT_OBJECT_FLAG_MODULE)
rt_module_free(mp->parent.module_id, mp->start_address); rt_module_free(mp->parent.module_id, mp->start_address);
else else
#endif #endif
@ -293,26 +293,26 @@ rt_err_t rt_mp_delete(rt_mp_t mp)
* @return the allocated memory block or RT_NULL on allocated failed * @return the allocated memory block or RT_NULL on allocated failed
* *
*/ */
void *rt_mp_alloc (rt_mp_t mp, rt_int32_t time) void *rt_mp_alloc(rt_mp_t mp, rt_int32_t time)
{ {
rt_uint8_t* block_ptr; rt_uint8_t *block_ptr;
register rt_base_t level; register rt_base_t level;
struct rt_thread* thread; struct rt_thread *thread;
/* disable interrupt */ /* disable interrupt */
level = rt_hw_interrupt_disable(); level = rt_hw_interrupt_disable();
if(mp->block_free_count) if (mp->block_free_count)
{ {
/* memory block is available. decrease the free block counter */ /* memory block is available. decrease the free block counter */
mp->block_free_count--; mp->block_free_count --;
/* get block from block list */ /* get block from block list */
block_ptr = mp->block_list; block_ptr = mp->block_list;
mp->block_list = *(rt_uint8_t**)block_ptr; mp->block_list = *(rt_uint8_t **)block_ptr;
/* point to memory pool */ /* point to memory pool */
*(rt_uint8_t**)block_ptr = (rt_uint8_t*)mp; *(rt_uint8_t **)block_ptr = (rt_uint8_t *)mp;
} }
else else
{ {
@ -333,7 +333,7 @@ void *rt_mp_alloc (rt_mp_t mp, rt_int32_t time)
/* need suspend thread */ /* need suspend thread */
rt_thread_suspend(thread); rt_thread_suspend(thread);
rt_list_insert_after(&(mp->suspend_thread), &(thread->tlist)); rt_list_insert_after(&(mp->suspend_thread), &(thread->tlist));
mp->suspend_thread_count++; mp->suspend_thread_count ++;
if (time > 0) if (time > 0)
{ {
@ -358,19 +358,19 @@ void *rt_mp_alloc (rt_mp_t mp, rt_int32_t time)
/* get block from block list */ /* get block from block list */
block_ptr = mp->block_list; block_ptr = mp->block_list;
mp->block_list = *(rt_uint8_t**)block_ptr; mp->block_list = *(rt_uint8_t **)block_ptr;
/* point to memory pool */ /* point to memory pool */
*(rt_uint8_t**)block_ptr = (rt_uint8_t*)mp; *(rt_uint8_t **)block_ptr = (rt_uint8_t *)mp;
} }
} }
/* enable interrupt */ /* enable interrupt */
rt_hw_interrupt_enable(level); rt_hw_interrupt_enable(level);
RT_OBJECT_HOOK_CALL(rt_mp_alloc_hook, (mp, (rt_uint8_t*)(block_ptr + sizeof(rt_uint8_t*)))); RT_OBJECT_HOOK_CALL(rt_mp_alloc_hook, (mp, (rt_uint8_t *)(block_ptr + sizeof(rt_uint8_t *))));
return (rt_uint8_t*)(block_ptr + sizeof(rt_uint8_t*)); return (rt_uint8_t *)(block_ptr + sizeof(rt_uint8_t *));
} }
/** /**
@ -379,7 +379,7 @@ void *rt_mp_alloc (rt_mp_t mp, rt_int32_t time)
* @param block the address of memory block to be released * @param block the address of memory block to be released
* *
*/ */
void rt_mp_free (void *block) void rt_mp_free(void *block)
{ {
rt_uint8_t **block_ptr; rt_uint8_t **block_ptr;
struct rt_mempool *mp; struct rt_mempool *mp;
@ -387,8 +387,8 @@ void rt_mp_free (void *block)
register rt_base_t level; register rt_base_t level;
/* get the control block of pool which the block belongs to */ /* get the control block of pool which the block belongs to */
block_ptr = (rt_uint8_t**)((rt_uint8_t*)block - sizeof(rt_uint8_t*)); block_ptr = (rt_uint8_t **)((rt_uint8_t *)block - sizeof(rt_uint8_t *));
mp = (struct rt_mempool*) *block_ptr; mp = (struct rt_mempool *)*block_ptr;
RT_OBJECT_HOOK_CALL(rt_mp_free_hook, (mp, block)); RT_OBJECT_HOOK_CALL(rt_mp_free_hook, (mp, block));
@ -400,7 +400,7 @@ void rt_mp_free (void *block)
/* link the block into the block list */ /* link the block into the block list */
*block_ptr = mp->block_list; *block_ptr = mp->block_list;
mp->block_list = (rt_uint8_t*)block_ptr; mp->block_list = (rt_uint8_t *)block_ptr;
if (mp->suspend_thread_count > 0) if (mp->suspend_thread_count > 0)
{ {

View File

@ -1,7 +1,7 @@
/* /*
* File : module.c * File : module.c
* This file is part of RT-Thread RTOS * This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006 - 2010, RT-Thread Development Team * COPYRIGHT (C) 2006 - 2011, RT-Thread Development Team
* *
* The license and distribution terms for this file may be * The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at * found in the file LICENSE in this distribution or at
@ -61,23 +61,23 @@ static struct rt_semaphore mod_sem;
static struct rt_module_symtab *_rt_module_symtab_begin = RT_NULL, *_rt_module_symtab_end = RT_NULL; static struct rt_module_symtab *_rt_module_symtab_begin = RT_NULL, *_rt_module_symtab_end = RT_NULL;
rt_list_t rt_module_symbol_list; rt_list_t rt_module_symbol_list;
static char* _strip_name(const char* string) static char *_strip_name(const char *string)
{ {
int i = 0, p = 0, q = 0; int i = 0, p = 0, q = 0;
const char* str = string; const char *str = string;
char* dest = RT_NULL; char *dest = RT_NULL;
while(*str != '\n' && *str != '\0') while (*str != '\n' && *str != '\0')
{ {
if(*str =='/' ) p = i + 1; if (*str =='/' ) p = i + 1;
if(*str == '.') q = i; if (*str == '.') q = i;
str++; i++; str++; i++;
} }
if(p < q) if (p < q)
{ {
int len = q - p; int len = q - p;
dest = (char*)rt_malloc(len + 1); dest = (char *)rt_malloc(len + 1);
rt_strncpy(dest, &string[p], len); rt_strncpy(dest, &string[p], len);
dest[len] = '\0'; dest[len] = '\0';
} }
@ -116,10 +116,10 @@ void rt_system_module_init(void)
rt_current_module = RT_NULL; rt_current_module = RT_NULL;
} }
static rt_uint32_t rt_module_symbol_find(const char* sym_str) static rt_uint32_t rt_module_symbol_find(const char *sym_str)
{ {
/* find in kernel symbol table */ /* find in kernel symbol table */
struct rt_module_symtab* index; struct rt_module_symtab *index;
for (index = _rt_module_symtab_begin; index != _rt_module_symtab_end; index ++) for (index = _rt_module_symtab_begin; index != _rt_module_symtab_end; index ++)
{ {
if (rt_strcmp(index->name, sym_str) == 0) if (rt_strcmp(index->name, sym_str) == 0)
@ -135,7 +135,7 @@ static rt_uint32_t rt_module_symbol_find(const char* sym_str)
* @return the self module object * @return the self module object
* *
*/ */
rt_module_t rt_module_self (void) rt_module_t rt_module_self(void)
{ {
/* return current module */ /* return current module */
return rt_current_module; return rt_current_module;
@ -146,7 +146,7 @@ rt_module_t rt_module_self (void)
* *
* @return RT_EOK * @return RT_EOK
*/ */
rt_err_t rt_module_set (rt_module_t module) rt_err_t rt_module_set(rt_module_t module)
{ {
/* set current module */ /* set current module */
rt_current_module = module; rt_current_module = module;
@ -154,12 +154,12 @@ rt_err_t rt_module_set (rt_module_t module)
return RT_EOK; return RT_EOK;
} }
static int rt_module_arm_relocate(struct rt_module* module, Elf32_Rel *rel, Elf32_Addr sym_val) static int rt_module_arm_relocate(struct rt_module *module, Elf32_Rel *rel, Elf32_Addr sym_val)
{ {
Elf32_Addr *where, tmp; Elf32_Addr *where, tmp;
Elf32_Sword addend; Elf32_Sword addend;
where = (Elf32_Addr *)((rt_uint8_t*)module->module_space + rel->r_offset); where = (Elf32_Addr *)((rt_uint8_t *)module->module_space + rel->r_offset);
switch (ELF32_R_TYPE(rel->r_info)) switch (ELF32_R_TYPE(rel->r_info))
{ {
case R_ARM_NONE: case R_ARM_NONE:
@ -168,8 +168,7 @@ static int rt_module_arm_relocate(struct rt_module* module, Elf32_Rel *rel, Elf3
case R_ARM_ABS32: case R_ARM_ABS32:
*where += (Elf32_Addr)sym_val; *where += (Elf32_Addr)sym_val;
RT_DEBUG_LOG(RT_DEBUG_MODULE, RT_DEBUG_LOG(RT_DEBUG_MODULE, ("R_ARM_ABS32: %x -> %x\n", where, *where));
("R_ARM_ABS32: %x -> %x\n", where, *where));
break; break;
@ -184,7 +183,7 @@ static int rt_module_arm_relocate(struct rt_module* module, Elf32_Rel *rel, Elf3
tmp >>= 2; tmp >>= 2;
*where = (*where & 0xff000000) | (tmp & 0x00ffffff); *where = (*where & 0xff000000) | (tmp & 0x00ffffff);
RT_DEBUG_LOG(RT_DEBUG_MODULE,("R_ARM_PC24: %x -> %x\n", where, *where)); RT_DEBUG_LOG(RT_DEBUG_MODULE, ("R_ARM_PC24: %x -> %x\n", where, *where));
break; break;
@ -214,7 +213,7 @@ static int rt_module_arm_relocate(struct rt_module* module, Elf32_Rel *rel, Elf3
return 0; return 0;
} }
static void rt_module_init_object_container(struct rt_module* module) static void rt_module_init_object_container(struct rt_module *module)
{ {
RT_ASSERT(module != RT_NULL); RT_ASSERT(module != RT_NULL);
@ -321,7 +320,7 @@ void rt_module_unload_sethook(void (*hook)(rt_module_t module))
* @return the module object * @return the module object
* *
*/ */
rt_module_t rt_module_load(const char* name, void* module_ptr) rt_module_t rt_module_load(const char *name, void *module_ptr)
{ {
rt_uint8_t *ptr = RT_NULL; rt_uint8_t *ptr = RT_NULL;
rt_module_t module = RT_NULL; rt_module_t module = RT_NULL;
@ -384,9 +383,9 @@ rt_module_t rt_module_load(const char* name, void* module_ptr)
for (index = 0; index < elf_module->e_phnum; index++) for (index = 0; index < elf_module->e_phnum; index++)
{ {
if(phdr[index].p_type == PT_LOAD) if (phdr[index].p_type == PT_LOAD)
{ {
rt_memcpy(ptr, (rt_uint8_t*)elf_module + phdr[index].p_offset, phdr[index].p_filesz); rt_memcpy(ptr, (rt_uint8_t *)elf_module + phdr[index].p_offset, phdr[index].p_filesz);
ptr += phdr[index].p_memsz; ptr += phdr[index].p_memsz;
} }
} }
@ -406,12 +405,12 @@ rt_module_t rt_module_load(const char* name, void* module_ptr)
static rt_bool_t unsolved = RT_FALSE; static rt_bool_t unsolved = RT_FALSE;
/* get relocate item */ /* get relocate item */
rel = (Elf32_Rel *) ((rt_uint8_t*)module_ptr + shdr[index].sh_offset); rel = (Elf32_Rel *)((rt_uint8_t *)module_ptr + shdr[index].sh_offset);
/* locate .rel.plt and .rel.dyn section */ /* locate .rel.plt and .rel.dyn section */
symtab =(Elf32_Sym *) ((rt_uint8_t*)module_ptr + shdr[shdr[index].sh_link].sh_offset); symtab =(Elf32_Sym *)((rt_uint8_t *)module_ptr + shdr[shdr[index].sh_link].sh_offset);
strtab = (rt_uint8_t*) module_ptr + shdr[shdr[shdr[index].sh_link].sh_link].sh_offset; strtab = (rt_uint8_t*)module_ptr + shdr[shdr[shdr[index].sh_link].sh_link].sh_offset;
nr_reloc = (rt_uint32_t) (shdr[index].sh_size / sizeof(Elf32_Rel)); nr_reloc = (rt_uint32_t)(shdr[index].sh_size / sizeof(Elf32_Rel));
/* relocate every items */ /* relocate every items */
for (i = 0; i < nr_reloc; i ++) for (i = 0; i < nr_reloc; i ++)
@ -421,9 +420,9 @@ rt_module_t rt_module_load(const char* name, void* module_ptr)
RT_DEBUG_LOG(RT_DEBUG_MODULE, RT_DEBUG_LOG(RT_DEBUG_MODULE,
("relocate symbol %s shndx %d\n", strtab + sym->st_name, sym->st_shndx)); ("relocate symbol %s shndx %d\n", strtab + sym->st_name, sym->st_shndx));
if((sym->st_shndx != SHT_NULL) || (ELF_ST_BIND(sym->st_info) == STB_LOCAL)) if ((sym->st_shndx != SHT_NULL) || (ELF_ST_BIND(sym->st_info) == STB_LOCAL))
rt_module_arm_relocate(module, rel, (Elf32_Addr)(module->module_space + sym->st_value)); rt_module_arm_relocate(module, rel, (Elf32_Addr)(module->module_space + sym->st_value));
else if(!linked) else if (!linked)
{ {
Elf32_Addr addr; Elf32_Addr addr;
@ -431,7 +430,7 @@ rt_module_t rt_module_load(const char* name, void* module_ptr)
("unresolved relocate symbol: %s\n", strtab + sym->st_name)); ("unresolved relocate symbol: %s\n", strtab + sym->st_name));
/* need to resolve symbol in kernel symbol table */ /* need to resolve symbol in kernel symbol table */
addr = rt_module_symbol_find((const char*)(strtab + sym->st_name)); addr = rt_module_symbol_find((const char *)(strtab + sym->st_name));
if (addr == 0) if (addr == 0)
{ {
rt_kprintf("can't find %s in kernel symbol table\n", strtab + sym->st_name); rt_kprintf("can't find %s in kernel symbol table\n", strtab + sym->st_name);
@ -442,7 +441,7 @@ rt_module_t rt_module_load(const char* name, void* module_ptr)
rel ++; rel ++;
} }
if(unsolved) if (unsolved)
{ {
rt_object_delete(&(module->parent)); rt_object_delete(&(module->parent));
rt_free(module); rt_free(module);
@ -455,39 +454,39 @@ rt_module_t rt_module_load(const char* name, void* module_ptr)
for (index = 0; index < elf_module->e_shnum; index ++) for (index = 0; index < elf_module->e_shnum; index ++)
{ {
/* find .dynsym section */ /* find .dynsym section */
rt_uint8_t* shstrab = (rt_uint8_t*) module_ptr + shdr[elf_module->e_shstrndx].sh_offset; rt_uint8_t *shstrab = (rt_uint8_t *)module_ptr + shdr[elf_module->e_shstrndx].sh_offset;
if (rt_strcmp((const char *)(shstrab + shdr[index].sh_name), ELF_DYNSYM) == 0) break; if (rt_strcmp((const char *)(shstrab + shdr[index].sh_name), ELF_DYNSYM) == 0) break;
} }
/* found .dynsym section */ /* found .dynsym section */
if(index != elf_module->e_shnum) if (index != elf_module->e_shnum)
{ {
int i, count = 0; int i, count = 0;
Elf32_Sym *symtab = RT_NULL; Elf32_Sym *symtab = RT_NULL;
rt_uint8_t *strtab = RT_NULL; rt_uint8_t *strtab = RT_NULL;
symtab =(Elf32_Sym *) ((rt_uint8_t*)module_ptr + shdr[index].sh_offset); symtab =(Elf32_Sym *)((rt_uint8_t *)module_ptr + shdr[index].sh_offset);
strtab = (rt_uint8_t*) module_ptr + shdr[shdr[index].sh_link].sh_offset; strtab = (rt_uint8_t *)module_ptr + shdr[shdr[index].sh_link].sh_offset;
for(i=0; i<shdr[index].sh_size/sizeof(Elf32_Sym); i++) for (i=0; i<shdr[index].sh_size/sizeof(Elf32_Sym); i++)
{ {
if((ELF_ST_BIND(symtab[i].st_info) == STB_GLOBAL) && (ELF_ST_TYPE(symtab[i].st_info) == STT_FUNC)) if ((ELF_ST_BIND(symtab[i].st_info) == STB_GLOBAL) && (ELF_ST_TYPE(symtab[i].st_info) == STT_FUNC))
count++; count ++;
} }
module->symtab = (struct rt_module_symtab*)rt_malloc(count * sizeof(struct rt_module_symtab)); module->symtab = (struct rt_module_symtab *)rt_malloc(count * sizeof(struct rt_module_symtab));
module->nsym = count; module->nsym = count;
for(i=0, count=0; i<shdr[index].sh_size/sizeof(Elf32_Sym); i++) for (i=0, count=0; i<shdr[index].sh_size/sizeof(Elf32_Sym); i++)
{ {
if((ELF_ST_BIND(symtab[i].st_info) == STB_GLOBAL) && (ELF_ST_TYPE(symtab[i].st_info) == STT_FUNC)) if ((ELF_ST_BIND(symtab[i].st_info) == STB_GLOBAL) && (ELF_ST_TYPE(symtab[i].st_info) == STT_FUNC))
{ {
rt_size_t length = rt_strlen((const char*)(strtab + symtab[i].st_name)) + 1; rt_size_t length = rt_strlen((const char *)(strtab + symtab[i].st_name)) + 1;
module->symtab[count].addr = (void*)(module->module_space + symtab[i].st_value); module->symtab[count].addr = (void *)(module->module_space + symtab[i].st_value);
module->symtab[count].name = rt_malloc(length); module->symtab[count].name = rt_malloc(length);
rt_memset((void*)module->symtab[count].name, 0, length); rt_memset((void *)module->symtab[count].name, 0, length);
rt_memcpy((void*)module->symtab[count].name, strtab + symtab[i].st_name, length); rt_memcpy((void *)module->symtab[count].name, strtab + symtab[i].st_name, length);
count++; count ++;
} }
} }
} }
@ -496,9 +495,9 @@ rt_module_t rt_module_load(const char* name, void* module_ptr)
rt_module_init_object_container(module); rt_module_init_object_container(module);
/* increase module reference count */ /* increase module reference count */
module->nref++; module->nref ++;
if(elf_module->e_entry != 0) if (elf_module->e_entry != 0)
{ {
/* init module memory allocator */ /* init module memory allocator */
module->mem_list = RT_NULL; module->mem_list = RT_NULL;
@ -528,7 +527,7 @@ rt_module_t rt_module_load(const char* name, void* module_ptr)
} }
#ifdef RT_USING_HOOK #ifdef RT_USING_HOOK
if(rt_module_load_hook != RT_NULL) if (rt_module_load_hook != RT_NULL)
{ {
rt_module_load_hook(module); rt_module_load_hook(module);
} }
@ -547,10 +546,10 @@ rt_module_t rt_module_load(const char* name, void* module_ptr)
* @return the module object * @return the module object
* *
*/ */
rt_module_t rt_module_open(const char* path) rt_module_t rt_module_open(const char *path)
{ {
int fd, length; int fd, length;
struct rt_module* module; struct rt_module *module;
struct stat s; struct stat s;
char *buffer, *offset_ptr, *name; char *buffer, *offset_ptr, *name;
@ -624,7 +623,7 @@ FINSH_FUNCTION_EXPORT_ALIAS(rt_module_open, exec, exec module from file);
rt_err_t rt_module_unload(rt_module_t module) rt_err_t rt_module_unload(rt_module_t module)
{ {
int i; int i;
struct rt_object* object; struct rt_object *object;
struct rt_list_node *list; struct rt_list_node *list;
RT_DEBUG_NOT_IN_INTERRUPT; RT_DEBUG_NOT_IN_INTERRUPT;
@ -635,10 +634,10 @@ rt_err_t rt_module_unload(rt_module_t module)
rt_kprintf("rt_module_unload: %s\n", module->parent.name); rt_kprintf("rt_module_unload: %s\n", module->parent.name);
/* module has entry point */ /* module has entry point */
if((module->parent.flag & RT_MODULE_FLAG_WITHOUTENTRY) != RT_MODULE_FLAG_WITHOUTENTRY) if ((module->parent.flag & RT_MODULE_FLAG_WITHOUTENTRY) != RT_MODULE_FLAG_WITHOUTENTRY)
{ {
/* suspend module main thread */ /* suspend module main thread */
if(module->module_thread != RT_NULL) if (module->module_thread != RT_NULL)
{ {
if (module->module_thread->stat == RT_THREAD_READY) if (module->module_thread->stat == RT_THREAD_READY)
rt_thread_suspend(module->module_thread); rt_thread_suspend(module->module_thread);
@ -646,7 +645,7 @@ rt_err_t rt_module_unload(rt_module_t module)
/* delete threads */ /* delete threads */
list = &module->module_object[RT_Object_Class_Thread].object_list; list = &module->module_object[RT_Object_Class_Thread].object_list;
while(list->next != list) while (list->next != list)
{ {
object = rt_list_entry(list->next, struct rt_object, list); object = rt_list_entry(list->next, struct rt_object, list);
if (rt_object_is_systemobject(object) == RT_EOK) if (rt_object_is_systemobject(object) == RT_EOK)
@ -664,7 +663,7 @@ rt_err_t rt_module_unload(rt_module_t module)
#ifdef RT_USING_SEMAPHORE #ifdef RT_USING_SEMAPHORE
/* delete semaphores */ /* delete semaphores */
list = &module->module_object[RT_Object_Class_Thread].object_list; list = &module->module_object[RT_Object_Class_Thread].object_list;
while(list->next != list) while (list->next != list)
{ {
object = rt_list_entry(list->next, struct rt_object, list); object = rt_list_entry(list->next, struct rt_object, list);
if (rt_object_is_systemobject(object) == RT_EOK) if (rt_object_is_systemobject(object) == RT_EOK)
@ -683,7 +682,7 @@ rt_err_t rt_module_unload(rt_module_t module)
#ifdef RT_USING_MUTEX #ifdef RT_USING_MUTEX
/* delete mutexs*/ /* delete mutexs*/
list = &module->module_object[RT_Object_Class_Mutex].object_list; list = &module->module_object[RT_Object_Class_Mutex].object_list;
while(list->next != list) while (list->next != list)
{ {
object = rt_list_entry(list->next, struct rt_object, list); object = rt_list_entry(list->next, struct rt_object, list);
if (rt_object_is_systemobject(object) == RT_EOK) if (rt_object_is_systemobject(object) == RT_EOK)
@ -702,7 +701,7 @@ rt_err_t rt_module_unload(rt_module_t module)
#ifdef RT_USING_EVENT #ifdef RT_USING_EVENT
/* delete mailboxs */ /* delete mailboxs */
list = &module->module_object[RT_Object_Class_Event].object_list; list = &module->module_object[RT_Object_Class_Event].object_list;
while(list->next != list) while (list->next != list)
{ {
object = rt_list_entry(list->next, struct rt_object, list); object = rt_list_entry(list->next, struct rt_object, list);
if (rt_object_is_systemobject(object) == RT_EOK) if (rt_object_is_systemobject(object) == RT_EOK)
@ -721,7 +720,7 @@ rt_err_t rt_module_unload(rt_module_t module)
#ifdef RT_USING_MAILBOX #ifdef RT_USING_MAILBOX
/* delete mailboxs */ /* delete mailboxs */
list = &module->module_object[RT_Object_Class_MailBox].object_list; list = &module->module_object[RT_Object_Class_MailBox].object_list;
while(list->next != list) while (list->next != list)
{ {
object = rt_list_entry(list->next, struct rt_object, list); object = rt_list_entry(list->next, struct rt_object, list);
if (rt_object_is_systemobject(object) == RT_EOK) if (rt_object_is_systemobject(object) == RT_EOK)
@ -740,7 +739,7 @@ rt_err_t rt_module_unload(rt_module_t module)
#ifdef RT_USING_MESSAGEQUEUE #ifdef RT_USING_MESSAGEQUEUE
/* delete msgqueues */ /* delete msgqueues */
list = &module->module_object[RT_Object_Class_MessageQueue].object_list; list = &module->module_object[RT_Object_Class_MessageQueue].object_list;
while(list->next != list) while (list->next != list)
{ {
object = rt_list_entry(list->next, struct rt_object, list); object = rt_list_entry(list->next, struct rt_object, list);
if (rt_object_is_systemobject(object) == RT_EOK) if (rt_object_is_systemobject(object) == RT_EOK)
@ -759,7 +758,7 @@ rt_err_t rt_module_unload(rt_module_t module)
#ifdef RT_USING_MEMPOOL #ifdef RT_USING_MEMPOOL
/* delete mempools */ /* delete mempools */
list = &module->module_object[RT_Object_Class_MemPool].object_list; list = &module->module_object[RT_Object_Class_MemPool].object_list;
while(list->next != list) while (list->next != list)
{ {
object = rt_list_entry(list->next, struct rt_object, list); object = rt_list_entry(list->next, struct rt_object, list);
if (rt_object_is_systemobject(object) == RT_EOK) if (rt_object_is_systemobject(object) == RT_EOK)
@ -778,7 +777,7 @@ rt_err_t rt_module_unload(rt_module_t module)
#ifdef RT_USING_DEVICE #ifdef RT_USING_DEVICE
/* delete devices */ /* delete devices */
list = &module->module_object[RT_Object_Class_Device].object_list; list = &module->module_object[RT_Object_Class_Device].object_list;
while(list->next != list) while (list->next != list)
{ {
object = rt_list_entry(list->next, struct rt_object, list); object = rt_list_entry(list->next, struct rt_object, list);
rt_device_unregister((rt_device_t)object); rt_device_unregister((rt_device_t)object);
@ -787,7 +786,7 @@ rt_err_t rt_module_unload(rt_module_t module)
/* delete timers */ /* delete timers */
list = &module->module_object[RT_Object_Class_Timer].object_list; list = &module->module_object[RT_Object_Class_Timer].object_list;
while(list->next != list) while (list->next != list)
{ {
object = rt_list_entry(list->next, struct rt_object, list); object = rt_list_entry(list->next, struct rt_object, list);
if (rt_object_is_systemobject(object) == RT_EOK) if (rt_object_is_systemobject(object) == RT_EOK)
@ -803,10 +802,10 @@ rt_err_t rt_module_unload(rt_module_t module)
} }
} }
if(module->page_cnt > 0) if (module->page_cnt > 0)
{ {
int i; int i;
struct rt_page_info* page = (struct rt_page_info*)module->page_array; struct rt_page_info *page = (struct rt_page_info *)module->page_array;
rt_kprintf("warning: some module memory still hasn't be freed\n"); rt_kprintf("warning: some module memory still hasn't be freed\n");
@ -822,11 +821,11 @@ rt_err_t rt_module_unload(rt_module_t module)
rt_free(module->module_space); rt_free(module->module_space);
/* release module symbol table */ /* release module symbol table */
for(i=0; i<module->nsym; i++) rt_free((void *)module->symtab[i].name); for (i=0; i<module->nsym; i++) rt_free((void *)module->symtab[i].name);
if(module->symtab != RT_NULL) rt_free(module->symtab); if (module->symtab != RT_NULL) rt_free(module->symtab);
#ifdef RT_USING_HOOK #ifdef RT_USING_HOOK
if(rt_module_unload_hook != RT_NULL) if (rt_module_unload_hook != RT_NULL)
{ {
rt_module_unload_hook(module); rt_module_unload_hook(module);
} }
@ -847,11 +846,11 @@ rt_err_t rt_module_unload(rt_module_t module)
* *
* @return the module * @return the module
*/ */
rt_module_t rt_module_find(const char* name) rt_module_t rt_module_find(const char *name)
{ {
struct rt_object_information *information; struct rt_object_information *information;
struct rt_object* object; struct rt_object *object;
struct rt_list_node* node; struct rt_list_node *node;
extern struct rt_object_information rt_object_container[]; extern struct rt_object_information rt_object_container[];
@ -891,16 +890,16 @@ rt_module_t rt_module_find(const char* name)
*/ */
static void *rt_module_malloc_page(rt_size_t npages) static void *rt_module_malloc_page(rt_size_t npages)
{ {
void* chunk; void *chunk;
struct rt_page_info *page; struct rt_page_info *page;
chunk = rt_page_alloc(npages); chunk = rt_page_alloc(npages);
if (chunk == RT_NULL) return RT_NULL; if (chunk == RT_NULL) return RT_NULL;
page = (struct rt_page_info*)rt_current_module->page_array; page = (struct rt_page_info *)rt_current_module->page_array;
page[rt_current_module->page_cnt].page_ptr = chunk; page[rt_current_module->page_cnt].page_ptr = chunk;
page[rt_current_module->page_cnt].npage = npages; page[rt_current_module->page_cnt].npage = npages;
rt_current_module->page_cnt++; rt_current_module->page_cnt ++;
RT_ASSERT(rt_current_module->page_cnt <= PAGE_COUNT_MAX); RT_ASSERT(rt_current_module->page_cnt <= PAGE_COUNT_MAX);
@ -924,19 +923,19 @@ static void rt_module_free_page(void *page_ptr, rt_size_t npages)
//rt_kprintf("rt_module_free_page 0x%x %d\n", page_ptr, npages); //rt_kprintf("rt_module_free_page 0x%x %d\n", page_ptr, npages);
rt_page_free(page_ptr, npages); rt_page_free(page_ptr, npages);
page = (struct rt_page_info*)rt_current_module->page_array; page = (struct rt_page_info *)rt_current_module->page_array;
for(i=0; i<rt_current_module->page_cnt; i++) for (i=0; i<rt_current_module->page_cnt; i++)
{ {
if(page[i].page_ptr == page_ptr) if (page[i].page_ptr == page_ptr)
{ {
if(page[i].npage == npages + 1) if (page[i].npage == npages + 1)
{ {
page[i].page_ptr += npages * RT_MM_PAGE_SIZE / sizeof(rt_uint32_t); page[i].page_ptr += npages * RT_MM_PAGE_SIZE / sizeof(rt_uint32_t);
} }
else if(page[i].npage == npages) else if (page[i].npage == npages)
{ {
for(index=i; index<rt_current_module->page_cnt-1; index++) for (index=i; index<rt_current_module->page_cnt-1; index++)
{ {
page[index].page_ptr = page[index + 1].page_ptr; page[index].page_ptr = page[index + 1].page_ptr;
page[index].npage = page[index + 1].npage; page[index].npage = page[index + 1].npage;
@ -946,7 +945,7 @@ static void rt_module_free_page(void *page_ptr, rt_size_t npages)
} }
else RT_ASSERT(RT_FALSE); else RT_ASSERT(RT_FALSE);
rt_current_module->page_cnt--; rt_current_module->page_cnt --;
return; return;
} }
@ -1006,7 +1005,7 @@ void *rt_module_malloc(rt_size_t size)
/* allocate pages from system heap */ /* allocate pages from system heap */
npage = (size + sizeof(struct rt_mem_head) + RT_MM_PAGE_SIZE - 1)/RT_MM_PAGE_SIZE; npage = (size + sizeof(struct rt_mem_head) + RT_MM_PAGE_SIZE - 1)/RT_MM_PAGE_SIZE;
if((up = (struct rt_mem_head*)rt_module_malloc_page(npage)) == RT_NULL) return RT_NULL; if ((up = (struct rt_mem_head *)rt_module_malloc_page(npage)) == RT_NULL) return RT_NULL;
up->size = npage * RT_MM_PAGE_SIZE / sizeof(struct rt_mem_head); up->size = npage * RT_MM_PAGE_SIZE / sizeof(struct rt_mem_head);
@ -1057,12 +1056,12 @@ void rt_module_free(rt_module_t module, void *addr)
} }
else b->size += n->size; else b->size += n->size;
if((rt_uint32_t)b % RT_MM_PAGE_SIZE == 0) if ((rt_uint32_t)b % RT_MM_PAGE_SIZE == 0)
{ {
int npage = b->size * sizeof(struct rt_page_info) / RT_MM_PAGE_SIZE; int npage = b->size * sizeof(struct rt_page_info) / RT_MM_PAGE_SIZE;
if(npage > 0) if (npage > 0)
{ {
if((b->size * sizeof(struct rt_page_info) % RT_MM_PAGE_SIZE) != 0) if ((b->size * sizeof(struct rt_page_info) % RT_MM_PAGE_SIZE) != 0)
{ {
rt_size_t nunits = npage * RT_MM_PAGE_SIZE / sizeof(struct rt_mem_head); rt_size_t nunits = npage * RT_MM_PAGE_SIZE / sizeof(struct rt_mem_head);
/* split memory */ /* split memory */
@ -1092,12 +1091,12 @@ void rt_module_free(rt_module_t module, void *addr)
n->size = b->size + n->size; n->size = b->size + n->size;
n->next = b->next; n->next = b->next;
if((rt_uint32_t)n % RT_MM_PAGE_SIZE == 0) if ((rt_uint32_t)n % RT_MM_PAGE_SIZE == 0)
{ {
int npage = n->size * sizeof(struct rt_page_info) / RT_MM_PAGE_SIZE; int npage = n->size * sizeof(struct rt_page_info) / RT_MM_PAGE_SIZE;
if(npage > 0) if (npage > 0)
{ {
if((n->size * sizeof(struct rt_page_info) % RT_MM_PAGE_SIZE) != 0) if ((n->size * sizeof(struct rt_page_info) % RT_MM_PAGE_SIZE) != 0)
{ {
rt_size_t nunits = npage * RT_MM_PAGE_SIZE / sizeof(struct rt_mem_head); rt_size_t nunits = npage * RT_MM_PAGE_SIZE / sizeof(struct rt_mem_head);
/* split memory */ /* split memory */
@ -1127,13 +1126,13 @@ void rt_module_free(rt_module_t module, void *addr)
prev = &(b->next); prev = &(b->next);
} }
if((rt_uint32_t)n % RT_MM_PAGE_SIZE == 0) if ((rt_uint32_t)n % RT_MM_PAGE_SIZE == 0)
{ {
int npage = n->size * sizeof(struct rt_page_info) / RT_MM_PAGE_SIZE; int npage = n->size * sizeof(struct rt_page_info) / RT_MM_PAGE_SIZE;
if(npage > 0) if (npage > 0)
{ {
rt_module_free_page(n, npage); rt_module_free_page(n, npage);
if(n->size % RT_MM_PAGE_SIZE != 0) if (n->size % RT_MM_PAGE_SIZE != 0)
{ {
rt_size_t nunits = npage * RT_MM_PAGE_SIZE / sizeof(struct rt_mem_head); rt_size_t nunits = npage * RT_MM_PAGE_SIZE / sizeof(struct rt_mem_head);
/* split memory */ /* split memory */
@ -1222,14 +1221,14 @@ void *rt_module_realloc(void *ptr, rt_size_t size)
prev->next = p; prev->next = p;
} }
rt_current_module->mem_list = (void *)prev; rt_current_module->mem_list = (void *)prev;
return (void *) (b + 1); return (void *)(b + 1);
} }
else /* allocate new memory and copy old data */ else /* allocate new memory and copy old data */
{ {
if ((p = rt_module_malloc(size)) == RT_NULL) return RT_NULL; if ((p = rt_module_malloc(size)) == RT_NULL) return RT_NULL;
rt_memmove(p, (b+1), ((b->size) * sizeof(struct rt_mem_head))); rt_memmove(p, (b+1), ((b->size) * sizeof(struct rt_mem_head)));
rt_module_free(rt_current_module, (void *)(b + 1)); rt_module_free(rt_current_module, (void *)(b + 1));
return (void *) (p); return (void *)(p);
} }
} }
} }
@ -1244,7 +1243,7 @@ void list_memlist(const char* name)
struct rt_mem_head *b; struct rt_mem_head *b;
module = rt_module_find(name); module = rt_module_find(name);
if(module == RT_NULL) return; if (module == RT_NULL) return;
for (prev = (struct rt_mem_head **)&module->mem_list; (b = *prev) != RT_NULL; prev = &(b->next)) for (prev = (struct rt_mem_head **)&module->mem_list; (b = *prev) != RT_NULL; prev = &(b->next))
{ {
@ -1253,18 +1252,18 @@ void list_memlist(const char* name)
} }
FINSH_FUNCTION_EXPORT(list_memlist, list module free memory information) FINSH_FUNCTION_EXPORT(list_memlist, list module free memory information)
void list_mempage(const char* name) void list_mempage(const char *name)
{ {
rt_module_t module; rt_module_t module;
struct rt_page_info *page; struct rt_page_info *page;
int i; int i;
module = rt_module_find(name); module = rt_module_find(name);
if(module == RT_NULL) return; if (module == RT_NULL) return;
page = (struct rt_page_info*)module->page_array; page = (struct rt_page_info*)module->page_array;
for(i=0; i<module->page_cnt; i++) for (i=0; i<module->page_cnt; i++)
{ {
rt_kprintf("0x%x--%d\n", page[i].page_ptr, page[i].npage); rt_kprintf("0x%x--%d\n", page[i].page_ptr, page[i].npage);
} }

View File

@ -1,3 +1,18 @@
/*
* File : module.c
* This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006 - 2011, RT-Thread Development Team
*
* The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at
* http://www.rt-thread.org/license/LICENSE
*
* Change Logs:
* Date Author Notes
* 2010-01-09 Bernard first version
* 2010-04-09 yi.qiu implement based on first version
*/
#ifndef __MODULE_H__ #ifndef __MODULE_H__
#define __MODULE_H__ #define __MODULE_H__
@ -42,7 +57,8 @@ typedef rt_uint16_t Elf32_Half; /* Unsigned medium integer */
(ehdr).e_ident[EI_MAG3] == ELFMAG3) (ehdr).e_ident[EI_MAG3] == ELFMAG3)
/* ELF Header */ /* ELF Header */
typedef struct elfhdr { typedef struct elfhdr
{
unsigned char e_ident[EI_NIDENT]; /* ELF Identification */ unsigned char e_ident[EI_NIDENT]; /* ELF Identification */
Elf32_Half e_type; /* object file type */ Elf32_Half e_type; /* object file type */
Elf32_Half e_machine; /* machine */ Elf32_Half e_machine; /* machine */
@ -61,7 +77,8 @@ typedef struct elfhdr {
} Elf32_Ehdr; } Elf32_Ehdr;
/* Section Header */ /* Section Header */
typedef struct { typedef struct
{
Elf32_Word sh_name; /* name - index into section header Elf32_Word sh_name; /* name - index into section header
string table section */ string table section */
Elf32_Word sh_type; /* type */ Elf32_Word sh_type; /* type */
@ -100,7 +117,8 @@ typedef struct {
#define ELF_RTMSYMTAB "RTMSymTab" #define ELF_RTMSYMTAB "RTMSymTab"
/* Symbol Table Entry */ /* Symbol Table Entry */
typedef struct elf32_sym { typedef struct elf32_sym
{
Elf32_Word st_name; /* name - index into string table */ Elf32_Word st_name; /* name - index into string table */
Elf32_Addr st_value; /* symbol value */ Elf32_Addr st_value; /* symbol value */
Elf32_Word st_size; /* symbol size */ Elf32_Word st_size; /* symbol size */
@ -136,13 +154,15 @@ typedef struct elf32_sym {
#define ELF_ST_INFO(bind, type) (((bind)<<4)+((type)&0xf)) #define ELF_ST_INFO(bind, type) (((bind)<<4)+((type)&0xf))
/* Relocation entry with implicit addend */ /* Relocation entry with implicit addend */
typedef struct { typedef struct
{
Elf32_Addr r_offset; /* offset of relocation */ Elf32_Addr r_offset; /* offset of relocation */
Elf32_Word r_info; /* symbol table index and type */ Elf32_Word r_info; /* symbol table index and type */
} Elf32_Rel; } Elf32_Rel;
/* Relocation entry with explicit addend */ /* Relocation entry with explicit addend */
typedef struct { typedef struct
{
Elf32_Addr r_offset; /* offset of relocation */ Elf32_Addr r_offset; /* offset of relocation */
Elf32_Word r_info; /* symbol table index and type */ Elf32_Word r_info; /* symbol table index and type */
Elf32_Sword r_addend; Elf32_Sword r_addend;
@ -168,7 +188,8 @@ typedef struct {
#define R_ARM_V4BX 40 #define R_ARM_V4BX 40
/* Program Header */ /* Program Header */
typedef struct { typedef struct
{
Elf32_Word p_type; /* segment type */ Elf32_Word p_type; /* segment type */
Elf32_Off p_offset; /* segment offset */ Elf32_Off p_offset; /* segment offset */
Elf32_Addr p_vaddr; /* virtual address of segment */ Elf32_Addr p_vaddr; /* virtual address of segment */
@ -211,6 +232,6 @@ typedef struct {
#define SHF_ALLOC 0x2 /* occupies memory */ #define SHF_ALLOC 0x2 /* occupies memory */
#define SHF_EXECINSTR 0x4 /* executable */ #define SHF_EXECINSTR 0x4 /* executable */
#define SHF_MASKPROC 0xf0000000 /* reserved bits for processor */ #define SHF_MASKPROC 0xf0000000 /* reserved bits for processor */
/* specific section attributes */ /* specific section attributes */
#endif #endif

View File

@ -1,7 +1,7 @@
/* /*
* File : object.c * File : object.c
* This file is part of RT-Thread RTOS * This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team * COPYRIGHT (C) 2006 - 2011, RT-Thread Development Team
* *
* The license and distribution terms for this file may be * The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at * found in the file LICENSE in this distribution or at
@ -65,11 +65,11 @@ struct rt_object_information rt_object_container[RT_Object_Class_Unknown] =
}; };
#ifdef RT_USING_HOOK #ifdef RT_USING_HOOK
static void (*rt_object_attach_hook)(struct rt_object* object); static void (*rt_object_attach_hook)(struct rt_object *object);
static void (*rt_object_detach_hook)(struct rt_object* object); static void (*rt_object_detach_hook)(struct rt_object *object);
void (*rt_object_trytake_hook)(struct rt_object* object); void (*rt_object_trytake_hook)(struct rt_object *object);
void (*rt_object_take_hook)(struct rt_object* object); void (*rt_object_take_hook)(struct rt_object *object);
void (*rt_object_put_hook)(struct rt_object* object); void (*rt_object_put_hook)(struct rt_object *object);
/** /**
* @addtogroup Hook * @addtogroup Hook
@ -82,7 +82,7 @@ void (*rt_object_put_hook)(struct rt_object* object);
* *
* @param hook the hook function * @param hook the hook function
*/ */
void rt_object_attach_sethook(void (*hook)(struct rt_object* object)) void rt_object_attach_sethook(void (*hook)(struct rt_object *object))
{ {
rt_object_attach_hook = hook; rt_object_attach_hook = hook;
} }
@ -93,7 +93,7 @@ void rt_object_attach_sethook(void (*hook)(struct rt_object* object))
* *
* @param hook the hook function * @param hook the hook function
*/ */
void rt_object_detach_sethook(void (*hook)(struct rt_object* object)) void rt_object_detach_sethook(void (*hook)(struct rt_object *object))
{ {
rt_object_detach_hook = hook; rt_object_detach_hook = hook;
} }
@ -111,7 +111,7 @@ void rt_object_detach_sethook(void (*hook)(struct rt_object* object))
* *
* @param hook the hook function * @param hook the hook function
*/ */
void rt_object_trytake_sethook(void (*hook)(struct rt_object* object)) void rt_object_trytake_sethook(void (*hook)(struct rt_object *object))
{ {
rt_object_trytake_hook = hook; rt_object_trytake_hook = hook;
} }
@ -130,7 +130,7 @@ void rt_object_trytake_sethook(void (*hook)(struct rt_object* object))
* *
* @param hook the hook function * @param hook the hook function
*/ */
void rt_object_take_sethook(void (*hook)(struct rt_object* object)) void rt_object_take_sethook(void (*hook)(struct rt_object *object))
{ {
rt_object_take_hook = hook; rt_object_take_hook = hook;
} }
@ -141,7 +141,7 @@ void rt_object_take_sethook(void (*hook)(struct rt_object* object))
* *
* @param hook the hook function * @param hook the hook function
*/ */
void rt_object_put_sethook(void (*hook)(struct rt_object* object)) void rt_object_put_sethook(void (*hook)(struct rt_object *object))
{ {
rt_object_put_hook = hook; rt_object_put_hook = hook;
} }
@ -184,7 +184,7 @@ struct rt_object_information *rt_object_get_information(enum rt_object_class_typ
* @param type the object type. * @param type the object type.
* @param name the object name. In system, the object's name must be unique. * @param name the object name. In system, the object's name must be unique.
*/ */
void rt_object_init(struct rt_object* object, enum rt_object_class_type type, const char* name) void rt_object_init(struct rt_object *object, enum rt_object_class_type type, const char *name)
{ {
register rt_base_t temp; register rt_base_t temp;
struct rt_object_information* information; struct rt_object_information* information;
@ -255,11 +255,11 @@ void rt_object_detach(rt_object_t object)
* *
* @return object * @return object
*/ */
rt_object_t rt_object_allocate(enum rt_object_class_type type, const char* name) rt_object_t rt_object_allocate(enum rt_object_class_type type, const char *name)
{ {
struct rt_object* object; struct rt_object *object;
register rt_base_t temp; register rt_base_t temp;
struct rt_object_information* information; struct rt_object_information *information;
RT_DEBUG_NOT_IN_INTERRUPT; RT_DEBUG_NOT_IN_INTERRUPT;
@ -272,7 +272,7 @@ rt_object_t rt_object_allocate(enum rt_object_class_type type, const char* name)
information = &rt_object_container[type]; information = &rt_object_container[type];
#endif #endif
object = (struct rt_object*)rt_malloc(information->object_size); object = (struct rt_object *)rt_malloc(information->object_size);
if (object == RT_NULL) if (object == RT_NULL)
{ {
/* no memory can be allocated */ /* no memory can be allocated */
@ -288,11 +288,11 @@ rt_object_t rt_object_allocate(enum rt_object_class_type type, const char* name)
object->flag = 0; object->flag = 0;
#ifdef RT_USING_MODULE #ifdef RT_USING_MODULE
if(rt_module_self() != RT_NULL) if (rt_module_self() != RT_NULL)
{ {
object->flag |= RT_OBJECT_FLAG_MODULE; object->flag |= RT_OBJECT_FLAG_MODULE;
} }
object->module_id = (void*)rt_module_self(); object->module_id = (void *)rt_module_self();
#endif #endif
/* copy name */ /* copy name */
@ -341,7 +341,7 @@ void rt_object_delete(rt_object_t object)
rt_hw_interrupt_enable(temp); rt_hw_interrupt_enable(temp);
#if defined(RT_USING_MODULE) && defined(RT_USING_SLAB) #if defined(RT_USING_MODULE) && defined(RT_USING_SLAB)
if(object->flag & RT_OBJECT_FLAG_MODULE) if (object->flag & RT_OBJECT_FLAG_MODULE)
rt_module_free((rt_module_t)object->module_id, object); rt_module_free((rt_module_t)object->module_id, object);
else else
#endif #endif
@ -382,16 +382,15 @@ rt_err_t rt_object_is_systemobject(rt_object_t object)
* *
* @note this function shall not be invoked in interrupt status. * @note this function shall not be invoked in interrupt status.
*/ */
rt_object_t rt_object_find(const char* name, rt_uint8_t type) rt_object_t rt_object_find(const char *name, rt_uint8_t type)
{ {
struct rt_object* object; struct rt_object *object;
struct rt_list_node* node; struct rt_list_node *node;
struct rt_object_information *information; struct rt_object_information *information;
extern volatile rt_uint8_t rt_interrupt_nest; extern volatile rt_uint8_t rt_interrupt_nest;
/* parameter check */ /* parameter check */
if ((name == RT_NULL) || if ((name == RT_NULL) || (type > RT_Object_Class_Unknown))
(type > RT_Object_Class_Unknown))
return RT_NULL; return RT_NULL;
/* which is invoke in interrupt status */ /* which is invoke in interrupt status */

View File

@ -1,7 +1,7 @@
/* /*
* File : rtm.c * File : rtm.c
* This file is part of RT-Thread RTOS * This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006 - 2010, RT-Thread Development Team * COPYRIGHT (C) 2006 - 2011, RT-Thread Development Team
* *
* The license and distribution terms for this file may be * The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at * found in the file LICENSE in this distribution or at

View File

@ -1,7 +1,7 @@
/* /*
* File : scheduler.c * File : scheduler.c
* This file is part of RT-Thread RTOS * This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team * COPYRIGHT (C) 2006 - 2011, RT-Thread Development Team
* *
* The license and distribution terms for this file may be * The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at * found in the file LICENSE in this distribution or at
@ -35,7 +35,7 @@ static rt_int16_t rt_scheduler_lock_nest;
extern volatile rt_uint8_t rt_interrupt_nest; extern volatile rt_uint8_t rt_interrupt_nest;
rt_list_t rt_thread_priority_table[RT_THREAD_PRIORITY_MAX]; rt_list_t rt_thread_priority_table[RT_THREAD_PRIORITY_MAX];
struct rt_thread* rt_current_thread; struct rt_thread *rt_current_thread;
rt_uint8_t rt_current_priority; rt_uint8_t rt_current_priority;
@ -71,7 +71,7 @@ const rt_uint8_t rt_lowest_bitmap[] =
}; };
#ifdef RT_USING_HOOK #ifdef RT_USING_HOOK
static void (*rt_scheduler_hook)(struct rt_thread* from, struct rt_thread* to); static void (*rt_scheduler_hook)(struct rt_thread *from, struct rt_thread *to);
/** /**
* @addtogroup Hook * @addtogroup Hook
@ -84,7 +84,7 @@ static void (*rt_scheduler_hook)(struct rt_thread* from, struct rt_thread* to);
* *
* @param hook the hook function * @param hook the hook function
*/ */
void rt_scheduler_sethook(void (*hook)(struct rt_thread* from, struct rt_thread* to)) void rt_scheduler_sethook(void (*hook)(struct rt_thread *from, struct rt_thread *to))
{ {
rt_scheduler_hook = hook; rt_scheduler_hook = hook;
} }
@ -93,13 +93,13 @@ void rt_scheduler_sethook(void (*hook)(struct rt_thread* from, struct rt_thread*
#endif #endif
#ifdef RT_USING_OVERFLOW_CHECK #ifdef RT_USING_OVERFLOW_CHECK
static void _rt_scheduler_stack_check(struct rt_thread* thread) static void _rt_scheduler_stack_check(struct rt_thread *thread)
{ {
RT_ASSERT(thread != RT_NULL); RT_ASSERT(thread != RT_NULL);
if ( (rt_uint32_t)thread->sp <= (rt_uint32_t)thread->stack_addr || if ((rt_uint32_t)thread->sp <= (rt_uint32_t)thread->stack_addr ||
(rt_uint32_t)thread->sp > (rt_uint32_t)thread->sp >
(rt_uint32_t)thread->stack_addr + (rt_uint32_t)thread->stack_size ) (rt_uint32_t)thread->stack_addr + (rt_uint32_t)thread->stack_size)
{ {
rt_uint32_t level; rt_uint32_t level;
@ -115,8 +115,7 @@ static void _rt_scheduler_stack_check(struct rt_thread* thread)
} }
else if ((rt_uint32_t)thread->sp <= ((rt_uint32_t)thread->stack_addr + 32)) else if ((rt_uint32_t)thread->sp <= ((rt_uint32_t)thread->stack_addr + 32))
{ {
rt_kprintf("warning: %s stack is close to end of stack address.\n", rt_kprintf("warning: %s stack is close to end of stack address.\n", thread->name);
thread->name);
} }
} }
#endif #endif
@ -160,7 +159,7 @@ void rt_system_scheduler_init(void)
* This function will startup scheduler. It will select one thread * This function will startup scheduler. It will select one thread
* with the highest priority level, then switch to it. * with the highest priority level, then switch to it.
*/ */
void rt_system_scheduler_start() void rt_system_scheduler_start(void)
{ {
register struct rt_thread *to_thread; register struct rt_thread *to_thread;
register rt_ubase_t highest_ready_priority; register rt_ubase_t highest_ready_priority;
@ -215,7 +214,7 @@ void rt_system_scheduler_start()
* This function will perform one schedule. It will select one thread * This function will perform one schedule. It will select one thread
* with the highest priority level, then switch to it. * with the highest priority level, then switch to it.
*/ */
void rt_schedule() void rt_schedule(void)
{ {
rt_base_t level; rt_base_t level;
struct rt_thread *to_thread; struct rt_thread *to_thread;
@ -269,7 +268,7 @@ void rt_schedule()
rt_current_thread = to_thread; rt_current_thread = to_thread;
#ifdef RT_USING_MODULE #ifdef RT_USING_MODULE
rt_module_set ((rt_current_thread->module_id != RT_NULL) ? rt_module_set((rt_current_thread->module_id != RT_NULL) ?
(rt_module_t)rt_current_thread->module_id : RT_NULL); (rt_module_t)rt_current_thread->module_id : RT_NULL);
#endif #endif
@ -290,10 +289,9 @@ void rt_schedule()
} }
else else
{ {
RT_DEBUG_LOG(RT_DEBUG_SCHEDULER,("switch in interrupt\n")); RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("switch in interrupt\n"));
rt_hw_context_switch_interrupt((rt_uint32_t)&from_thread->sp, rt_hw_context_switch_interrupt((rt_uint32_t)&from_thread->sp, (rt_uint32_t)&to_thread->sp);
(rt_uint32_t)&to_thread->sp);
} }
} }
} }
@ -309,7 +307,7 @@ void rt_schedule()
* @param thread the thread to be inserted * @param thread the thread to be inserted
* @note Please do not invoke this function in user application. * @note Please do not invoke this function in user application.
*/ */
void rt_schedule_insert_thread(struct rt_thread* thread) void rt_schedule_insert_thread(struct rt_thread *thread)
{ {
register rt_base_t temp; register rt_base_t temp;
@ -322,8 +320,7 @@ void rt_schedule_insert_thread(struct rt_thread* thread)
thread->stat = RT_THREAD_READY; thread->stat = RT_THREAD_READY;
/* insert thread to ready list */ /* insert thread to ready list */
rt_list_insert_before(&(rt_thread_priority_table[thread->current_priority]), rt_list_insert_before(&(rt_thread_priority_table[thread->current_priority]), &(thread->tlist));
&(thread->tlist));
/* set priority mask */ /* set priority mask */
#if RT_THREAD_PRIORITY_MAX <= 32 #if RT_THREAD_PRIORITY_MAX <= 32
@ -350,7 +347,7 @@ void rt_schedule_insert_thread(struct rt_thread* thread)
* *
* @note Please do not invoke this function in user application. * @note Please do not invoke this function in user application.
*/ */
void rt_schedule_remove_thread(struct rt_thread* thread) void rt_schedule_remove_thread(struct rt_thread *thread)
{ {
register rt_base_t temp; register rt_base_t temp;
@ -389,7 +386,7 @@ void rt_schedule_remove_thread(struct rt_thread* thread)
/** /**
* This function will lock the thread scheduler. * This function will lock the thread scheduler.
*/ */
void rt_enter_critical() void rt_enter_critical(void)
{ {
register rt_base_t level; register rt_base_t level;
@ -398,7 +395,7 @@ void rt_enter_critical()
/* the maximal number of nest is RT_UINT16_MAX, which is big /* the maximal number of nest is RT_UINT16_MAX, which is big
* enough and does not check here */ * enough and does not check here */
rt_scheduler_lock_nest++; rt_scheduler_lock_nest ++;
/* enable interrupt */ /* enable interrupt */
rt_hw_interrupt_enable(level); rt_hw_interrupt_enable(level);
@ -407,7 +404,7 @@ void rt_enter_critical()
/** /**
* This function will unlock the thread scheduler. * This function will unlock the thread scheduler.
*/ */
void rt_exit_critical() void rt_exit_critical(void)
{ {
register rt_base_t level; register rt_base_t level;

View File

@ -1,7 +1,7 @@
/* /*
* File : slab.c * File : slab.c
* This file is part of RT-Thread RTOS * This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2008 - 2009, RT-Thread Development Team * COPYRIGHT (C) 2008 - 2011, RT-Thread Development Team
* *
* The license and distribution terms for this file may be * The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at * found in the file LICENSE in this distribution or at
@ -164,7 +164,8 @@ typedef struct slab_chunk
/* /*
* The IN-BAND zone header is placed at the beginning of each zone. * The IN-BAND zone header is placed at the beginning of each zone.
*/ */
typedef struct slab_zone { typedef struct slab_zone
{
rt_int32_t z_magic; /* magic number for sanity check */ rt_int32_t z_magic; /* magic number for sanity check */
rt_int32_t z_nfree; /* total free chunks / ualloc space in zone */ rt_int32_t z_nfree; /* total free chunks / ualloc space in zone */
rt_int32_t z_nmax; /* maximum free chunks */ rt_int32_t z_nmax; /* maximum free chunks */
@ -207,7 +208,8 @@ static int zone_page_cnt;
#define PAGE_TYPE_FREE 0x00 #define PAGE_TYPE_FREE 0x00
#define PAGE_TYPE_SMALL 0x01 #define PAGE_TYPE_SMALL 0x01
#define PAGE_TYPE_LARGE 0x02 #define PAGE_TYPE_LARGE 0x02
struct memusage { struct memusage
{
rt_uint32_t type:2 ; /* page type */ rt_uint32_t type:2 ; /* page type */
rt_uint32_t size:30; /* pages allocated or offset from zone */ rt_uint32_t size:30; /* pages allocated or offset from zone */
}; };
@ -317,7 +319,7 @@ _return:
/* /*
* Initialize the page allocator * Initialize the page allocator
*/ */
static void rt_page_init(void* addr, rt_size_t npages) static void rt_page_init(void *addr, rt_size_t npages)
{ {
RT_ASSERT(addr != RT_NULL); RT_ASSERT(addr != RT_NULL);
RT_ASSERT(npages != 0); RT_ASSERT(npages != 0);
@ -335,7 +337,7 @@ static void rt_page_init(void* addr, rt_size_t npages)
* @param end_addr the end address of system page * @param end_addr the end address of system page
* *
*/ */
void rt_system_heap_init(void *begin_addr, void* end_addr) void rt_system_heap_init(void *begin_addr, void *end_addr)
{ {
rt_uint32_t limsize, npages; rt_uint32_t limsize, npages;
@ -345,7 +347,8 @@ void rt_system_heap_init(void *begin_addr, void* end_addr)
heap_start = RT_ALIGN((rt_uint32_t)begin_addr, RT_MM_PAGE_SIZE); heap_start = RT_ALIGN((rt_uint32_t)begin_addr, RT_MM_PAGE_SIZE);
heap_end = RT_ALIGN_DOWN((rt_uint32_t)end_addr, RT_MM_PAGE_SIZE); heap_end = RT_ALIGN_DOWN((rt_uint32_t)end_addr, RT_MM_PAGE_SIZE);
if(heap_start >= heap_end) { if (heap_start >= heap_end)
{
rt_kprintf("rt_system_heap_init, wrong address[0x%x - 0x%x]\n", rt_kprintf("rt_system_heap_init, wrong address[0x%x - 0x%x]\n",
(rt_uint32_t)begin_addr, (rt_uint32_t)end_addr); (rt_uint32_t)begin_addr, (rt_uint32_t)end_addr);
return; return;
@ -361,7 +364,7 @@ void rt_system_heap_init(void *begin_addr, void* end_addr)
("heap[0x%x - 0x%x], size 0x%x, 0x%x pages\n", heap_start, heap_end, limsize, npages)); ("heap[0x%x - 0x%x], size 0x%x, 0x%x pages\n", heap_start, heap_end, limsize, npages));
/* init pages */ /* init pages */
rt_page_init((void*)heap_start, npages); rt_page_init((void *)heap_start, npages);
/* calculate zone size */ /* calculate zone size */
zone_size = ZALLOC_MIN_ZONE_SIZE; zone_size = ZALLOC_MIN_ZONE_SIZE;
@ -383,7 +386,6 @@ void rt_system_heap_init(void *begin_addr, void* end_addr)
RT_DEBUG_LOG(RT_DEBUG_SLAB, RT_DEBUG_LOG(RT_DEBUG_SLAB,
("memusage 0x%x, size 0x%x\n", (rt_uint32_t)memusage, limsize)); ("memusage 0x%x, size 0x%x\n", (rt_uint32_t)memusage, limsize));
} }
/* /*
@ -468,7 +470,7 @@ void *rt_malloc(rt_size_t size)
if (size == 0) return RT_NULL; if (size == 0) return RT_NULL;
#ifdef RT_USING_MODULE #ifdef RT_USING_MODULE
if(rt_module_self() != RT_NULL) return rt_module_malloc(size); if (rt_module_self() != RT_NULL) return rt_module_malloc(size);
#endif #endif
/* /*
@ -487,7 +489,7 @@ void *rt_malloc(rt_size_t size)
kup->type = PAGE_TYPE_LARGE; kup->type = PAGE_TYPE_LARGE;
kup->size = size >> RT_MM_PAGE_BITS; kup->size = size >> RT_MM_PAGE_BITS;
RT_DEBUG_LOG(RT_DEBUG_SLAB,("malloc a large memory 0x%x, page cnt %d, kup %d\n", RT_DEBUG_LOG(RT_DEBUG_SLAB, ("malloc a large memory 0x%x, page cnt %d, kup %d\n",
size, size,
size >> RT_MM_PAGE_BITS, size >> RT_MM_PAGE_BITS,
((rt_uint32_t)chunk - heap_start) >> RT_MM_PAGE_BITS)); ((rt_uint32_t)chunk - heap_start) >> RT_MM_PAGE_BITS));
@ -516,8 +518,7 @@ void *rt_malloc(rt_size_t size)
zi = zoneindex(&size); zi = zoneindex(&size);
RT_ASSERT(zi < NZONES); RT_ASSERT(zi < NZONES);
RT_DEBUG_LOG(RT_DEBUG_SLAB, RT_DEBUG_LOG(RT_DEBUG_SLAB, ("try to malloc 0x%x on zone: %d\n", size, zi));
("try to malloc 0x%x on zone: %d\n", size, zi));
if ((z = zone_array[zi]) != RT_NULL) if ((z = zone_array[zi]) != RT_NULL)
{ {
@ -573,7 +574,7 @@ void *rt_malloc(rt_size_t size)
{ {
/* remove zone from free zone list */ /* remove zone from free zone list */
zone_free = z->z_next; zone_free = z->z_next;
--zone_free_cnt; -- zone_free_cnt;
} }
else else
{ {
@ -587,7 +588,7 @@ void *rt_malloc(rt_size_t size)
/* lock heap */ /* lock heap */
rt_sem_take(&heap_sem, RT_WAITING_FOREVER); rt_sem_take(&heap_sem, RT_WAITING_FOREVER);
RT_DEBUG_LOG(RT_DEBUG_SLAB,("alloc a new zone: 0x%x\n", (rt_uint32_t)z)); RT_DEBUG_LOG(RT_DEBUG_SLAB, ("alloc a new zone: 0x%x\n", (rt_uint32_t)z));
/* set message usage */ /* set message usage */
for (off = 0, kup = btokup(z); off < zone_page_cnt; off ++) for (off = 0, kup = btokup(z); off < zone_page_cnt; off ++)
@ -618,7 +619,7 @@ void *rt_malloc(rt_size_t size)
z->z_zoneindex = zi; z->z_zoneindex = zi;
z->z_nmax = (zone_size - off) / size; z->z_nmax = (zone_size - off) / size;
z->z_nfree = z->z_nmax - 1; z->z_nfree = z->z_nmax - 1;
z->z_baseptr = (rt_uint8_t*)z + off; z->z_baseptr = (rt_uint8_t *)z + off;
z->z_uindex = 0; z->z_uindex = 0;
z->z_chunksize = size; z->z_chunksize = size;
@ -637,7 +638,7 @@ void *rt_malloc(rt_size_t size)
done: done:
rt_sem_release(&heap_sem); rt_sem_release(&heap_sem);
RT_OBJECT_HOOK_CALL(rt_malloc_hook, ((char*)chunk, size)); RT_OBJECT_HOOK_CALL(rt_malloc_hook, ((char *)chunk, size));
return chunk; return chunk;
@ -668,7 +669,7 @@ void *rt_realloc(void *ptr, rt_size_t size)
} }
#ifdef RT_USING_MODULE #ifdef RT_USING_MODULE
if(rt_module_self() != RT_NULL) return rt_module_realloc(ptr, size); if (rt_module_self() != RT_NULL) return rt_module_realloc(ptr, size);
#endif #endif
/* /*
@ -682,14 +683,14 @@ void *rt_realloc(void *ptr, rt_size_t size)
osize = kup->size << RT_MM_PAGE_BITS; osize = kup->size << RT_MM_PAGE_BITS;
if ((nptr = rt_malloc(size)) == RT_NULL) return RT_NULL; if ((nptr = rt_malloc(size)) == RT_NULL) return RT_NULL;
rt_memcpy(nptr, ptr, size > osize? osize : size); rt_memcpy(nptr, ptr, size > osize ? osize : size);
rt_free(ptr); rt_free(ptr);
return nptr; return nptr;
} }
else if (kup->type == PAGE_TYPE_SMALL) else if (kup->type == PAGE_TYPE_SMALL)
{ {
z = (slab_zone*)(((rt_uint32_t)ptr & ~RT_MM_PAGE_MASK) - kup->size * RT_MM_PAGE_SIZE); z = (slab_zone *)(((rt_uint32_t)ptr & ~RT_MM_PAGE_MASK) - kup->size * RT_MM_PAGE_SIZE);
RT_ASSERT(z->z_magic == ZALLOC_SLAB_MAGIC); RT_ASSERT(z->z_magic == ZALLOC_SLAB_MAGIC);
zoneindex(&size); zoneindex(&size);
@ -702,7 +703,7 @@ void *rt_realloc(void *ptr, rt_size_t size)
*/ */
if ((nptr = rt_malloc(size)) == RT_NULL) return RT_NULL; if ((nptr = rt_malloc(size)) == RT_NULL) return RT_NULL;
rt_memcpy(nptr, ptr, size > z->z_chunksize? z->z_chunksize : size); rt_memcpy(nptr, ptr, size > z->z_chunksize ? z->z_chunksize : size);
rt_free(ptr); rt_free(ptr);
return nptr; return nptr;
@ -802,10 +803,10 @@ void rt_free(void *ptr)
rt_sem_take(&heap_sem, RT_WAITING_FOREVER); rt_sem_take(&heap_sem, RT_WAITING_FOREVER);
/* zone case. get out zone. */ /* zone case. get out zone. */
z = (slab_zone*)(((rt_uint32_t)ptr & ~RT_MM_PAGE_MASK) - kup->size * RT_MM_PAGE_SIZE); z = (slab_zone *)(((rt_uint32_t)ptr & ~RT_MM_PAGE_MASK) - kup->size * RT_MM_PAGE_SIZE);
RT_ASSERT(z->z_magic == ZALLOC_SLAB_MAGIC); RT_ASSERT(z->z_magic == ZALLOC_SLAB_MAGIC);
chunk = (slab_chunk*)ptr; chunk = (slab_chunk *)ptr;
chunk->c_next = z->z_freechunk; chunk->c_next = z->z_freechunk;
z->z_freechunk = chunk; z->z_freechunk = chunk;
@ -829,16 +830,14 @@ void rt_free(void *ptr)
* this code can be called from an IPI callback, do *NOT* try to mess * this code can be called from an IPI callback, do *NOT* try to mess
* with kernel_map here. Hysteresis will be performed at malloc() time. * with kernel_map here. Hysteresis will be performed at malloc() time.
*/ */
if (z->z_nfree == z->z_nmax && if (z->z_nfree == z->z_nmax && (z->z_next || zone_array[z->z_zoneindex] != z))
(z->z_next || zone_array[z->z_zoneindex] != z))
{ {
slab_zone **pz; slab_zone **pz;
RT_DEBUG_LOG(RT_DEBUG_SLAB, RT_DEBUG_LOG(RT_DEBUG_SLAB, ("free zone 0x%x\n", (rt_uint32_t)z, z->z_zoneindex));
("free zone 0x%x\n", (rt_uint32_t)z, z->z_zoneindex));
/* remove zone from zone array list */ /* remove zone from zone array list */
for (pz = &zone_array[z->z_zoneindex]; z != *pz; pz = &(*pz)->z_next) ; for (pz = &zone_array[z->z_zoneindex]; z != *pz; pz = &(*pz)->z_next);
*pz = z->z_next; *pz = z->z_next;
/* reset zone */ /* reset zone */
@ -848,7 +847,7 @@ void rt_free(void *ptr)
z->z_next = zone_free; z->z_next = zone_free;
zone_free = z; zone_free = z;
++zone_free_cnt; ++ zone_free_cnt;
/* release zone to page allocator */ /* release zone to page allocator */
if (zone_free_cnt > ZONE_RELEASE_THRESH) if (zone_free_cnt > ZONE_RELEASE_THRESH)
@ -857,7 +856,7 @@ void rt_free(void *ptr)
z = zone_free; z = zone_free;
zone_free = z->z_next; zone_free = z->z_next;
--zone_free_cnt; -- zone_free_cnt;
/* set message usage */ /* set message usage */
for (i = 0, kup = btokup(z); i < zone_page_cnt; i ++) for (i = 0, kup = btokup(z); i < zone_page_cnt; i ++)
@ -880,9 +879,7 @@ void rt_free(void *ptr)
} }
#ifdef RT_MEM_STATS #ifdef RT_MEM_STATS
void rt_memory_info(rt_uint32_t *total, void rt_memory_info(rt_uint32_t *total, rt_uint32_t *used, rt_uint32_t *max_used)
rt_uint32_t *used,
rt_uint32_t *max_used)
{ {
if (total != RT_NULL) *total = heap_end - heap_start; if (total != RT_NULL) *total = heap_end - heap_start;
if (used != RT_NULL) *used = used_mem; if (used != RT_NULL) *used = used_mem;
@ -891,7 +888,7 @@ void rt_memory_info(rt_uint32_t *total,
#ifdef RT_USING_FINSH #ifdef RT_USING_FINSH
#include <finsh.h> #include <finsh.h>
void list_mem() void list_mem(void)
{ {
rt_kprintf("total memory: %d\n", heap_end - heap_start); rt_kprintf("total memory: %d\n", heap_end - heap_start);
rt_kprintf("used memory : %d\n", used_mem); rt_kprintf("used memory : %d\n", used_mem);

View File

@ -1,7 +1,7 @@
/* /*
* File : thread.c * File : thread.c
* This file is part of RT-Thread RTOS * This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006 - 2010, RT-Thread Development Team * COPYRIGHT (C) 2006 - 2011, RT-Thread Development Team
* *
* The license and distribution terms for this file may be * The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at * found in the file LICENSE in this distribution or at
@ -29,25 +29,24 @@
#include <rthw.h> #include <rthw.h>
#include "kservice.h" #include "kservice.h"
extern rt_list_t rt_thread_priority_table[RT_THREAD_PRIORITY_MAX]; extern rt_list_t rt_thread_priority_table[RT_THREAD_PRIORITY_MAX];
extern struct rt_thread* rt_current_thread; extern struct rt_thread *rt_current_thread;
extern rt_uint8_t rt_current_priority; extern rt_uint8_t rt_current_priority;
extern rt_list_t rt_thread_defunct; extern rt_list_t rt_thread_defunct;
static void rt_thread_exit(void); static void rt_thread_exit(void);
void rt_thread_timeout(void* parameter); void rt_thread_timeout(void *parameter);
static rt_err_t _rt_thread_init(struct rt_thread* thread, static rt_err_t _rt_thread_init(struct rt_thread *thread,
const char* name, const char *name,
void (*entry)(void* parameter), void* parameter, void (*entry)(void *parameter), void *parameter,
void* stack_start, rt_uint32_t stack_size, void *stack_start, rt_uint32_t stack_size,
rt_uint8_t priority, rt_uint32_t tick) rt_uint8_t priority, rt_uint32_t tick)
{ {
/* init thread list */ /* init thread list */
rt_list_init(&(thread->tlist)); rt_list_init(&(thread->tlist));
thread->entry = (void*)entry; thread->entry = (void *)entry;
thread->parameter = parameter; thread->parameter = parameter;
/* stack init */ /* stack init */
@ -56,9 +55,9 @@ static rt_err_t _rt_thread_init(struct rt_thread* thread,
/* init thread stack */ /* init thread stack */
rt_memset(thread->stack_addr, '#', thread->stack_size); rt_memset(thread->stack_addr, '#', thread->stack_size);
thread->sp = (void*)rt_hw_stack_init(thread->entry, thread->parameter, thread->sp = (void *)rt_hw_stack_init(thread->entry, thread->parameter,
(void *) ((char *)thread->stack_addr + thread->stack_size - 4), (void *) ((char *)thread->stack_addr + thread->stack_size - 4),
(void*)rt_thread_exit); (void *)rt_thread_exit);
/* priority init */ /* priority init */
RT_ASSERT(priority < RT_THREAD_PRIORITY_MAX); RT_ASSERT(priority < RT_THREAD_PRIORITY_MAX);
@ -110,10 +109,10 @@ static rt_err_t _rt_thread_init(struct rt_thread* thread,
* @return the operation status, RT_EOK on OK, -RT_ERROR on error * @return the operation status, RT_EOK on OK, -RT_ERROR on error
* *
*/ */
rt_err_t rt_thread_init(struct rt_thread* thread, rt_err_t rt_thread_init(struct rt_thread *thread,
const char* name, const char *name,
void (*entry)(void* parameter), void* parameter, void (*entry)(void *parameter), void *parameter,
void* stack_start, rt_uint32_t stack_size, void *stack_start, rt_uint32_t stack_size,
rt_uint8_t priority, rt_uint32_t tick) rt_uint8_t priority, rt_uint32_t tick)
{ {
/* thread check */ /* thread check */
@ -143,19 +142,19 @@ rt_err_t rt_thread_init(struct rt_thread* thread,
* @return the created thread object * @return the created thread object
* *
*/ */
rt_thread_t rt_thread_create (const char* name, rt_thread_t rt_thread_create (const char *name,
void (*entry)(void* parameter), void* parameter, void (*entry)(void *parameter), void *parameter,
rt_uint32_t stack_size, rt_uint32_t stack_size,
rt_uint8_t priority, rt_uint8_t priority,
rt_uint32_t tick) rt_uint32_t tick)
{ {
struct rt_thread* thread; struct rt_thread *thread;
void* stack_start; void *stack_start;
thread = (struct rt_thread*) rt_object_allocate(RT_Object_Class_Thread, name); thread = (struct rt_thread *) rt_object_allocate(RT_Object_Class_Thread, name);
if (thread == RT_NULL) return RT_NULL; if (thread == RT_NULL) return RT_NULL;
stack_start = (void*)rt_malloc(stack_size); stack_start = (void *)rt_malloc(stack_size);
if (stack_start == RT_NULL) if (stack_start == RT_NULL)
{ {
/* allocate stack failure */ /* allocate stack failure */
@ -177,7 +176,7 @@ rt_thread_t rt_thread_create (const char* name,
* @return the self thread object * @return the self thread object
* *
*/ */
rt_thread_t rt_thread_self (void) rt_thread_t rt_thread_self(void)
{ {
return rt_current_thread; return rt_current_thread;
} }
@ -190,7 +189,7 @@ rt_thread_t rt_thread_self (void)
* @return the operation status, RT_EOK on OK, -RT_ERROR on error * @return the operation status, RT_EOK on OK, -RT_ERROR on error
* *
*/ */
rt_err_t rt_thread_startup (rt_thread_t thread) rt_err_t rt_thread_startup(rt_thread_t thread)
{ {
/* thread check */ /* thread check */
RT_ASSERT(thread != RT_NULL); RT_ASSERT(thread != RT_NULL);
@ -205,7 +204,7 @@ rt_err_t rt_thread_startup (rt_thread_t thread)
thread->number_mask = 1L << thread->number; thread->number_mask = 1L << thread->number;
thread->high_mask = 1L << (thread->current_priority & 0x07); /* 3bit */ thread->high_mask = 1L << (thread->current_priority & 0x07); /* 3bit */
#else #else
thread->number_mask = 1L << thread->current_priority; //1L means long int,fixed compile mistake with IAR EW M16C v3.401,fify 20100410 thread->number_mask = 1L << thread->current_priority;
#endif #endif
RT_DEBUG_LOG(RT_DEBUG_THREAD,\ RT_DEBUG_LOG(RT_DEBUG_THREAD,\
@ -223,9 +222,9 @@ rt_err_t rt_thread_startup (rt_thread_t thread)
return RT_EOK; return RT_EOK;
} }
static void rt_thread_exit() static void rt_thread_exit(void)
{ {
struct rt_thread* thread; struct rt_thread *thread;
register rt_base_t level; register rt_base_t level;
/* get current thread */ /* get current thread */
@ -270,7 +269,7 @@ static void rt_thread_exit()
* @return the operation status, RT_EOK on OK, -RT_ERROR on error * @return the operation status, RT_EOK on OK, -RT_ERROR on error
* *
*/ */
rt_err_t rt_thread_detach (rt_thread_t thread) rt_err_t rt_thread_detach(rt_thread_t thread)
{ {
rt_base_t lock; rt_base_t lock;
@ -314,7 +313,7 @@ rt_err_t rt_thread_detach (rt_thread_t thread)
* @return the operation status, RT_EOK on OK, -RT_ERROR on error * @return the operation status, RT_EOK on OK, -RT_ERROR on error
* *
*/ */
rt_err_t rt_thread_delete (rt_thread_t thread) rt_err_t rt_thread_delete(rt_thread_t thread)
{ {
rt_base_t lock; rt_base_t lock;
@ -351,7 +350,7 @@ rt_err_t rt_thread_delete (rt_thread_t thread)
* @return RT_EOK * @return RT_EOK
* *
*/ */
rt_err_t rt_thread_yield () rt_err_t rt_thread_yield(void)
{ {
register rt_base_t level; register rt_base_t level;
struct rt_thread *thread; struct rt_thread *thread;
@ -394,7 +393,7 @@ rt_err_t rt_thread_yield ()
* @return RT_EOK * @return RT_EOK
* *
*/ */
rt_err_t rt_thread_sleep (rt_tick_t tick) rt_err_t rt_thread_sleep(rt_tick_t tick)
{ {
register rt_base_t temp; register rt_base_t temp;
struct rt_thread *thread; struct rt_thread *thread;
@ -449,7 +448,7 @@ rt_err_t rt_thread_delay(rt_tick_t tick)
* *
* @return RT_EOK * @return RT_EOK
*/ */
rt_err_t rt_thread_control (rt_thread_t thread, rt_uint8_t cmd, void* arg) rt_err_t rt_thread_control(rt_thread_t thread, rt_uint8_t cmd, void *arg)
{ {
register rt_base_t temp; register rt_base_t temp;
@ -469,7 +468,7 @@ rt_err_t rt_thread_control (rt_thread_t thread, rt_uint8_t cmd, void* arg)
rt_schedule_remove_thread(thread); rt_schedule_remove_thread(thread);
/* change thread priority */ /* change thread priority */
thread->current_priority = *(rt_uint8_t*) arg; thread->current_priority = *(rt_uint8_t *)arg;
/* recalculate priority attribute */ /* recalculate priority attribute */
#if RT_THREAD_PRIORITY_MAX > 32 #if RT_THREAD_PRIORITY_MAX > 32
@ -485,7 +484,7 @@ rt_err_t rt_thread_control (rt_thread_t thread, rt_uint8_t cmd, void* arg)
} }
else else
{ {
thread->current_priority = *(rt_uint8_t*) arg; thread->current_priority = *(rt_uint8_t *)arg;
/* recalculate priority attribute */ /* recalculate priority attribute */
#if RT_THREAD_PRIORITY_MAX > 32 #if RT_THREAD_PRIORITY_MAX > 32
@ -526,7 +525,7 @@ rt_err_t rt_thread_control (rt_thread_t thread, rt_uint8_t cmd, void* arg)
* @note if suspend self thread, after this function call, the * @note if suspend self thread, after this function call, the
* rt_schedule() must be invoked. * rt_schedule() must be invoked.
*/ */
rt_err_t rt_thread_suspend (rt_thread_t thread) rt_err_t rt_thread_suspend(rt_thread_t thread)
{ {
register rt_base_t temp; register rt_base_t temp;
@ -564,7 +563,7 @@ rt_err_t rt_thread_suspend (rt_thread_t thread)
* @return the operation status, RT_EOK on OK, -RT_ERROR on error * @return the operation status, RT_EOK on OK, -RT_ERROR on error
* *
*/ */
rt_err_t rt_thread_resume (rt_thread_t thread) rt_err_t rt_thread_resume(rt_thread_t thread)
{ {
register rt_base_t temp; register rt_base_t temp;
@ -609,11 +608,11 @@ rt_err_t rt_thread_resume (rt_thread_t thread)
* @param parameter the parameter of thread timeout function * @param parameter the parameter of thread timeout function
* *
*/ */
void rt_thread_timeout(void* parameter) void rt_thread_timeout(void *parameter)
{ {
struct rt_thread* thread; struct rt_thread *thread;
thread = (struct rt_thread*) parameter; thread = (struct rt_thread *)parameter;
/* thread check */ /* thread check */
RT_ASSERT(thread != RT_NULL); RT_ASSERT(thread != RT_NULL);
@ -641,11 +640,11 @@ void rt_thread_timeout(void* parameter)
* *
* @note please don't invoke this function in interrupt status. * @note please don't invoke this function in interrupt status.
*/ */
rt_thread_t rt_thread_find(char* name) rt_thread_t rt_thread_find(char *name)
{ {
struct rt_object_information *information; struct rt_object_information *information;
struct rt_object* object; struct rt_object *object;
struct rt_list_node* node; struct rt_list_node *node;
extern struct rt_object_information rt_object_container[]; extern struct rt_object_information rt_object_container[];

View File

@ -1,7 +1,7 @@
/* /*
* File : timer.c * File : timer.c
* This file is part of RT-Thread RTOS * This file is part of RT-Thread RTOS
* COPYRIGHT (C) 2006 - 2009, RT-Thread Development Team * COPYRIGHT (C) 2006 - 2011, RT-Thread Development Team
* *
* The license and distribution terms for this file may be * The license and distribution terms for this file may be
* found in the file LICENSE in this distribution or at * found in the file LICENSE in this distribution or at
@ -33,9 +33,9 @@ static rt_list_t rt_soft_timer_list;
#endif #endif
#ifdef RT_USING_HOOK #ifdef RT_USING_HOOK
extern void (*rt_object_take_hook)(struct rt_object* object); extern void (*rt_object_take_hook)(struct rt_object *object);
extern void (*rt_object_put_hook)(struct rt_object* object); extern void (*rt_object_put_hook)(struct rt_object *object);
static void (*rt_timer_timeout_hook)(struct rt_timer* timer); static void (*rt_timer_timeout_hook)(struct rt_timer *timer);
/** /**
* @addtogroup Hook * @addtogroup Hook
@ -48,7 +48,7 @@ static void (*rt_timer_timeout_hook)(struct rt_timer* timer);
* *
* @param hook the hook function * @param hook the hook function
*/ */
void rt_timer_timeout_sethook(void (*hook)(struct rt_timer* timer)) void rt_timer_timeout_sethook(void (*hook)(struct rt_timer *timer))
{ {
rt_timer_timeout_hook = hook; rt_timer_timeout_hook = hook;
} }
@ -57,7 +57,7 @@ void rt_timer_timeout_sethook(void (*hook)(struct rt_timer* timer))
#endif #endif
static void _rt_timer_init(rt_timer_t timer, static void _rt_timer_init(rt_timer_t timer,
void (*timeout)(void* parameter), void* parameter, void (*timeout)(void *parameter), void *parameter,
rt_tick_t time, rt_uint8_t flag) rt_tick_t time, rt_uint8_t flag)
{ {
/* set flag */ /* set flag */
@ -93,8 +93,8 @@ static void _rt_timer_init(rt_timer_t timer,
* @param flag the flag of timer * @param flag the flag of timer
*/ */
void rt_timer_init(rt_timer_t timer, void rt_timer_init(rt_timer_t timer,
const char* name, const char *name,
void (*timeout)(void* parameter), void* parameter, void (*timeout)(void *parameter), void *parameter,
rt_tick_t time, rt_uint8_t flag) rt_tick_t time, rt_uint8_t flag)
{ {
/* timer check */ /* timer check */
@ -146,12 +146,12 @@ rt_err_t rt_timer_detach(rt_timer_t timer)
* *
* @return the created timer object * @return the created timer object
*/ */
rt_timer_t rt_timer_create(const char* name, void (*timeout)(void* parameter), void* parameter, rt_tick_t time, rt_uint8_t flag) rt_timer_t rt_timer_create(const char *name, void (*timeout)(void *parameter), void *parameter, rt_tick_t time, rt_uint8_t flag)
{ {
struct rt_timer* timer; struct rt_timer *timer;
/* allocate a object */ /* allocate a object */
timer = (struct rt_timer*)rt_object_allocate(RT_Object_Class_Timer, name); timer = (struct rt_timer *)rt_object_allocate(RT_Object_Class_Timer, name);
if (timer == RT_NULL) if (timer == RT_NULL)
{ {
return RT_NULL; return RT_NULL;
@ -202,7 +202,7 @@ rt_err_t rt_timer_delete(rt_timer_t timer)
*/ */
rt_err_t rt_timer_start(rt_timer_t timer) rt_err_t rt_timer_start(rt_timer_t timer)
{ {
struct rt_timer* t; struct rt_timer *t;
register rt_base_t level; register rt_base_t level;
rt_list_t *n, *timer_list; rt_list_t *n, *timer_list;
@ -273,7 +273,7 @@ rt_err_t rt_timer_stop(rt_timer_t timer)
/* timer check */ /* timer check */
RT_ASSERT(timer != RT_NULL); RT_ASSERT(timer != RT_NULL);
if(!(timer->parent.flag & RT_TIMER_FLAG_ACTIVATED)) return -RT_ERROR; if (!(timer->parent.flag & RT_TIMER_FLAG_ACTIVATED)) return -RT_ERROR;
RT_OBJECT_HOOK_CALL(rt_object_put_hook, (&(timer->parent))); RT_OBJECT_HOOK_CALL(rt_object_put_hook, (&(timer->parent)));
@ -302,7 +302,7 @@ rt_err_t rt_timer_stop(rt_timer_t timer)
* @return RT_EOK * @return RT_EOK
* *
*/ */
rt_err_t rt_timer_control(rt_timer_t timer, rt_uint8_t cmd, void* arg) rt_err_t rt_timer_control(rt_timer_t timer, rt_uint8_t cmd, void *arg)
{ {
/* timer check */ /* timer check */
RT_ASSERT(timer != RT_NULL); RT_ASSERT(timer != RT_NULL);
@ -310,11 +310,11 @@ rt_err_t rt_timer_control(rt_timer_t timer, rt_uint8_t cmd, void* arg)
switch (cmd) switch (cmd)
{ {
case RT_TIMER_CTRL_GET_TIME: case RT_TIMER_CTRL_GET_TIME:
*(rt_tick_t*)arg = timer->init_tick; *(rt_tick_t *)arg = timer->init_tick;
break; break;
case RT_TIMER_CTRL_SET_TIME: case RT_TIMER_CTRL_SET_TIME:
timer->init_tick = *(rt_tick_t*)arg; timer->init_tick = *(rt_tick_t *)arg;
break; break;
case RT_TIMER_CTRL_SET_ONESHOT: case RT_TIMER_CTRL_SET_ONESHOT:
@ -336,7 +336,7 @@ rt_err_t rt_timer_control(rt_timer_t timer, rt_uint8_t cmd, void* arg)
* @note this function shall be invoked in operating system timer interrupt. * @note this function shall be invoked in operating system timer interrupt.
*/ */
#ifdef RT_USING_TIMER_SOFT #ifdef RT_USING_TIMER_SOFT
void rt_soft_timer_tick_increase (void); void rt_soft_timer_tick_increase(void);
#endif #endif
void rt_timer_check(void) void rt_timer_check(void)
{ {
@ -344,7 +344,7 @@ void rt_timer_check(void)
rt_tick_t current_tick; rt_tick_t current_tick;
register rt_base_t level; register rt_base_t level;
RT_DEBUG_LOG(RT_DEBUG_TIMER,("timer check enter\n")); RT_DEBUG_LOG(RT_DEBUG_TIMER, ("timer check enter\n"));
current_tick = rt_tick_get(); current_tick = rt_tick_get();
@ -371,7 +371,7 @@ void rt_timer_check(void)
/* re-get tick */ /* re-get tick */
current_tick = rt_tick_get(); current_tick = rt_tick_get();
RT_DEBUG_LOG(RT_DEBUG_TIMER,("current tick: %d\n", current_tick)); RT_DEBUG_LOG(RT_DEBUG_TIMER, ("current tick: %d\n", current_tick));
if ((t->parent.flag & RT_TIMER_FLAG_PERIODIC) && if ((t->parent.flag & RT_TIMER_FLAG_PERIODIC) &&
(t->parent.flag & RT_TIMER_FLAG_ACTIVATED)) (t->parent.flag & RT_TIMER_FLAG_ACTIVATED))
@ -394,11 +394,10 @@ void rt_timer_check(void)
/* increase soft timer tick */ /* increase soft timer tick */
#ifdef RT_USING_TIMER_SOFT #ifdef RT_USING_TIMER_SOFT
rt_soft_timer_tick_increase ( ); rt_soft_timer_tick_increase();
#endif #endif
RT_DEBUG_LOG(RT_DEBUG_TIMER,("timer check leave\n")); RT_DEBUG_LOG(RT_DEBUG_TIMER, ("timer check leave\n"));
} }
#ifdef RT_USING_TIMER_SOFT #ifdef RT_USING_TIMER_SOFT
@ -409,9 +408,9 @@ static struct rt_semaphore timer_sem;
static rt_uint16_t timer_ex_cnt; static rt_uint16_t timer_ex_cnt;
void rt_soft_timer_tick_increase (void) void rt_soft_timer_tick_increase(void)
{ {
timer_ex_cnt++; timer_ex_cnt ++;
if (timer_ex_cnt >= (RT_TICK_PER_SECOND / RT_TIMER_TICK_PER_SECOND)) if (timer_ex_cnt >= (RT_TICK_PER_SECOND / RT_TIMER_TICK_PER_SECOND))
{ {
timer_ex_cnt = 0; timer_ex_cnt = 0;
@ -424,17 +423,17 @@ void rt_soft_timer_tick_increase (void)
* corresponding timeout function will be invoked. * corresponding timeout function will be invoked.
* *
*/ */
void rt_soft_timer_check() void rt_soft_timer_check(void)
{ {
rt_tick_t current_tick; rt_tick_t current_tick;
rt_list_t *n; rt_list_t *n;
struct rt_timer *t; struct rt_timer *t;
RT_DEBUG_LOG(RT_DEBUG_TIMER,("software timer check enter\n")); RT_DEBUG_LOG(RT_DEBUG_TIMER, ("software timer check enter\n"));
current_tick = rt_tick_get(); current_tick = rt_tick_get();
for (n = rt_soft_timer_list.next; n != &(rt_soft_timer_list); ) for (n = rt_soft_timer_list.next; n != &(rt_soft_timer_list);)
{ {
t = rt_list_entry(n, struct rt_timer, list); t = rt_list_entry(n, struct rt_timer, list);
@ -457,7 +456,7 @@ void rt_soft_timer_check()
/* re-get tick */ /* re-get tick */
current_tick = rt_tick_get(); current_tick = rt_tick_get();
RT_DEBUG_LOG(RT_DEBUG_TIMER,("current tick: %d\n", current_tick)); RT_DEBUG_LOG(RT_DEBUG_TIMER, ("current tick: %d\n", current_tick));
if ((t->parent.flag & RT_TIMER_FLAG_PERIODIC) && if ((t->parent.flag & RT_TIMER_FLAG_PERIODIC) &&
(t->parent.flag & RT_TIMER_FLAG_ACTIVATED)) (t->parent.flag & RT_TIMER_FLAG_ACTIVATED))
@ -475,17 +474,16 @@ void rt_soft_timer_check()
else break; /* not check anymore */ else break; /* not check anymore */
} }
RT_DEBUG_LOG(RT_DEBUG_TIMER,("software timer check leave\n")); RT_DEBUG_LOG(RT_DEBUG_TIMER, ("software timer check leave\n"));
} }
/* system timer thread entry */ /* system timer thread entry */
static void rt_thread_timer_entry(void* parameter) static void rt_thread_timer_entry(void *parameter)
{ {
while (1) while (1)
{ {
/* take software timer semaphore */ /* take software timer semaphore */
rt_sem_take(&timer_sem,RT_WAITING_FOREVER); rt_sem_take(&timer_sem, RT_WAITING_FOREVER);
/* lock scheduler */ /* lock scheduler */
rt_enter_critical(); rt_enter_critical();
@ -505,7 +503,7 @@ static void rt_thread_timer_entry(void* parameter)
* This function will initialize system timer * This function will initialize system timer
* *
*/ */
void rt_system_timer_init() void rt_system_timer_init(void)
{ {
rt_list_init(&rt_timer_list); rt_list_init(&rt_timer_list);
@ -521,7 +519,7 @@ void rt_system_timer_init()
* This function will initialize system timer thread * This function will initialize system timer thread
* *
*/ */
void rt_system_timer_thread_init() void rt_system_timer_thread_init(void)
{ {
#ifdef RT_USING_TIMER_SOFT #ifdef RT_USING_TIMER_SOFT
/* start software timer thread */ /* start software timer thread */