Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
DenisBiryukov91 committed Jun 20, 2024
1 parent b60080b commit 58a73de
Show file tree
Hide file tree
Showing 17 changed files with 1,031 additions and 24 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ if(UNIX OR MSVC)
add_executable(z_test_fragment_rx ${PROJECT_SOURCE_DIR}/tests/z_test_fragment_rx.c)
add_executable(z_perf_tx ${PROJECT_SOURCE_DIR}/tests/z_perf_tx.c)
add_executable(z_perf_rx ${PROJECT_SOURCE_DIR}/tests/z_perf_rx.c)
add_executable(z_bytes_test ${PROJECT_SOURCE_DIR}/tests/z_bytes_test.c)

target_link_libraries(z_data_struct_test ${Libname})
target_link_libraries(z_channels_test ${Libname})
Expand All @@ -356,6 +357,7 @@ if(UNIX OR MSVC)
target_link_libraries(z_test_fragment_rx ${Libname})
target_link_libraries(z_perf_tx ${Libname})
target_link_libraries(z_perf_rx ${Libname})
target_link_libraries(z_bytes_test ${Libname})

configure_file(${PROJECT_SOURCE_DIR}/tests/modularity.py ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/modularity.py COPYONLY)
configure_file(${PROJECT_SOURCE_DIR}/tests/raweth.py ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/raweth.py COPYONLY)
Expand All @@ -373,6 +375,7 @@ if(UNIX OR MSVC)
add_test(z_keyexpr_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_keyexpr_test)
add_test(z_api_null_drop_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_api_null_drop_test)
add_test(z_api_double_drop_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_api_double_drop_test)
add_test(z_bytes_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_bytes_test)
endif()

if(BUILD_MULTICAST)
Expand Down
56 changes: 56 additions & 0 deletions include/zenoh-pico/collections/arc_slice.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
//
// Copyright (c) 2024 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_ARC_SLICE_H
#define ZENOH_PICO_COLLECTIONS_ARC_SLICE_H

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <string.h>

#include "slice.h"
#include "refcount.h"
#include "zenoh-pico/system/platform-common.h"


_Z_REFCOUNT_DEFINE(_z_slice, _z_slice);

/*-------- ArcSlice --------*/
/**
* An atomically reference counted subslice.
*
* Members:
* _z_slice_rc_t len: Rc counted slice.
* size_t start: Offset to the subslice start.
* size_t len: Length of the subslice.
*/

typedef struct {
_z_slice_rc_t slice;
size_t start;
size_t len;
} _z_arc_slice_t;

_z_arc_slice_t _z_arc_slice_empty(void);
_z_arc_slice_t _z_arc_slice_wrap(_z_slice_t s, size_t offset, size_t len);
_z_arc_slice_t _z_arc_slice_get_subslice(const _z_arc_slice_t* s, size_t offset, size_t len);
size_t _z_arc_slice_len(const _z_arc_slice_t* s);
_Bool _z_arc_slice_is_empty(const _z_arc_slice_t* s);
const uint8_t* _z_arc_slice_data(const _z_arc_slice_t* s);
int8_t _z_arc_slice_copy(_z_arc_slice_t *dst, const _z_arc_slice_t *src);
int8_t _z_arc_slice_move(_z_arc_slice_t *dst, _z_arc_slice_t *src);
int8_t _z_arc_slice_drop(_z_arc_slice_t *s);

#endif /* ZENOH_PICO_COLLECTIONS_ARC_SLICE_H */
99 changes: 99 additions & 0 deletions include/zenoh-pico/collections/bytes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
//
// Copyright (c) 2024 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_BYTES_H
#define ZENOH_PICO_COLLECTIONS_BYTES_H

#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include "vec.h"
#include "arc_slice.h"
#include "zenoh-pico/protocol/iobuf.h"



inline size_t _z_arc_slice_size(const _z_arc_slice_t *s) {
(void)s;
return sizeof(_z_arc_slice_size);
}
static inline void _z_arc_slice_elem_move(void *dst, void *src) {
_z_arc_slice_move((_z_arc_slice_t *)dst, (_z_arc_slice_t*)src);
}
_Z_ELEM_DEFINE(_z_arc_slice, _z_arc_slice_t, _z_arc_slice_size, _z_arc_slice_drop, _z_arc_slice_copy)
_Z_SVEC_DEFINE(_z_arc_slice, _z_arc_slice_t)


