From bf8d4f9e354fedcf0b13608c11afa0c5c717d1a8 Mon Sep 17 00:00:00 2001 From: Denis Biryukov Date: Wed, 28 Feb 2024 10:54:18 +0100 Subject: [PATCH] add memory-related API --- docs/examples.rst | 8 ++++---- examples/z_ping.c | 8 ++++---- examples/z_pub_thr.c | 2 +- examples/z_scout.c | 4 ++-- examples/z_sub_thr.c | 4 ++-- include/zenoh.h | 1 + include/zenoh_memory.h | 7 +++++++ tests/z_api_alignment_test.c | 4 ++-- 8 files changed, 23 insertions(+), 15 deletions(-) create mode 100644 include/zenoh_memory.h diff --git a/docs/examples.rst b/docs/examples.rst index 045187253..bea19cbbe 100644 --- a/docs/examples.rst +++ b/docs/examples.rst @@ -44,10 +44,10 @@ Subscribe #include "zenoh.h" void data_handler(const z_sample_t *sample, const void *arg) { - char *keystr = z_keyexpr_to_string(sample->keyexpr); + z_owned_str_t keystr = z_keyexpr_to_string(sample->keyexpr); printf(">> Received (%s, %.*s)\n", keystr, (int)sample->payload.len, sample->payload.start); - free(keystr); + z_drop(z_move(keystr)); } int main(int argc, char **argv) { @@ -87,9 +87,9 @@ Query if (z_reply_is_ok(&reply)) { z_sample_t sample = z_reply_ok(&reply); - char *keystr = z_keyexpr_to_string(sample.keyexpr); + z_owned_str_t keystr = z_keyexpr_to_string(sample.keyexpr); printf(">> Received ('%s': '%.*s')\n", keystr, (int)sample.payload.len, sample.payload.start); - free(keystr); + z_drop(z_move(keystr)); } } diff --git a/examples/z_ping.c b/examples/z_ping.c index 535f1d079..45393aaec 100644 --- a/examples/z_ping.c +++ b/examples/z_ping.c @@ -52,7 +52,7 @@ int main(int argc, char** argv) { z_owned_publisher_t pub = z_declare_publisher(z_loan(session), ping, NULL); z_owned_closure_sample_t respond = z_closure(callback, drop, (void*)(&pub)); z_owned_subscriber_t sub = z_declare_subscriber(z_loan(session), pong, z_move(respond), NULL); - uint8_t* data = malloc(args.size); + uint8_t* data = z_malloc(args.size); for (int i = 0; i < args.size; i++) { data[i] = i % 10; } @@ -76,7 +76,7 @@ int main(int argc, char** argv) { } } struct timespec t_start, t_stop, t_timeout; - unsigned long* results = malloc(sizeof(unsigned long) * args.number_of_pings); + unsigned long* results = z_malloc(sizeof(unsigned long) * args.number_of_pings); for (int i = 0; i < args.number_of_pings; i++) { clock_gettime(CLOCK_REALTIME, &t_timeout); t_timeout.tv_sec += PING_TIMEOUT_SEC; @@ -93,8 +93,8 @@ int main(int argc, char** argv) { printf("%d bytes: seq=%d rtt=%luµs, lat=%luµs\n", args.size, i, results[i], results[i] / 2); } pthread_mutex_unlock(&mutex); - free(results); - free(data); + z_free(results); + z_free(data); z_drop(z_move(sub)); z_drop(z_move(pub)); z_close(z_move(session)); diff --git a/examples/z_pub_thr.c b/examples/z_pub_thr.c index eb72432a0..8686c33eb 100644 --- a/examples/z_pub_thr.c +++ b/examples/z_pub_thr.c @@ -24,7 +24,7 @@ int main(int argc, char **argv) { char *keyexpr = "test/thr"; size_t len = atoi(argv[1]); - uint8_t *value = (uint8_t *)malloc(len); + uint8_t *value = (uint8_t *)z_malloc(len); memset(value, 1, len); z_owned_config_t config = z_config_default(); diff --git a/examples/z_scout.c b/examples/z_scout.c index ca5e8944b..3b882f547 100644 --- a/examples/z_scout.c +++ b/examples/z_scout.c @@ -83,14 +83,14 @@ void callback(z_owned_hello_t *hello, void *context) { void drop(void *context) { printf("Dropping scout\n"); int count = *(int *)context; - free(context); + z_free(context); if (!count) { printf("Did not find any zenoh process.\n"); } } int main(int argc, char **argv) { - int *context = malloc(sizeof(int)); + int *context = z_malloc(sizeof(int)); *context = 0; z_owned_scouting_config_t config = z_scouting_config_default(); z_owned_closure_hello_t closure = z_closure(callback, drop, context); diff --git a/examples/z_sub_thr.c b/examples/z_sub_thr.c index d82b6ea81..d32480174 100644 --- a/examples/z_sub_thr.c +++ b/examples/z_sub_thr.c @@ -26,7 +26,7 @@ typedef struct { } z_stats_t; z_stats_t *z_stats_make() { - z_stats_t *stats = malloc(sizeof(z_stats_t)); + z_stats_t *stats = z_malloc(sizeof(z_stats_t)); stats->count = 0; stats->finished_rounds = 0; stats->first_start.tv_nsec = 0; @@ -63,7 +63,7 @@ void drop_stats(void *context) { double elapsed_s = get_elapsed_s(&stats->first_start, &end); printf("Stats being dropped after unsubscribing: sent %ld messages over %f seconds (%f msg/s)\n", sent_messages, elapsed_s, (double)sent_messages / elapsed_s); - free(context); + z_free(context); } int main(int argc, char **argv) { diff --git a/include/zenoh.h b/include/zenoh.h index 94f90c2c4..444caf082 100644 --- a/include/zenoh.h +++ b/include/zenoh.h @@ -40,4 +40,5 @@ extern "C" { } #endif #include "zenoh_macros.h" +#include "zenoh_memory.h" #endif diff --git a/include/zenoh_memory.h b/include/zenoh_memory.h new file mode 100644 index 000000000..0cf2a095a --- /dev/null +++ b/include/zenoh_memory.h @@ -0,0 +1,7 @@ +#pragma once +#include + +/*------------------ Memory ------------------*/ +static inline void *z_malloc(size_t size) { return malloc(size); } +static inline void *z_realloc(void *ptr, size_t size) {return realloc(ptr, size); } +static inline void z_free(void *ptr) {return free(ptr); } \ No newline at end of file diff --git a/tests/z_api_alignment_test.c b/tests/z_api_alignment_test.c index 98ac3bc94..ee63017c2 100644 --- a/tests/z_api_alignment_test.c +++ b/tests/z_api_alignment_test.c @@ -138,7 +138,7 @@ int main(int argc, char **argv) { sleep(SLEEP); size_t keyexpr_len = strlen(URI); - char *keyexpr_str = (char *)malloc(keyexpr_len + 1); + char *keyexpr_str = (char *)z_malloc(keyexpr_len + 1); memcpy(keyexpr_str, URI, keyexpr_len); keyexpr_str[keyexpr_len] = '\0'; int8_t _ret_int8 = z_keyexpr_is_canon(keyexpr_str, keyexpr_len); @@ -374,7 +374,7 @@ int main(int argc, char **argv) { sleep(SLEEP * 5); - free(keyexpr_str); + z_free(keyexpr_str); return 0; }