Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for custom / user-defined allocators #231

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion .github/workflows/emscripten.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
- name: Compile debug
run: |
mkdir build
emcmake cmake -E env CFLAGS="-DZ_LINK_WS=1 -DZ_LINK_TCP=0 -DZ_LINK_UDP_MULTICAST=0 -DZ_LINK_UDP_UNICAST=0 -DZ_SCOUTING_UDP=0" cmake -DBUILD_SHARED_LIBS=OFF -DCMAKE_C_STANDARD=11 -DBUILD_EXAMPLES=OFF -DCMAKE_BUILD_TYPE=Debug -DBUILD_TESTING=OFF -DBUILD_MULTICAST=OFF -DBUILD_INTEGRATION=OFF -DBUILD_TOOLS=OFF -DZENOH_DEBUG=3 -H. -Bbuild
emcmake cmake -E env CFLAGS="-DZ_LINK_WS=1 -DZ_LINK_TCP=0 -DZ_LINK_UDP_MULTICAST=0 -DZ_LINK_UDP_UNICAST=0 -DZ_SCOUTING_UDP=0" cmake -DBUILD_SHARED_LIBS=OFF -DCMAKE_C_STANDARD=11 -DBUILD_WITH_CUSTOM_ALLOCATOR=OFF -DBUILD_EXAMPLES=OFF -DCMAKE_BUILD_TYPE=Debug -DBUILD_TESTING=OFF -DBUILD_MULTICAST=OFF -DBUILD_INTEGRATION=OFF -DBUILD_TOOLS=OFF -DZENOH_DEBUG=3 -H. -Bbuild
make -C build


6 changes: 5 additions & 1 deletion BSDmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ BUILD_TYPE?=Release
# Accepted values: ON, OFF
BUILD_EXAMPLES?=ON

# Build with custom / user allocators. This sets the BUILD_WITH_CUSTOM_ALLOCATOR variable.
# Accepted values: ON, OFF
BUILD_WITH_CUSTOM_ALLOCATOR?=OFF

# Build testing. This sets the BUILD_TESTING variable.
# Accepted values: ON, OFF
BUILD_TESTING?=ON
Expand Down Expand Up @@ -58,7 +62,7 @@ CROSSIMG_PREFIX=zenoh-pico_
# NOTES:
# - ARM: old versions of dockcross/dockcross were creating some issues since they used an old GCC (4.8.3) which lacks <stdatomic.h> (even using -std=gnu11)

CMAKE_OPT=-DZENOH_DEBUG=$(ZENOH_DEBUG) -DBUILD_EXAMPLES=$(BUILD_EXAMPLES) -DCMAKE_BUILD_TYPE=$(BUILD_TYPE) -DBUILD_TESTING=$(BUILD_TESTING) -DBUILD_MULTICAST=$(BUILD_MULTICAST) -DBUILD_INTEGRATION=$(BUILD_INTEGRATION) -DBUILD_TOOLS=$(BUILD_TOOLS) -DBUILD_SHARED_LIBS=$(BUILD_SHARED_LIBS) -H.
CMAKE_OPT=-DZENOH_DEBUG=$(ZENOH_DEBUG) -DBUILD_EXAMPLES=$(BUILD_EXAMPLES) -DBUILD_WITH_CUSTOM_ALLOCATOR=$(BUILD_WITH_CUSTOM_ALLOCATOR) -D -DCMAKE_BUILD_TYPE=$(BUILD_TYPE) -DBUILD_TESTING=$(BUILD_TESTING) -DBUILD_MULTICAST=$(BUILD_MULTICAST) -DBUILD_INTEGRATION=$(BUILD_INTEGRATION) -DBUILD_TOOLS=$(BUILD_TOOLS) -DBUILD_SHARED_LIBS=$(BUILD_SHARED_LIBS) -H.

all: make

Expand Down
32 changes: 18 additions & 14 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -254,49 +254,51 @@ if(UNIX)
install(FILES "${CMAKE_SOURCE_DIR}/zenohpico.pc" CONFIGURATIONS Release RelWithDebInfo DESTINATION "${CMAKE_INSTALL_LIBDIR}/pkgconfig")
endif()

if(BUILD_WITH_CUSTOM_ALLOCATOR)
add_definitions(-DZ_CUSTOM_ALLOCATOR=1)
endif()

if(BUILD_EXAMPLES)
add_subdirectory(examples)
milyin marked this conversation as resolved.
Show resolved Hide resolved
endif()

if(UNIX OR MSVC)
if(BUILD_TOOLS)
if(BUILD_TOOLS)
if(BUILD_WITH_CUSTOM_ALLOCATOR)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/tools)
add_executable(z_keyexpr_canonizer ${PROJECT_SOURCE_DIR}/tools/z_keyexpr_canonizer.c)
target_link_libraries(z_keyexpr_canonizer ${Libname})
endif()
endif()

