From 2d479b8393cfacb4416c06fab9ae3d40c2cc60c7 Mon Sep 17 00:00:00 2001
From: Tommy Olofsson <tommy.olofsson.90@gmail.com>
Date: Fri, 18 Apr 2014 13:31:39 +0200
Subject: [PATCH] Fixed a problem with copy for strings.

---
 compiler/C_CodeGen.jrag        | 18 +++++++--
 lib/c/Makefile                 |  5 ++-
 lib/c/test/test_labcomm_copy.c | 72 ++++++++++++++++++++++++++++------
 3 files changed, 79 insertions(+), 16 deletions(-)

diff --git a/compiler/C_CodeGen.jrag b/compiler/C_CodeGen.jrag
index 4a7f03a..50bc2e3 100644
--- a/compiler/C_CodeGen.jrag
+++ b/compiler/C_CodeGen.jrag
@@ -743,8 +743,20 @@ aspect C_copy {
   }
 
   public void PrimType.C_emitCopy(C_env env_src, C_env env_dst) {
-    env_src.println(env_dst.accessor() + env_dst.qualid + " = " +
-		    env_src.accessor() + env_src.qualid + ";");
+    if (C_isDynamic()) {
+      env_src.println(String.format(
+	  "%s%s = labcomm_memory_alloc(mem, 1, strlen(%s%s)+1);",
+	  env_dst.accessor(), env_dst.qualid,
+	  env_src.accessor(), env_src.qualid));
+      env_src.println(String.format(
+	  "memcpy(%s%s, %s%s, strlen(%s%s)+1);",
+	  env_dst.accessor(), env_dst.qualid,
+	  env_src.accessor(), env_src.qualid,
+	  env_src.accessor(), env_src.qualid));
+    } else {
+      env_src.println(env_dst.accessor() + env_dst.qualid + " = " +
+		      env_src.accessor() + env_src.qualid + ";");
+    }
   }
 
   public void UserType.C_emitCopy(C_env env_src, C_env env_dst) {
@@ -880,7 +892,7 @@ aspect C_copy {
   public void PrimType.C_emitCopyDeallocation(C_env env) {
     if (C_isDynamic()) {
       env.println("labcomm_memory_free(mem, 1, " +
-                  env.qualid + ");");
+                  env.accessor() + env.qualid + ");");
     }
   }
 
diff --git a/lib/c/Makefile b/lib/c/Makefile
index fc601a6..b60a962 100644
--- a/lib/c/Makefile
+++ b/lib/c/Makefile
@@ -3,7 +3,7 @@ UNAME_S=$(shell uname -s)
 ALL_DEPS=liblabcomm.a liblabcomm.so.1
 
 ifeq ($(UNAME_S),Linux)
-  CFLAGS=-std=c99 -g -Wall -Werror -O3  -I. -Itest -Wno-unused-function
+  CFLAGS=-std=c99 -g -Wall -Werror -O3  -I. -Itest
   CC=$(CROSS_COMPILE)gcc
   LD=$(CROSS_COMPILE)ld
   LDFLAGS=-L.
@@ -134,6 +134,7 @@ clean:
 	$(RM) test/testdata/gen/*.[cho]
 	$(RM) test/gen/*.[cho]
 	$(RM) $(TEST_DIR)/test_labcomm
+	$(RM) $(TEST_DIR)/test_labcomm_copy
 
 distclean: clean
 	$(RM) liblabcomm.so.1
@@ -151,7 +152,7 @@ $(TEST_DIR)/test_signature_numbers.c: $(TEST_DIR)/gen/another_encoding.h
 $(TEST_DIR)/test_signature_numbers.c: $(TEST_DIR)/gen/generated_encoding.h
 $(TEST_DIR)/test_signature_numbers: $(TEST_DIR)/gen/another_encoding.o
 $(TEST_DIR)/test_signature_numbers: $(TEST_DIR)/gen/generated_encoding.o
-$(TEST_DIR)/test_labcomm_copy:  $(TEST_DIR)/gen/generated_encoding.o $(TEST_DIR)/gen/test_sample.o
+$(TEST_DIR)/test_labcomm_copy:  $(TEST_DIR)/gen/generated_encoding.o $(TEST_DIR)/gen/test_sample.o $(TEST_DIR)/gen/more_types.o
 labcomm_fd_reader.o: labcomm_private.h
 labcomm_fd_writer.o: labcomm_private.h
 labcomm_dynamic_buffer_writer.o: labcomm_private.h
diff --git a/lib/c/test/test_labcomm_copy.c b/lib/c/test/test_labcomm_copy.c
index edadd4d..6fc204b 100644
--- a/lib/c/test/test_labcomm_copy.c
+++ b/lib/c/test/test_labcomm_copy.c
@@ -15,6 +15,7 @@
 #include "labcomm_fd_reader.h"
 #include "test/gen/generated_encoding.h"
 #include "test/gen/test_sample.h"
+#include "test/gen/more_types.h"
 
 #define DATA_FILE "copy_test.dat"
 
@@ -43,6 +44,21 @@ static void handle_test_var(test_sample_test_var *v, void *context)
   labcomm_copy_test_sample_test_var(labcomm_default_memory, context, v);
 }
 
+static void handle_a(more_types_A *v, void *context)
+{
+  labcomm_copy_more_types_A(labcomm_default_memory, context, v);
+}
+
+static void handle_s(more_types_S *v, void *context)
+{
+  labcomm_copy_more_types_S(labcomm_default_memory, context, v);
+}
+
+static void handle_ns(more_types_NS *v, void *context)
+{
+  labcomm_copy_more_types_NS(labcomm_default_memory, context, v);
+}
+
 int main(int argc, char **argv)
 {
   struct labcomm_encoder *encoder;
@@ -58,6 +74,12 @@ int main(int argc, char **argv)
   generated_encoding_P cache_p;
   test_sample_test_var test_var;
   test_sample_test_var cache_test_var;
+  more_types_A a;
+  more_types_A cache_a;
+  more_types_S s;
+  more_types_S cache_s = NULL;
+  more_types_NS ns;
+  more_types_NS cache_ns;
 
   fd = open(DATA_FILE, O_RDWR | O_CREAT | O_TRUNC, 0644);
   if (fd == -1)
@@ -100,6 +122,19 @@ int main(int argc, char **argv)
       test_var.a[i] = 10 * i + j;
   labcomm_encode_test_sample_test_var(encoder, &test_var);
 
+  labcomm_encoder_register_more_types_A(encoder);
+  for (int i = 0; i < sizeof(a.a) / sizeof(a.a[0]); i++)
+    a.a[i] = i;
+  labcomm_encode_more_types_A(encoder, &a);
+
+  labcomm_encoder_register_more_types_S(encoder);
+  s = "this is a string";
+  labcomm_encode_more_types_S(encoder, &s);
+
+  labcomm_encoder_register_more_types_NS(encoder);
+  ns.s = "this is a another string";
+  labcomm_encode_more_types_NS(encoder, &ns);
+
   labcomm_encoder_free(encoder);
   encoder = NULL;
   lseek(fd, 0, SEEK_SET);
@@ -112,35 +147,34 @@ int main(int argc, char **argv)
   labcomm_decoder_register_generated_encoding_S1(decoder, handle_s1, &cache_s1);
   labcomm_decoder_register_generated_encoding_B(decoder, handle_b, &cache_b);
   labcomm_decoder_register_generated_encoding_I(decoder, handle_i, &cache_I);
+  labcomm_decoder_register_generated_encoding_P(decoder, handle_p, &cache_p);
   labcomm_decoder_register_test_sample_test_var(decoder, handle_test_var,
 						&cache_test_var);
-  labcomm_decoder_register_generated_encoding_P(decoder, handle_p, &cache_p);
-  labcomm_decoder_decode_one(decoder); /* S1 */
-  labcomm_decoder_decode_one(decoder);
-  labcomm_decoder_decode_one(decoder); /* B */
-  labcomm_decoder_decode_one(decoder);
-  labcomm_decoder_decode_one(decoder); /* I */
-  labcomm_decoder_decode_one(decoder);
-  labcomm_decoder_decode_one(decoder); /* P */
-  labcomm_decoder_decode_one(decoder);
-  labcomm_decoder_decode_one(decoder); /* test_var */
-  labcomm_decoder_decode_one(decoder);
+  labcomm_decoder_register_more_types_A(decoder, handle_a, &cache_a);
+  labcomm_decoder_register_more_types_S(decoder, handle_s, &cache_s);
+  labcomm_decoder_register_more_types_NS(decoder, handle_ns, &cache_ns);
+
+  while (labcomm_decoder_decode_one(decoder) > 0) ;
 
   assert(cache_s1.i == s1.i);
   puts("S1 copied ok");
+
   assert(cache_b == b);
   puts("B copied ok");
+
   assert(cache_I.n_0 == I.n_0);
   assert(cache_I.a[0] == I.a[0]);
   assert(cache_I.a[1] == I.a[1]);
   assert(cache_I.a[2] == I.a[2]);
   free(I.a);
   puts("I copied ok");
+
   assert(cache_p.n_0 == p.n_0);
   for (int i = 0; i < p.n_0; i++)
     assert(cache_p.a[i].i == p.a[i].i);
   free(p.a);
   puts("P copied ok");
+
   assert(cache_test_var.n_0 == test_var.n_0);
   assert(cache_test_var.n_1 == test_var.n_1);
   for (int i = 0; i < test_var.n_0; i++)
@@ -149,6 +183,16 @@ int main(int argc, char **argv)
   free(test_var.a);
   puts("test_var copied ok");
 
+  for (int i = 0; i < sizeof(a.a) / sizeof(a.a[0]); i++)
+    assert(cache_a.a[i] == a.a[i]);
+  puts("A copied ok");
+
+  assert(!strcmp(cache_s, s));
+  puts("S copied ok");
+
+  assert(!strcmp(cache_ns.s, ns.s));
+  puts("NS copied ok");
+
   labcomm_decoder_free(decoder);
   close(fd);
   unlink(DATA_FILE);
@@ -163,4 +207,10 @@ int main(int argc, char **argv)
   puts("P deallocated ok");
   labcomm_copy_free_test_sample_test_var(labcomm_default_memory, &cache_test_var);
   puts("test_var deallocated ok");
+  labcomm_copy_free_more_types_A(labcomm_default_memory, &cache_a);
+  puts("A deallocated ok");
+  labcomm_copy_free_more_types_S(labcomm_default_memory, &cache_s);
+  puts("S deallocated ok");
+  labcomm_copy_free_more_types_NS(labcomm_default_memory, &cache_ns);
+  puts("NS deallocated ok");
 }
-- 
GitLab