This is an automated email from the ASF dual-hosted git repository.

kasjer pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-core.git

commit b1eab024245bbed052d787bf58c446fc5c17a692
Author: Jerzy Kasenberg <[email protected]>
AuthorDate: Fri May 29 15:03:08 2026 +0200

    util/ringbuffer: Add ring_buffer functionality
    
    This moves ring buffer functionality from
    user_console.c to separate package so it can be
    reused.
    
    Names were slighlty change to match new package.
    
    Signed-off-by: Jerzy Kasenberg <[email protected]>
---
 util/ring_buffer/include/util/ring_buffer.h | 84 +++++++++++++++++++++++++++++
 util/ring_buffer/pkg.yml                    | 24 +++++++++
 util/ring_buffer/src/ring_buffer.c          | 57 ++++++++++++++++++++
 3 files changed, 165 insertions(+)

diff --git a/util/ring_buffer/include/util/ring_buffer.h 
b/util/ring_buffer/include/util/ring_buffer.h
new file mode 100644
index 000000000..e628730b9
--- /dev/null
+++ b/util/ring_buffer/include/util/ring_buffer.h
@@ -0,0 +1,84 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#ifndef _UTIL_RING_BUFFER_H_
+#define _UTIL_RING_BUFFER_H_
+
+#include <stddef.h>
+#include <stdint.h>
+#include <stdbool.h>
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+struct ring_buffer {
+    /* Write pointer */
+    uint8_t head;
+    /* Read pointer */
+    uint8_t tail;
+    /** Ring buffer size, must be power of 2 */
+    uint16_t size;
+    /** Data buffer */
+    uint8_t *buf;
+};
+
+/**
+ * Push new element to ring buffer.
+ * Ring buffer must have at least one empty space.
+ * ring_buffer_is_full should be called before pushing element into
+ * buffer.
+ *
+ * @param rb ring buffer
+ * @param ch element to add to ring buffer
+ */
+void ring_buffer_push(struct ring_buffer *rb, uint8_t ch);
+
+/**
+ * Pull oldest element from ring buffer.
+ * Ring buffer can not be empty.
+ * Function ring_buffer_is_empty() should be called before pulling
+ * element from the buffer.
+ *
+ * @param rb ring buffer
+ * @return oldest emenet in ring buffer
+ */
+uint8_t ring_buffer_pull(struct ring_buffer *rb);
+
+/**
+ * Check if ring buffer is full.
+ *
+ * @param rb ring buffer
+ * @return true if ring buffer is full
+ */
+bool ring_buffer_is_full(const struct ring_buffer *rb);
+
+/**
+ * Check if ring buffer is empty.
+ *
+ * @param rb ring buffer
+ * @return true if ring buffer is empty
+ */
+bool ring_buffer_is_empty(const struct ring_buffer *rb);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* _UTIL_RING_BUFFER_H_ */
diff --git a/util/ring_buffer/pkg.yml b/util/ring_buffer/pkg.yml
new file mode 100644
index 000000000..483d566e1
--- /dev/null
+++ b/util/ring_buffer/pkg.yml
@@ -0,0 +1,24 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#  http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+pkg.name: util/ring_buffer
+pkg.description: Simple implementation of ring buffer
+pkg.author: "Apache Mynewt <[email protected]>"
+pkg.homepage: "http://mynewt.apache.org/";
+pkg.keywords:
diff --git a/util/ring_buffer/src/ring_buffer.c 
b/util/ring_buffer/src/ring_buffer.c
new file mode 100644
index 000000000..ab177ecc5
--- /dev/null
+++ b/util/ring_buffer/src/ring_buffer.c
@@ -0,0 +1,57 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *  http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include <stdint.h>
+#include <util/ring_buffer.h>
+
+static int
+ring_buffer_inc_and_wrap(int i, int max)
+{
+    return (i + 1) & (max - 1);
+}
+
+void
+ring_buffer_push(struct ring_buffer *rb, uint8_t ch)
+{
+    rb->buf[rb->head] = ch;
+    rb->head = ring_buffer_inc_and_wrap(rb->head, rb->size);
+}
+
+uint8_t
+ring_buffer_pull(struct ring_buffer *rb)
+{
+    uint8_t ch;
+
+    ch = rb->buf[rb->tail];
+    rb->tail = ring_buffer_inc_and_wrap(rb->tail, rb->size);
+
+    return ch;
+}
+
+bool
+ring_buffer_is_full(const struct ring_buffer *rb)
+{
+    return ring_buffer_inc_and_wrap(rb->head, rb->size) == rb->tail;
+}
+
+bool
+ring_buffer_is_empty(const struct ring_buffer *rb)
+{
+    return rb->head == rb->tail;
+}

Reply via email to