/*-------- Bytes --------*/
/**
* A container for slices.
*
* Members:
* _z_slice_vec_t _slices: contents of the container.
*/

typedef struct {
_z_arc_slice_svec_t _slices;
} _zz_bytes_t;


_Bool _zz_bytes_check(const _zz_bytes_t *bytes);
_zz_bytes_t _zz_bytes_null(void);
_zz_bytes_t _zz_bytes_make(size_t capacity);
int8_t _zz_bytes_append(_zz_bytes_t* dst, _zz_bytes_t* src);
int8_t _zz_bytes_append_slice(_zz_bytes_t* dst, _z_arc_slice_t* s);
int8_t _zz_bytes_copy(_zz_bytes_t *dst, const _zz_bytes_t *src);
_zz_bytes_t _zz_bytes_duplicate(const _zz_bytes_t *src);
void _zz_bytes_move(_zz_bytes_t *dst, _zz_bytes_t *src);
void _zz_bytes_clear(_zz_bytes_t *bytes);
void _zz_bytes_free(_zz_bytes_t **bs);
size_t _zz_bytes_num_slices(const _zz_bytes_t *bs);
_z_arc_slice_t* _zz_bytes_get_slice(const _zz_bytes_t *bs, size_t i);
size_t _zz_bytes_len(const _zz_bytes_t *bs);
_Bool _zz_bytes_is_empty(const _zz_bytes_t *bs);
uint8_t _zz_bytes_to_uint8(const _zz_bytes_t *bs);
uint16_t _zz_bytes_to_uint16(const _zz_bytes_t *bs);
uint32_t _zz_bytes_to_uint32(const _zz_bytes_t *bs);
uint64_t _zz_bytes_to_uint64(const _zz_bytes_t *bs);
float _zz_bytes_to_float(const _zz_bytes_t *bs);
double _zz_bytes_to_double(const _zz_bytes_t *bs);
_z_slice_t _zz_bytes_to_slice(const _zz_bytes_t *bytes);
_zz_bytes_t _zz_bytes_from_slice(_z_slice_t s);
_zz_bytes_t _zz_bytes_from_uint8(uint8_t val);
_zz_bytes_t _zz_bytes_from_uint16(uint16_t val);
_zz_bytes_t _zz_bytes_from_uint32(uint32_t val);
_zz_bytes_t _zz_bytes_from_uint64(uint64_t val);
_zz_bytes_t _zz_bytes_from_float(float val);
_zz_bytes_t _zz_bytes_from_double(double val);
size_t _zz_bytes_to_buf(const _zz_bytes_t *bytes, uint8_t* dst, size_t len);
_zz_bytes_t _zz_bytes_from_buf(uint8_t* src, size_t len);


typedef struct {
size_t slice_idx;
size_t in_slice_idx;
size_t byte_idx;
const _zz_bytes_t* bytes;
} _zz_bytes_reader_t;



_zz_bytes_reader_t _zz_bytes_get_reader(const _zz_bytes_t *bytes);
int8_t _zz_bytes_reader_seek(_zz_bytes_reader_t *reader, int64_t offset, int origin);
int64_t _zz_bytes_reader_tell(const _zz_bytes_reader_t *reader);
int8_t _zz_bytes_reader_read_slices(_zz_bytes_reader_t* reader, size_t len, _zz_bytes_t* out);
int8_t _zz_bytes_reader_read(_zz_bytes_reader_t *reader, uint8_t* buf, size_t len);
int8_t _zz_bytes_reader_read_next(_zz_bytes_reader_t* reader, _zz_bytes_t* out);

#endif /* ZENOH_PICO_COLLECTIONS_BYTES_H */
5 changes: 5 additions & 0 deletions include/zenoh-pico/collections/element.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ static inline void _z_noop_copy(void *dst, const void *src) {
(void)(src);
}