if(BUILD_TESTING)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/tests")
if(BUILD_TESTING)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/tests")

if(!BUILD_WITH_CUSTOM_ALLOCATOR)
add_executable(z_data_struct_test ${PROJECT_SOURCE_DIR}/tests/z_data_struct_test.c)
add_executable(z_endpoint_test ${PROJECT_SOURCE_DIR}/tests/z_endpoint_test.c)
add_executable(z_iobuf_test ${PROJECT_SOURCE_DIR}/tests/z_iobuf_test.c)
add_executable(z_msgcodec_test ${PROJECT_SOURCE_DIR}/tests/z_msgcodec_test.c)
add_executable(z_keyexpr_test ${PROJECT_SOURCE_DIR}/tests/z_keyexpr_test.c)
add_executable(z_api_null_drop_test ${PROJECT_SOURCE_DIR}/tests/z_api_null_drop_test.c)
milyin marked this conversation as resolved.
Show resolved Hide resolved
add_executable(z_api_double_drop_test ${PROJECT_SOURCE_DIR}/tests/z_api_double_drop_test.c)

target_link_libraries(z_data_struct_test ${Libname})
target_link_libraries(z_endpoint_test ${Libname})
target_link_libraries(z_iobuf_test ${Libname})
target_link_libraries(z_msgcodec_test ${Libname})
target_link_libraries(z_keyexpr_test ${Libname})
target_link_libraries(z_api_null_drop_test ${Libname})
target_link_libraries(z_api_double_drop_test ${Libname})

enable_testing()
add_test(z_data_struct_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_data_struct_test)
add_test(z_endpoint_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_endpoint_test)
add_test(z_iobuf_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_iobuf_test)
add_test(z_msgcodec_test ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/z_msgcodec_test)
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)
endif()
endif()

if(BUILD_MULTICAST)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/tests")
if(BUILD_MULTICAST)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/tests")

if(!BUILD_WITH_CUSTOM_ALLOCATOR)
if(CMAKE_C_STANDARD MATCHES "11")
add_executable(z_peer_multicast_test ${PROJECT_SOURCE_DIR}/tests/z_peer_multicast_test.c)
target_link_libraries(z_peer_multicast_test ${Libname})
Expand All @@ -307,10 +309,12 @@ if(UNIX OR MSVC)
add_test(z_peer_multicast_test bash ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/multicast.sh z_peer_multicast_test)
endif()
endif()
endif()

if(BUILD_INTEGRATION)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/tests")
if(BUILD_INTEGRATION)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/tests")

if(!BUILD_WITH_CUSTOM_ALLOCATOR)
if(CMAKE_C_STANDARD MATCHES "11")
add_executable(z_client_test ${PROJECT_SOURCE_DIR}/tests/z_client_test.c)
add_executable(z_api_alignment_test ${PROJECT_SOURCE_DIR}/tests/z_api_alignment_test.c)
Expand Down
6 changes: 5 additions & 1 deletion GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ BUILD_TYPE?=Release
# Accepted values: ON, OFF
BUILD_EXAMPLES?=ON

# Build with custom / user allocators. This sets the BUILD_WITH_CUSTOM_ALLOCATOR variable.
# Accepted values: ON, OFF
BUILD_WITH_CUSTOM_ALLOCATOR?=OFF

# Build testing. This sets the BUILD_TESTING variable.
# Accepted values: ON, OFF
BUILD_TESTING?=ON
Expand Down Expand Up @@ -58,7 +62,7 @@ CROSSIMG_PREFIX=zenoh-pico_
# NOTES:
# - ARM: old versions of dockcross/dockcross were creating some issues since they used an old GCC (4.8.3) which lacks <stdatomic.h> (even using -std=gnu11)

CMAKE_OPT=-DZENOH_DEBUG=$(ZENOH_DEBUG) -DBUILD_EXAMPLES=$(BUILD_EXAMPLES) -DCMAKE_BUILD_TYPE=$(BUILD_TYPE) -DBUILD_TESTING=$(BUILD_TESTING) -DBUILD_MULTICAST=$(BUILD_MULTICAST) -DBUILD_INTEGRATION=$(BUILD_INTEGRATION) -DBUILD_TOOLS=$(BUILD_TOOLS) -DBUILD_SHARED_LIBS=$(BUILD_SHARED_LIBS) -H.
CMAKE_OPT=-DZENOH_DEBUG=$(ZENOH_DEBUG) -DBUILD_EXAMPLES=$(BUILD_EXAMPLES) -DBUILD_WITH_CUSTOM_ALLOCATOR=$(BUILD_WITH_CUSTOM_ALLOCATOR) -DCMAKE_BUILD_TYPE=$(BUILD_TYPE) -DBUILD_TESTING=$(BUILD_TESTING) -DBUILD_MULTICAST=$(BUILD_MULTICAST) -DBUILD_INTEGRATION=$(BUILD_INTEGRATION) -DBUILD_TOOLS=$(BUILD_TOOLS) -DBUILD_SHARED_LIBS=$(BUILD_SHARED_LIBS) -H.

