From 3425f75440dc00172ebd8ba3620aee54f37b21f8 Mon Sep 17 00:00:00 2001 From: Rongsong Shen Date: Fri, 25 Apr 2025 10:21:04 +0800 Subject: [PATCH] improve ptr --- compiler/capnpc-c.c | 10 ++++++---- examples/book/book.h | 2 +- examples/book/test.c | 36 ++++++++++++++++++------------------ 3 files changed, 25 insertions(+), 23 deletions(-) diff --git a/compiler/capnpc-c.c b/compiler/capnpc-c.c index ff4fdca..4484632 100644 --- a/compiler/capnpc-c.c +++ b/compiler/capnpc-c.c @@ -1081,7 +1081,7 @@ static void encode_member(capnp_ctx_t *ctx, struct str *func, struct field *f, if (n != NULL) { str_add(func, tab, -1); - str_addf(func, "encode_%s_ptr(cs, &(d->%s), &(s->%s));\n", n->name.str, + str_addf(func, "encode_%s_ptr(cs, &(d->%s), s->%s);\n", n->name.str, var, var2); } break; @@ -1315,12 +1315,14 @@ void mk_struct_ptr_decoder(capnp_ctx_t *ctx, struct node *n) { } str_addf(&(ctx->SRC), - "void decode_%s_ptr(%s *d," + "void decode_%s_ptr(%s **d," "%s_ptr p) {\n", n->name.str, buf, n->name.str); str_addf(&(ctx->SRC), "\tstruct %s s;\n", n->name.str); + str_addf(&(ctx->SRC), "\t*d = (%s *)calloc(1, sizeof(%s));\n", + buf, buf); str_addf(&(ctx->SRC), "\tread_%s(&s, p);\n", n->name.str); - str_addf(&(ctx->SRC), "\tdecode_%s(d, &s);\n", n->name.str); + str_addf(&(ctx->SRC), "\tdecode_%s(*d, &s);\n", n->name.str); str_addf(&(ctx->SRC), "}\n"); } @@ -2092,7 +2094,7 @@ static void mk_codec_declares(capnp_ctx_t *ctx, const char *n1, str_addf(&(ctx->HDR), "void encode_%s_ptr(struct capn_segment*, %s_ptr *, %s *);\n", n1, n1, n2); - str_addf(&(ctx->HDR), "void decode_%s_ptr(%s *, %s_ptr);\n", n1, n2, n1); + str_addf(&(ctx->HDR), "void decode_%s_ptr(%s **, %s_ptr);\n", n1, n2, n1); } static void declare_codec(capnp_ctx_t *ctx, struct node *file_node) { struct node *n; diff --git a/examples/book/book.h b/examples/book/book.h index 544360f..d678176 100644 --- a/examples/book/book.h +++ b/examples/book/book.h @@ -22,7 +22,7 @@ typedef struct { char **authors; int n_chapters; chapter_t *chapters_; - publish_t publish; + publish_t *publish; int n_magic1; uint32_t *magic_1; int acquire_method; diff --git a/examples/book/test.c b/examples/book/test.c index ef8c731..7e771d1 100644 --- a/examples/book/test.c +++ b/examples/book/test.c @@ -44,7 +44,7 @@ int encode() { book.authors = authors; book.n_chapters = 3; book.chapters_ = &chapters[0]; - memcpy(&(book.publish), &publish, sizeof(publish)); + book.publish = &publish; book.n_magic1 = 2; book.magic_1 = &magic1[0]; book.acquire_method = Book_acquire_buy; @@ -67,7 +67,7 @@ int encode() { int decode() { struct capn c; Book_ptr p; - book_t book; + book_t *book; int i; capn_init_fp(&c, stdin, 0); @@ -75,36 +75,36 @@ int decode() { decode_Book_ptr(&book, p); - printf("title: %s\n", book.title); + printf("title: %s\n", book->title); - printf("authors(%d):\n", book.n_authors); + printf("authors(%d):\n", book->n_authors); - for(i = 0; i < book.n_authors; i ++) { - printf("\t%s\n", book.authors[i]); + for(i = 0; i < book->n_authors; i ++) { + printf("\t%s\n", book->authors[i]); } - printf("chapters(%d):\n", book.n_chapters); - for(i = 0; i < book.n_chapters; i ++) { - printf("\tcaption: %s\n", book.chapters_[i].caption); + printf("chapters(%d):\n", book->n_chapters); + for(i = 0; i < book->n_chapters; i ++) { + printf("\tcaption: %s\n", book->chapters_[i].caption); printf("\tfrom %d to %d\n", - book.chapters_[i].start, - book.chapters_[i].end); + book->chapters_[i].start, + book->chapters_[i].end); } printf("ISBN: %lu year: %u\n", - book.publish.isbn, - book.publish.year); + book->publish->isbn, + book->publish->year); printf("magic1:\n"); - for(i = 0; i < book.n_magic1; i ++) { - printf("\t%d\n", book.magic_1[i]); + for(i = 0; i < book->n_magic1; i ++) { + printf("\t%d\n", book->magic_1[i]); } - if (book.acquire_method == Book_acquire_buy) { - printf("%s\n", book.acquire.buy); + if (book->acquire_method == Book_acquire_buy) { + printf("%s\n", book->acquire.buy); } else { - printf("%s\n", book.acquire.donation); + printf("%s\n", book->acquire.donation); } return 0;