changeset 145:609ed47a58f2

Decrease number of malloc().
author Thinker K.F. Li <thinker@branda.to>
date Thu, 25 Sep 2008 02:13:50 +0800
parents 7444dea34c6b
children e96a584487af
files src/shape_path.c
diffstat 1 files changed, 8 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/src/shape_path.c	Thu Sep 25 01:47:14 2008 +0800
+++ b/src/shape_path.c	Thu Sep 25 02:13:50 2008 +0800
@@ -300,8 +300,6 @@
     sh_path_t *path = (sh_path_t *)shape;
     if(path->user_data)
 	free(path->user_data);
-    if(path->dev_data)
-	free(path->dev_data);
     free(path);
 }
 
@@ -685,24 +683,19 @@
     path->cmd_len = cmd_cnt;
     path->arg_len = arg_cnt;
     path->fix_arg_len = fix_arg_cnt;
+
     msz = cmd_cnt + sizeof(co_aix) * arg_cnt + sizeof(int) * fix_arg_cnt;
-    path->user_data = (char *)malloc(msz);
+    path->user_data = (char *)malloc(msz * 2);
     if(path->user_data == NULL) {
 	free(path);
 	return NULL;
     }
-    path->dev_data = (char *)malloc(msz);
-    if(path->dev_data == NULL) {
-	free(path->dev_data);
-	free(path);
-	return NULL;
-    }
 
-    memset(path->user_data, 0, cmd_cnt);
+    path->dev_data = path->user_data + msz;
+
     r = sh_path_cmd_arg_fill(data, path);
     if(r == ERR) {
 	free(path->user_data);
-	free(path->dev_data);
 	free(path);
 	return NULL;
     }
@@ -847,8 +840,8 @@
     CU_ASSERT(path != NULL);
     CU_ASSERT(path->cmd_len == ((5 + RESERVED_AIXS + 3) & ~0x3));
     CU_ASSERT(path->arg_len == 12);
-    CU_ASSERT(strcmp(path->user_data, "MLCLZ") == 0);
-    CU_ASSERT(strcmp(path->dev_data, "MLCLZ") == 0);
+    CU_ASSERT(strncmp(path->user_data, "MLCLZ", 5) == 0);
+    CU_ASSERT(strncmp(path->dev_data, "MLCLZ", 5) == 0);
 
     args = (co_aix *)(path->user_data + path->cmd_len);
     CU_ASSERT(args[0] == 33);
@@ -876,8 +869,8 @@
     CU_ASSERT(path != NULL);
     CU_ASSERT(path->cmd_len == ((5 + RESERVED_AIXS + 3) & ~0x3));
     CU_ASSERT(path->arg_len == 12);
-    CU_ASSERT(strcmp(path->user_data, "MLCLZ") == 0);
-    CU_ASSERT(strcmp(path->dev_data, "MLCLZ") == 0);
+    CU_ASSERT(strncmp(path->user_data, "MLCLZ", 5) == 0);
+    CU_ASSERT(strncmp(path->dev_data, "MLCLZ", 5) == 0);
 
     geo_init(&geo);
     path->shape.geo = &geo;