Skip to content

Commit

Permalink
Add fifo and lifo collections
Browse files Browse the repository at this point in the history
  • Loading branch information
Mallets committed Mar 21, 2024
1 parent 3d68a8c commit 2e13913
Show file tree
Hide file tree
Showing 5 changed files with 341 additions and 69 deletions.
63 changes: 63 additions & 0 deletions include/zenoh-pico/collections/fifo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//
// Copyright (c) 2022 ZettaScale Technology
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
//
// Contributors:
// ZettaScale Zenoh Team, <[email protected]>
//
#ifndef ZENOH_PICO_COLLECTIONS_FIFO_H
#define ZENOH_PICO_COLLECTIONS_FIFO_H

#include <stddef.h>

Check warning

Code scanning / Cppcheck (reported by Codacy)

Include file: <stddef.h> not found. Please note: Cppcheck does not need standard library headers to get proper results. Warning

Include file: <stddef.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <stdint.h>

Check warning

Code scanning / Cppcheck (reported by Codacy)

Include file: <stdint.h> not found. Please note: Cppcheck does not need standard library headers to get proper results. Warning

Include file: <stdint.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include "zenoh-pico/collections/element.h"
#include "zenoh-pico/collections/ring.h"

/*-------- Fifo Buffer --------*/
typedef struct {
_z_ring_t _ring;
} _z_fifo_t;

int8_t _z_fifo_init(_z_fifo_t *fifo, size_t capacity);
_z_fifo_t _z_fifo_make(size_t capacity);

size_t _z_fifo_capacity(const _z_fifo_t *r);
size_t _z_fifo_len(const _z_fifo_t *r);
_Bool _z_fifo_is_empty(const _z_fifo_t *r);
_Bool _z_fifo_is_full(const _z_fifo_t *r);

void *_z_fifo_push(_z_fifo_t *r, void *e);
void _z_fifo_push_drop(_z_fifo_t *r, void *e, z_element_free_f f);
void *_z_fifo_pull(_z_fifo_t *r);

_z_fifo_t *_z_fifo_clone(const _z_fifo_t *xs, z_element_clone_f d_f);

void _z_fifo_clear(_z_fifo_t *v, z_element_free_f f);
void _z_fifo_free(_z_fifo_t **xs, z_element_free_f f_f);

#define _Z_FIFO_DEFINE(name, type) \

Check warning

Code scanning / Cppcheck (reported by Codacy)

misra violation 2101 with no text in the supplied rule-texts-file Warning

misra violation 2101 with no text in the supplied rule-texts-file

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 20.7 rule Note

MISRA 20.7 rule

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 0.10 rule Note

MISRA 0.10 rule
typedef _z_fifo_t name##_fifo_t; \
static inline int8_t name##_fifo_init(name##_fifo_t *fifo, size_t capacity) { \
return _z_fifo_init(fifo, capacity); \
} \
static inline name##_fifo_t name##_fifo_make(size_t capacity) { return _z_fifo_make(capacity); } \
static inline size_t name##_fifo_capacity(const name##_fifo_t *r) { return _z_fifo_capacity(r); } \
static inline size_t name##_fifo_len(const name##_fifo_t *r) { return _z_fifo_len(r); } \
static inline _Bool name##_fifo_is_empty(const name##_fifo_t *r) { return _z_fifo_is_empty(r); } \
static inline _Bool name##_fifo_is_full(const name##_fifo_t *r) { return _z_fifo_is_full(r); } \
static inline type *name##_fifo_push(name##_fifo_t *r, type *e) { return _z_fifo_push(r, (void *)e); } \
static inline void name##_fifo_push_drop(name##_fifo_t *r, type *e) { \
return _z_fifo_push_drop(r, (void *)e, name##_elem_free); \
} \
static inline type *name##_fifo_pull(name##_fifo_t *r) { return (type *)_z_fifo_pull(r); } \
static inline void name##_fifo_clear(name##_fifo_t *r) { _z_fifo_clear(r, name##_elem_free); } \
static inline void name##_fifo_free(name##_fifo_t **r) { _z_fifo_free(r, name##_elem_free); }

#endif /* ZENOH_PICO_COLLECTIONS_FIFO_H */
64 changes: 64 additions & 0 deletions include/zenoh-pico/collections/lifo.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//
// Copyright (c) 2022 ZettaScale Technology
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
//
// Contributors:
// ZettaScale Zenoh Team, <[email protected]>
//
#ifndef ZENOH_PICO_COLLECTIONS_LIFO_H
#define ZENOH_PICO_COLLECTIONS_LIFO_H

#include <stddef.h>