static inline void _z_noop_move(void *dst, void *src) {
(void)(dst);
(void)(src);
}

_Z_ELEM_DEFINE(_z_noop, _z_noop_t, _z_noop_size, _z_noop_clear, _z_noop_copy)

#endif /* ZENOH_PICO_COLLECTIONS_ELEMENT_H */
1 change: 1 addition & 0 deletions include/zenoh-pico/collections/slice.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ inline static _Bool _z_slice_check(_z_slice_t value) { return value.start != NUL
int8_t _z_slice_init(_z_slice_t *bs, size_t capacity);
_z_slice_t _z_slice_make(size_t capacity);
_z_slice_t _z_slice_wrap(const uint8_t *bs, size_t len);
_z_slice_t _z_slice_wrap_copy(const uint8_t *bs, size_t len);
_z_slice_t _z_slice_steal(_z_slice_t *b);
void _z_slice_copy(_z_slice_t *dst, const _z_slice_t *src);
_z_slice_t _z_slice_duplicate(const _z_slice_t *src);
Expand Down
1 change: 1 addition & 0 deletions include/zenoh-pico/collections/string.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ void _z_string_free(_z_string_t **s);
void _z_string_reset(_z_string_t *s);
_z_string_t _z_string_convert_bytes(const _z_slice_t *bs);
_z_string_t _z_string_from_bytes(const _z_slice_t *bs);
_z_string_t _z_string_preallocate(const size_t len);

_Z_ELEM_DEFINE(_z_string, _z_string_t, _z_string_size, _z_string_clear, _z_string_copy)

Expand Down
58 changes: 57 additions & 1 deletion include/zenoh-pico/collections/vec.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ void _z_vec_remove(_z_vec_t *sv, size_t pos, z_element_free_f f);
void _z_vec_reset(_z_vec_t *v, z_element_free_f f);
void _z_vec_clear(_z_vec_t *v, z_element_free_f f);
void _z_vec_free(_z_vec_t **v, z_element_free_f f);
void _z_vec_release(_z_vec_t *v);

#define _Z_VEC_DEFINE(name, type) \
typedef _z_vec_t name##_vec_t; \
Expand All @@ -60,6 +61,61 @@ void _z_vec_free(_z_vec_t **v, z_element_free_f f);
} \
static inline void name##_vec_reset(name##_vec_t *v) { _z_vec_reset(v, name##_elem_free); } \
static inline void name##_vec_clear(name##_vec_t *v) { _z_vec_clear(v, name##_elem_free); } \
static inline void name##_vec_free(name##_vec_t **v) { _z_vec_free(v, name##_elem_free); }
static inline void name##_vec_free(name##_vec_t **v) { _z_vec_free(v, name##_elem_free); } \
static inline void name##_vec_release(name##_vec_t *v) { _z_vec_release(v); }


/*-------- Dynamically allocated sized vector --------*/
/**
* A dynamically allocate vector.
*/
typedef struct {
size_t _capacity;
size_t _len;
void *_val;
} _z_svec_t;

_z_svec_t _z_svec_make(size_t capacity, size_t element_size);
void _z_svec_copy(_z_svec_t *dst, const _z_svec_t *src, z_element_copy_f copy, size_t element_size);

size_t _z_svec_len(const _z_svec_t *v);
_Bool _z_svec_is_empty(const _z_svec_t *v);

bool _z_svec_append(_z_svec_t *v, const void *e, z_element_move_f m, size_t element_size);
void *_z_svec_get(const _z_svec_t *v, size_t pos, size_t element_size);
void _z_svec_set(_z_svec_t *sv, size_t pos, void *e, z_element_clear_f f, size_t element_size);
void _z_svec_remove(_z_svec_t *sv, size_t pos, z_element_clear_f f, z_element_move_f m, size_t element_size);

void _z_svec_reset(_z_svec_t *v, z_element_clear_f f, size_t element_size);
void _z_svec_clear(_z_svec_t *v, z_element_clear_f f, size_t element_size);
void _z_svec_free(_z_svec_t **v, z_element_clear_f f, size_t element_size);
void _z_svec_release(_z_svec_t *v);

