Mercurial > MadButterfly
comparison src/shape_path.c @ 865:48df0f97f09e
Allocate sh_path_t objects from an elmpool
author | Thinker K.F. Li <thinker@codemud.net> |
---|---|
date | Thu, 23 Sep 2010 09:55:37 +0800 |
parents | 586e50f82c1f |
children | a8d20bc8ce40 |
comparison
equal
deleted
inserted
replaced
864:6eaeec6806f2 | 865:48df0f97f09e |
---|---|
23 int cmd_len; | 23 int cmd_len; |
24 int pnt_len; | 24 int pnt_len; |
25 int float_arg_len; | 25 int float_arg_len; |
26 char *user_data; | 26 char *user_data; |
27 char *dev_data; /* device space data */ | 27 char *dev_data; /* device space data */ |
28 | |
29 redraw_man_t *rdman; /*!< \brief This is used by sh_path_free() */ | |
28 } sh_path_t; | 30 } sh_path_t; |
29 #define RESERVED_AIXS sizeof(co_aix[2]) | 31 #define RESERVED_AIXS sizeof(co_aix[2]) |
32 | |
33 int _sh_path_size = sizeof(sh_path_t); | |
30 | 34 |
31 #define ASSERT(x) | 35 #define ASSERT(x) |
32 #define SKIP_SPACE(x) while(*(x) && (isspace(*(x)) || *(x) == ',')) { (x)++; } | 36 #define SKIP_SPACE(x) while(*(x) && (isspace(*(x)) || *(x) == ',')) { (x)++; } |
33 #define SKIP_NUM(x) \ | 37 #define SKIP_NUM(x) \ |
34 while(*(x) && \ | 38 while(*(x) && \ |
45 #define PI 3.1415926535897931 | 49 #define PI 3.1415926535897931 |
46 | 50 |
47 #ifdef UNITTEST | 51 #ifdef UNITTEST |
48 #undef rdman_shape_man | 52 #undef rdman_shape_man |
49 #define rdman_shape_man(x, y) | 53 #define rdman_shape_man(x, y) |
54 | |
55 #undef elmpool_elm_alloc | |
56 #define elmpool_elm_alloc(pool) _elmpool_elm_alloc(pool) | |
57 static void * | |
58 _elmpool_elm_alloc(void *dummy) { | |
59 return malloc(sizeof(sh_path_t)); | |
60 } | |
61 | |
62 #undef elmpool_elm_free | |
63 #define elmpool_elm_free(pool, elm) _elmpool_elm_free(pool, elm) | |
64 static void | |
65 _elmpool_elm_free(void *pool, void *elm) { | |
66 free(elm); | |
67 } | |
50 #endif | 68 #endif |
51 | 69 |
52 /* ============================================================ | 70 /* ============================================================ |
53 * Implement arc in path. | 71 * Implement arc in path. |
54 */ | 72 */ |
380 sh_path_t *path = (sh_path_t *)shape; | 398 sh_path_t *path = (sh_path_t *)shape; |
381 | 399 |
382 mb_obj_destroy(path); | 400 mb_obj_destroy(path); |
383 if(path->user_data) | 401 if(path->user_data) |
384 free(path->user_data); | 402 free(path->user_data); |
385 free(path); | 403 elmpool_elm_free(path->rdman->sh_path_pool, path); |
386 } | 404 } |
387 | 405 |
388 /*! \brief Count number of arguments. | 406 /*! \brief Count number of arguments. |
389 * | 407 * |
390 * \todo Notify programmers that syntax or value error of path data. | 408 * \todo Notify programmers that syntax or value error of path data. |
798 */ | 816 */ |
799 cmd_cnt += RESERVED_AIXS; | 817 cmd_cnt += RESERVED_AIXS; |
800 cmd_cnt = (cmd_cnt + 3) & ~0x3; | 818 cmd_cnt = (cmd_cnt + 3) & ~0x3; |
801 | 819 |
802 /*! \todo Use elmpool to manage sh_path_t objects. */ | 820 /*! \todo Use elmpool to manage sh_path_t objects. */ |
803 path = (sh_path_t *)malloc(sizeof(sh_path_t)); | 821 path = (sh_path_t *)elmpool_elm_alloc(rdman->sh_path_pool); |
804 /*! \todo Remove this memset()? */ | 822 /*! \todo Remove this memset()? */ |
805 memset(&path->shape, 0, sizeof(shape_t)); | 823 memset(&path->shape, 0, sizeof(shape_t)); |
806 mb_obj_init(path, MBO_PATH); | 824 mb_obj_init(path, MBO_PATH); |
807 path->cmd_len = cmd_cnt; | 825 path->cmd_len = cmd_cnt; |
808 path->pnt_len = pnt_cnt; | 826 path->pnt_len = pnt_cnt; |
810 | 828 |
811 msz = cmd_cnt + sizeof(co_aix) * pnt_cnt + | 829 msz = cmd_cnt + sizeof(co_aix) * pnt_cnt + |
812 sizeof(co_aix) * float_arg_cnt; | 830 sizeof(co_aix) * float_arg_cnt; |
813 path->user_data = (char *)malloc(msz * 2); | 831 path->user_data = (char *)malloc(msz * 2); |
814 if(path->user_data == NULL) { | 832 if(path->user_data == NULL) { |
815 free(path); | 833 elmpool_elm_free(rdman->sh_path_pool, path); |
816 return NULL; | 834 return NULL; |
817 } | 835 } |
818 | 836 |
819 path->dev_data = path->user_data + msz; | 837 path->dev_data = path->user_data + msz; |
820 | 838 |
821 r = sh_path_cmd_arg_fill(data, path); | 839 r = sh_path_cmd_arg_fill(data, path); |
822 if(r == ERR) { | 840 if(r == ERR) { |
823 free(path->user_data); | 841 free(path->user_data); |
824 free(path); | 842 elmpool_elm_free(rdman->sh_path_pool, path); |
825 return NULL; | 843 return NULL; |
826 } | 844 } |
827 memcpy(path->dev_data, path->user_data, msz); | 845 memcpy(path->dev_data, path->user_data, msz); |
828 | 846 |
829 path->shape.free = sh_path_free; | 847 path->shape.free = sh_path_free; |
848 path->rdman = rdman; | |
830 | 849 |
831 rdman_shape_man(rdman, (shape_t *)path); | 850 rdman_shape_man(rdman, (shape_t *)path); |
832 | 851 |
833 return (shape_t *)path; | 852 return (shape_t *)path; |
834 } | 853 } |
841 sh_path_t *path; | 860 sh_path_t *path; |
842 int msz; | 861 int msz; |
843 int cmd_cnt = strlen(commands); | 862 int cmd_cnt = strlen(commands); |
844 | 863 |
845 /*! \todo Use elmpool to manage sh_path_t objects. */ | 864 /*! \todo Use elmpool to manage sh_path_t objects. */ |
846 path = (sh_path_t *)malloc(sizeof(sh_path_t)); | 865 path = (sh_path_t *)elmpool_elm_alloc(rdman->sh_path_pool); |
847 /*! \todo Remove this memset()? */ | 866 /*! \todo Remove this memset()? */ |
848 memset(&path->shape, 0, sizeof(shape_t)); | 867 memset(&path->shape, 0, sizeof(shape_t)); |
849 mb_obj_init(path, MBO_PATH); | 868 mb_obj_init(path, MBO_PATH); |
850 cmd_cnt = (cmd_cnt + 3) & ~0x3; | 869 cmd_cnt = (cmd_cnt + 3) & ~0x3; |
851 path->cmd_len = cmd_cnt; | 870 path->cmd_len = cmd_cnt; |
853 path->float_arg_len = float_arg_cnt; | 872 path->float_arg_len = float_arg_cnt; |
854 msz = cmd_cnt + sizeof(co_aix) * pnt_cnt + | 873 msz = cmd_cnt + sizeof(co_aix) * pnt_cnt + |
855 sizeof(co_aix) * float_arg_cnt; | 874 sizeof(co_aix) * float_arg_cnt; |
856 path->user_data = (char *)malloc(msz * 2); | 875 path->user_data = (char *)malloc(msz * 2); |
857 if(path->user_data == NULL) { | 876 if(path->user_data == NULL) { |
858 free(path); | 877 elmpool_elm_free(rdman->sh_path_pool, path); |
859 return NULL; | 878 return NULL; |
860 } | 879 } |
861 | 880 |
862 path->dev_data = path->user_data + msz; | 881 path->dev_data = path->user_data + msz; |
863 memcpy(path->user_data, commands, strlen(commands)); | 882 memcpy(path->user_data, commands, strlen(commands)); |
865 memcpy(path->user_data + cmd_cnt + pnt_cnt * sizeof(co_aix), | 884 memcpy(path->user_data + cmd_cnt + pnt_cnt * sizeof(co_aix), |
866 float_args, sizeof(co_aix) * float_arg_cnt); | 885 float_args, sizeof(co_aix) * float_arg_cnt); |
867 memcpy(path->dev_data, path->user_data, msz); | 886 memcpy(path->dev_data, path->user_data, msz); |
868 | 887 |
869 path->shape.free = sh_path_free; | 888 path->shape.free = sh_path_free; |
889 path->rdman = rdman; | |
870 | 890 |
871 rdman_shape_man(rdman, (shape_t *)path); | 891 rdman_shape_man(rdman, (shape_t *)path); |
872 | 892 |
873 return (shape_t *)path; | 893 return (shape_t *)path; |
874 } | 894 } |
1001 #include <CUnit/Basic.h> | 1021 #include <CUnit/Basic.h> |
1002 | 1022 |
1003 void test_rdman_shape_path_new(void) { | 1023 void test_rdman_shape_path_new(void) { |
1004 sh_path_t *path; | 1024 sh_path_t *path; |
1005 co_aix *pnts; | 1025 co_aix *pnts; |
1006 | 1026 redraw_man_t rdman; |
1007 path = (sh_path_t *)rdman_shape_path_new(NULL, "M 33 25l33 55c 33 87 44 22 55 99L33 77z"); | 1027 |
1028 path = (sh_path_t *)rdman_shape_path_new(&rdman, "M 33 25l33 55c 33 87 44 22 55 99L33 77z"); | |
1008 CU_ASSERT(path != NULL); | 1029 CU_ASSERT(path != NULL); |
1009 CU_ASSERT(path->cmd_len == ((5 + RESERVED_AIXS + 3) & ~0x3)); | 1030 CU_ASSERT(path->cmd_len == ((5 + RESERVED_AIXS + 3) & ~0x3)); |
1010 CU_ASSERT(path->pnt_len == 12); | 1031 CU_ASSERT(path->pnt_len == 12); |
1011 CU_ASSERT(strncmp(path->user_data, "MLCLZ", 5) == 0); | 1032 CU_ASSERT(strncmp(path->user_data, "MLCLZ", 5) == 0); |
1012 CU_ASSERT(strncmp(path->dev_data, "MLCLZ", 5) == 0); | 1033 CU_ASSERT(strncmp(path->dev_data, "MLCLZ", 5) == 0); |
1030 void test_path_transform(void) { | 1051 void test_path_transform(void) { |
1031 sh_path_t *path; | 1052 sh_path_t *path; |
1032 co_aix *pnts; | 1053 co_aix *pnts; |
1033 coord_t coord; | 1054 coord_t coord; |
1034 geo_t geo; | 1055 geo_t geo; |
1035 | 1056 redraw_man_t rdman; |
1036 path = (sh_path_t *)rdman_shape_path_new(NULL, "M 33 25l33 55C 33 87 44 22 55 99L33 77z"); | 1057 |
1058 path = (sh_path_t *)rdman_shape_path_new(&rdman, "M 33 25l33 55C 33 87 44 22 55 99L33 77z"); | |
1037 CU_ASSERT(path != NULL); | 1059 CU_ASSERT(path != NULL); |
1038 CU_ASSERT(path->cmd_len == ((5 + RESERVED_AIXS + 3) & ~0x3)); | 1060 CU_ASSERT(path->cmd_len == ((5 + RESERVED_AIXS + 3) & ~0x3)); |
1039 CU_ASSERT(path->pnt_len == 12); | 1061 CU_ASSERT(path->pnt_len == 12); |
1040 CU_ASSERT(strncmp(path->user_data, "MLCLZ", 5) == 0); | 1062 CU_ASSERT(strncmp(path->user_data, "MLCLZ", 5) == 0); |
1041 CU_ASSERT(strncmp(path->dev_data, "MLCLZ", 5) == 0); | 1063 CU_ASSERT(strncmp(path->dev_data, "MLCLZ", 5) == 0); |
1070 sh_path_free((shape_t *)path); | 1092 sh_path_free((shape_t *)path); |
1071 } | 1093 } |
1072 | 1094 |
1073 void test_spaces_head_tail(void) { | 1095 void test_spaces_head_tail(void) { |
1074 sh_path_t *path; | 1096 sh_path_t *path; |
1097 redraw_man_t rdman; | |
1075 | 1098 |
1076 path = (sh_path_t *) | 1099 path = (sh_path_t *) |
1077 rdman_shape_path_new(NULL, | 1100 rdman_shape_path_new(&rdman, |
1078 " M 33 25l33 55C 33 87 44 22 55 99L33 77z "); | 1101 " M 33 25l33 55C 33 87 44 22 55 99L33 77z "); |
1079 CU_ASSERT(path != NULL); | 1102 CU_ASSERT(path != NULL); |
1080 sh_path_free((shape_t *)path); | 1103 sh_path_free((shape_t *)path); |
1081 } | 1104 } |
1082 | 1105 |