all: make

Expand Down
122 changes: 73 additions & 49 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,62 +1,86 @@
#
# 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]>
#
if(${CMAKE_SOURCE_DIR} STREQUAL ${CMAKE_CURRENT_SOURCE_DIR})
# Settings when 'examples' is the root projet
cmake_minimum_required(VERSION 3.20)
project(zenohpico_examples LANGUAGES C)
include(../cmake/helpers.cmake)
set_default_build_type(Release)
configure_include_project(ZENOHPICO zenohpico zenohpico ".." zenohc "https://github.com/eclipse-zenoh/zenoh-pico" "")
add_custom_target(examples ALL)
# Settings when 'examples' is the root projet
cmake_minimum_required(VERSION 3.20)
project(zenohpico_examples LANGUAGES C)
include(../cmake/helpers.cmake)
set_default_build_type(Release)
configure_include_project(ZENOHPICO zenohpico zenohpico ".." zenohc "https://github.com/eclipse-zenoh/zenoh-pico" "")
add_custom_target(examples ALL)
else()
message(STATUS "zenoh-pico examples")
add_custom_target(examples)
message(STATUS "zenoh-pico examples")
add_custom_target(examples)
endif()

function(add_example name)
add_executable(${name} ${ARGN})
set_property(TARGET ${name} PROPERTY C_STANDARD 11)
target_link_libraries(${name} zenohpico)
add_dependencies(examples ${name})
add_executable(${name} ${ARGN})
set_property(TARGET ${name} PROPERTY C_STANDARD 11)
target_link_libraries(${name} zenohpico)
add_dependencies(examples ${name})
endfunction()

if(UNIX)
if(BUILD_WITH_CUSTOM_ALLOCATOR)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/examples)
if(CMAKE_C_STANDARD MATCHES "99")
add_example(z_put unix/c99/z_put.c)
add_example(z_pub unix/c99/z_pub.c)
add_example(z_pub_st unix/c99/z_pub_st.c)
add_example(z_sub unix/c99/z_sub.c)
add_example(z_sub_st unix/c99/z_sub_st.c)
add_example(z_pull unix/c99/z_pull.c)
add_example(z_get unix/c99/z_get.c)
add_example(z_queryable unix/c99/z_queryable.c)
add_example(z_info unix/c99/z_info.c)
add_example(z_scout unix/c99/z_scout.c)
add_example(z_ping unix/c99/z_ping.c)
add_example(z_pong unix/c99/z_pong.c)
add_executable(z_pub_ualloc ${PROJECT_SOURCE_DIR}/examples/unix/c99/z_pub_ualloc.c)
else()
add_example(z_put unix/c11/z_put.c)
add_example(z_pub unix/c11/z_pub.c)
add_example(z_pub_st unix/c11/z_pub_st.c)
add_example(z_sub unix/c11/z_sub.c)
add_example(z_sub_st unix/c11/z_sub_st.c)
add_example(z_pull unix/c11/z_pull.c)
add_example(z_get unix/c11/z_get.c)
add_example(z_queryable unix/c11/z_queryable.c)
add_example(z_info unix/c11/z_info.c)
add_example(z_scout unix/c11/z_scout.c)
add_example(z_ping unix/c11/z_ping.c)
add_example(z_pong unix/c11/z_pong.c)
add_executable(z_pub_ualloc ${PROJECT_SOURCE_DIR}/examples/unix/c11/z_pub_ualloc.c)
endif()

