* @brief Put a block of data into the ring buffer. If the capacity of ring buffer is insufficient, it will overwrite the existing data in the ring buffer. * * @param rb A pointer to the ring buffer object. * @param ptr A pointer to the data buffer. * @param length The size of data in bytes. * * @return Return the data size we put into the ring buffer. */
| 114 | * @return Return the data size we put into the ring buffer. |
| 115 | */ |
| 116 | rt_size_t rt_ringbuffer_put_force(struct rt_ringbuffer *rb, |
| 117 | const rt_uint8_t *ptr, |
| 118 | rt_uint32_t length) |
| 119 | { |
| 120 | rt_uint32_t space_length; |
| 121 | |
| 122 | RT_ASSERT(rb != RT_NULL); |
| 123 | |
| 124 | space_length = rt_ringbuffer_space_len(rb); |
| 125 | |
| 126 | if (length > rb->buffer_size) |
| 127 | { |
| 128 | ptr = &ptr[length - rb->buffer_size]; |
| 129 | length = rb->buffer_size; |
| 130 | } |
| 131 | |
| 132 | if (rb->buffer_size - rb->write_index > length) |
| 133 | { |
| 134 | /* read_index - write_index = empty space */ |
| 135 | rt_memcpy(&rb->buffer_ptr[rb->write_index], ptr, length); |
| 136 | /* this should not cause overflow because there is enough space for |
| 137 | * length of data in current mirror */ |
| 138 | rb->write_index += length; |
| 139 | |
| 140 | if (length > space_length) |
| 141 | rb->read_index = rb->write_index; |
| 142 | |
| 143 | return length; |
| 144 | } |
| 145 | |
| 146 | rt_memcpy(&rb->buffer_ptr[rb->write_index], |
| 147 | &ptr[0], |
| 148 | rb->buffer_size - rb->write_index); |
| 149 | rt_memcpy(&rb->buffer_ptr[0], |
| 150 | &ptr[rb->buffer_size - rb->write_index], |
| 151 | length - (rb->buffer_size - rb->write_index)); |
| 152 | |
| 153 | /* we are going into the other side of the mirror */ |
| 154 | rb->write_mirror = ~rb->write_mirror; |
| 155 | rb->write_index = length - (rb->buffer_size - rb->write_index); |
| 156 | |
| 157 | if (length > space_length) |
| 158 | { |
| 159 | if (rb->write_index <= rb->read_index) |
| 160 | rb->read_mirror = ~rb->read_mirror; |
| 161 | rb->read_index = rb->write_index; |
| 162 | } |
| 163 | |
| 164 | return length; |
| 165 | } |
| 166 | RTM_EXPORT(rt_ringbuffer_put_force); |
| 167 | |
| 168 | /** |