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

add memory-related API #3

Merged
merged 1 commit into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions docs/examples.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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));
}
}

Expand Down
8 changes: 4 additions & 4 deletions examples/z_ping.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
Expand All @@ -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));
Expand Down
2 changes: 1 addition & 1 deletion examples/z_pub_thr.c
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 2 additions & 2 deletions examples/z_scout.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions examples/z_sub_thr.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions include/zenoh.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,5 @@ extern "C" {
}
#endif
#include "zenoh_macros.h"
#include "zenoh_memory.h"
#endif
7 changes: 7 additions & 0 deletions include/zenoh_memory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#pragma once
#include <stdlib.h>

/*------------------ 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); }
4 changes: 2 additions & 2 deletions tests/z_api_alignment_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -374,7 +374,7 @@ int main(int argc, char **argv) {

sleep(SLEEP * 5);

free(keyexpr_str);
z_free(keyexpr_str);

return 0;
}
Loading