target_link_libraries(z_pub_ualloc ${Libname})
else()
if(CMAKE_C_STANDARD MATCHES "99")
add_example(z_put unix/c99/z_put.c)
add_example(z_pub unix/c99/z_pub.c)
add_example(z_pub_st unix/c99/z_pub_st.c)
add_example(z_sub unix/c99/z_sub.c)
add_example(z_sub_st unix/c99/z_sub_st.c)
add_example(z_pull unix/c99/z_pull.c)
add_example(z_get unix/c99/z_get.c)
add_example(z_queryable unix/c99/z_queryable.c)
add_example(z_info unix/c99/z_info.c)
add_example(z_scout unix/c99/z_scout.c)
add_example(z_ping unix/c99/z_ping.c)
add_example(z_pong unix/c99/z_pong.c)
else()
add_example(z_put unix/c11/z_put.c)
add_example(z_pub unix/c11/z_pub.c)
add_example(z_pub_st unix/c11/z_pub_st.c)
add_example(z_sub unix/c11/z_sub.c)
add_example(z_sub_st unix/c11/z_sub_st.c)
add_example(z_pull unix/c11/z_pull.c)
add_example(z_get unix/c11/z_get.c)
add_example(z_queryable unix/c11/z_queryable.c)
add_example(z_info unix/c11/z_info.c)
add_example(z_scout unix/c11/z_scout.c)
add_example(z_ping unix/c11/z_ping.c)
add_example(z_pong unix/c11/z_pong.c)
endif()
endif()
elseif(MSVC)
add_example(z_put windows/z_put.c)
add_example(z_pub windows/z_pub.c)
add_example(z_pub_st windows/z_pub_st.c)
add_example(z_sub windows/z_sub.c)
add_example(z_sub_st windows/z_sub_st.c)
add_example(z_pull windows/z_pull.c)
add_example(z_get windows/z_get.c)
add_example(z_queryable windows/z_queryable.c)
add_example(z_info windows/z_info.c)
add_example(z_scout windows/z_scout.c)
add_example(z_ping windows/z_ping.c)
add_example(z_pong windows/z_pong.c)
add_example(z_put windows/z_put.c)
add_example(z_pub windows/z_pub.c)
add_example(z_pub_st windows/z_pub_st.c)
add_example(z_sub windows/z_sub.c)
add_example(z_sub_st windows/z_sub_st.c)
add_example(z_pull windows/z_pull.c)
add_example(z_get windows/z_get.c)
add_example(z_queryable windows/z_queryable.c)
add_example(z_info windows/z_info.c)
add_example(z_scout windows/z_scout.c)
add_example(z_ping windows/z_ping.c)
add_example(z_pong windows/z_pong.c)
endif()
20 changes: 10 additions & 10 deletions examples/unix/c11/z_ping.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@

// WARNING: for the sake of this example we are using "internal" structs and functions (starting with "_").
// Synchronisation primitives are planned to be added to the API in the future.
_z_condvar_t cond;
_z_mutex_t mutex;
z_condvar_t cond;
z_mutex_t mutex;

void callback(const z_sample_t* sample, void* context) {
(void)sample;
(void)context;
_z_condvar_signal(&cond);
z_condvar_signal(&cond);
}
void drop(void* context) {
(void)context;
_z_condvar_free(&cond);
z_condvar_free(&cond);
}

struct args_t {
Expand All @@ -42,8 +42,8 @@ int main(int argc, char** argv) {
");
return 1;
}
_z_mutex_init(&mutex);
_z_condvar_init(&cond);
z_mutex_init(&mutex);
z_condvar_init(&cond);
z_owned_config_t config = z_config_default();
z_owned_session_t session = z_open(z_move(config));
if (!z_check(session)) {
Expand Down Expand Up @@ -76,28 +76,28 @@ int main(int argc, char** argv) {
for (unsigned int i = 0; i < args.size; i++) {
data[i] = i % 10;
}
_z_mutex_lock(&mutex);
z_mutex_lock(&mutex);
if (args.warmup_ms) {
printf("Warming up for %dms...\n", args.warmup_ms);
clock_t warmup_end = clock() + CLOCKS_PER_SEC * args.warmup_ms / 1000;
for (clock_t now = clock(); now < warmup_end; now = clock()) {
z_publisher_put(z_loan(pub), data, args.size, NULL);
_z_condvar_wait(&cond, &mutex);
z_condvar_wait(&cond, &mutex);
}
}
clock_t* results = z_malloc(sizeof(clock_t) * args.number_of_pings);
for (unsigned int i = 0; i < args.number_of_pings; i++) {
clock_t start = clock();
z_publisher_put(z_loan(pub), data, args.size, NULL);
_z_condvar_wait(&cond, &mutex);
z_condvar_wait(&cond, &mutex);
clock_t end = clock();
results[i] = end - start;
}
for (unsigned int i = 0; i < args.number_of_pings; i++) {
clock_t rtt = results[i] * 1000000 / CLOCKS_PER_SEC;
printf("%d bytes: seq=%d rtt=%ldµs lat=%ldµs\n", args.size, i, rtt, rtt / 2);
}
_z_mutex_unlock(&mutex);
z_mutex_unlock(&mutex);
z_free(results);
z_free(data);
z_drop(z_move(pub));
Expand Down
Loading