1。代码优化。
This commit is contained in:
@@ -20,19 +20,18 @@ static struct mr_dev root_dev =
|
||||
{&root_dev.clist, &root_dev.clist}
|
||||
};
|
||||
|
||||
static int dev_is_root(struct mr_dev *dev)
|
||||
MR_INLINE int dev_is_root(struct mr_dev *dev)
|
||||
{
|
||||
return (int)dev->type == Mr_Dev_Type_Root;
|
||||
}
|
||||
|
||||
static struct mr_dev *dev_find_child(struct mr_dev *parent, const char *name)
|
||||
MR_INLINE struct mr_dev *dev_find_child(struct mr_dev *parent, const char *name)
|
||||
{
|
||||
/* Disable interrupt */
|
||||
mr_interrupt_disable();
|
||||
|
||||
/* Find the child device */
|
||||
struct mr_list *list = MR_NULL;
|
||||
for (list = parent->clist.next; list != &parent->clist; list = list->next)
|
||||
for (struct mr_list *list = parent->clist.next; list != &parent->clist; list = list->next)
|
||||
{
|
||||
struct mr_dev *dev = (struct mr_dev *)mr_container_of(list, struct mr_dev, list);
|
||||
if (strncmp(name, dev->name, MR_CFG_NAME_MAX) == 0)
|
||||
@@ -48,7 +47,7 @@ static struct mr_dev *dev_find_child(struct mr_dev *parent, const char *name)
|
||||
return MR_NULL;
|
||||
}
|
||||
|
||||
static int dev_register_child(struct mr_dev *parent, struct mr_dev *child, const char *name)
|
||||
MR_INLINE int dev_register_child(struct mr_dev *parent, struct mr_dev *child, const char *name)
|
||||
{
|
||||
/* Check whether the device with the same name exists */
|
||||
if (dev_find_child(parent, name) != MR_NULL)
|
||||
@@ -70,7 +69,7 @@ static int dev_register_child(struct mr_dev *parent, struct mr_dev *child, const
|
||||
return MR_EOK;
|
||||
}
|
||||
|
||||
static const char *dev_clear_path(const char *path)
|
||||
MR_INLINE const char *dev_clear_path(const char *path)
|
||||
{
|
||||
/* Skip the leading '/' */
|
||||
if (*path == '/')
|
||||
@@ -86,7 +85,7 @@ static const char *dev_clear_path(const char *path)
|
||||
return path;
|
||||
}
|
||||
|
||||
static int dev_register_by_path(struct mr_dev *parent, struct mr_dev *dev, const char *path)
|
||||
MR_INLINE int dev_register_by_path(struct mr_dev *parent, struct mr_dev *dev, const char *path)
|
||||
{
|
||||
char parent_name[MR_CFG_NAME_MAX + 1] = {0};
|
||||
char *parent_path = MR_NULL;
|
||||
@@ -119,7 +118,7 @@ static int dev_register_by_path(struct mr_dev *parent, struct mr_dev *dev, const
|
||||
}
|
||||
}
|
||||
|
||||
static struct mr_dev *dev_find_by_path(struct mr_dev *parent, const char *path)
|
||||
MR_INLINE struct mr_dev *dev_find_by_path(struct mr_dev *parent, const char *path)
|
||||
{
|
||||
char parent_name[MR_CFG_NAME_MAX + 1] = {0};
|
||||
char *parent_path = MR_NULL;
|
||||
@@ -149,7 +148,7 @@ static struct mr_dev *dev_find_by_path(struct mr_dev *parent, const char *path)
|
||||
}
|
||||
|
||||
#ifdef MR_USING_RDWR_CTL
|
||||
static int dev_lock_take(struct mr_dev *dev, int take, int set)
|
||||
MR_INLINE int dev_lock_take(struct mr_dev *dev, int take, int set)
|
||||
{
|
||||
/* Continue iterating until reach the root device */
|
||||
if (dev_is_root(dev->parent) != MR_TRUE)
|
||||
@@ -172,7 +171,7 @@ static int dev_lock_take(struct mr_dev *dev, int take, int set)
|
||||
return MR_EOK;
|
||||
}
|
||||
|
||||
static void dev_lock_release(struct mr_dev *dev, int release)
|
||||
MR_INLINE void dev_lock_release(struct mr_dev *dev, int release)
|
||||
{
|
||||
/* Continue iterating until reach the root device */
|
||||
if (dev_is_root(dev->parent) != MR_TRUE)
|
||||
@@ -185,19 +184,19 @@ static void dev_lock_release(struct mr_dev *dev, int release)
|
||||
}
|
||||
#endif /* MR_USING_RDWR_CTL */
|
||||
|
||||
static int dev_register(struct mr_dev *dev, const char *path)
|
||||
MR_INLINE int dev_register(struct mr_dev *dev, const char *path)
|
||||
{
|
||||
/* Register the device to the root device */
|
||||
return dev_register_by_path(&root_dev, dev, path);
|
||||
}
|
||||
|
||||
static struct mr_dev *dev_find(const char *path)
|
||||
MR_INLINE struct mr_dev *dev_find(const char *path)
|
||||
{
|
||||
/* Find the device from the root device */
|
||||
return dev_find_by_path(&root_dev, path);
|
||||
}
|
||||
|
||||
static int dev_open(struct mr_dev *dev, int oflags)
|
||||
MR_INLINE int dev_open(struct mr_dev *dev, int oflags)
|
||||
{
|
||||
#ifdef MR_USING_RDWR_CTL
|
||||
if (mr_bits_is_set(dev->sflags, oflags) != MR_ENABLE)
|
||||
@@ -241,7 +240,7 @@ static int dev_open(struct mr_dev *dev, int oflags)
|
||||
return MR_EOK;
|
||||
}
|
||||
|
||||
static int dev_close(struct mr_dev *dev)
|
||||
MR_INLINE int dev_close(struct mr_dev *dev)
|
||||
{
|
||||
/* Decrease the reference count */
|
||||
dev->ref_count--;
|
||||
@@ -268,7 +267,7 @@ static int dev_close(struct mr_dev *dev)
|
||||
return MR_EOK;
|
||||
}
|
||||
|
||||
static ssize_t dev_read(struct mr_dev *dev, int off, void *buf, size_t size, int async)
|
||||
MR_INLINE ssize_t dev_read(struct mr_dev *dev, int off, void *buf, size_t size, int async)
|
||||
{
|
||||
#ifdef MR_USING_RDWR_CTL
|
||||
do
|
||||
@@ -296,7 +295,7 @@ static ssize_t dev_read(struct mr_dev *dev, int off, void *buf, size_t size, int
|
||||
return ret;
|
||||
}
|
||||
|
||||
static ssize_t dev_write(struct mr_dev *dev, int offset, const void *buf, size_t size, int async)
|
||||
MR_INLINE ssize_t dev_write(struct mr_dev *dev, int offset, const void *buf, size_t size, int async)
|
||||
{
|
||||
#ifdef MR_USING_RDWR_CTL
|
||||
do
|
||||
@@ -334,7 +333,7 @@ static ssize_t dev_write(struct mr_dev *dev, int offset, const void *buf, size_t
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int dev_ioctl(struct mr_dev *dev, int desc, int off, int cmd, void *args)
|
||||
MR_INLINE int dev_ioctl(struct mr_dev *dev, int desc, int off, int cmd, void *args)
|
||||
{
|
||||
/* Check whether the device has an ioctl function */
|
||||
if (dev->ops->ioctl == MR_NULL)
|
||||
@@ -579,14 +578,13 @@ static struct mr_desc
|
||||
static int desc_allocate(const char *path)
|
||||
{
|
||||
int desc = -1;
|
||||
int i = 0;
|
||||
|
||||
/* Find a free descriptor */
|
||||
for (i = 0; i < MR_CFG_DESC_MAX; i++)
|
||||
for (size_t i = 0; i < MR_CFG_DESC_MAX; i++)
|
||||
{
|
||||
if (desc_of(i).dev == MR_NULL)
|
||||
{
|
||||
desc = i;
|
||||
desc = (int)i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -721,7 +719,6 @@ ssize_t mr_dev_write(int desc, const void *buf, size_t size)
|
||||
#ifdef MR_USING_RDWR_CTL
|
||||
if (mr_bits_is_set(desc_of(desc).oflags, MR_OFLAG_WRONLY) == MR_DISABLE)
|
||||
{
|
||||
|
||||
return MR_ENOTSUP;
|
||||
}
|
||||
#endif /* MR_USING_RDWR_CTL */
|
||||
@@ -771,6 +768,7 @@ int mr_dev_ioctl(int desc, int cmd, void *args)
|
||||
|
||||
default:
|
||||
{
|
||||
/* I/O control to the device */
|
||||
return dev_ioctl(desc_of(desc).dev, desc, desc_of(desc).offset, cmd, args);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -102,8 +102,6 @@ MR_WEAK void *mr_malloc(size_t size)
|
||||
{
|
||||
struct mr_heap_block *block_prev = &heap_start;
|
||||
struct mr_heap_block *block = block_prev->next;
|
||||
void *memory = MR_NULL;
|
||||
size_t residual = 0;
|
||||
|
||||
/* Disable interrupt */
|
||||
mr_interrupt_disable();
|
||||
@@ -134,8 +132,8 @@ MR_WEAK void *mr_malloc(size_t size)
|
||||
block_prev->next = block->next;
|
||||
|
||||
/* Allocate memory */
|
||||
memory = (void *)((uint8_t *)block + sizeof(struct mr_heap_block));
|
||||
residual = block->size - size;
|
||||
void *memory = (void *)((uint8_t *)block + sizeof(struct mr_heap_block));
|
||||
size_t residual = block->size - size;
|
||||
|
||||
/* Set the block information */
|
||||
block->size = size;
|
||||
@@ -158,7 +156,6 @@ MR_WEAK void *mr_malloc(size_t size)
|
||||
|
||||
/* Enable interrupt */
|
||||
mr_interrupt_enable();
|
||||
|
||||
return memory;
|
||||
}
|
||||
|
||||
@@ -201,6 +198,7 @@ MR_WEAK size_t mr_malloc_usable_size(void *memory)
|
||||
{
|
||||
if (memory != MR_NULL)
|
||||
{
|
||||
/* Get the block information */
|
||||
struct mr_heap_block *block = (struct mr_heap_block *)((uint8_t *)memory - sizeof(struct mr_heap_block));
|
||||
return block->size;
|
||||
}
|
||||
@@ -218,9 +216,8 @@ MR_WEAK size_t mr_malloc_usable_size(void *memory)
|
||||
MR_WEAK void *mr_calloc(size_t num, size_t size)
|
||||
{
|
||||
size_t total = num * size;
|
||||
void *memory = MR_NULL;
|
||||
|
||||
memory = mr_malloc(total);
|
||||
void *memory = mr_malloc(total);
|
||||
if (memory != MR_NULL)
|
||||
{
|
||||
memset(memory, 0, total);
|
||||
@@ -239,9 +236,8 @@ MR_WEAK void *mr_calloc(size_t num, size_t size)
|
||||
MR_WEAK void *mr_realloc(void *memory, size_t size)
|
||||
{
|
||||
size_t old_size = mr_malloc_usable_size(memory);
|
||||
void *new_memory = MR_NULL;
|
||||
|
||||
new_memory = mr_malloc(size);
|
||||
void *new_memory = mr_malloc(size);
|
||||
if (new_memory != MR_NULL)
|
||||
{
|
||||
memcpy(new_memory, memory, old_size);
|
||||
|
||||
@@ -27,10 +27,8 @@ MR_INIT_EXPORT(end, "5.end");
|
||||
*/
|
||||
void mr_auto_init(void)
|
||||
{
|
||||
volatile const mr_init_fn_t *fn = MR_NULL;
|
||||
|
||||
/* Auto-initialization */
|
||||
for (fn = &_mr_auto_init_start; fn < &_mr_auto_init_end; fn++)
|
||||
for (volatile const mr_init_fn_t *fn = &_mr_auto_init_start; fn < &_mr_auto_init_end; fn++)
|
||||
{
|
||||
(*fn)();
|
||||
}
|
||||
@@ -59,8 +57,6 @@ MR_WEAK void mr_interrupt_enable(void)
|
||||
*/
|
||||
MR_WEAK void mr_delay_us(uint32_t us)
|
||||
{
|
||||
volatile uint32_t i = 0;
|
||||
|
||||
#ifndef MR_CFG_SYSCLK_FREQ
|
||||
#define MR_CFG_SYSCLK_FREQ (72000000)
|
||||
#endif /* MR_CFG_SYSCLK_FREQ */
|
||||
@@ -69,7 +65,7 @@ MR_WEAK void mr_delay_us(uint32_t us)
|
||||
#else
|
||||
#define MR_DELAY_COUNT (1)
|
||||
#endif /* (MR_CFG_SYSCLK_FREQ > 1000000) */
|
||||
for (i = 0; i < us * MR_DELAY_COUNT; i++)
|
||||
for (volatile uint32_t i = 0; i < us * MR_DELAY_COUNT; i++)
|
||||
{
|
||||
__asm__("nop");
|
||||
}
|
||||
@@ -84,9 +80,7 @@ MR_WEAK void mr_delay_us(uint32_t us)
|
||||
*/
|
||||
MR_WEAK void mr_delay_ms(uint32_t ms)
|
||||
{
|
||||
volatile uint32_t i = 0;
|
||||
|
||||
for (i = 0; i < ms; i++)
|
||||
for (volatile uint32_t i = 0; i < ms; i++)
|
||||
{
|
||||
mr_delay_us(1000);
|
||||
}
|
||||
@@ -209,8 +203,6 @@ void mr_ringbuf_init(struct mr_ringbuf *ringbuf, void *pool, size_t size)
|
||||
*/
|
||||
int mr_ringbuf_allocate(struct mr_ringbuf *ringbuf, size_t size)
|
||||
{
|
||||
void *pool = MR_NULL;
|
||||
|
||||
mr_assert(ringbuf != MR_NULL);
|
||||
|
||||
/* Check the buffer size */
|
||||
@@ -227,7 +219,7 @@ int mr_ringbuf_allocate(struct mr_ringbuf *ringbuf, size_t size)
|
||||
}
|
||||
|
||||
/* Allocate new buffer */
|
||||
pool = mr_malloc(size);
|
||||
void *pool = mr_malloc(size);
|
||||
if (pool == MR_NULL && size != 0)
|
||||
{
|
||||
return MR_ENOMEM;
|
||||
@@ -261,7 +253,6 @@ void mr_ringbuf_reset(struct mr_ringbuf *ringbuf)
|
||||
|
||||
ringbuf->read_index = 0;
|
||||
ringbuf->write_index = 0;
|
||||
|
||||
ringbuf->read_mirror = 0;
|
||||
ringbuf->write_mirror = 0;
|
||||
}
|
||||
@@ -370,13 +361,12 @@ size_t mr_ringbuf_pop(struct mr_ringbuf *ringbuf, uint8_t *data)
|
||||
size_t mr_ringbuf_read(struct mr_ringbuf *ringbuf, void *buffer, size_t size)
|
||||
{
|
||||
uint8_t *read_buffer = (uint8_t *)buffer;
|
||||
size_t data_size = 0;
|
||||
|
||||
mr_assert(ringbuf != MR_NULL);
|
||||
mr_assert((buffer != MR_NULL) || (size == 0));
|
||||
|
||||
/* Get the buf size */
|
||||
data_size = mr_ringbuf_get_data_size(ringbuf);
|
||||
size_t data_size = mr_ringbuf_get_data_size(ringbuf);
|
||||
if (data_size == 0)
|
||||
{
|
||||
return 0;
|
||||
@@ -498,13 +488,12 @@ size_t mr_ringbuf_push_force(struct mr_ringbuf *ringbuf, uint8_t data)
|
||||
size_t mr_ringbuf_write(struct mr_ringbuf *ringbuf, const void *buffer, size_t size)
|
||||
{
|
||||
uint8_t *write_buffer = (uint8_t *)buffer;
|
||||
size_t space_size = 0;
|
||||
|
||||
mr_assert(ringbuf != MR_NULL);
|
||||
mr_assert((buffer != MR_NULL) || (size == 0));
|
||||
|
||||
/* Get the space size */
|
||||
space_size = mr_ringbuf_get_space_size(ringbuf);
|
||||
size_t space_size = mr_ringbuf_get_space_size(ringbuf);
|
||||
if (space_size == 0)
|
||||
{
|
||||
return 0;
|
||||
@@ -547,7 +536,6 @@ size_t mr_ringbuf_write(struct mr_ringbuf *ringbuf, const void *buffer, size_t s
|
||||
size_t mr_ringbuf_write_force(struct mr_ringbuf *ringbuf, const void *buffer, size_t size)
|
||||
{
|
||||
uint8_t *write_buffer = (uint8_t *)buffer;
|
||||
size_t space_size = 0;
|
||||
|
||||
mr_assert(ringbuf != MR_NULL);
|
||||
mr_assert((buffer != MR_NULL) || (size == 0));
|
||||
@@ -558,7 +546,7 @@ size_t mr_ringbuf_write_force(struct mr_ringbuf *ringbuf, const void *buffer, si
|
||||
}
|
||||
|
||||
/* Get the space size */
|
||||
space_size = mr_ringbuf_get_space_size(ringbuf);
|
||||
size_t space_size = mr_ringbuf_get_space_size(ringbuf);
|
||||
|
||||
/* If the buf exceeds the buffer space_size, the front buf is discarded */
|
||||
if (size > ringbuf->size)
|
||||
@@ -626,8 +614,8 @@ static void mr_avl_left_rotate(struct mr_avl **node)
|
||||
right_child->left_child = (*node);
|
||||
|
||||
(*node)->height = mr_max(mr_avl_get_height((*node)->left_child), mr_avl_get_height((*node)->right_child)) + 1;
|
||||
right_child->height =
|
||||
mr_max(mr_avl_get_height(right_child->left_child), mr_avl_get_height(right_child->right_child)) + 1;
|
||||
right_child->height = mr_max(mr_avl_get_height(right_child->left_child),
|
||||
mr_avl_get_height(right_child->right_child)) + 1;
|
||||
|
||||
(*node) = right_child;
|
||||
}
|
||||
@@ -640,8 +628,8 @@ static void mr_avl_right_rotate(struct mr_avl **node)
|
||||
left_child->right_child = (*node);
|
||||
|
||||
(*node)->height = mr_max(mr_avl_get_height((*node)->left_child), mr_avl_get_height((*node)->right_child)) + 1;
|
||||
left_child->height =
|
||||
mr_max(mr_avl_get_height(left_child->left_child), mr_avl_get_height(left_child->right_child)) + 1;
|
||||
left_child->height = mr_max(mr_avl_get_height(left_child->left_child),
|
||||
mr_avl_get_height(left_child->right_child)) + 1;
|
||||
|
||||
(*node) = left_child;
|
||||
}
|
||||
@@ -670,8 +658,6 @@ void mr_avl_init(struct mr_avl *node, uint32_t value)
|
||||
*/
|
||||
void mr_avl_insert(struct mr_avl **tree, struct mr_avl *node)
|
||||
{
|
||||
int balance = 0;
|
||||
|
||||
mr_assert(tree != MR_NULL);
|
||||
mr_assert(node != MR_NULL);
|
||||
|
||||
@@ -693,7 +679,7 @@ void mr_avl_insert(struct mr_avl **tree, struct mr_avl *node)
|
||||
|
||||
(*tree)->height = mr_max(mr_avl_get_height((*tree)->left_child), mr_avl_get_height((*tree)->right_child)) + 1;
|
||||
|
||||
balance = mr_avl_get_balance((*tree));
|
||||
int balance = mr_avl_get_balance((*tree));
|
||||
if (balance > 1 && node->value < (*tree)->left_child->value)
|
||||
{
|
||||
mr_avl_right_rotate(&(*tree));
|
||||
|
||||
Reference in New Issue
Block a user