From 1aab195a7dd1557eb02e403f4667933cc76d4347 Mon Sep 17 00:00:00 2001 From: "dzzxzz@gmail.com" Date: Fri, 2 Nov 2012 04:05:38 +0000 Subject: fixed coding style in component/drivers/src git-svn-id: https://rt-thread.googlecode.com/svn/trunk@2384 bbd45198-f89e-11dd-88c7-29a3b14d5316 --- components/drivers/src/ringbuffer.c | 191 +++++++++++++++++++----------------- 1 file changed, 99 insertions(+), 92 deletions(-) (limited to 'components/drivers/src/ringbuffer.c') diff --git a/components/drivers/src/ringbuffer.c b/components/drivers/src/ringbuffer.c index 0b1bee1906..6223ba6935 100644 --- a/components/drivers/src/ringbuffer.c +++ b/components/drivers/src/ringbuffer.c @@ -16,51 +16,56 @@ #include #include -void rt_ringbuffer_init(struct rt_ringbuffer* rb, rt_uint8_t *pool, rt_uint16_t size) +void rt_ringbuffer_init(struct rt_ringbuffer *rb, + rt_uint8_t *pool, + rt_uint16_t size) { - RT_ASSERT(rb != RT_NULL); + RT_ASSERT(rb != RT_NULL); - /* initialize read and write index */ - rb->read_index = rb->write_index = 0; + /* initialize read and write index */ + rb->read_index = rb->write_index = 0; - /* set buffer pool and size */ - rb->buffer_ptr = pool; - rb->buffer_size = RT_ALIGN_DOWN(size, RT_ALIGN_SIZE); + /* set buffer pool and size */ + rb->buffer_ptr = pool; + rb->buffer_size = RT_ALIGN_DOWN(size, RT_ALIGN_SIZE); } RTM_EXPORT(rt_ringbuffer_init); -rt_size_t rt_ringbuffer_put(struct rt_ringbuffer* rb, const rt_uint8_t *ptr, rt_uint16_t length) +rt_size_t rt_ringbuffer_put(struct rt_ringbuffer *rb, + const rt_uint8_t *ptr, + rt_uint16_t length) { - rt_uint16_t size; - rt_uint16_t mask; - rt_uint16_t write_position; - - RT_ASSERT(rb != RT_NULL); - - mask = rb->buffer_size - 1; - /* whether has enough space */ - size = rb->buffer_size - (rb->write_index - rb->read_index); - - /* no space */ - if (size == 0) return 0; - /* drop some data */ - if (size < length) length = size; - - write_position = (rb->write_index & mask); - if (rb->buffer_size - write_position> length) - { - /* read_index - write_index = empty space */ - memcpy(&rb->buffer_ptr[write_position], ptr, length); - } - else - { - memcpy(&rb->buffer_ptr[write_position], ptr, rb->buffer_size - write_position); - memcpy(&rb->buffer_ptr[0], &ptr[rb->buffer_size - write_position], - length - (rb->buffer_size - write_position)); - } - rb->write_index += length; - - return length; + rt_uint16_t size; + rt_uint16_t mask; + rt_uint16_t write_position; + + RT_ASSERT(rb != RT_NULL); + + mask = rb->buffer_size - 1; + /* whether has enough space */ + size = rb->buffer_size - (rb->write_index - rb->read_index); + + /* no space */ + if (size == 0) return 0; + /* drop some data */ + if (size < length) length = size; + + write_position = (rb->write_index & mask); + if (rb->buffer_size - write_position> length) + { + /* read_index - write_index = empty space */ + memcpy(&rb->buffer_ptr[write_position], ptr, length); + } + else + { + memcpy(&rb->buffer_ptr[write_position], ptr, + rb->buffer_size - write_position); + memcpy(&rb->buffer_ptr[0], &ptr[rb->buffer_size - write_position], + length - (rb->buffer_size - write_position)); + } + rb->write_index += length; + + return length; } RTM_EXPORT(rt_ringbuffer_put); @@ -69,80 +74,82 @@ RTM_EXPORT(rt_ringbuffer_put); */ rt_size_t rt_ringbuffer_putchar(struct rt_ringbuffer* rb, const rt_uint8_t ch) { - rt_uint16_t mask; + rt_uint16_t mask; - RT_ASSERT(rb != RT_NULL); - /* whether has enough space */ - mask = rb->buffer_size - 1; - - /* whether has enough space */ - if (rb->write_index - rb->read_index == rb->buffer_size) return 0; + RT_ASSERT(rb != RT_NULL); + /* whether has enough space */ + mask = rb->buffer_size - 1; + + /* whether has enough space */ + if (rb->write_index - rb->read_index == rb->buffer_size) return 0; - /* put character */ - rb->buffer_ptr[rb->write_index & mask] = ch; - rb->write_index += 1; + /* put character */ + rb->buffer_ptr[rb->write_index & mask] = ch; + rb->write_index += 1; - return 1; + return 1; } RTM_EXPORT(rt_ringbuffer_putchar); /** * get data from ring buffer */ -rt_size_t rt_ringbuffer_get(struct rt_ringbuffer* rb, rt_uint8_t *ptr, rt_uint16_t length) +rt_size_t rt_ringbuffer_get(struct rt_ringbuffer *rb, + rt_uint8_t *ptr, + rt_uint16_t length) { - rt_size_t size; - rt_uint16_t mask; - rt_uint16_t read_position; - - RT_ASSERT(rb != RT_NULL); - /* whether has enough data */ - mask = rb->buffer_size - 1; - size = rb->write_index - rb->read_index; - - /* no data */ - if (size == 0) return 0; - /* less data */ - if (size < length) length = size; - - read_position = rb->read_index & mask; - if (rb->buffer_size - read_position >= length) - { - /* copy all of data */ - memcpy(ptr, &rb->buffer_ptr[read_position], length); - } - else - { - /* copy first and second */ - memcpy(ptr, &rb->buffer_ptr[read_position], rb->buffer_size - read_position); - memcpy(&ptr[rb->buffer_size - read_position], &rb->buffer_ptr[0], - length - rb->buffer_size + read_position); - } - rb->read_index += length; - - return length; + rt_size_t size; + rt_uint16_t mask; + rt_uint16_t read_position; + + RT_ASSERT(rb != RT_NULL); + /* whether has enough data */ + mask = rb->buffer_size - 1; + size = rb->write_index - rb->read_index; + + /* no data */ + if (size == 0) return 0; + /* less data */ + if (size < length) length = size; + + read_position = rb->read_index & mask; + if (rb->buffer_size - read_position >= length) + { + /* copy all of data */ + memcpy(ptr, &rb->buffer_ptr[read_position], length); + } + else + { + /* copy first and second */ + memcpy(ptr, &rb->buffer_ptr[read_position], + rb->buffer_size - read_position); + memcpy(&ptr[rb->buffer_size - read_position], &rb->buffer_ptr[0], + length - rb->buffer_size + read_position); + } + rb->read_index += length; + + return length; } RTM_EXPORT(rt_ringbuffer_get); /** * get a character from a ringbuffer */ -rt_size_t rt_ringbuffer_getchar(struct rt_ringbuffer* rb, rt_uint8_t *ch) +rt_size_t rt_ringbuffer_getchar(struct rt_ringbuffer *rb, rt_uint8_t *ch) { - rt_uint16_t mask; + rt_uint16_t mask; - RT_ASSERT(rb != RT_NULL); - - /* ringbuffer is empty */ - if (rb->read_index == rb->write_index) return 0; + RT_ASSERT(rb != RT_NULL); + + /* ringbuffer is empty */ + if (rb->read_index == rb->write_index) return 0; - mask = rb->buffer_size - 1; + mask = rb->buffer_size - 1; - /* put character */ - *ch = rb->buffer_ptr[rb->read_index & mask]; - rb->read_index += 1; + /* put character */ + *ch = rb->buffer_ptr[rb->read_index & mask]; + rb->read_index += 1; - return 1; + return 1; } RTM_EXPORT(rt_ringbuffer_getchar); - -- cgit v1.3.1