Check warning

Code scanning / Cppcheck (reported by Codacy)

Include file: <stddef.h> not found. Please note: Cppcheck does not need standard library headers to get proper results. Warning

Include file: <stddef.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <stdint.h>

Check warning

Code scanning / Cppcheck (reported by Codacy)

Include file: <stdint.h> not found. Please note: Cppcheck does not need standard library headers to get proper results. Warning

Include file: <stdint.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.

#include "zenoh-pico/collections/element.h"

/*-------- Ring Buffer --------*/
typedef struct {
void **_val;
size_t _capacity;
size_t _len;
} _z_lifo_t;

int8_t _z_lifo_init(_z_lifo_t *lifo, size_t capacity);
_z_lifo_t _z_lifo_make(size_t capacity);

size_t _z_lifo_capacity(const _z_lifo_t *r);
size_t _z_lifo_len(const _z_lifo_t *r);
_Bool _z_lifo_is_empty(const _z_lifo_t *r);
_Bool _z_lifo_is_full(const _z_lifo_t *r);

void *_z_lifo_push(_z_lifo_t *r, void *e);
void _z_lifo_push_drop(_z_lifo_t *r, void *e, z_element_free_f f);
void *_z_lifo_pull(_z_lifo_t *r);

_z_lifo_t *_z_lifo_clone(const _z_lifo_t *xs, z_element_clone_f d_f);

void _z_lifo_clear(_z_lifo_t *v, z_element_free_f f);
void _z_lifo_free(_z_lifo_t **xs, z_element_free_f f_f);

#define _Z_LIFO_DEFINE(name, type) \

Check warning

Code scanning / Cppcheck (reported by Codacy)

misra violation 2101 with no text in the supplied rule-texts-file Warning

misra violation 2101 with no text in the supplied rule-texts-file

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 0.10 rule Note

MISRA 0.10 rule

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 20.7 rule Note

MISRA 20.7 rule
typedef _z_lifo_t name##_lifo_t; \
static inline int8_t name##_lifo_init(name##_lifo_t *lifo, size_t capacity) { \
return _z_lifo_init(lifo, capacity); \
} \
static inline name##_lifo_t name##_lifo_make(size_t capacity) { return _z_lifo_make(capacity); } \
static inline size_t name##_lifo_capacity(const name##_lifo_t *r) { return _z_lifo_capacity(r); } \
static inline size_t name##_lifo_len(const name##_lifo_t *r) { return _z_lifo_len(r); } \
static inline _Bool name##_lifo_is_empty(const name##_lifo_t *r) { return _z_lifo_is_empty(r); } \
static inline _Bool name##_lifo_is_full(const name##_lifo_t *r) { return _z_lifo_is_full(r); } \
static inline type *name##_lifo_push(name##_lifo_t *r, type *e) { return _z_lifo_push(r, (void *)e); } \
static inline void name##_lifo_push_drop(name##_lifo_t *r, type *e) { \
return _z_lifo_push_drop(r, (void *)e, name##_elem_free); \
} \
static inline type *name##_lifo_pull(name##_lifo_t *r) { return (type *)_z_lifo_pull(r); } \
static inline void name##_lifo_clear(name##_lifo_t *r) { _z_lifo_clear(r, name##_elem_free); } \
static inline void name##_lifo_free(name##_lifo_t **r) { _z_lifo_free(r, name##_elem_free); }

#endif /* ZENOH_PICO_COLLECTIONS_LIFO_H */
53 changes: 53 additions & 0 deletions src/collections/fifo.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//
// Copyright (c) 2022 ZettaScale Technology
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
//
// Contributors:
// ZettaScale Zenoh Team, <[email protected]>
//
#include "zenoh-pico/collections/fifo.h"

#include <assert.h>

Check warning

Code scanning / Cppcheck (reported by Codacy)

Include file: <assert.h> not found. Please note: Cppcheck does not need standard library headers to get proper results. Warning

Include file: <assert.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <stddef.h>

Check warning

Code scanning / Cppcheck (reported by Codacy)

Include file: <stddef.h> not found. Please note: Cppcheck does not need standard library headers to get proper results. Warning

Include file: <stddef.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <string.h>

Check warning

Code scanning / Cppcheck (reported by Codacy)

Include file: <string.h> not found. Please note: Cppcheck does not need standard library headers to get proper results. Warning

Include file: <string.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.

/*-------- fifo --------*/
int8_t _z_fifo_init(_z_fifo_t *r, size_t capacity) {

Check warning

Code scanning / Cppcheck (reported by Codacy)

misra violation 804 with no text in the supplied rule-texts-file Warning

misra violation 804 with no text in the supplied rule-texts-file
_z_ring_init(&r->_ring, capacity);
return 0;
}

