From c0f80bdf9a7fc66776fcb1ab5f94ef6af06fd17f Mon Sep 17 00:00:00 2001 From: dzzxzz Date: Wed, 21 Sep 2011 03:56:42 +0000 Subject: [PATCH] cleanup kernel code git-svn-id: https://rt-thread.googlecode.com/svn/trunk@1715 bbd45198-f89e-11dd-88c7-29a3b14d5316 --- src/clock.c | 16 +- src/device.c | 39 +++-- src/idle.c | 8 +- src/ipc.c | 148 +++++++++---------- src/irq.c | 10 +- src/kservice.c | 229 +++++++++++++++-------------- src/kservice.h | 2 +- src/mem.c | 43 +++--- src/mempool.c | 80 +++++----- src/module.c | 353 ++++++++++++++++++++++---------------------- src/module.h | 335 ++++++++++++++++++++++-------------------- src/object.c | 47 +++--- src/rtm.c | 22 +-- src/scheduler.c | 381 ++++++++++++++++++++++++------------------------ src/slab.c | 135 +++++++++-------- src/thread.c | 99 +++++++------ src/timer.c | 88 ++++++----- 17 files changed, 1019 insertions(+), 1016 deletions(-) diff --git a/src/clock.c b/src/clock.c index f23ddc3279..e5cf3ea75c 100644 --- a/src/clock.c +++ b/src/clock.c @@ -1,7 +1,7 @@ /* * File : clock.c * 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 * found in the file LICENSE in this distribution or at @@ -31,7 +31,7 @@ extern void rt_timer_switch(void); * @ingroup SystemInit * */ -void rt_system_tick_init() +void rt_system_tick_init(void) { rt_tick = 0; } @@ -47,7 +47,7 @@ void rt_system_tick_init() * * @return current tick */ -rt_tick_t rt_tick_get() +rt_tick_t rt_tick_get(void) { /* return the global 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 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 */ ++ rt_tick; @@ -87,10 +87,10 @@ void rt_tick_increase() thread->remaining_tick = thread->init_tick; /* yield */ - rt_thread_yield(); + rt_thread_yield(); } - /* check timer */ + /* check timer */ rt_timer_check(); } @@ -104,7 +104,7 @@ void rt_tick_increase() rt_tick_t rt_tick_from_millisecond(rt_uint32_t ms) { /* return the calculated tick */ - return (RT_TICK_PER_SECOND * ms+999) / 1000; + return (RT_TICK_PER_SECOND * ms + 999) / 1000; } /*@}*/ diff --git a/src/device.c b/src/device.c index d8e467ee91..f164b17852 100644 --- a/src/device.c +++ b/src/device.c @@ -1,7 +1,7 @@ /* * File : device.c * 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 * found in the file LICENSE in this distribution or at @@ -27,7 +27,7 @@ * * @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; @@ -58,10 +58,10 @@ rt_err_t rt_device_unregister(rt_device_t dev) * * @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_list_node* node; + struct rt_device *device; + struct rt_list_node *node; struct rt_object_information *information; 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) { 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 */ 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. */ -rt_device_t rt_device_find(const char* name) +rt_device_t rt_device_find(const char *name) { - struct rt_object* object; - struct rt_list_node* node; + struct rt_object *object; + struct rt_list_node *node; struct rt_object_information *information; extern struct rt_object_information rt_object_container[]; @@ -167,7 +167,7 @@ rt_err_t rt_device_init(rt_device_t dev) } } else result = -RT_ENOSYS; - + return result; } @@ -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 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); @@ -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 */ - if ((dev->flag & RT_DEVICE_FLAG_STANDALONE) && - (dev->open_flag & RT_DEVICE_OFLAG_OPEN)) + if ((dev->flag & RT_DEVICE_FLAG_STANDALONE) && (dev->open_flag & RT_DEVICE_OFLAG_OPEN)) return -RT_EBUSY; /* 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. */ -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); @@ -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. */ -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); @@ -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 */ -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); @@ -355,7 +354,7 @@ rt_err_t rt_device_control(rt_device_t dev, rt_uint8_t cmd, void* arg) * * @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); diff --git a/src/idle.c b/src/idle.c index cc03bd499d..b847ff1f60 100644 --- a/src/idle.c +++ b/src/idle.c @@ -84,7 +84,7 @@ void rt_thread_idle_excute(void) /* if the thread is module's main thread */ if (module != RT_NULL && module->module_thread == thread) - { + { /* detach module's main thread */ module->module_thread = RT_NULL; } @@ -134,8 +134,8 @@ void rt_thread_idle_excute(void) if ((module->module_thread == RT_NULL) && rt_list_isempty(&module->module_object[RT_Object_Class_Thread].object_list) ) { - module->nref--; - } + module->nref --; + } } /* unload module */ @@ -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) { diff --git a/src/ipc.c b/src/ipc.c index 92adfe4e7b..ded99e4613 100644 --- a/src/ipc.c +++ b/src/ipc.c @@ -42,9 +42,9 @@ #include "kservice.h" #ifdef RT_USING_HOOK -extern void (*rt_object_trytake_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_trytake_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); #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: { - struct rt_list_node* n; - struct rt_thread* sthread; + struct rt_list_node *n; + struct rt_thread *sthread; /* find a suitable position */ 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 */ -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 */ 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 */ -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; /* 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 */ -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); @@ -244,7 +244,7 @@ rt_err_t rt_sem_detach(rt_sem_t sem) * * @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; @@ -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) { register rt_base_t temp; - struct rt_thread* thread; + struct rt_thread *thread; 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, ("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) { @@ -344,8 +344,7 @@ rt_err_t rt_sem_take(rt_sem_t sem, rt_int32_t time) /* reset thread error number */ thread->error = RT_EOK; - RT_DEBUG_LOG(RT_DEBUG_IPC, - ("sem take: suspend thread - %s\n", thread->name)); + RT_DEBUG_LOG(RT_DEBUG_IPC, ("sem take: suspend thread - %s\n", thread->name)); /* 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, ("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)) @@ -442,7 +441,7 @@ rt_err_t rt_sem_release(rt_sem_t sem) * * @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_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 */ -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); @@ -537,9 +536,9 @@ rt_err_t rt_mutex_detach(rt_mutex_t mutex) * * @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; @@ -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) { 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 */ 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) { register rt_base_t temp; - struct rt_thread* thread; + struct rt_thread *thread; rt_bool_t need_schedule; need_schedule = RT_FALSE; @@ -768,8 +767,7 @@ rt_err_t rt_mutex_release(rt_mutex_t mutex) /* get suspended thread */ thread = rt_list_entry(mutex->parent.suspend_thread.next, struct rt_thread, tlist); - RT_DEBUG_LOG(RT_DEBUG_IPC, - ("mutex_release: resume thread: %s\n", thread->name)); + RT_DEBUG_LOG(RT_DEBUG_IPC, ("mutex_release: resume thread: %s\n", thread->name)); /* set new owner and priority */ mutex->owner = thread; @@ -810,7 +808,7 @@ rt_err_t rt_mutex_release(rt_mutex_t mutex) * * @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; } @@ -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 */ -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); @@ -876,7 +874,7 @@ rt_err_t rt_event_detach(rt_event_t event) * * @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; @@ -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) { - struct rt_list_node* n; - struct rt_thread* thread; + struct rt_list_node *n; + struct rt_thread *thread; register rt_ubase_t level; register rt_base_t status; 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 */ -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_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; /* put thread to suspended thread list */ - rt_ipc_list_suspend(&(event->parent.suspend_thread), - thread, event->parent.parent.flag); + rt_ipc_list_suspend(&(event->parent.suspend_thread), thread, event->parent.parent.flag); /* if there is a waiting timeout, active thread timer */ 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 */ -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_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 */ -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); @@ -1177,11 +1174,11 @@ rt_err_t rt_mb_init(rt_mailbox_t mb, const char* name, void* msgpool, rt_size_t rt_ipc_object_init(&(mb->parent)); /* init mailbox */ - mb->msg_pool = msgpool; - mb->size = size; - mb->entry = 0; - mb->in_offset = 0; - mb->out_offset = 0; + mb->msg_pool = msgpool; + mb->size = size; + mb->entry = 0; + mb->in_offset = 0; + mb->out_offset = 0; /* init an additional list of sender suspend thread */ rt_list_init(&(mb->suspend_sender_thread)); @@ -1222,7 +1219,7 @@ rt_err_t rt_mb_detach(rt_mailbox_t mb) * * @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; @@ -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) { - struct rt_thread* thread; + struct rt_thread *thread; register rt_ubase_t temp; 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; /* suspend current thread */ - rt_ipc_list_suspend(&(mb->suspend_sender_thread), - thread, mb->parent.parent.flag); + rt_ipc_list_suspend(&(mb->suspend_sender_thread), thread, mb->parent.parent.flag); /* has waiting time, start thread timer */ 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 */ -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; 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; /* suspend current thread */ - rt_ipc_list_suspend(&(mb->parent.suspend_thread), - thread, mb->parent.parent.flag); + rt_ipc_list_suspend(&(mb->parent.suspend_thread), thread, mb->parent.parent.flag); /* has waiting time, start thread timer */ 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]; /* increase output offset */ - ++mb->out_offset; + ++ mb->out_offset; if (mb->out_offset >= mb->size) mb->out_offset = 0; /* decrease message 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 */ -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_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 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 */ -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; /* 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; 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))); head->next = mq->msg_queue_free; 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 */ -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_mq_message* head; + struct rt_messagequeue *mq; + struct rt_mq_message *head; register rt_base_t temp; RT_DEBUG_NOT_IN_INTERRUPT; @@ -1789,10 +1784,10 @@ rt_err_t rt_mq_delete(rt_mq_t mq) * * @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; - struct rt_mq_message* msg; + struct rt_mq_message *msg; /* greater than one message size */ 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 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 */ @@ -1869,10 +1864,10 @@ rt_err_t rt_mq_send(rt_mq_t mq, void* buffer, rt_size_t size) * * @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; - struct rt_mq_message* msg; + struct rt_mq_message *msg; /* greater than one message size */ 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(); /* 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 */ 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 */ -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; - struct rt_mq_message* msg; + struct rt_mq_message *msg; rt_uint32_t tick_delta; /* 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 */ - rt_ipc_list_suspend(&(mq->parent.suspend_thread), - thread, mq->parent.parent.flag); + rt_ipc_list_suspend(&(mq->parent.suspend_thread), thread, mq->parent.parent.flag); /* has waiting time, start thread timer */ 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 */ tick_delta = rt_tick_get(); - RT_DEBUG_LOG(RT_DEBUG_IPC, - ("set thread:%s to timer list\n", thread->name)); + RT_DEBUG_LOG(RT_DEBUG_IPC, ("set thread:%s to timer list\n", thread->name)); /* reset the timeout of thread timer and start it */ 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 */ - msg = (struct rt_mq_message*) mq->msg_queue_head; + msg = (struct rt_mq_message *)mq->msg_queue_head; /* move message queue head */ 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); /* copy message */ - rt_memcpy(buffer, msg + 1, - size > mq->msg_size? mq->msg_size : size); + rt_memcpy(buffer, msg + 1, size > mq->msg_size ? mq->msg_size : size); /* disable interrupt */ temp = rt_hw_interrupt_disable(); /* 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; /* enable interrupt */ 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 */ -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; - struct rt_mq_message* msg; + struct rt_mq_message *msg; 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) { /* 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 */ 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; /* 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; } diff --git a/src/irq.c b/src/irq.c index 72a714fa24..140a6f4367 100644 --- a/src/irq.c +++ b/src/irq.c @@ -1,7 +1,7 @@ /* * File : irq.c * 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 * 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 */ -void rt_interrupt_enter() +void rt_interrupt_enter(void) { 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(); rt_interrupt_nest ++; @@ -51,11 +51,11 @@ void rt_interrupt_enter() * * @see rt_interrupt_enter */ -void rt_interrupt_leave() +void rt_interrupt_leave(void) { 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(); rt_interrupt_nest --; diff --git a/src/kservice.c b/src/kservice.c index 70a8e9060a..6ecb2a5995 100644 --- a/src/kservice.c +++ b/src/kservice.c @@ -1,7 +1,7 @@ /* * File : kservice.c * 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 * found in the file LICENSE in this distribution or at @@ -26,7 +26,7 @@ /*@{*/ #ifndef RT_USING_NEWLIB -/* global errno in RT-Thread*/ +/* global errno in RT-Thread */ static volatile int _errno; #else #include @@ -44,7 +44,7 @@ rt_err_t rt_get_errno(void) { rt_thread_t tid; - if(rt_interrupt_get_nest() != 0) + if (rt_interrupt_get_nest() != 0) { /* it's in interrupt context */ return _errno; @@ -65,7 +65,7 @@ void rt_set_errno(rt_err_t error) { rt_thread_t tid; - if(rt_interrupt_get_nest() != 0) + if (rt_interrupt_get_nest() != 0) { /* it's in interrupt context */ _errno = error; @@ -105,19 +105,19 @@ int *_rt_errno(void) * @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 - char *xs = (char *) s; + char *xs = (char *)s; while (count--) *xs++ = c; return s; #else -#define LBLOCKSIZE (sizeof(rt_int32_t)) -#define UNALIGNED(X) ((rt_int32_t)X & (LBLOCKSIZE - 1)) -#define TOO_SMALL(LEN) ((LEN) < LBLOCKSIZE) +#define LBLOCKSIZE (sizeof(rt_int32_t)) +#define UNALIGNED(X) ((rt_int32_t)X & (LBLOCKSIZE - 1)) +#define TOO_SMALL(LEN) ((LEN) < LBLOCKSIZE) int i; char *m = (char *)s; @@ -125,10 +125,10 @@ void *rt_memset(void * s, int c, rt_ubase_t count) rt_uint32_t *aligned_addr; 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. */ - aligned_addr = (rt_uint32_t*)s; + aligned_addr = (rt_uint32_t *)s; /* Store D into each char sized location in BUFFER so that * we can set large blocks quickly. @@ -160,8 +160,8 @@ void *rt_memset(void * s, int c, rt_ubase_t count) count -= LBLOCKSIZE; } - /* Pick up the remainder with a bytewise loop. */ - m = (char*)aligned_addr; + /* Pick up the remainder with a bytewise loop. */ + m = (char *)aligned_addr; } while (count--) @@ -188,10 +188,10 @@ void *rt_memset(void * s, int c, rt_ubase_t count) * @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 - char *tmp = (char *) dst, *s = (char *) src; + char *tmp = (char *)dst, *s = (char *)src; while (count--) *tmp++ = *s++; @@ -200,25 +200,25 @@ void *rt_memcpy(void * dst, const void *src, rt_ubase_t count) #else #define UNALIGNED(X, Y) \ - (((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 LITTLEBLOCKSIZE (sizeof (rt_int32_t)) + (((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 LITTLEBLOCKSIZE (sizeof(rt_int32_t)) #define TOO_SMALL(LEN) ((LEN) < BIGBLOCKSIZE) - char *dst_ptr = (char*)dst; - char *src_ptr = (char*)src; + char *dst_ptr = (char *)dst; + char *src_ptr = (char *)src; rt_int32_t *aligned_dst; rt_int32_t *aligned_src; int len = count; /* If the size is small, or either SRC or DST is unaligned, - then punt into the byte copy loop. This should be rare. */ - if (!TOO_SMALL(len) && !UNALIGNED (src_ptr, dst_ptr)) + then punt into the byte copy loop. This should be rare. */ + if (!TOO_SMALL(len) && !UNALIGNED(src_ptr, dst_ptr)) { - aligned_dst = (rt_int32_t*)dst_ptr; - aligned_src = (rt_int32_t*)src_ptr; + aligned_dst = (rt_int32_t *)dst_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) { *aligned_dst++ = *aligned_src++; @@ -228,16 +228,16 @@ void *rt_memcpy(void * dst, const void *src, rt_ubase_t count) len -= BIGBLOCKSIZE; } - /* Copy one long word at a time if possible. */ + /* Copy one long word at a time if possible. */ while (len >= LITTLEBLOCKSIZE) { *aligned_dst++ = *aligned_src++; len -= LITTLEBLOCKSIZE; } - /* Pick up any residual with a byte copier. */ - dst_ptr = (char*)aligned_dst; - src_ptr = (char*)aligned_src; + /* Pick up any residual with a byte copier. */ + dst_ptr = (char *)aligned_dst; + src_ptr = (char *)aligned_src; } while (len--) @@ -262,14 +262,14 @@ void *rt_memcpy(void * dst, const void *src, rt_ubase_t count) * @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) { - tmp+=n; - s+=n; + tmp += n; + s += n; while (n--) *tmp-- = *s--; @@ -284,17 +284,20 @@ void* rt_memmove(void *dest, const void *src, rt_ubase_t n) } /** - * memcmp - Compare two areas of memory - * @param cs: One area of memory - * @param ct: Another area of memory - * @param count: The size of the area. + * This function will compare two areas of memory + * + * @param cs one area of memory + * @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; 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) break; 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. */ -char * rt_strstr(const char * s1,const char * s2) +char *rt_strstr(const char *s1, const char *s2) { int l1, l2; l2 = rt_strlen(s2); if (!l2) - return (char *) s1; + return (char *)s1; l1 = rt_strlen(s1); while (l1 >= l2) { - l1--; - if (!rt_memcmp(s1,s2,l2)) - return (char *) s1; - s1++; + l1 --; + if (!rt_memcmp(s1, s2, l2)) + return (char *)s1; + s1 ++; } 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 *tmp = (char *) dest, *s = (char *) src; + char *tmp = (char *)dest, *s = (char *)src; - while(n--) + while (n--) *tmp++ = *s++; return dest; @@ -380,7 +383,7 @@ char *rt_strncpy(char *dest, const char *src, rt_ubase_t n) * * @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; @@ -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++) break; - count--; + count --; } return __res; @@ -402,7 +405,7 @@ rt_ubase_t rt_strncmp(const char * cs, const char * ct, rt_ubase_t count) * * @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) cs++, ct++; @@ -440,7 +443,7 @@ char *rt_strdup(const char *s) rt_size_t len = rt_strlen(s) + 1; char *tmp = (char *)rt_malloc(len); - if(!tmp) return RT_NULL; + if (!tmp) return RT_NULL; rt_memcpy(tmp, s, len); return tmp; @@ -450,7 +453,7 @@ char *rt_strdup(const char *s) /** * 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("- RT - Thread Operating System\n"); @@ -488,18 +491,18 @@ rt_inline int skip_atoi(const char **s) return i; } -#define ZEROPAD (1 << 0) /* pad with zero */ -#define SIGN (1 << 1) /* unsigned/signed long */ -#define PLUS (1 << 2) /* show plus */ -#define SPACE (1 << 3) /* space if plus */ -#define LEFT (1 << 4) /* left justified */ -#define SPECIAL (1 << 5) /* 0x */ -#define LARGE (1 << 6) /* use 'ABCDEF' instead of 'abcdef' */ +#define ZEROPAD (1 << 0) /* pad with zero */ +#define SIGN (1 << 1) /* unsigned/signed long */ +#define PLUS (1 << 2) /* show plus */ +#define SPACE (1 << 3) /* space if plus */ +#define LEFT (1 << 4) /* left justified */ +#define SPECIAL (1 << 5) /* 0x */ +#define LARGE (1 << 6) /* use 'ABCDEF' instead of 'abcdef' */ #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 -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 { 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))) { - while(size-->0) + while (size-->0) { 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) { *buf = sign; - --size; + -- size; } - ++buf; + ++ buf; } #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 (buf <= end) *buf = '0'; - ++buf; + ++ buf; } - else if (base==16) + else if (base == 16) { if (buf <= end) *buf = '0'; - ++buf; + ++ buf; if (buf <= end) { *buf = type & LARGE? 'X' : 'x'; } - ++buf; + ++ buf; } } #endif @@ -602,7 +605,7 @@ static char *print_number(char * buf, char * end, long num, int base, int s, int while (size-- > 0) { 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--) { if (buf <= end) *buf = '0'; - ++buf; + ++ buf; } #endif @@ -618,13 +621,13 @@ static char *print_number(char * buf, char * end, long num, int base, int s, int while (i-- > 0) { if (buf <= end) *buf = tmp[i]; - ++buf; + ++ buf; } while (size-- > 0) { if (buf <= end) *buf = ' '; - ++buf; + ++ 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 (str <= end) *str = *fmt; - ++str; + ++ str; continue; } /* process flags */ flags = 0; - while(1) + while (1) { /* skips the first '%' also */ - ++fmt; + ++ fmt; if (*fmt == '-') flags |= LEFT; else if (*fmt == '+') flags |= PLUS; 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); else if (*fmt == '*') { - ++fmt; + ++ fmt; /* it's the next argument */ field_width = va_arg(args, int); 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; if (*fmt == '.') { - ++fmt; + ++ fmt; if (isdigit(*fmt)) precision = skip_atoi(&fmt); else if (*fmt == '*') { - ++fmt; + ++ fmt; /* it's the next argument */ 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; - ++fmt; + ++ fmt; #ifdef RT_PRINTF_LONGLONG if (qualifier == 'l' && *fmt == 'l') { qualifier = 'L'; - ++fmt; + ++ fmt; } #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) { if (str <= end) *str = ' '; - ++str; + ++ str; } } /* get character */ - c = (rt_uint8_t) va_arg(args, int); + c = (rt_uint8_t)va_arg(args, int); if (str <= end) *str = c; - ++str; + ++ str; /* put width */ while (--field_width > 0) { if (str <= end) *str = ' '; - ++str; + ++ str; } 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--) { if (str <= end) *str = ' '; - ++str; + ++ str; } } for (i = 0; i < len; ++i) { if (str <= end) *str = *s; - ++str; - ++s; + ++ str; + ++ s; } while (len < field_width--) { if (str <= end) *str = ' '; - ++str; + ++ str; } 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 str = print_number(str, end, - (long) va_arg(args, void *), + (long)va_arg(args, void *), 16, field_width, precision, flags); #else str = print_number(str, end, - (long) va_arg(args, void *), + (long)va_arg(args, void *), 16, field_width, flags); #endif continue; case '%': if (str <= end) *str = '%'; - ++str; + ++ str; continue; /* 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: if (str <= end) *str = '%'; - ++str; + ++ str; if (*fmt) { if (str <= end) *str = *fmt; - ++str; + ++ str; } else { - --fmt; + -- fmt; } continue; } @@ -857,17 +860,17 @@ static rt_int32_t vsnprintf(char *buf, rt_size_t size, const char *fmt, va_list #endif { 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') { - num = (rt_uint16_t) va_arg(args, rt_int32_t); - if (flags & SIGN) num = (rt_int16_t) num; + num = (rt_uint16_t)va_arg(args, rt_int32_t); + if (flags & SIGN) num = (rt_int16_t)num; } else { 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 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 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; va_list arg_ptr; va_start(arg_ptr, format); - n = rt_vsprintf(buf ,format,arg_ptr); - va_end (arg_ptr); + n = rt_vsprintf(buf ,format, arg_ptr); + va_end(arg_ptr); return n; } @@ -946,7 +949,7 @@ rt_int32_t rt_sprintf(char *buf ,const char *format,...) * * @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; @@ -973,18 +976,18 @@ rt_device_t rt_console_set_device(const char* name) #endif #if defined(__GNUC__) -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) __attribute__((weak)); +void rt_hw_console_output(const char *str) #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__) #if __VER__ > 540 __weak #endif -void rt_hw_console_output(const char* str) +void rt_hw_console_output(const char *str) #endif { - /* empty console output */ + /* empty console output */ } /** @@ -1004,7 +1007,7 @@ void rt_kprintf(const char *fmt, ...) #ifdef RT_USING_DEVICE if (_console_device == RT_NULL) { - rt_hw_console_output(rt_log_buf); + rt_hw_console_output(rt_log_buf); } else { @@ -1023,9 +1026,9 @@ void rt_kprintf(const char *fmt, ...) #if !defined (RT_USING_NEWLIB) && defined (RT_USING_MINILIBC) && defined (__GNUC__) #include -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* memmove(void *dest, const void *src, size_t n) __attribute__((weak, alias("rt_memmove"))); +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 *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"))); 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"))); #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 vsprintf(char *buf, const char *format, va_list arg_ptr) __attribute__((weak, alias("rt_vsprintf"))); diff --git a/src/kservice.h b/src/kservice.h index 2e7e5015ed..6ed90793ba 100644 --- a/src/kservice.h +++ b/src/kservice.h @@ -1,7 +1,7 @@ /* * File : kservice.h * 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 * found in the file LICENSE in this distribution or at diff --git a/src/mem.c b/src/mem.c index c59c855f93..8d94ee26ec 100644 --- a/src/mem.c +++ b/src/mem.c @@ -1,7 +1,7 @@ /* * File : mem.c * 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 * 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 * */ -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; 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; /* alignment addr */ - if((end_align > (2 * SIZEOF_STRUCT_MEM) ) && - ((end_align - 2 * SIZEOF_STRUCT_MEM) >= begin_align )) { - /* calculate the aligned memory size */ + if ((end_align > (2 * SIZEOF_STRUCT_MEM)) && + ((end_align - 2 * SIZEOF_STRUCT_MEM) >= begin_align)) + { + /* calculate the aligned memory size */ mem_size_aligned = end_align - begin_align - 2 * SIZEOF_STRUCT_MEM; } - else { + 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); return; } @@ -242,13 +244,13 @@ void *rt_malloc(rt_size_t size) 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; } /* 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 */ 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]; - if ((!mem->used) && - (mem->next - (ptr + SIZEOF_STRUCT_MEM)) >= size) + if ((!mem->used) && (mem->next - (ptr + SIZEOF_STRUCT_MEM)) >= size) { /* mem is not used and at least perfect fit is possible: * mem->next - (ptr + SIZEOF_STRUCT_MEM) gives us the 'user data size' of mem */ @@ -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)mem) & (RT_ALIGN_SIZE-1)) == 0); - 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)(mem->next - ((rt_uint8_t*)mem - heap_ptr)))); + 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)(mem->next - ((rt_uint8_t *)mem - heap_ptr)))); RT_OBJECT_HOOK_CALL(rt_malloc_hook, (((void*)((rt_uint8_t *)mem + SIZEOF_STRUCT_MEM)), size)); /* 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 ptr, ptr2; struct heap_mem *mem, *mem2; - void* nmem; + void *nmem; 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); 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; } @@ -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) { - RT_DEBUG_LOG(RT_DEBUG_MEM,("illegal memory\n")); + RT_DEBUG_LOG(RT_DEBUG_MEM, ("illegal memory\n")); return; } @@ -486,9 +487,9 @@ void rt_free(void *rmem) /* Get the corresponding struct heap_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)(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 */ @@ -517,9 +518,7 @@ void rt_free(void *rmem) } #ifdef RT_MEM_STATS -void rt_memory_info(rt_uint32_t *total, - rt_uint32_t *used, - rt_uint32_t *max_used) +void rt_memory_info(rt_uint32_t *total, rt_uint32_t *used, rt_uint32_t *max_used) { if (total != RT_NULL) *total = mem_size_aligned; if (used != RT_NULL) *used = used_mem; @@ -528,7 +527,7 @@ void rt_memory_info(rt_uint32_t *total, #ifdef RT_USING_FINSH #include -void list_mem() +void list_mem(void) { rt_kprintf("total memory: %d\n", mem_size_aligned); rt_kprintf("used memory : %d\n", used_mem); diff --git a/src/mempool.c b/src/mempool.c index df6ece85fe..473d324ad4 100644 --- a/src/mempool.c +++ b/src/mempool.c @@ -1,7 +1,7 @@ /* * File : mempool.c * 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 * found in the file LICENSE in this distribution or at @@ -27,8 +27,8 @@ #ifdef RT_USING_MEMPOOL #ifdef RT_USING_HOOK -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_alloc_hook)(struct rt_mempool *mp, void *block); +static void (*rt_mp_free_hook)(struct rt_mempool *mp, void *block); /** * @addtogroup Hook @@ -41,7 +41,7 @@ static void (*rt_mp_free_hook)(struct rt_mempool* mp, void *block); * * @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; } @@ -52,7 +52,7 @@ void rt_mp_alloc_sethook(void (*hook)(struct rt_mempool* mp, void *block)) * * @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; } @@ -79,7 +79,7 @@ void rt_mp_free_sethook(void (*hook)(struct rt_mempool* mp, void *block)) * @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; 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; /* 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; /* 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; /* 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 ++) { - *(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 * (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; @@ -126,9 +126,9 @@ rt_err_t rt_mp_init(struct rt_mempool* mp, const char* name, void *start, rt_siz * * @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; /* parameter check */ @@ -176,24 +176,24 @@ rt_err_t rt_mp_detach(struct rt_mempool* mp) * @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; - struct rt_mempool* mp; + struct rt_mempool *mp; register rt_base_t offset; RT_DEBUG_NOT_IN_INTERRUPT; /* 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 */ /* init memory pool */ 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 */ - 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) { /* 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; /* 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 ++) { - *(rt_uint8_t**)(block_ptr + offset * (block_size + sizeof(rt_uint8_t*))) - = block_ptr + (offset + 1) * (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 *)); } - *(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; @@ -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) { - struct rt_thread* thread; + struct rt_thread *thread; register rt_ubase_t temp; 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) /* 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); else #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 * */ -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; - struct rt_thread* thread; + struct rt_thread *thread; /* disable interrupt */ level = rt_hw_interrupt_disable(); - if(mp->block_free_count) + if (mp->block_free_count) { /* memory block is available. decrease the free block counter */ - mp->block_free_count--; + mp->block_free_count --; /* get block from 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 */ - *(rt_uint8_t**)block_ptr = (rt_uint8_t*)mp; + *(rt_uint8_t **)block_ptr = (rt_uint8_t *)mp; } else { @@ -333,7 +333,7 @@ void *rt_mp_alloc (rt_mp_t mp, rt_int32_t time) /* need suspend thread */ rt_thread_suspend(thread); rt_list_insert_after(&(mp->suspend_thread), &(thread->tlist)); - mp->suspend_thread_count++; + mp->suspend_thread_count ++; if (time > 0) { @@ -358,19 +358,19 @@ void *rt_mp_alloc (rt_mp_t mp, rt_int32_t time) /* get block from 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 */ - *(rt_uint8_t**)block_ptr = (rt_uint8_t*)mp; + *(rt_uint8_t **)block_ptr = (rt_uint8_t *)mp; } } /* enable interrupt */ 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 * */ -void rt_mp_free (void *block) +void rt_mp_free(void *block) { rt_uint8_t **block_ptr; struct rt_mempool *mp; @@ -387,8 +387,8 @@ void rt_mp_free (void *block) register rt_base_t level; /* get the control block of pool which the block belongs to */ - block_ptr = (rt_uint8_t**)((rt_uint8_t*)block - sizeof(rt_uint8_t*)); - mp = (struct rt_mempool*) *block_ptr; + block_ptr = (rt_uint8_t **)((rt_uint8_t *)block - sizeof(rt_uint8_t *)); + mp = (struct rt_mempool *)*block_ptr; 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 */ *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) { diff --git a/src/module.c b/src/module.c index 93a409c38d..4666419700 100644 --- a/src/module.c +++ b/src/module.c @@ -1,19 +1,19 @@ /* * File : module.c * 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 * 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 - * 2010-10-23 yi.qiu implement module memory allocator - * 2011-05-25 yi.qiu implement module hook function - * 2011-06-23 yi.qiu rewrite module memory allocator + * Date Author Notes + * 2010-01-09 Bernard first version + * 2010-04-09 yi.qiu implement based on first version + * 2010-10-23 yi.qiu implement module memory allocator + * 2011-05-25 yi.qiu implement module hook function + * 2011-06-23 yi.qiu rewrite module memory allocator */ #include @@ -26,17 +26,17 @@ #ifdef RT_USING_MODULE #include "module.h" -#define elf_module ((Elf32_Ehdr *)module_ptr) -#define shdr ((Elf32_Shdr *)((rt_uint8_t *)module_ptr + elf_module->e_shoff)) -#define phdr ((Elf32_Phdr *)((rt_uint8_t *)module_ptr + elf_module->e_phoff)) +#define elf_module ((Elf32_Ehdr *)module_ptr) +#define shdr ((Elf32_Shdr *)((rt_uint8_t *)module_ptr + elf_module->e_shoff)) +#define phdr ((Elf32_Phdr *)((rt_uint8_t *)module_ptr + elf_module->e_phoff)) -#define IS_PROG(s) (s.sh_type == SHT_PROGBITS) -#define IS_NOPROG(s) (s.sh_type == SHT_NOBITS) -#define IS_REL(s) (s.sh_type == SHT_REL) -#define IS_RELA(s) (s.sh_type == SHT_RELA) -#define IS_ALLOC(s) (s.sh_flags == SHF_ALLOC) -#define IS_AX(s) ((s.sh_flags & SHF_ALLOC) && (s.sh_flags & SHF_EXECINSTR)) -#define IS_AW(s) ((s.sh_flags & SHF_ALLOC) && (s.sh_flags & SHF_WRITE)) +#define IS_PROG(s) (s.sh_type == SHT_PROGBITS) +#define IS_NOPROG(s) (s.sh_type == SHT_NOBITS) +#define IS_REL(s) (s.sh_type == SHT_REL) +#define IS_RELA(s) (s.sh_type == SHT_RELA) +#define IS_ALLOC(s) (s.sh_flags == SHF_ALLOC) +#define IS_AX(s) ((s.sh_flags & SHF_ALLOC) && (s.sh_flags & SHF_EXECINSTR)) +#define IS_AW(s) ((s.sh_flags & SHF_ALLOC) && (s.sh_flags & SHF_WRITE)) #define PAGE_COUNT_MAX 256 @@ -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; 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; - const char* str = string; - char* dest = RT_NULL; + const char *str = string; + char *dest = RT_NULL; - while(*str != '\n' && *str != '\0') + while (*str != '\n' && *str != '\0') { - if(*str =='/' ) p = i + 1; - if(*str == '.') q = i; + if (*str =='/' ) p = i + 1; + if (*str == '.') q = i; str++; i++; } - if(p < q) + if (p < q) { int len = q - p; - dest = (char*)rt_malloc(len + 1); + dest = (char *)rt_malloc(len + 1); rt_strncpy(dest, &string[p], len); dest[len] = '\0'; } @@ -99,10 +99,10 @@ void rt_system_module_init(void) _rt_module_symtab_begin = (struct rt_module_symtab *)&__rtmsymtab_start; _rt_module_symtab_end = (struct rt_module_symtab *)&__rtmsymtab_end; -#elif defined (__CC_ARM) +#elif defined (__CC_ARM) extern int RTMSymTab$$Base; extern int RTMSymTab$$Limit; - + _rt_module_symtab_begin = (struct rt_module_symtab *)&RTMSymTab$$Base; _rt_module_symtab_end = (struct rt_module_symtab *)&RTMSymTab$$Limit; #endif @@ -116,10 +116,10 @@ void rt_system_module_init(void) 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 */ - struct rt_module_symtab* index; + struct rt_module_symtab *index; for (index = _rt_module_symtab_begin; index != _rt_module_symtab_end; index ++) { 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 * */ -rt_module_t rt_module_self (void) +rt_module_t rt_module_self(void) { /* return current module */ return rt_current_module; @@ -146,7 +146,7 @@ rt_module_t rt_module_self (void) * * @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 */ rt_current_module = module; @@ -154,12 +154,12 @@ rt_err_t rt_module_set (rt_module_t module) 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_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)) { 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: *where += (Elf32_Addr)sym_val; - RT_DEBUG_LOG(RT_DEBUG_MODULE, - ("R_ARM_ABS32: %x -> %x\n", where, *where)); + RT_DEBUG_LOG(RT_DEBUG_MODULE, ("R_ARM_ABS32: %x -> %x\n", where, *where)); break; @@ -184,7 +183,7 @@ static int rt_module_arm_relocate(struct rt_module* module, Elf32_Rel *rel, Elf3 tmp >>= 2; *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; @@ -214,7 +213,7 @@ static int rt_module_arm_relocate(struct rt_module* module, Elf32_Rel *rel, Elf3 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); @@ -321,7 +320,7 @@ void rt_module_unload_sethook(void (*hook)(rt_module_t module)) * @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_module_t module = RT_NULL; @@ -358,7 +357,7 @@ rt_module_t rt_module_load(const char* name, void* module_ptr) module_size += phdr[index].p_memsz; } - if (module_size == 0) + if (module_size == 0) { rt_kprintf(" module size error\n"); return module; @@ -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++) { - 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; } } @@ -406,12 +405,12 @@ rt_module_t rt_module_load(const char* name, void* module_ptr) static rt_bool_t unsolved = RT_FALSE; /* 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 */ - 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; - nr_reloc = (rt_uint32_t) (shdr[index].sh_size / sizeof(Elf32_Rel)); + 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; + nr_reloc = (rt_uint32_t)(shdr[index].sh_size / sizeof(Elf32_Rel)); /* relocate every items */ 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, ("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)) - rt_module_arm_relocate(module, rel, (Elf32_Addr)(module->module_space + sym->st_value)); - else if(!linked) + 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)); + else if (!linked) { 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)); /* 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) { 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 ++; } - if(unsolved) + if (unsolved) { rt_object_delete(&(module->parent)); 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 ++) { /* 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; } /* found .dynsym section */ - if(index != elf_module->e_shnum) + if (index != elf_module->e_shnum) { int i, count = 0; 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); - strtab = (rt_uint8_t*) module_ptr + shdr[shdr[index].sh_link].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; - for(i=0; isymtab = (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; - for(i=0, count=0; isymtab[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); - rt_memset((void*)module->symtab[count].name, 0, length); - rt_memcpy((void*)module->symtab[count].name, strtab + symtab[i].st_name, length); - count++; + rt_memset((void *)module->symtab[count].name, 0, length); + rt_memcpy((void *)module->symtab[count].name, strtab + symtab[i].st_name, length); + count ++; } } } @@ -495,10 +494,10 @@ rt_module_t rt_module_load(const char* name, void* module_ptr) /* init module object container */ rt_module_init_object_container(module); - /* increase module reference count */ - module->nref++; + /* increase module reference count */ + module->nref ++; - if(elf_module->e_entry != 0) + if (elf_module->e_entry != 0) { /* init module memory allocator */ 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 - if(rt_module_load_hook != RT_NULL) + if (rt_module_load_hook != RT_NULL) { 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 * */ -rt_module_t rt_module_open(const char* path) +rt_module_t rt_module_open(const char *path) { int fd, length; - struct rt_module* module; + struct rt_module *module; struct stat s; char *buffer, *offset_ptr, *name; @@ -614,7 +613,7 @@ FINSH_FUNCTION_EXPORT_ALIAS(rt_module_open, exec, exec module from file); #endif /** - * This function will unload a module from memory and release resources + * This function will unload a module from memory and release resources * * @param module the module to be unloaded * @@ -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) { int i; - struct rt_object* object; + struct rt_object *object; struct rt_list_node *list; RT_DEBUG_NOT_IN_INTERRUPT; @@ -635,18 +634,18 @@ rt_err_t rt_module_unload(rt_module_t module) rt_kprintf("rt_module_unload: %s\n", module->parent.name); /* 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 */ - if(module->module_thread != RT_NULL) - { + if (module->module_thread != RT_NULL) + { if (module->module_thread->stat == RT_THREAD_READY) rt_thread_suspend(module->module_thread); } /* delete threads */ - list = &module->module_object[RT_Object_Class_Thread].object_list; - while(list->next != list) + list = &module->module_object[RT_Object_Class_Thread].object_list; + while (list->next != list) { object = rt_list_entry(list->next, struct rt_object, list); if (rt_object_is_systemobject(object) == RT_EOK) @@ -655,16 +654,16 @@ rt_err_t rt_module_unload(rt_module_t module) rt_thread_detach((rt_thread_t)object); } else - { + { /* delete dynamic object */ rt_thread_delete((rt_thread_t)object); - } + } } #ifdef RT_USING_SEMAPHORE /* delete semaphores */ 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); if (rt_object_is_systemobject(object) == RT_EOK) @@ -673,17 +672,17 @@ rt_err_t rt_module_unload(rt_module_t module) rt_sem_detach((rt_sem_t)object); } else - { + { /* delete dynamic object */ rt_sem_delete((rt_sem_t)object); - } + } } #endif #ifdef RT_USING_MUTEX /* delete mutexs*/ 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); if (rt_object_is_systemobject(object) == RT_EOK) @@ -692,17 +691,17 @@ rt_err_t rt_module_unload(rt_module_t module) rt_mutex_detach((rt_mutex_t)object); } else - { + { /* delete dynamic object */ rt_mutex_delete((rt_mutex_t)object); - } + } } #endif #ifdef RT_USING_EVENT /* delete mailboxs */ 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); if (rt_object_is_systemobject(object) == RT_EOK) @@ -711,17 +710,17 @@ rt_err_t rt_module_unload(rt_module_t module) rt_event_detach((rt_event_t)object); } else - { + { /* delete dynamic object */ rt_event_delete((rt_event_t)object); - } + } } #endif #ifdef RT_USING_MAILBOX /* delete mailboxs */ 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); if (rt_object_is_systemobject(object) == RT_EOK) @@ -730,17 +729,17 @@ rt_err_t rt_module_unload(rt_module_t module) rt_mb_detach((rt_mailbox_t)object); } else - { + { /* delete dynamic object */ rt_mb_delete((rt_mailbox_t)object); - } + } } #endif #ifdef RT_USING_MESSAGEQUEUE /* delete msgqueues */ - list = &module->module_object[RT_Object_Class_MessageQueue].object_list; - while(list->next != list) + list = &module->module_object[RT_Object_Class_MessageQueue].object_list; + while (list->next != list) { object = rt_list_entry(list->next, struct rt_object, list); if (rt_object_is_systemobject(object) == RT_EOK) @@ -749,17 +748,17 @@ rt_err_t rt_module_unload(rt_module_t module) rt_mq_detach((rt_mq_t)object); } else - { + { /* delete dynamic object */ rt_mq_delete((rt_mq_t)object); - } + } } #endif #ifdef RT_USING_MEMPOOL /* delete mempools */ 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); if (rt_object_is_systemobject(object) == RT_EOK) @@ -768,26 +767,26 @@ rt_err_t rt_module_unload(rt_module_t module) rt_mp_detach((rt_mp_t)object); } else - { + { /* delete dynamic object */ rt_mp_delete((rt_mp_t)object); - } + } } #endif #ifdef RT_USING_DEVICE /* delete devices */ 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); rt_device_unregister((rt_device_t)object); - } + } #endif /* delete timers */ 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); if (rt_object_is_systemobject(object) == RT_EOK) @@ -796,19 +795,19 @@ rt_err_t rt_module_unload(rt_module_t module) rt_timer_detach((rt_timer_t)object); } else - { + { /* delete dynamic object */ rt_timer_delete((rt_timer_t)object); - } - } + } + } } - if(module->page_cnt > 0) + if (module->page_cnt > 0) { 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"); //list_memlist("tetris"); @@ -817,19 +816,19 @@ rt_err_t rt_module_unload(rt_module_t module) rt_module_free_page(page[i].page_ptr, page[i].npage); } } - + /* release module space memory */ rt_free(module->module_space); /* release module symbol table */ - for(i=0; insym; i++) rt_free((void *)module->symtab[i].name); - if(module->symtab != RT_NULL) rt_free(module->symtab); + for (i=0; insym; i++) rt_free((void *)module->symtab[i].name); + if (module->symtab != RT_NULL) rt_free(module->symtab); #ifdef RT_USING_HOOK - if(rt_module_unload_hook != RT_NULL) - { - rt_module_unload_hook(module); - } + if (rt_module_unload_hook != RT_NULL) + { + rt_module_unload_hook(module); + } #endif rt_free(module->page_array); @@ -847,11 +846,11 @@ rt_err_t rt_module_unload(rt_module_t 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* object; - struct rt_list_node* node; + struct rt_object *object; + struct rt_list_node *node; extern struct rt_object_information rt_object_container[]; @@ -891,24 +890,24 @@ rt_module_t rt_module_find(const char* name) */ static void *rt_module_malloc_page(rt_size_t npages) { - void* chunk; + void *chunk; struct rt_page_info *page; chunk = rt_page_alloc(npages); 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].npage = npages; - rt_current_module->page_cnt++; + page[rt_current_module->page_cnt].npage = npages; + rt_current_module->page_cnt ++; RT_ASSERT(rt_current_module->page_cnt <= PAGE_COUNT_MAX); - + return chunk; } /* - * This function will release the previously allocated memory page + * This function will release the previously allocated memory page * by rt_malloc_page. * * @param page_ptr the page address to be released. @@ -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_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; ipage_cnt; i++) + for (i=0; ipage_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); } - else if(page[i].npage == npages) - { - for(index=i; indexpage_cnt-1; index++) + else if (page[i].npage == npages) + { + for (index=i; indexpage_cnt-1; index++) { page[index].page_ptr = page[index + 1].page_ptr; page[index].npage = page[index + 1].npage; @@ -944,9 +943,9 @@ static void rt_module_free_page(void *page_ptr, rt_size_t npages) page[rt_current_module->page_cnt - 1].page_ptr = RT_NULL; page[rt_current_module->page_cnt - 1].npage = 0; } - else RT_ASSERT(RT_FALSE); + else RT_ASSERT(RT_FALSE); - rt_current_module->page_cnt--; + rt_current_module->page_cnt --; return; } @@ -963,19 +962,19 @@ void *rt_module_malloc(rt_size_t size) { struct rt_mem_head *b, *n, *up; struct rt_mem_head **prev; - rt_uint32_t npage; + rt_uint32_t npage; rt_size_t nunits; RT_DEBUG_NOT_IN_INTERRUPT; - nunits = (size + sizeof(struct rt_mem_head) -1)/sizeof(struct rt_mem_head) + 1; + nunits = (size + sizeof(struct rt_mem_head) -1)/sizeof(struct rt_mem_head) + 1; RT_ASSERT(size != 0); RT_ASSERT(nunits != 0); rt_sem_take(&mod_sem, RT_WAITING_FOREVER); - for (prev = (struct rt_mem_head **)&rt_current_module->mem_list; (b = *prev) != RT_NULL; prev = &(b->next)) + for (prev = (struct rt_mem_head **)&rt_current_module->mem_list; (b = *prev) != RT_NULL; prev = &(b->next)) { if (b->size > nunits) { @@ -998,7 +997,7 @@ void *rt_module_malloc(rt_size_t size) *prev = b->next; rt_kprintf("rt_module_malloc 0x%x, %d\n",b + 1, size); - //list_memlist("tetris"); + //list_memlist("tetris"); rt_sem_release(&mod_sem); return (void *)(b + 1); } @@ -1006,7 +1005,7 @@ void *rt_module_malloc(rt_size_t size) /* allocate pages from system heap */ 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); @@ -1019,7 +1018,7 @@ void *rt_module_malloc(rt_size_t size) *prev = up; rt_sem_release(&mod_sem); - + return rt_module_malloc(size); } @@ -1037,18 +1036,18 @@ void rt_module_free(rt_module_t module, void *addr) RT_ASSERT((((rt_uint32_t)addr) & (sizeof(struct rt_mem_head) -1)) == 0); //rt_kprintf("rt_module_free 0x%x\n", addr); - + rt_sem_take(&mod_sem, RT_WAITING_FOREVER); - + n = (struct rt_mem_head *)addr - 1; prev = (struct rt_mem_head **)&module->mem_list; while ((b = *prev) != RT_NULL) - { + { RT_ASSERT(b->size > 0); RT_ASSERT(b > n || b + b->size <= n); - if (b + b->size == n && ((rt_uint32_t)n % RT_MM_PAGE_SIZE != 0)) + if (b + b->size == n && ((rt_uint32_t)n % RT_MM_PAGE_SIZE != 0)) { if (b + (b->size + n->size) == b->next) { @@ -1057,12 +1056,12 @@ void rt_module_free(rt_module_t module, void *addr) } 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; - 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); /* split memory */ @@ -1073,7 +1072,7 @@ void rt_module_free(rt_module_t module, void *addr) } else { - *prev = b->next; + *prev = b->next; } rt_module_free_page(b, npage); @@ -1083,7 +1082,7 @@ void rt_module_free(rt_module_t module, void *addr) /* unlock */ rt_sem_release(&mod_sem); ////list_memlist("tetris"); - + return; } @@ -1092,12 +1091,12 @@ void rt_module_free(rt_module_t module, void *addr) n->size = b->size + n->size; 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; - 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); /* split memory */ @@ -1106,11 +1105,11 @@ void rt_module_free(rt_module_t module, void *addr) r->size = n->size - nunits; *prev = r; } - else *prev = n->next; + else *prev = n->next; rt_module_free_page(n, npage); } - } + } else { *prev = n; @@ -1127,13 +1126,13 @@ void rt_module_free(rt_module_t module, void *addr) 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; - if(npage > 0) + if (npage > 0) { 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); /* split memory */ @@ -1148,14 +1147,14 @@ void rt_module_free(rt_module_t module, void *addr) } } } - else - { + else + { n->next = b; *prev = n; } /* unlock */ - rt_sem_release(&mod_sem); + rt_sem_release(&mod_sem); //list_memlist("tetris"); } @@ -1179,11 +1178,11 @@ void *rt_module_realloc(void *ptr, rt_size_t size) nunits = (size + sizeof(struct rt_mem_head) - 1) / sizeof(struct rt_mem_head) + 1; b = (struct rt_mem_head *)ptr - 1; - if (nunits <= b->size) - { - /* new size is smaller or equal then before */ + if (nunits <= b->size) + { + /* new size is smaller or equal then before */ if (nunits == b->size) return ptr; - else + else { p = b + nunits; p->size = b->size - nunits; @@ -1192,8 +1191,8 @@ void *rt_module_realloc(void *ptr, rt_size_t size) return (void *)(b + 1); } } - else - { + else + { /* more space then required */ prev = (struct rt_mem_head *)rt_current_module->mem_list; for (p = prev->next; p != (b->size + b) && p != RT_NULL; prev = p, p = p->next) break; @@ -1210,26 +1209,26 @@ void *rt_module_realloc(void *ptr, rt_size_t size) else /* more space then required, split block*/ { /* pointer to old header */ - tmpp = p; + tmpp = p; p = b + nunits; /* restoring old pointer */ p->next = tmpp->next; /* new size for p */ - p->size = tmpp->size + b->size - nunits; + p->size = tmpp->size + b->size - nunits; b->size = nunits; prev->next = p; } rt_current_module->mem_list = (void *)prev; - return (void *) (b + 1); + return (void *)(b + 1); } else /* allocate new memory and copy old data */ { if ((p = rt_module_malloc(size)) == RT_NULL) return RT_NULL; rt_memmove(p, (b+1), ((b->size) * sizeof(struct rt_mem_head))); rt_module_free(rt_current_module, (void *)(b + 1)); - return (void *) (p); + return (void *)(p); } } } @@ -1244,30 +1243,30 @@ void list_memlist(const char* name) struct rt_mem_head *b; 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)) { rt_kprintf("0x%x--%d\n", b, b->size * sizeof(struct rt_mem_head)); } } 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; struct rt_page_info *page; int i; module = rt_module_find(name); - if(module == RT_NULL) return; + if (module == RT_NULL) return; page = (struct rt_page_info*)module->page_array; - for(i=0; ipage_cnt; i++) + for (i=0; ipage_cnt; i++) { rt_kprintf("0x%x--%d\n", page[i].page_ptr, page[i].npage); - } + } } FINSH_FUNCTION_EXPORT(list_mempage, list module using memory page information) #endif diff --git a/src/module.h b/src/module.h index 1272770038..d50d3d364f 100644 --- a/src/module.h +++ b/src/module.h @@ -1,39 +1,54 @@ +/* + * 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__ #define __MODULE_H__ #include -typedef rt_uint8_t Elf_Byte; +typedef rt_uint8_t Elf_Byte; -typedef rt_uint32_t Elf32_Addr; /* Unsigned program address */ -typedef rt_uint32_t Elf32_Off; /* Unsigned file offset */ -typedef rt_int32_t Elf32_Sword; /* Signed large integer */ -typedef rt_uint32_t Elf32_Word; /* Unsigned large integer */ -typedef rt_uint16_t Elf32_Half; /* Unsigned medium integer */ +typedef rt_uint32_t Elf32_Addr; /* Unsigned program address */ +typedef rt_uint32_t Elf32_Off; /* Unsigned file offset */ +typedef rt_int32_t Elf32_Sword; /* Signed large integer */ +typedef rt_uint32_t Elf32_Word; /* Unsigned large integer */ +typedef rt_uint16_t Elf32_Half; /* Unsigned medium integer */ /* e_ident[] magic number */ -#define ELFMAG0 0x7f /* e_ident[EI_MAG0] */ -#define ELFMAG1 'E' /* e_ident[EI_MAG1] */ -#define ELFMAG2 'L' /* e_ident[EI_MAG2] */ -#define ELFMAG3 'F' /* e_ident[EI_MAG3] */ -#define RTMMAG "\177RTM" /* magic */ -#define ELFMAG "\177ELF" /* magic */ -#define SELFMAG 4 /* size of magic */ +#define ELFMAG0 0x7f /* e_ident[EI_MAG0] */ +#define ELFMAG1 'E' /* e_ident[EI_MAG1] */ +#define ELFMAG2 'L' /* e_ident[EI_MAG2] */ +#define ELFMAG3 'F' /* e_ident[EI_MAG3] */ +#define RTMMAG "\177RTM" /* magic */ +#define ELFMAG "\177ELF" /* magic */ +#define SELFMAG 4 /* size of magic */ -#define EI_CLASS 4 /* file class */ -#define EI_NIDENT 16 /* Size of e_ident[] */ +#define EI_CLASS 4 /* file class */ +#define EI_NIDENT 16 /* Size of e_ident[] */ /* e_ident[] file class */ -#define ELFCLASSNONE 0 /* invalid */ -#define ELFCLASS32 1 /* 32-bit objs */ -#define ELFCLASS64 2 /* 64-bit objs */ -#define ELFCLASSNUM 3 /* number of classes */ +#define ELFCLASSNONE 0 /* invalid */ +#define ELFCLASS32 1 /* 32-bit objs */ +#define ELFCLASS64 2 /* 64-bit objs */ +#define ELFCLASSNUM 3 /* number of classes */ /* e_ident[] data encoding */ -#define ELFDATANONE 0 /* invalid */ -#define ELFDATA2LSB 1 /* Little-Endian */ -#define ELFDATA2MSB 2 /* Big-Endian */ -#define ELFDATANUM 3 /* number of data encode defines */ +#define ELFDATANONE 0 /* invalid */ +#define ELFDATA2LSB 1 /* Little-Endian */ +#define ELFDATA2MSB 2 /* Big-Endian */ +#define ELFDATANUM 3 /* number of data encode defines */ /* e_ident */ #define IS_ELF(ehdr) ((ehdr).e_ident[EI_MAG0] == ELFMAG0 && \ @@ -42,175 +57,181 @@ typedef rt_uint16_t Elf32_Half; /* Unsigned medium integer */ (ehdr).e_ident[EI_MAG3] == ELFMAG3) /* ELF Header */ -typedef struct elfhdr { - unsigned char e_ident[EI_NIDENT]; /* ELF Identification */ - Elf32_Half e_type; /* object file type */ - Elf32_Half e_machine; /* machine */ - Elf32_Word e_version; /* object file version */ - Elf32_Addr e_entry; /* virtual entry point */ - Elf32_Off e_phoff; /* program header table offset */ - Elf32_Off e_shoff; /* section header table offset */ - Elf32_Word e_flags; /* processor-specific flags */ - Elf32_Half e_ehsize; /* ELF header size */ - Elf32_Half e_phentsize; /* program header entry size */ - Elf32_Half e_phnum; /* number of program header entries */ - Elf32_Half e_shentsize; /* section header entry size */ - Elf32_Half e_shnum; /* number of section header entries */ - Elf32_Half e_shstrndx; /* section header table's "section - header string table" entry offset */ +typedef struct elfhdr +{ + unsigned char e_ident[EI_NIDENT]; /* ELF Identification */ + Elf32_Half e_type; /* object file type */ + Elf32_Half e_machine; /* machine */ + Elf32_Word e_version; /* object file version */ + Elf32_Addr e_entry; /* virtual entry point */ + Elf32_Off e_phoff; /* program header table offset */ + Elf32_Off e_shoff; /* section header table offset */ + Elf32_Word e_flags; /* processor-specific flags */ + Elf32_Half e_ehsize; /* ELF header size */ + Elf32_Half e_phentsize; /* program header entry size */ + Elf32_Half e_phnum; /* number of program header entries */ + Elf32_Half e_shentsize; /* section header entry size */ + Elf32_Half e_shnum; /* number of section header entries */ + Elf32_Half e_shstrndx; /* section header table's "section + header string table" entry offset */ } Elf32_Ehdr; /* Section Header */ -typedef struct { - Elf32_Word sh_name; /* name - index into section header - string table section */ - Elf32_Word sh_type; /* type */ - Elf32_Word sh_flags; /* flags */ - Elf32_Addr sh_addr; /* address */ - Elf32_Off sh_offset; /* file offset */ - Elf32_Word sh_size; /* section size */ - Elf32_Word sh_link; /* section header table index link */ - Elf32_Word sh_info; /* extra information */ - Elf32_Word sh_addralign; /* address alignment */ - Elf32_Word sh_entsize; /* section entry size */ +typedef struct +{ + Elf32_Word sh_name; /* name - index into section header + string table section */ + Elf32_Word sh_type; /* type */ + Elf32_Word sh_flags; /* flags */ + Elf32_Addr sh_addr; /* address */ + Elf32_Off sh_offset; /* file offset */ + Elf32_Word sh_size; /* section size */ + Elf32_Word sh_link; /* section header table index link */ + Elf32_Word sh_info; /* extra information */ + Elf32_Word sh_addralign; /* address alignment */ + Elf32_Word sh_entsize; /* section entry size */ } Elf32_Shdr; /* Section names */ -#define ELF_BSS ".bss" /* uninitialized data */ -#define ELF_DATA ".data" /* initialized data */ -#define ELF_DEBUG ".debug" /* debug */ -#define ELF_DYNAMIC ".dynamic" /* dynamic linking information */ -#define ELF_DYNSTR ".dynstr" /* dynamic string table */ -#define ELF_DYNSYM ".dynsym" /* dynamic symbol table */ -#define ELF_FINI ".fini" /* termination code */ -#define ELF_GOT ".got" /* global offset table */ -#define ELF_HASH ".hash" /* symbol hash table */ -#define ELF_INIT ".init" /* initialization code */ -#define ELF_REL_DATA ".rel.data" /* relocation data */ -#define ELF_REL_FINI ".rel.fini" /* relocation termination code */ -#define ELF_REL_INIT ".rel.init" /* relocation initialization code */ -#define ELF_REL_DYN ".rel.dyn" /* relocaltion dynamic link info */ -#define ELF_REL_RODATA ".rel.rodata" /* relocation read-only data */ -#define ELF_REL_TEXT ".rel.text" /* relocation code */ -#define ELF_RODATA ".rodata" /* read-only data */ -#define ELF_SHSTRTAB ".shstrtab" /* section header string table */ -#define ELF_STRTAB ".strtab" /* string table */ -#define ELF_SYMTAB ".symtab" /* symbol table */ -#define ELF_TEXT ".text" /* code */ -#define ELF_RTMSYMTAB "RTMSymTab" +#define ELF_BSS ".bss" /* uninitialized data */ +#define ELF_DATA ".data" /* initialized data */ +#define ELF_DEBUG ".debug" /* debug */ +#define ELF_DYNAMIC ".dynamic" /* dynamic linking information */ +#define ELF_DYNSTR ".dynstr" /* dynamic string table */ +#define ELF_DYNSYM ".dynsym" /* dynamic symbol table */ +#define ELF_FINI ".fini" /* termination code */ +#define ELF_GOT ".got" /* global offset table */ +#define ELF_HASH ".hash" /* symbol hash table */ +#define ELF_INIT ".init" /* initialization code */ +#define ELF_REL_DATA ".rel.data" /* relocation data */ +#define ELF_REL_FINI ".rel.fini" /* relocation termination code */ +#define ELF_REL_INIT ".rel.init" /* relocation initialization code */ +#define ELF_REL_DYN ".rel.dyn" /* relocaltion dynamic link info */ +#define ELF_REL_RODATA ".rel.rodata" /* relocation read-only data */ +#define ELF_REL_TEXT ".rel.text" /* relocation code */ +#define ELF_RODATA ".rodata" /* read-only data */ +#define ELF_SHSTRTAB ".shstrtab" /* section header string table */ +#define ELF_STRTAB ".strtab" /* string table */ +#define ELF_SYMTAB ".symtab" /* symbol table */ +#define ELF_TEXT ".text" /* code */ +#define ELF_RTMSYMTAB "RTMSymTab" /* Symbol Table Entry */ -typedef struct elf32_sym { - Elf32_Word st_name; /* name - index into string table */ - Elf32_Addr st_value; /* symbol value */ - Elf32_Word st_size; /* symbol size */ - unsigned char st_info; /* type and binding */ - unsigned char st_other; /* 0 - no defined meaning */ - Elf32_Half st_shndx; /* section header index */ +typedef struct elf32_sym +{ + Elf32_Word st_name; /* name - index into string table */ + Elf32_Addr st_value; /* symbol value */ + Elf32_Word st_size; /* symbol size */ + unsigned char st_info; /* type and binding */ + unsigned char st_other; /* 0 - no defined meaning */ + Elf32_Half st_shndx; /* section header index */ } Elf32_Sym; -#define STB_LOCAL 0 /* BIND */ -#define STB_GLOBAL 1 -#define STB_WEAK 2 -#define STB_NUM 3 +#define STB_LOCAL 0 /* BIND */ +#define STB_GLOBAL 1 +#define STB_WEAK 2 +#define STB_NUM 3 -#define STB_LOPROC 13 /* processor specific range */ -#define STB_HIPROC 15 - -#define STT_NOTYPE 0 /* symbol type is unspecified */ -#define STT_OBJECT 1 /* data object */ -#define STT_FUNC 2 /* code object */ -#define STT_SECTION 3 /* symbol identifies an ELF section */ -#define STT_FILE 4 /* symbol's name is file name */ -#define STT_COMMON 5 /* common data object */ -#define STT_TLS 6 /* thread-local data object */ -#define STT_NUM 7 /* # defined types in generic range */ -#define STT_LOOS 10 /* OS specific range */ -#define STT_HIOS 12 -#define STT_LOPROC 13 /* processor specific range */ -#define STT_HIPROC 15 +#define STB_LOPROC 13 /* processor specific range */ +#define STB_HIPROC 15 + +#define STT_NOTYPE 0 /* symbol type is unspecified */ +#define STT_OBJECT 1 /* data object */ +#define STT_FUNC 2 /* code object */ +#define STT_SECTION 3 /* symbol identifies an ELF section */ +#define STT_FILE 4 /* symbol's name is file name */ +#define STT_COMMON 5 /* common data object */ +#define STT_TLS 6 /* thread-local data object */ +#define STT_NUM 7 /* # defined types in generic range */ +#define STT_LOOS 10 /* OS specific range */ +#define STT_HIOS 12 +#define STT_LOPROC 13 /* processor specific range */ +#define STT_HIPROC 15 -#define ELF_ST_BIND(info) ((info) >> 4) -#define ELF_ST_TYPE(info) ((info) & 0xf) -#define ELF_ST_INFO(bind, type) (((bind)<<4)+((type)&0xf)) +#define ELF_ST_BIND(info) ((info) >> 4) +#define ELF_ST_TYPE(info) ((info) & 0xf) +#define ELF_ST_INFO(bind, type) (((bind)<<4)+((type)&0xf)) /* Relocation entry with implicit addend */ -typedef struct { - Elf32_Addr r_offset; /* offset of relocation */ - Elf32_Word r_info; /* symbol table index and type */ +typedef struct +{ + Elf32_Addr r_offset; /* offset of relocation */ + Elf32_Word r_info; /* symbol table index and type */ } Elf32_Rel; /* Relocation entry with explicit addend */ -typedef struct { - Elf32_Addr r_offset; /* offset of relocation */ - Elf32_Word r_info; /* symbol table index and type */ - Elf32_Sword r_addend; +typedef struct +{ + Elf32_Addr r_offset; /* offset of relocation */ + Elf32_Word r_info; /* symbol table index and type */ + Elf32_Sword r_addend; } Elf32_Rela; /* Extract relocation info - r_info */ -#define ELF32_R_SYM(i) ((i) >> 8) -#define ELF32_R_TYPE(i) ((unsigned char) (i)) -#define ELF32_R_INFO(s,t) (((s) << 8) + (unsigned char)(t)) +#define ELF32_R_SYM(i) ((i) >> 8) +#define ELF32_R_TYPE(i) ((unsigned char) (i)) +#define ELF32_R_INFO(s,t) (((s) << 8) + (unsigned char)(t)) /* * Relocation type for arm */ -#define R_ARM_NONE 0 -#define R_ARM_PC24 1 -#define R_ARM_ABS32 2 -#define R_ARM_GLOB_DAT 21 -#define R_ARM_JUMP_SLOT 22 -#define R_ARM_RELATIVE 23 -#define R_ARM_PLT32 27 -#define R_ARM_CALL 28 -#define R_ARM_JUMP24 29 -#define R_ARM_V4BX 40 +#define R_ARM_NONE 0 +#define R_ARM_PC24 1 +#define R_ARM_ABS32 2 +#define R_ARM_GLOB_DAT 21 +#define R_ARM_JUMP_SLOT 22 +#define R_ARM_RELATIVE 23 +#define R_ARM_PLT32 27 +#define R_ARM_CALL 28 +#define R_ARM_JUMP24 29 +#define R_ARM_V4BX 40 /* Program Header */ -typedef struct { - Elf32_Word p_type; /* segment type */ - Elf32_Off p_offset; /* segment offset */ - Elf32_Addr p_vaddr; /* virtual address of segment */ - Elf32_Addr p_paddr; /* physical address - ignored? */ - Elf32_Word p_filesz; /* number of bytes in file for seg. */ - Elf32_Word p_memsz; /* number of bytes in mem. for seg. */ - Elf32_Word p_flags; /* flags */ - Elf32_Word p_align; /* memory alignment */ +typedef struct +{ + Elf32_Word p_type; /* segment type */ + Elf32_Off p_offset; /* segment offset */ + Elf32_Addr p_vaddr; /* virtual address of segment */ + Elf32_Addr p_paddr; /* physical address - ignored? */ + Elf32_Word p_filesz; /* number of bytes in file for seg. */ + Elf32_Word p_memsz; /* number of bytes in mem. for seg. */ + Elf32_Word p_flags; /* flags */ + Elf32_Word p_align; /* memory alignment */ } Elf32_Phdr; /* p_type */ -#define PT_LOAD 1 +#define PT_LOAD 1 /* p_flags */ -#define PF_X 1 -#define PF_W 2 -#define PF_R 4 +#define PF_X 1 +#define PF_W 2 +#define PF_R 4 /* sh_type */ -#define SHT_NULL 0 /* inactive */ -#define SHT_PROGBITS 1 /* program defined information */ -#define SHT_SYMTAB 2 /* symbol table section */ -#define SHT_STRTAB 3 /* string table section */ -#define SHT_RELA 4 /* relocation section with addends*/ -#define SHT_HASH 5 /* symbol hash table section */ -#define SHT_DYNAMIC 6 /* dynamic section */ -#define SHT_NOTE 7 /* note section */ -#define SHT_NOBITS 8 /* no space section */ -#define SHT_REL 9 /* relation section without addends */ -#define SHT_SHLIB 10 /* reserved - purpose unknown */ -#define SHT_DYNSYM 11 /* dynamic symbol table section */ -#define SHT_NUM 12 /* number of section types */ -#define SHT_LOPROC 0x70000000 /* reserved range for processor */ -#define SHT_HIPROC 0x7fffffff /* specific section header types */ -#define SHT_LOUSER 0x80000000 /* reserved range for application */ -#define SHT_HIUSER 0xffffffff /* specific indexes */ +#define SHT_NULL 0 /* inactive */ +#define SHT_PROGBITS 1 /* program defined information */ +#define SHT_SYMTAB 2 /* symbol table section */ +#define SHT_STRTAB 3 /* string table section */ +#define SHT_RELA 4 /* relocation section with addends*/ +#define SHT_HASH 5 /* symbol hash table section */ +#define SHT_DYNAMIC 6 /* dynamic section */ +#define SHT_NOTE 7 /* note section */ +#define SHT_NOBITS 8 /* no space section */ +#define SHT_REL 9 /* relation section without addends */ +#define SHT_SHLIB 10 /* reserved - purpose unknown */ +#define SHT_DYNSYM 11 /* dynamic symbol table section */ +#define SHT_NUM 12 /* number of section types */ +#define SHT_LOPROC 0x70000000 /* reserved range for processor */ +#define SHT_HIPROC 0x7fffffff /* specific section header types */ +#define SHT_LOUSER 0x80000000 /* reserved range for application */ +#define SHT_HIUSER 0xffffffff /* specific indexes */ /* Section Attribute Flags - sh_flags */ -#define SHF_WRITE 0x1 /* Writable */ -#define SHF_ALLOC 0x2 /* occupies memory */ -#define SHF_EXECINSTR 0x4 /* executable */ -#define SHF_MASKPROC 0xf0000000 /* reserved bits for processor */ - /* specific section attributes */ +#define SHF_WRITE 0x1 /* Writable */ +#define SHF_ALLOC 0x2 /* occupies memory */ +#define SHF_EXECINSTR 0x4 /* executable */ +#define SHF_MASKPROC 0xf0000000 /* reserved bits for processor */ +/* specific section attributes */ #endif diff --git a/src/object.c b/src/object.c index 8ebfe2add8..c5abcccc16 100644 --- a/src/object.c +++ b/src/object.c @@ -1,7 +1,7 @@ /* * File : object.c * 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 * 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 -static void (*rt_object_attach_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_take_hook)(struct rt_object* object); -void (*rt_object_put_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); +void (*rt_object_trytake_hook)(struct rt_object *object); +void (*rt_object_take_hook)(struct rt_object *object); +void (*rt_object_put_hook)(struct rt_object *object); /** * @addtogroup Hook @@ -82,7 +82,7 @@ void (*rt_object_put_hook)(struct rt_object* object); * * @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; } @@ -93,7 +93,7 @@ void rt_object_attach_sethook(void (*hook)(struct rt_object* object)) * * @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; } @@ -111,7 +111,7 @@ void rt_object_detach_sethook(void (*hook)(struct rt_object* object)) * * @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; } @@ -130,7 +130,7 @@ void rt_object_trytake_sethook(void (*hook)(struct rt_object* object)) * * @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; } @@ -141,7 +141,7 @@ void rt_object_take_sethook(void (*hook)(struct rt_object* object)) * * @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; } @@ -184,7 +184,7 @@ struct rt_object_information *rt_object_get_information(enum rt_object_class_typ * @param type the object type. * @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; struct rt_object_information* information; @@ -255,11 +255,11 @@ void rt_object_detach(rt_object_t 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; - struct rt_object_information* information; + struct rt_object_information *information; 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]; #endif - object = (struct rt_object*)rt_malloc(information->object_size); + object = (struct rt_object *)rt_malloc(information->object_size); if (object == RT_NULL) { /* 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; #ifdef RT_USING_MODULE - if(rt_module_self() != RT_NULL) + if (rt_module_self() != RT_NULL) { object->flag |= RT_OBJECT_FLAG_MODULE; } - object->module_id = (void*)rt_module_self(); + object->module_id = (void *)rt_module_self(); #endif /* copy name */ @@ -341,7 +341,7 @@ void rt_object_delete(rt_object_t object) rt_hw_interrupt_enable(temp); #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); else #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. */ -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_list_node* node; + struct rt_object *object; + struct rt_list_node *node; struct rt_object_information *information; extern volatile rt_uint8_t rt_interrupt_nest; /* parameter check */ - if ((name == RT_NULL) || - (type > RT_Object_Class_Unknown)) + if ((name == RT_NULL) || (type > RT_Object_Class_Unknown)) return RT_NULL; /* which is invoke in interrupt status */ diff --git a/src/rtm.c b/src/rtm.c index d2ae74b955..5c234a6be8 100644 --- a/src/rtm.c +++ b/src/rtm.c @@ -1,18 +1,18 @@ /* * File : rtm.c * 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 * found in the file LICENSE in this distribution or at * http://www.rt-thread.org/license/LICENSE * * Change Logs: - * Date Author Notes - * 2010-04-12 yi.qiu first version + * Date Author Notes + * 2010-04-12 yi.qiu first version */ -#include +#include #include #include #include @@ -25,7 +25,7 @@ RTM_EXPORT(rt_object_get_information); /* - * thread interface symbol + * thread interface symbol */ RTM_EXPORT(rt_thread_init); @@ -79,7 +79,7 @@ RTM_EXPORT(rt_event_create); RTM_EXPORT(rt_event_delete); RTM_EXPORT(rt_event_send); RTM_EXPORT(rt_event_recv); -RTM_EXPORT(rt_event_control); +RTM_EXPORT(rt_event_control); #endif #ifdef RT_USING_MAILBOX @@ -92,7 +92,7 @@ RTM_EXPORT(rt_mb_create); RTM_EXPORT(rt_mb_delete); RTM_EXPORT(rt_mb_send); RTM_EXPORT(rt_mb_recv); -RTM_EXPORT(rt_mb_control); +RTM_EXPORT(rt_mb_control); #endif #ifdef RT_USING_MESSAGEQUEUE @@ -105,8 +105,8 @@ RTM_EXPORT(rt_mq_create); RTM_EXPORT(rt_mq_delete); RTM_EXPORT(rt_mq_send); RTM_EXPORT(rt_mq_urgent); -RTM_EXPORT(rt_mq_recv); -RTM_EXPORT(rt_mq_control); +RTM_EXPORT(rt_mq_recv); +RTM_EXPORT(rt_mq_control); #endif #ifdef RT_USING_MEMPOOL @@ -159,8 +159,8 @@ RTM_EXPORT(rt_snprintf); /* * misc interface symbol */ -extern int __aeabi_idiv; -extern int __aeabi_ddiv; +extern int __aeabi_idiv; +extern int __aeabi_ddiv; extern int __aeabi_dmul; extern int __aeabi_i2d; extern int __aeabi_uidiv; diff --git a/src/scheduler.c b/src/scheduler.c index e0567f4a5a..8d6ba08acf 100644 --- a/src/scheduler.c +++ b/src/scheduler.c @@ -1,7 +1,7 @@ /* * File : scheduler.c * 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 * 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; 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; @@ -71,7 +71,7 @@ const rt_uint8_t rt_lowest_bitmap[] = }; #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 @@ -84,40 +84,39 @@ static void (*rt_scheduler_hook)(struct rt_thread* from, struct rt_thread* to); * * @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; } /*@}*/ #endif #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 || - (rt_uint32_t)thread->sp > - (rt_uint32_t)thread->stack_addr + (rt_uint32_t)thread->stack_size ) - { - rt_uint32_t level; + if ((rt_uint32_t)thread->sp <= (rt_uint32_t)thread->stack_addr || + (rt_uint32_t)thread->sp > + (rt_uint32_t)thread->stack_addr + (rt_uint32_t)thread->stack_size) + { + rt_uint32_t level; - rt_kprintf("thread:%s stack overflow\n", thread->name); + rt_kprintf("thread:%s stack overflow\n", thread->name); #ifdef RT_USING_FINSH { extern long list_thread(void); list_thread(); } #endif - level = rt_hw_interrupt_disable(); - while (level); - } - 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", - thread->name); - } + level = rt_hw_interrupt_disable(); + while (level); + } + 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", thread->name); + } } #endif @@ -128,31 +127,31 @@ static void _rt_scheduler_stack_check(struct rt_thread* thread) */ void rt_system_scheduler_init(void) { - register rt_base_t offset; + register rt_base_t offset; - rt_scheduler_lock_nest = 0; + rt_scheduler_lock_nest = 0; RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("start scheduler: max priority 0x%02x\n", RT_THREAD_PRIORITY_MAX)); - for (offset = 0; offset < RT_THREAD_PRIORITY_MAX; offset ++) - { - rt_list_init(&rt_thread_priority_table[offset]); - } + for (offset = 0; offset < RT_THREAD_PRIORITY_MAX; offset ++) + { + rt_list_init(&rt_thread_priority_table[offset]); + } - rt_current_priority = RT_THREAD_PRIORITY_MAX - 1; - rt_current_thread = RT_NULL; + rt_current_priority = RT_THREAD_PRIORITY_MAX - 1; + rt_current_thread = RT_NULL; - /* init ready priority group */ - rt_thread_ready_priority_group = 0; + /* init ready priority group */ + rt_thread_ready_priority_group = 0; #if RT_THREAD_PRIORITY_MAX > 32 - /* init ready table */ - rt_memset(rt_thread_ready_table, 0, sizeof(rt_thread_ready_table)); + /* init ready table */ + rt_memset(rt_thread_ready_table, 0, sizeof(rt_thread_ready_table)); #endif - /* init thread defunct */ - rt_list_init(&rt_thread_defunct); + /* init thread defunct */ + rt_list_init(&rt_thread_defunct); } /** @@ -160,50 +159,50 @@ void rt_system_scheduler_init(void) * This function will startup scheduler. It will select one thread * 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 rt_ubase_t highest_ready_priority; + register struct rt_thread *to_thread; + register rt_ubase_t highest_ready_priority; #if RT_THREAD_PRIORITY_MAX == 8 highest_ready_priority = rt_lowest_bitmap[rt_thread_ready_priority_group]; #else - register rt_ubase_t number; - /* find out the highest priority task */ - if (rt_thread_ready_priority_group & 0xff) - { - number = rt_lowest_bitmap[rt_thread_ready_priority_group & 0xff]; - } - else if (rt_thread_ready_priority_group & 0xff00) - { - number = rt_lowest_bitmap[(rt_thread_ready_priority_group >> 8) & 0xff] + 8; - } - else if (rt_thread_ready_priority_group & 0xff0000) - { - number = rt_lowest_bitmap[(rt_thread_ready_priority_group >> 16) & 0xff] + 16; - } - else - { - number = rt_lowest_bitmap[(rt_thread_ready_priority_group >> 24) & 0xff] + 24; - } + register rt_ubase_t number; + /* find out the highest priority task */ + if (rt_thread_ready_priority_group & 0xff) + { + number = rt_lowest_bitmap[rt_thread_ready_priority_group & 0xff]; + } + else if (rt_thread_ready_priority_group & 0xff00) + { + number = rt_lowest_bitmap[(rt_thread_ready_priority_group >> 8) & 0xff] + 8; + } + else if (rt_thread_ready_priority_group & 0xff0000) + { + number = rt_lowest_bitmap[(rt_thread_ready_priority_group >> 16) & 0xff] + 16; + } + else + { + number = rt_lowest_bitmap[(rt_thread_ready_priority_group >> 24) & 0xff] + 24; + } #if RT_THREAD_PRIORITY_MAX > 32 - highest_ready_priority = (number << 3) + rt_lowest_bitmap[rt_thread_ready_table[number]]; + highest_ready_priority = (number << 3) + rt_lowest_bitmap[rt_thread_ready_table[number]]; #else - highest_ready_priority = number; + highest_ready_priority = number; #endif #endif - /* get switch to thread */ - to_thread = rt_list_entry(rt_thread_priority_table[highest_ready_priority].next, - struct rt_thread, tlist); + /* get switch to thread */ + to_thread = rt_list_entry(rt_thread_priority_table[highest_ready_priority].next, + struct rt_thread, tlist); - rt_current_thread = to_thread; + rt_current_thread = to_thread; - /* switch to new thread */ - rt_hw_context_switch_to((rt_uint32_t)&to_thread->sp); + /* switch to new thread */ + rt_hw_context_switch_to((rt_uint32_t)&to_thread->sp); - /* never come back */ + /* never come back */ } /** @@ -215,91 +214,90 @@ void rt_system_scheduler_start() * This function will perform one schedule. It will select one thread * with the highest priority level, then switch to it. */ -void rt_schedule() +void rt_schedule(void) { - rt_base_t level; - struct rt_thread *to_thread; - struct rt_thread *from_thread; + rt_base_t level; + struct rt_thread *to_thread; + struct rt_thread *from_thread; - /* disable interrupt */ - level = rt_hw_interrupt_disable(); + /* disable interrupt */ + level = rt_hw_interrupt_disable(); - /* check the scheduler is enabled or not */ - if (rt_scheduler_lock_nest == 0) - { - register rt_ubase_t highest_ready_priority; + /* check the scheduler is enabled or not */ + if (rt_scheduler_lock_nest == 0) + { + register rt_ubase_t highest_ready_priority; #if RT_THREAD_PRIORITY_MAX == 8 highest_ready_priority = rt_lowest_bitmap[rt_thread_ready_priority_group]; #else - register rt_ubase_t number; - /* find out the highest priority task */ - if (rt_thread_ready_priority_group & 0xff) - { - number = rt_lowest_bitmap[rt_thread_ready_priority_group & 0xff]; - } - else if (rt_thread_ready_priority_group & 0xff00) - { - number = rt_lowest_bitmap[(rt_thread_ready_priority_group >> 8) & 0xff] + 8; - } - else if (rt_thread_ready_priority_group & 0xff0000) - { - number = rt_lowest_bitmap[(rt_thread_ready_priority_group >> 16) & 0xff] + 16; - } - else - { - number = rt_lowest_bitmap[(rt_thread_ready_priority_group >> 24) & 0xff] + 24; - } + register rt_ubase_t number; + /* find out the highest priority task */ + if (rt_thread_ready_priority_group & 0xff) + { + number = rt_lowest_bitmap[rt_thread_ready_priority_group & 0xff]; + } + else if (rt_thread_ready_priority_group & 0xff00) + { + number = rt_lowest_bitmap[(rt_thread_ready_priority_group >> 8) & 0xff] + 8; + } + else if (rt_thread_ready_priority_group & 0xff0000) + { + number = rt_lowest_bitmap[(rt_thread_ready_priority_group >> 16) & 0xff] + 16; + } + else + { + number = rt_lowest_bitmap[(rt_thread_ready_priority_group >> 24) & 0xff] + 24; + } #if RT_THREAD_PRIORITY_MAX > 32 - highest_ready_priority = (number << 3) + rt_lowest_bitmap[rt_thread_ready_table[number]]; + highest_ready_priority = (number << 3) + rt_lowest_bitmap[rt_thread_ready_table[number]]; #else - highest_ready_priority = number; + highest_ready_priority = number; #endif #endif - /* get switch to thread */ - to_thread = rt_list_entry(rt_thread_priority_table[highest_ready_priority].next, - struct rt_thread, tlist); + /* get switch to thread */ + to_thread = rt_list_entry(rt_thread_priority_table[highest_ready_priority].next, + struct rt_thread, tlist); - /* if the destination thread is not the same as current thread */ - if (to_thread != rt_current_thread) - { - rt_current_priority = highest_ready_priority; - from_thread = rt_current_thread; - rt_current_thread = to_thread; + /* if the destination thread is not the same as current thread */ + if (to_thread != rt_current_thread) + { + rt_current_priority = highest_ready_priority; + from_thread = rt_current_thread; + rt_current_thread = to_thread; #ifdef RT_USING_MODULE - rt_module_set ((rt_current_thread->module_id != RT_NULL) ? - (rt_module_t)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); #endif RT_OBJECT_HOOK_CALL(rt_scheduler_hook, (from_thread, to_thread)); - /* switch to new thread */ - RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, - ("[%d]switch to priority#%d thread:%s\n", rt_interrupt_nest, - highest_ready_priority, to_thread->name)); + /* switch to new thread */ + RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, + ("[%d]switch to priority#%d thread:%s\n", rt_interrupt_nest, + highest_ready_priority, to_thread->name)); #ifdef RT_USING_OVERFLOW_CHECK - _rt_scheduler_stack_check(to_thread); + _rt_scheduler_stack_check(to_thread); #endif - if (rt_interrupt_nest == 0) - { - rt_hw_context_switch((rt_uint32_t)&from_thread->sp, (rt_uint32_t)&to_thread->sp); - } - else - { - RT_DEBUG_LOG(RT_DEBUG_SCHEDULER,("switch in interrupt\n")); - - rt_hw_context_switch_interrupt((rt_uint32_t)&from_thread->sp, - (rt_uint32_t)&to_thread->sp); - } - } - } - - /* enable interrupt */ - rt_hw_interrupt_enable(level); + if (rt_interrupt_nest == 0) + { + rt_hw_context_switch((rt_uint32_t)&from_thread->sp, (rt_uint32_t)&to_thread->sp); + } + else + { + RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("switch in interrupt\n")); + + rt_hw_context_switch_interrupt((rt_uint32_t)&from_thread->sp, (rt_uint32_t)&to_thread->sp); + } + } + } + + /* enable interrupt */ + rt_hw_interrupt_enable(level); } /* @@ -309,38 +307,37 @@ void rt_schedule() * @param thread the thread to be inserted * @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; - RT_ASSERT(thread != RT_NULL); + RT_ASSERT(thread != RT_NULL); - /* disable interrupt */ - temp = rt_hw_interrupt_disable(); + /* disable interrupt */ + temp = rt_hw_interrupt_disable(); - /* change stat */ - thread->stat = RT_THREAD_READY; + /* change stat */ + thread->stat = RT_THREAD_READY; - /* insert thread to ready list */ - rt_list_insert_before(&(rt_thread_priority_table[thread->current_priority]), - &(thread->tlist)); + /* insert thread to ready list */ + rt_list_insert_before(&(rt_thread_priority_table[thread->current_priority]), &(thread->tlist)); - /* set priority mask */ + /* set priority mask */ #if RT_THREAD_PRIORITY_MAX <= 32 - RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("insert thread[%s], the priority: %d\n", + RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("insert thread[%s], the priority: %d\n", thread->name, thread->current_priority)); #else - RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("insert thread[%s], the priority: %d 0x%x %d\n", + RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("insert thread[%s], the priority: %d 0x%x %d\n", thread->name, thread->number, thread->number_mask, thread->high_mask)); #endif #if RT_THREAD_PRIORITY_MAX > 32 - rt_thread_ready_table[thread->number] |= thread->high_mask; + rt_thread_ready_table[thread->number] |= thread->high_mask; #endif - rt_thread_ready_priority_group |= thread->number_mask; + rt_thread_ready_priority_group |= thread->number_mask; - /* enable interrupt */ - rt_hw_interrupt_enable(temp); + /* enable interrupt */ + rt_hw_interrupt_enable(temp); } /* @@ -350,85 +347,85 @@ void rt_schedule_insert_thread(struct rt_thread* thread) * * @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; - RT_ASSERT(thread != RT_NULL); + RT_ASSERT(thread != RT_NULL); - /* disable interrupt */ - temp = rt_hw_interrupt_disable(); + /* disable interrupt */ + temp = rt_hw_interrupt_disable(); #if RT_THREAD_PRIORITY_MAX <= 32 - RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("remove thread[%s], the priority: %d\n", + RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("remove thread[%s], the priority: %d\n", thread->name, thread->current_priority)); #else - RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("remove thread[%s], the priority: %d 0x%x %d\n", + RT_DEBUG_LOG(RT_DEBUG_SCHEDULER, ("remove thread[%s], the priority: %d 0x%x %d\n", thread->name, thread->number, thread->number_mask, thread->high_mask)); #endif - /* remove thread from ready list */ - rt_list_remove(&(thread->tlist)); - if (rt_list_isempty(&(rt_thread_priority_table[thread->current_priority]))) - { + /* remove thread from ready list */ + rt_list_remove(&(thread->tlist)); + if (rt_list_isempty(&(rt_thread_priority_table[thread->current_priority]))) + { #if RT_THREAD_PRIORITY_MAX > 32 - rt_thread_ready_table[thread->number] &= ~thread->high_mask; - if (rt_thread_ready_table[thread->number] == 0) - { - rt_thread_ready_priority_group &= ~thread->number_mask; - } + rt_thread_ready_table[thread->number] &= ~thread->high_mask; + if (rt_thread_ready_table[thread->number] == 0) + { + rt_thread_ready_priority_group &= ~thread->number_mask; + } #else - rt_thread_ready_priority_group &= ~thread->number_mask; + rt_thread_ready_priority_group &= ~thread->number_mask; #endif - } + } - /* enable interrupt */ - rt_hw_interrupt_enable(temp); + /* enable interrupt */ + rt_hw_interrupt_enable(temp); } /** * 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; - /* disable interrupt */ - level = rt_hw_interrupt_disable(); + /* disable interrupt */ + level = rt_hw_interrupt_disable(); /* the maximal number of nest is RT_UINT16_MAX, which is big * enough and does not check here */ - rt_scheduler_lock_nest++; + rt_scheduler_lock_nest ++; - /* enable interrupt */ - rt_hw_interrupt_enable(level); + /* enable interrupt */ + rt_hw_interrupt_enable(level); } /** * This function will unlock the thread scheduler. */ -void rt_exit_critical() +void rt_exit_critical(void) { - register rt_base_t level; - - /* disable interrupt */ - level = rt_hw_interrupt_disable(); - - rt_scheduler_lock_nest --; - - if (rt_scheduler_lock_nest <= 0) - { - rt_scheduler_lock_nest = 0; - /* enable interrupt */ - rt_hw_interrupt_enable(level); - - rt_schedule(); - } - else - { - /* enable interrupt */ - rt_hw_interrupt_enable(level); - } + register rt_base_t level; + + /* disable interrupt */ + level = rt_hw_interrupt_disable(); + + rt_scheduler_lock_nest --; + + if (rt_scheduler_lock_nest <= 0) + { + rt_scheduler_lock_nest = 0; + /* enable interrupt */ + rt_hw_interrupt_enable(level); + + rt_schedule(); + } + else + { + /* enable interrupt */ + rt_hw_interrupt_enable(level); + } } /*@}*/ diff --git a/src/slab.c b/src/slab.c index ceb6e8a3b2..51cc7c3c9d 100644 --- a/src/slab.c +++ b/src/slab.c @@ -1,7 +1,7 @@ /* * File : slab.c * 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 * found in the file LICENSE in this distribution or at @@ -11,8 +11,8 @@ * Date Author Notes * 2008-07-12 Bernard the first version * 2010-07-13 Bernard fix RT_ALIGN issue found by kuronca - * 2010-10-23 yi.qiu add module memory allocator - * 2010-12-18 yi.qiu fix zone release bug + * 2010-10-23 yi.qiu add module memory allocator + * 2010-12-18 yi.qiu fix zone release bug */ /* @@ -158,36 +158,37 @@ void rt_free_sethook(void (*hook)(void *ptr)) */ typedef struct slab_chunk { - struct slab_chunk *c_next; + struct slab_chunk *c_next; } slab_chunk; /* * The IN-BAND zone header is placed at the beginning of each zone. */ -typedef struct slab_zone { - 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_nmax; /* maximum free chunks */ +typedef struct slab_zone +{ + 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_nmax; /* maximum free chunks */ - struct slab_zone *z_next; /* zoneary[] link if z_nfree non-zero */ - rt_uint8_t *z_baseptr; /* pointer to start of chunk array */ + struct slab_zone *z_next; /* zoneary[] link if z_nfree non-zero */ + rt_uint8_t *z_baseptr; /* pointer to start of chunk array */ - rt_int32_t z_uindex; /* current initial allocation index */ - rt_int32_t z_chunksize; /* chunk size for validation */ + rt_int32_t z_uindex; /* current initial allocation index */ + rt_int32_t z_chunksize; /* chunk size for validation */ - rt_int32_t z_zoneindex; /* zone index */ - slab_chunk *z_freechunk; /* free chunk list */ + rt_int32_t z_zoneindex; /* zone index */ + slab_chunk *z_freechunk; /* free chunk list */ } slab_zone; -#define ZALLOC_SLAB_MAGIC 0x51ab51ab -#define ZALLOC_ZONE_LIMIT (16 * 1024) /* max slab-managed alloc */ -#define ZALLOC_MIN_ZONE_SIZE (32 * 1024) /* minimum zone size */ -#define ZALLOC_MAX_ZONE_SIZE (128 * 1024) /* maximum zone size */ -#define NZONES 72 /* number of zones */ -#define ZONE_RELEASE_THRESH 2 /* threshold number of zones */ +#define ZALLOC_SLAB_MAGIC 0x51ab51ab +#define ZALLOC_ZONE_LIMIT (16 * 1024) /* max slab-managed alloc */ +#define ZALLOC_MIN_ZONE_SIZE (32 * 1024) /* minimum zone size */ +#define ZALLOC_MAX_ZONE_SIZE (128 * 1024) /* maximum zone size */ +#define NZONES 72 /* number of zones */ +#define ZONE_RELEASE_THRESH 2 /* threshold number of zones */ -static slab_zone *zone_array[NZONES]; /* linked list of zones NFree > 0 */ -static slab_zone *zone_free; /* whole zones that have become free */ +static slab_zone *zone_array[NZONES]; /* linked list of zones NFree > 0 */ +static slab_zone *zone_free; /* whole zones that have become free */ static int zone_free_cnt; static int zone_size; @@ -198,18 +199,19 @@ static int zone_page_cnt; * Misc constants. Note that allocations that are exact multiples of * RT_MM_PAGE_SIZE, or exceed the zone limit, fall through to the kmem module. */ -#define MIN_CHUNK_SIZE 8 /* in bytes */ -#define MIN_CHUNK_MASK (MIN_CHUNK_SIZE - 1) +#define MIN_CHUNK_SIZE 8 /* in bytes */ +#define MIN_CHUNK_MASK (MIN_CHUNK_SIZE - 1) /* * Array of descriptors that describe the contents of each page */ -#define PAGE_TYPE_FREE 0x00 -#define PAGE_TYPE_SMALL 0x01 -#define PAGE_TYPE_LARGE 0x02 -struct memusage { +#define PAGE_TYPE_FREE 0x00 +#define PAGE_TYPE_SMALL 0x01 +#define PAGE_TYPE_LARGE 0x02 +struct memusage +{ 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 */ }; static struct memusage *memusage = RT_NULL; #define btokup(addr) (&memusage[((rt_uint32_t)(addr) - heap_start) >> RT_MM_PAGE_BITS]) @@ -219,8 +221,8 @@ static rt_uint32_t heap_start, heap_end; /* page allocator */ struct rt_page_head { - struct rt_page_head *next; /* next valid page */ - rt_size_t page; /* number of page */ + struct rt_page_head *next; /* next valid page */ + rt_size_t page; /* number of page */ /* dummy */ char dummy[RT_MM_PAGE_SIZE - (sizeof(struct rt_page_head*) + sizeof (rt_size_t))]; @@ -317,7 +319,7 @@ _return: /* * 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(npages != 0); @@ -335,17 +337,18 @@ static void rt_page_init(void* addr, rt_size_t npages) * @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_DEBUG_NOT_IN_INTERRUPT; /* align begin and end addr to page */ - 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_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); - if(heap_start >= heap_end) { + if (heap_start >= heap_end) + { rt_kprintf("rt_system_heap_init, wrong address[0x%x - 0x%x]\n", (rt_uint32_t)begin_addr, (rt_uint32_t)end_addr); 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)); /* init pages */ - rt_page_init((void*)heap_start, npages); + rt_page_init((void *)heap_start, npages); /* calculate 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, ("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; #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 /* @@ -487,7 +489,7 @@ void *rt_malloc(rt_size_t size) kup->type = PAGE_TYPE_LARGE; 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 >> 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); RT_ASSERT(zi < NZONES); - RT_DEBUG_LOG(RT_DEBUG_SLAB, - ("try to malloc 0x%x on zone: %d\n", size, zi)); + RT_DEBUG_LOG(RT_DEBUG_SLAB, ("try to malloc 0x%x on zone: %d\n", size, zi)); if ((z = zone_array[zi]) != RT_NULL) { @@ -573,7 +574,7 @@ void *rt_malloc(rt_size_t size) { /* remove zone from free zone list */ zone_free = z->z_next; - --zone_free_cnt; + -- zone_free_cnt; } else { @@ -587,7 +588,7 @@ void *rt_malloc(rt_size_t size) /* lock heap */ 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 */ for (off = 0, kup = btokup(z); off < zone_page_cnt; off ++) @@ -614,13 +615,13 @@ void *rt_malloc(rt_size_t size) else off = (off + MIN_CHUNK_MASK) & ~MIN_CHUNK_MASK; - z->z_magic = ZALLOC_SLAB_MAGIC; - z->z_zoneindex = zi; - z->z_nmax = (zone_size - off) / size; - z->z_nfree = z->z_nmax - 1; - z->z_baseptr = (rt_uint8_t*)z + off; - z->z_uindex = 0; - z->z_chunksize = size; + z->z_magic = ZALLOC_SLAB_MAGIC; + z->z_zoneindex = zi; + z->z_nmax = (zone_size - off) / size; + z->z_nfree = z->z_nmax - 1; + z->z_baseptr = (rt_uint8_t *)z + off; + z->z_uindex = 0; + z->z_chunksize = size; chunk = (slab_chunk *)(z->z_baseptr + z->z_uindex * size); @@ -637,7 +638,7 @@ void *rt_malloc(rt_size_t size) done: 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; @@ -668,7 +669,7 @@ void *rt_realloc(void *ptr, rt_size_t size) } #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 /* @@ -682,14 +683,14 @@ void *rt_realloc(void *ptr, rt_size_t size) osize = kup->size << RT_MM_PAGE_BITS; 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); return nptr; } 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); 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; - 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); return nptr; @@ -802,10 +803,10 @@ void rt_free(void *ptr) rt_sem_take(&heap_sem, RT_WAITING_FOREVER); /* 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); - chunk = (slab_chunk*)ptr; + chunk = (slab_chunk *)ptr; chunk->c_next = z->z_freechunk; 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 * with kernel_map here. Hysteresis will be performed at malloc() time. */ - if (z->z_nfree == z->z_nmax && - (z->z_next || zone_array[z->z_zoneindex] != z)) + if (z->z_nfree == z->z_nmax && (z->z_next || zone_array[z->z_zoneindex] != z)) { slab_zone **pz; - RT_DEBUG_LOG(RT_DEBUG_SLAB, - ("free zone 0x%x\n", (rt_uint32_t)z, z->z_zoneindex)); + RT_DEBUG_LOG(RT_DEBUG_SLAB, ("free zone 0x%x\n", (rt_uint32_t)z, z->z_zoneindex)); /* 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; /* reset zone */ @@ -848,7 +847,7 @@ void rt_free(void *ptr) z->z_next = zone_free; zone_free = z; - ++zone_free_cnt; + ++ zone_free_cnt; /* release zone to page allocator */ if (zone_free_cnt > ZONE_RELEASE_THRESH) @@ -857,7 +856,7 @@ void rt_free(void *ptr) z = zone_free; zone_free = z->z_next; - --zone_free_cnt; + -- zone_free_cnt; /* set message usage */ for (i = 0, kup = btokup(z); i < zone_page_cnt; i ++) @@ -880,9 +879,7 @@ void rt_free(void *ptr) } #ifdef RT_MEM_STATS -void rt_memory_info(rt_uint32_t *total, - rt_uint32_t *used, - rt_uint32_t *max_used) +void rt_memory_info(rt_uint32_t *total, rt_uint32_t *used, rt_uint32_t *max_used) { if (total != RT_NULL) *total = heap_end - heap_start; if (used != RT_NULL) *used = used_mem; @@ -891,7 +888,7 @@ void rt_memory_info(rt_uint32_t *total, #ifdef RT_USING_FINSH #include -void list_mem() +void list_mem(void) { rt_kprintf("total memory: %d\n", heap_end - heap_start); rt_kprintf("used memory : %d\n", used_mem); diff --git a/src/thread.c b/src/thread.c index f7f0bffafe..92ea9d4595 100644 --- a/src/thread.c +++ b/src/thread.c @@ -1,7 +1,7 @@ /* * File : thread.c * 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 * found in the file LICENSE in this distribution or at @@ -29,25 +29,24 @@ #include #include "kservice.h" - 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_list_t rt_thread_defunct; 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, - const char* name, - void (*entry)(void* parameter), void* parameter, - void* stack_start, rt_uint32_t stack_size, +static rt_err_t _rt_thread_init(struct rt_thread *thread, + const char *name, + void (*entry)(void *parameter), void *parameter, + void *stack_start, rt_uint32_t stack_size, rt_uint8_t priority, rt_uint32_t tick) { /* init thread list */ rt_list_init(&(thread->tlist)); - thread->entry = (void*)entry; + thread->entry = (void *)entry; thread->parameter = parameter; /* stack init */ @@ -56,9 +55,9 @@ static rt_err_t _rt_thread_init(struct rt_thread* thread, /* init thread stack */ 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*)rt_thread_exit); + (void *)rt_thread_exit); /* priority init */ 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 * */ -rt_err_t rt_thread_init(struct rt_thread* thread, - const char* name, - void (*entry)(void* parameter), void* parameter, - void* stack_start, rt_uint32_t stack_size, +rt_err_t rt_thread_init(struct rt_thread *thread, + const char *name, + void (*entry)(void *parameter), void *parameter, + void *stack_start, rt_uint32_t stack_size, rt_uint8_t priority, rt_uint32_t tick) { /* thread check */ @@ -143,19 +142,19 @@ rt_err_t rt_thread_init(struct rt_thread* thread, * @return the created thread object * */ -rt_thread_t rt_thread_create (const char* name, - void (*entry)(void* parameter), void* parameter, +rt_thread_t rt_thread_create (const char *name, + void (*entry)(void *parameter), void *parameter, rt_uint32_t stack_size, rt_uint8_t priority, rt_uint32_t tick) { - struct rt_thread* thread; - void* stack_start; + struct rt_thread *thread; + 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; - stack_start = (void*)rt_malloc(stack_size); + stack_start = (void *)rt_malloc(stack_size); if (stack_start == RT_NULL) { /* allocate stack failure */ @@ -177,7 +176,7 @@ rt_thread_t rt_thread_create (const char* name, * @return the self thread object * */ -rt_thread_t rt_thread_self (void) +rt_thread_t rt_thread_self(void) { 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 * */ -rt_err_t rt_thread_startup (rt_thread_t thread) +rt_err_t rt_thread_startup(rt_thread_t thread) { /* thread check */ RT_ASSERT(thread != RT_NULL); @@ -201,11 +200,11 @@ rt_err_t rt_thread_startup (rt_thread_t thread) /* calculate priority attribute */ #if RT_THREAD_PRIORITY_MAX > 32 - thread->number = thread->current_priority >> 3; /* 5bit */ - thread->number_mask = 1L << thread->number; - thread->high_mask = 1L << (thread->current_priority & 0x07); /* 3bit */ + thread->number = thread->current_priority >> 3; /* 5bit */ + thread->number_mask = 1L << thread->number; + thread->high_mask = 1L << (thread->current_priority & 0x07); /* 3bit */ #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 RT_DEBUG_LOG(RT_DEBUG_THREAD,\ @@ -223,9 +222,9 @@ rt_err_t rt_thread_startup (rt_thread_t thread) 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; /* get current thread */ @@ -270,7 +269,7 @@ static void rt_thread_exit() * @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; @@ -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 * */ -rt_err_t rt_thread_delete (rt_thread_t thread) +rt_err_t rt_thread_delete(rt_thread_t thread) { rt_base_t lock; @@ -351,7 +350,7 @@ rt_err_t rt_thread_delete (rt_thread_t thread) * @return RT_EOK * */ -rt_err_t rt_thread_yield () +rt_err_t rt_thread_yield(void) { register rt_base_t level; struct rt_thread *thread; @@ -394,7 +393,7 @@ rt_err_t rt_thread_yield () * @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; struct rt_thread *thread; @@ -449,7 +448,7 @@ rt_err_t rt_thread_delay(rt_tick_t tick) * * @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; @@ -469,13 +468,13 @@ rt_err_t rt_thread_control (rt_thread_t thread, rt_uint8_t cmd, void* arg) rt_schedule_remove_thread(thread); /* change thread priority */ - thread->current_priority = *(rt_uint8_t*) arg; + thread->current_priority = *(rt_uint8_t *)arg; /* recalculate priority attribute */ #if RT_THREAD_PRIORITY_MAX > 32 - thread->number = thread->current_priority >> 3; /* 5bit */ - thread->number_mask = 1 << thread->number; - thread->high_mask = 1 << (thread->current_priority & 0x07); /* 3bit */ + thread->number = thread->current_priority >> 3; /* 5bit */ + thread->number_mask = 1 << thread->number; + thread->high_mask = 1 << (thread->current_priority & 0x07); /* 3bit */ #else thread->number_mask = 1 << thread->current_priority; #endif @@ -485,13 +484,13 @@ rt_err_t rt_thread_control (rt_thread_t thread, rt_uint8_t cmd, void* arg) } else { - thread->current_priority = *(rt_uint8_t*) arg; + thread->current_priority = *(rt_uint8_t *)arg; /* recalculate priority attribute */ #if RT_THREAD_PRIORITY_MAX > 32 - thread->number = thread->current_priority >> 3; /* 5bit */ - thread->number_mask = 1 << thread->number; - thread->high_mask = 1 << (thread->current_priority & 0x07); /* 3bit */ + thread->number = thread->current_priority >> 3; /* 5bit */ + thread->number_mask = 1 << thread->number; + thread->high_mask = 1 << (thread->current_priority & 0x07); /* 3bit */ #else thread->number_mask = 1 << thread->current_priority; #endif @@ -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 * 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; @@ -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 * */ -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; @@ -609,11 +608,11 @@ rt_err_t rt_thread_resume (rt_thread_t thread) * @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 */ 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. */ -rt_thread_t rt_thread_find(char* name) +rt_thread_t rt_thread_find(char *name) { struct rt_object_information *information; - struct rt_object* object; - struct rt_list_node* node; + struct rt_object *object; + struct rt_list_node *node; extern struct rt_object_information rt_object_container[]; diff --git a/src/timer.c b/src/timer.c index b2e1c1e13a..3b812094af 100644 --- a/src/timer.c +++ b/src/timer.c @@ -1,7 +1,7 @@ /* * File : timer.c * 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 * found in the file LICENSE in this distribution or at @@ -33,9 +33,9 @@ static rt_list_t rt_soft_timer_list; #endif #ifdef RT_USING_HOOK -extern void (*rt_object_take_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); +extern void (*rt_object_take_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); /** * @addtogroup Hook @@ -48,7 +48,7 @@ static void (*rt_timer_timeout_hook)(struct rt_timer* timer); * * @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; } @@ -57,20 +57,20 @@ void rt_timer_timeout_sethook(void (*hook)(struct rt_timer* timer)) #endif 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) { /* set flag */ - timer->parent.flag = flag; + timer->parent.flag = flag; /* set deactivated */ timer->parent.flag &= ~RT_TIMER_FLAG_ACTIVATED; timer->timeout_func = timeout; - timer->parameter = parameter; + timer->parameter = parameter; - timer->timeout_tick = 0; - timer->init_tick = time; + timer->timeout_tick = 0; + timer->init_tick = time; /* initialize timer list */ rt_list_init(&(timer->list)); @@ -93,8 +93,8 @@ static void _rt_timer_init(rt_timer_t timer, * @param flag the flag of timer */ void rt_timer_init(rt_timer_t timer, - const char* name, - void (*timeout)(void* parameter), void* parameter, + const char *name, + void (*timeout)(void *parameter), void *parameter, rt_tick_t time, rt_uint8_t flag) { /* timer check */ @@ -146,12 +146,12 @@ rt_err_t rt_timer_detach(rt_timer_t timer) * * @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 */ - 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) { 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) { - struct rt_timer* t; + struct rt_timer *t; register rt_base_t level; rt_list_t *n, *timer_list; @@ -273,7 +273,7 @@ rt_err_t rt_timer_stop(rt_timer_t timer) /* timer check */ 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))); @@ -302,7 +302,7 @@ rt_err_t rt_timer_stop(rt_timer_t timer) * @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 */ 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) { case RT_TIMER_CTRL_GET_TIME: - *(rt_tick_t*)arg = timer->init_tick; + *(rt_tick_t *)arg = timer->init_tick; break; case RT_TIMER_CTRL_SET_TIME: - timer->init_tick = *(rt_tick_t*)arg; + timer->init_tick = *(rt_tick_t *)arg; break; 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. */ #ifdef RT_USING_TIMER_SOFT -void rt_soft_timer_tick_increase (void); +void rt_soft_timer_tick_increase(void); #endif void rt_timer_check(void) { @@ -344,7 +344,7 @@ void rt_timer_check(void) rt_tick_t current_tick; 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(); @@ -354,7 +354,7 @@ void rt_timer_check(void) while (!rt_list_isempty(&rt_timer_list)) { t = rt_list_entry(rt_timer_list.next, struct rt_timer, list); - + /* * It supposes that the new tick shall less than the half duration of tick max. */ @@ -371,7 +371,7 @@ void rt_timer_check(void) /* re-get tick */ 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) && (t->parent.flag & RT_TIMER_FLAG_ACTIVATED)) @@ -394,11 +394,10 @@ void rt_timer_check(void) /* increase soft timer tick */ #ifdef RT_USING_TIMER_SOFT - rt_soft_timer_tick_increase ( ); + rt_soft_timer_tick_increase(); #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 @@ -409,9 +408,9 @@ static struct rt_semaphore timer_sem; 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)) { timer_ex_cnt = 0; @@ -424,20 +423,20 @@ void rt_soft_timer_tick_increase (void) * corresponding timeout function will be invoked. * */ -void rt_soft_timer_check() +void rt_soft_timer_check(void) { rt_tick_t current_tick; rt_list_t *n; 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(); - 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); - + /* * It supposes that the new tick shall less than the half duration of tick max. */ @@ -457,7 +456,7 @@ void rt_soft_timer_check() /* re-get tick */ 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) && (t->parent.flag & RT_TIMER_FLAG_ACTIVATED)) @@ -475,25 +474,24 @@ void rt_soft_timer_check() 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 */ -static void rt_thread_timer_entry(void* parameter) +static void rt_thread_timer_entry(void *parameter) { while (1) { /* 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(); - /* check software timer */ + /* check software timer */ rt_soft_timer_check(); - /* unlock scheduler */ + /* unlock scheduler */ rt_exit_critical(); } } @@ -505,13 +503,13 @@ static void rt_thread_timer_entry(void* parameter) * This function will initialize system timer * */ -void rt_system_timer_init() +void rt_system_timer_init(void) { rt_list_init(&rt_timer_list); #ifdef RT_USING_TIMER_SOFT rt_list_init(&rt_soft_timer_list); - rt_sem_init(&timer_sem, "timer", 0, RT_IPC_FLAG_FIFO); + rt_sem_init(&timer_sem, "timer", 0, RT_IPC_FLAG_FIFO); #endif } @@ -521,10 +519,10 @@ void rt_system_timer_init() * 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 - /* start software timer thread */ + /* start software timer thread */ rt_thread_init(&timer_thread, "timer", rt_thread_timer_entry, RT_NULL, -- GitLab