#define _Z_SVEC_DEFINE(name, type) \
typedef _z_svec_t name##_svec_t; \
static inline name##_svec_t name##_svec_make(size_t capacity) { \
return _z_svec_make(capacity, sizeof(type)); \
} \
static inline size_t name##_svec_len(const name##_svec_t *v) { return _z_svec_len(v); } \
static inline _Bool name##_svec_is_empty(const name##_svec_t *v) { return _z_svec_is_empty(v); } \
static inline _Bool name##_svec_append(name##_svec_t *v, type *e) { \
return _z_svec_append(v, e, name##_elem_move, sizeof(type)); \
} \
static inline type *name##_svec_get(const name##_svec_t *v, size_t pos) { \
return (type *)_z_svec_get(v, pos, sizeof(type)); \
} \
static inline void name##_svec_set(name##_svec_t *v, size_t pos, type *e) { \
_z_svec_set(v, pos, e, name##_elem_clear, sizeof(type)); \
} \
static inline void name##_svec_remove(name##_svec_t *v, size_t pos) { \
_z_svec_remove(v, pos, name##_elem_clear, name##_elem_move, sizeof(type)); \
} \
static inline void name##_svec_copy(name##_svec_t *dst, const name##_svec_t *src) { \
_z_svec_copy(dst, src, name##_elem_copy, sizeof(type)); \
} \
static inline void name##_svec_reset(name##_svec_t *v) { _z_svec_reset(v, name##_elem_clear, sizeof(type)); } \
static inline void name##_svec_clear(name##_svec_t *v) { _z_svec_clear(v, name##_elem_clear, sizeof(type)); } \
static inline void name##_svec_free(name##_svec_t **v) { _z_svec_free(v, name##_elem_clear, sizeof(type)); } \


#endif /* ZENOH_PICO_COLLECTIONS_VECTOR_H */
8 changes: 8 additions & 0 deletions include/zenoh-pico/protocol/codec/core.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
} \
}

typedef int8_t (*__z_single_byte_reader_t)(uint8_t*, void* context);
/*------------------ Internal Zenoh-net Macros ------------------*/
int8_t _z_consolidation_mode_encode(_z_wbuf_t *wbf, z_consolidation_mode_t en);
int8_t _z_consolidation_mode_decode(z_consolidation_mode_t *en, _z_zbuf_t *zbf);
Expand All @@ -56,11 +57,18 @@ int8_t _z_uint16_encode(_z_wbuf_t *buf, uint16_t v);
int8_t _z_uint16_decode(uint16_t *u16, _z_zbuf_t *buf);

uint8_t _z_zint_len(uint64_t v);
uint8_t _z_zint64_encode_buf(uint8_t* buf, uint64_t v);
static inline uint8_t _z_zsize_encode_buf(uint8_t* buf, _z_zint_t v) {
return _z_zint64_encode_buf(buf, (uint64_t)v);
}

int8_t _z_zsize_encode(_z_wbuf_t *buf, _z_zint_t v);
int8_t _z_zint64_encode(_z_wbuf_t *buf, uint64_t v);
int8_t _z_zint16_decode(uint16_t *zint, _z_zbuf_t *buf);
int8_t _z_zint32_decode(uint32_t *zint, _z_zbuf_t *buf);
int8_t _z_zint64_decode(uint64_t *zint, _z_zbuf_t *buf);
int8_t _z_zint64_decode_with_reader(uint64_t *zint, __z_single_byte_reader_t reader, void *context);
int8_t _z_zsize_decode_with_reader(_z_zint_t *zint, __z_single_byte_reader_t reader, void* context);
int8_t _z_zsize_decode(_z_zint_t *zint, _z_zbuf_t *buf);

int8_t _z_slice_val_encode(_z_wbuf_t *buf, const _z_slice_t *bs);
Expand Down
2 changes: 2 additions & 0 deletions include/zenoh-pico/utils/result.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ typedef enum {

_Z_ERR_CONNECTION_CLOSED = -77,

_Z_ERR_DID_NOT_READ = -76,

_Z_ERR_GENERIC = -128
} _z_res_t;

Expand Down
Loading

0 comments on commit 58a73de

Please sign in to comment.