_z_fifo_t _z_fifo_make(size_t capacity) {

Check warning

Code scanning / Cppcheck (reported by Codacy)

misra violation 804 with no text in the supplied rule-texts-file Warning

misra violation 804 with no text in the supplied rule-texts-file
_z_fifo_t v;
_z_fifo_init(&v, capacity);

Check notice

Code scanning / Cppcheck (reported by Codacy)

MISRA 17.7 rule Note

MISRA 17.7 rule
return v;
}

size_t _z_fifo_capacity(const _z_fifo_t *r) { return _z_ring_capacity(&r->_ring); }

Check warning

Code scanning / Cppcheck (reported by Codacy)

misra violation 804 with no text in the supplied rule-texts-file Warning

misra violation 804 with no text in the supplied rule-texts-file
size_t _z_fifo_len(const _z_fifo_t *r) { return _z_ring_len(&r->_ring); }

Check warning

Code scanning / Cppcheck (reported by Codacy)

misra violation 804 with no text in the supplied rule-texts-file Warning

misra violation 804 with no text in the supplied rule-texts-file
bool _z_fifo_is_empty(const _z_fifo_t *r) { return _z_ring_is_empty(&r->_ring); }

Check warning

Code scanning / Cppcheck (reported by Codacy)

misra violation 804 with no text in the supplied rule-texts-file Warning

misra violation 804 with no text in the supplied rule-texts-file
bool _z_fifo_is_full(const _z_fifo_t *r) { return _z_fifo_len(r) == _z_fifo_capacity(r); }

Check warning

Code scanning / Cppcheck (reported by Codacy)

misra violation 804 with no text in the supplied rule-texts-file Warning

misra violation 804 with no text in the supplied rule-texts-file

void *_z_fifo_push(_z_fifo_t *r, void *e) { return _z_ring_push(&r->_ring, e); }

Check warning

Code scanning / Cppcheck (reported by Codacy)

misra violation 804 with no text in the supplied rule-texts-file Warning

misra violation 804 with no text in the supplied rule-texts-file
void _z_fifo_push_drop(_z_fifo_t *r, void *e, z_element_free_f free_f) {

Check warning

Code scanning / Cppcheck (reported by Codacy)

misra violation 804 with no text in the supplied rule-texts-file Warning

misra violation 804 with no text in the supplied rule-texts-file
void *ret = _z_fifo_push(r, e);
if (ret != NULL) {
free_f(&ret);
}
}
void *_z_fifo_pull(_z_fifo_t *r) { return _z_ring_pull(&r->_ring); }

Check warning

Code scanning / Cppcheck (reported by Codacy)

misra violation 804 with no text in the supplied rule-texts-file Warning

misra violation 804 with no text in the supplied rule-texts-file
void _z_fifo_clear(_z_fifo_t *r, z_element_free_f free_f) { _z_ring_clear(&r->_ring, free_f); }

Check warning

Code scanning / Cppcheck (reported by Codacy)

misra violation 804 with no text in the supplied rule-texts-file Warning

misra violation 804 with no text in the supplied rule-texts-file
void _z_fifo_free(_z_fifo_t **r, z_element_free_f free_f) {

Check warning

Code scanning / Cppcheck (reported by Codacy)

misra violation 804 with no text in the supplied rule-texts-file Warning

misra violation 804 with no text in the supplied rule-texts-file
_z_fifo_t *ptr = (_z_fifo_t *)*r;
if (ptr != NULL) {
_z_fifo_clear(ptr, free_f);
zp_free(ptr);
*r = NULL;
}
}
90 changes: 90 additions & 0 deletions src/collections/lifo.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
//
// Copyright (c) 2022 ZettaScale Technology
//
// This program and the accompanying materials are made available under the
// terms of the Eclipse Public License 2.0 which is available at
// http://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0
// which is available at https://www.apache.org/licenses/LICENSE-2.0.
//
// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0
//
// Contributors:
// ZettaScale Zenoh Team, <[email protected]>
//
#include "zenoh-pico/collections/lifo.h"

#include <assert.h>

Check warning

Code scanning / Cppcheck (reported by Codacy)

Include file: <assert.h> not found. Please note: Cppcheck does not need standard library headers to get proper results. Warning

Include file: <assert.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <stddef.h>

Check warning

Code scanning / Cppcheck (reported by Codacy)

Include file: <stddef.h> not found. Please note: Cppcheck does not need standard library headers to get proper results. Warning

Include file: <stddef.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <string.h>

