comparison src/shape_path.c @ 145:609ed47a58f2

Decrease number of malloc().
author Thinker K.F. Li <thinker@branda.to>
date Thu, 25 Sep 2008 02:13:50 +0800
parents abb5511c23f7
children e96a584487af
comparison
equal deleted inserted replaced
144:7444dea34c6b 145:609ed47a58f2
298 298
299 static void sh_path_free(shape_t *shape) { 299 static void sh_path_free(shape_t *shape) {
300 sh_path_t *path = (sh_path_t *)shape; 300 sh_path_t *path = (sh_path_t *)shape;
301 if(path->user_data) 301 if(path->user_data)
302 free(path->user_data); 302 free(path->user_data);
303 if(path->dev_data)
304 free(path->dev_data);
305 free(path); 303 free(path);
306 } 304 }
307 305
308 /*! \brief Count number of arguments. 306 /*! \brief Count number of arguments.
309 * 307 *
683 memset(&path->shape, 0, sizeof(shape_t)); 681 memset(&path->shape, 0, sizeof(shape_t));
684 path->shape.sh_type = SHT_PATH; 682 path->shape.sh_type = SHT_PATH;
685 path->cmd_len = cmd_cnt; 683 path->cmd_len = cmd_cnt;
686 path->arg_len = arg_cnt; 684 path->arg_len = arg_cnt;
687 path->fix_arg_len = fix_arg_cnt; 685 path->fix_arg_len = fix_arg_cnt;
686
688 msz = cmd_cnt + sizeof(co_aix) * arg_cnt + sizeof(int) * fix_arg_cnt; 687 msz = cmd_cnt + sizeof(co_aix) * arg_cnt + sizeof(int) * fix_arg_cnt;
689 path->user_data = (char *)malloc(msz); 688 path->user_data = (char *)malloc(msz * 2);
690 if(path->user_data == NULL) { 689 if(path->user_data == NULL) {
691 free(path); 690 free(path);
692 return NULL; 691 return NULL;
693 } 692 }
694 path->dev_data = (char *)malloc(msz); 693
695 if(path->dev_data == NULL) { 694 path->dev_data = path->user_data + msz;
696 free(path->dev_data); 695
697 free(path);
698 return NULL;
699 }
700
701 memset(path->user_data, 0, cmd_cnt);
702 r = sh_path_cmd_arg_fill(data, path); 696 r = sh_path_cmd_arg_fill(data, path);
703 if(r == ERR) { 697 if(r == ERR) {
704 free(path->user_data); 698 free(path->user_data);
705 free(path->dev_data);
706 free(path); 699 free(path);
707 return NULL; 700 return NULL;
708 } 701 }
709 memcpy(path->dev_data, path->user_data, msz); 702 memcpy(path->dev_data, path->user_data, msz);
710 703
845 838
846 path = (sh_path_t *)sh_path_new("M 33 25l33 55c 33 87 44 22 55 99L33 77z"); 839 path = (sh_path_t *)sh_path_new("M 33 25l33 55c 33 87 44 22 55 99L33 77z");
847 CU_ASSERT(path != NULL); 840 CU_ASSERT(path != NULL);
848 CU_ASSERT(path->cmd_len == ((5 + RESERVED_AIXS + 3) & ~0x3)); 841 CU_ASSERT(path->cmd_len == ((5 + RESERVED_AIXS + 3) & ~0x3));
849 CU_ASSERT(path->arg_len == 12); 842 CU_ASSERT(path->arg_len == 12);
850 CU_ASSERT(strcmp(path->user_data, "MLCLZ") == 0); 843 CU_ASSERT(strncmp(path->user_data, "MLCLZ", 5) == 0);
851 CU_ASSERT(strcmp(path->dev_data, "MLCLZ") == 0); 844 CU_ASSERT(strncmp(path->dev_data, "MLCLZ", 5) == 0);
852 845
853 args = (co_aix *)(path->user_data + path->cmd_len); 846 args = (co_aix *)(path->user_data + path->cmd_len);
854 CU_ASSERT(args[0] == 33); 847 CU_ASSERT(args[0] == 33);
855 CU_ASSERT(args[1] == 25); 848 CU_ASSERT(args[1] == 25);
856 CU_ASSERT(args[2] == 66); 849 CU_ASSERT(args[2] == 66);
874 867
875 path = (sh_path_t *)sh_path_new("M 33 25l33 55C 33 87 44 22 55 99L33 77z"); 868 path = (sh_path_t *)sh_path_new("M 33 25l33 55C 33 87 44 22 55 99L33 77z");
876 CU_ASSERT(path != NULL); 869 CU_ASSERT(path != NULL);
877 CU_ASSERT(path->cmd_len == ((5 + RESERVED_AIXS + 3) & ~0x3)); 870 CU_ASSERT(path->cmd_len == ((5 + RESERVED_AIXS + 3) & ~0x3));
878 CU_ASSERT(path->arg_len == 12); 871 CU_ASSERT(path->arg_len == 12);
879 CU_ASSERT(strcmp(path->user_data, "MLCLZ") == 0); 872 CU_ASSERT(strncmp(path->user_data, "MLCLZ", 5) == 0);
880 CU_ASSERT(strcmp(path->dev_data, "MLCLZ") == 0); 873 CU_ASSERT(strncmp(path->dev_data, "MLCLZ", 5) == 0);
881 874
882 geo_init(&geo); 875 geo_init(&geo);
883 path->shape.geo = &geo; 876 path->shape.geo = &geo;
884 geo.shape = (shape_t *)path; 877 geo.shape = (shape_t *)path;
885 878