Check warning

Code scanning / Cppcheck (reported by Codacy)

Include file: <string.h> not found. Please note: Cppcheck does not need standard library headers to get proper results. Warning

Include file: <string.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.

/*-------- lifo --------*/
int8_t _z_lifo_init(_z_lifo_t *r, size_t capacity) {

Check warning

Code scanning / Cppcheck (reported by Codacy)

misra violation 804 with no text in the supplied rule-texts-file Warning

misra violation 804 with no text in the supplied rule-texts-file
memset(r, 0, sizeof(_z_lifo_t));
if (capacity != (size_t)0) {
r->_val = (void **)zp_malloc(sizeof(void *) * capacity);
}
if (r->_val != NULL) {
memset(r->_val, 0, capacity);
r->_capacity = capacity;
}
return 0;
}

_z_lifo_t _z_lifo_make(size_t capacity) {

Check warning

Code scanning / Cppcheck (reported by Codacy)

misra violation 804 with no text in the supplied rule-texts-file Warning

misra violation 804 with no text in the supplied rule-texts-file
_z_lifo_t v;
_z_lifo_init(&v, capacity);
return v;
}

size_t _z_lifo_capacity(const _z_lifo_t *r) { return r->_capacity; }

Check warning

Code scanning / Cppcheck (reported by Codacy)

misra violation 804 with no text in the supplied rule-texts-file Warning

misra violation 804 with no text in the supplied rule-texts-file
size_t _z_lifo_len(const _z_lifo_t *r) { return r->_len; }

Check warning

Code scanning / Cppcheck (reported by Codacy)

misra violation 804 with no text in the supplied rule-texts-file Warning

misra violation 804 with no text in the supplied rule-texts-file
_Bool _z_lifo_is_empty(const _z_lifo_t *r) { return r->_len == 0; }

Check warning

Code scanning / Cppcheck (reported by Codacy)

misra violation 804 with no text in the supplied rule-texts-file Warning

misra violation 804 with no text in the supplied rule-texts-file
_Bool _z_lifo_is_full(const _z_lifo_t *r) { return r->_len == r->_capacity; }

Check warning

Code scanning / Cppcheck (reported by Codacy)

misra violation 804 with no text in the supplied rule-texts-file Warning

misra violation 804 with no text in the supplied rule-texts-file

void *_z_lifo_push(_z_lifo_t *r, void *e) {

Check warning

Code scanning / Cppcheck (reported by Codacy)

misra violation 804 with no text in the supplied rule-texts-file Warning

misra violation 804 with no text in the supplied rule-texts-file
void *ret = e;
if (!_z_lifo_is_full(r)) {
r->_val[r->_len] = e;
r->_len++;
ret = NULL;
}
return ret;
}

void _z_lifo_push_drop(_z_lifo_t *r, void *e, z_element_free_f free_f) {

Check warning

Code scanning / Cppcheck (reported by Codacy)

misra violation 804 with no text in the supplied rule-texts-file Warning

misra violation 804 with no text in the supplied rule-texts-file
void *ret = _z_lifo_push(r, e);
if (ret != NULL) {
free_f(&ret);
}
}

void *_z_lifo_pull(_z_lifo_t *r) {

Check warning

Code scanning / Cppcheck (reported by Codacy)

misra violation 804 with no text in the supplied rule-texts-file Warning

misra violation 804 with no text in the supplied rule-texts-file
void *ret = NULL;
if (!_z_lifo_is_empty(r)) {
r->_len--;
ret = r->_val[r->_len];
}
return ret;
}

void _z_lifo_clear(_z_lifo_t *r, z_element_free_f free_f) {

Check warning

Code scanning / Cppcheck (reported by Codacy)

misra violation 804 with no text in the supplied rule-texts-file Warning

misra violation 804 with no text in the supplied rule-texts-file
void *e = _z_lifo_pull(r);
while (e != NULL) {
free_f(&e);
e = _z_lifo_pull(r);
}
zp_free(r->_val);

r->_val = NULL;
r->_capacity = (size_t)0;
r->_len = (size_t)0;
}

void _z_lifo_free(_z_lifo_t **r, z_element_free_f free_f) {

Check warning

Code scanning / Cppcheck (reported by Codacy)

misra violation 804 with no text in the supplied rule-texts-file Warning

misra violation 804 with no text in the supplied rule-texts-file
_z_lifo_t *ptr = (_z_lifo_t *)*r;
if (ptr != NULL) {
_z_lifo_clear(ptr, free_f);
zp_free(ptr);
*r = NULL;
}
}
Loading

0 comments on commit 2e13913

Please sign in to comment.