Changeset 11356
- Timestamp:
- Oct 26, 2010, 7:30:35 PM (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/libtransmission/bencode.c
r11300 r11356 942 942 }; 943 943 944 static struct SaveNode*945 node NewDict(const tr_benc * val )944 static void 945 nodeInitDict( struct SaveNode * node, const tr_benc * val ) 946 946 { 947 947 int i, j; 948 948 int nKeys; 949 struct SaveNode * node;950 949 struct KeyIndex * indices; 951 950 … … 953 952 954 953 nKeys = val->val.l.count / 2; 955 node = tr_new0( struct SaveNode, 1 );956 954 node->val = val; 957 955 node->children = tr_new0( int, nKeys * 2 ); … … 974 972 assert( node->childCount == nKeys * 2 ); 975 973 tr_free( indices ); 976 return node; 977 } 978 979 static struct SaveNode* 980 nodeNewList( const tr_benc * val ) 974 } 975 976 static void 977 nodeInitList( struct SaveNode * node, const tr_benc * val ) 981 978 { 982 979 int i, n; 983 struct SaveNode * node;984 980 985 981 assert( tr_bencIsList( val ) ); 986 982 987 983 n = val->val.l.count; 988 node = tr_new0( struct SaveNode, 1 );989 984 node->val = val; 990 985 node->childCount = n; … … 992 987 for( i = 0; i < n; ++i ) /* a list's children don't need to be reordered */ 993 988 node->children[i] = i; 994 995 return node; 996 } 997 998 static struct SaveNode* 999 nodeNewLeaf( const tr_benc * val ) 1000 { 1001 struct SaveNode * node; 1002 989 } 990 991 static void 992 nodeInitLeaf( struct SaveNode * node, const tr_benc * val ) 993 { 1003 994 assert( !isContainer( val ) ); 1004 995 1005 node = tr_new0( struct SaveNode, 1 );1006 996 node->val = val; 1007 return node; 1008 } 1009 1010 static struct SaveNode* 1011 nodeNew( const tr_benc * val ) 1012 { 1013 struct SaveNode * node; 1014 1015 if( tr_bencIsList( val ) ) 1016 node = nodeNewList( val ); 1017 else if( tr_bencIsDict( val ) ) 1018 node = nodeNewDict( val ); 1019 else 1020 node = nodeNewLeaf( val ); 1021 1022 return node; 997 } 998 999 static void 1000 nodeInit( struct SaveNode * node, const tr_benc * val ) 1001 { 1002 static const struct SaveNode INIT_NODE = { NULL, 0, 0, 0, NULL }; 1003 *node = INIT_NODE; 1004 1005 if( tr_bencIsList( val ) ) nodeInitList( node, val ); 1006 else if( tr_bencIsDict( val ) ) nodeInitDict( node, val ); 1007 else nodeInitLeaf( node, val ); 1023 1008 } 1024 1009 … … 1046 1031 void * user_data ) 1047 1032 { 1048 tr_ptrArray stack = TR_PTR_ARRAY_INIT; 1049 1050 tr_ptrArrayAppend( &stack, nodeNew( top ) ); 1051 1052 while( !tr_ptrArrayEmpty( &stack ) ) 1033 int stackSize = 0; 1034 int stackAlloc = 64; 1035 struct SaveNode * stack = tr_new( struct SaveNode, stackAlloc ); 1036 1037 nodeInit( &stack[stackSize++], top ); 1038 1039 while( stackSize > 0 ) 1053 1040 { 1054 struct SaveNode * node = tr_ptrArrayBack( &stack );1041 struct SaveNode * node = &stack[stackSize-1]; 1055 1042 const tr_benc * val; 1056 1043 … … 1069 1056 if( isContainer( node->val ) ) 1070 1057 walkFuncs->containerEndFunc( node->val, user_data ); 1071 tr_ptrArrayPop( &stack );1058 --stackSize; 1072 1059 tr_free( node->children ); 1073 tr_free( node );1074 1060 continue; 1075 1061 } … … 1094 1080 1095 1081 case TR_TYPE_LIST: 1096 if( val != node->val ) 1097 tr_ptrArrayAppend( &stack, nodeNew( val ) ); 1098 else 1082 if( val == node->val ) 1099 1083 walkFuncs->listBeginFunc( val, user_data ); 1084 else { 1085 if( stackAlloc == stackSize ) { 1086 stackAlloc *= 2; 1087 stack = tr_renew( struct SaveNode, stack, stackAlloc ); 1088 } 1089 nodeInit( &stack[stackSize++], val ); 1090 } 1100 1091 break; 1101 1092 1102 1093 case TR_TYPE_DICT: 1103 if( val != node->val ) 1104 tr_ptrArrayAppend( &stack, nodeNew( val ) ); 1105 else 1094 if( val == node->val ) 1106 1095 walkFuncs->dictBeginFunc( val, user_data ); 1096 else { 1097 if( stackAlloc == stackSize ) { 1098 stackAlloc *= 2; 1099 stack = tr_renew( struct SaveNode, stack, stackAlloc ); 1100 } 1101 nodeInit( &stack[stackSize++], val ); 1102 } 1107 1103 break; 1108 1104 … … 1114 1110 } 1115 1111 1116 tr_ ptrArrayDestruct( &stack, NULL);1112 tr_free( stack ); 1117 1113 } 1118 1114 … … 1192 1188 1193 1189 static void 1194 freeDummyFunc( const tr_benc * val UNUSED, 1195 void * buf UNUSED ) 1190 freeDummyFunc( const tr_benc * val UNUSED, void * buf UNUSED ) 1196 1191 {} 1197 1192 1198 1193 static void 1199 freeStringFunc( const tr_benc * val, 1200 void * freeme ) 1194 freeStringFunc( const tr_benc * val, void * unused UNUSED ) 1201 1195 { 1202 1196 if( stringIsAlloced( val ) ) 1203 tr_ptrArrayAppend( freeme, val->val.s.str.ptr ); 1204 } 1205 1206 static void 1207 freeContainerBeginFunc( const tr_benc * val, 1208 void * freeme ) 1209 { 1210 tr_ptrArrayAppend( freeme, val->val.l.vals ); 1197 tr_free( val->val.s.str.ptr ); 1198 } 1199 1200 static void 1201 freeContainerEndFunc( const tr_benc * val, void * unused UNUSED ) 1202 { 1203 tr_free( val->val.l.vals ); 1211 1204 } 1212 1205 … … 1215 1208 freeDummyFunc, 1216 1209 freeStringFunc, 1217 free ContainerBeginFunc,1218 free ContainerBeginFunc,1219 free DummyFunc };1210 freeDummyFunc, 1211 freeDummyFunc, 1212 freeContainerEndFunc }; 1220 1213 1221 1214 void … … 1223 1216 { 1224 1217 if( isSomething( val ) ) 1225 { 1226 tr_ptrArray a = TR_PTR_ARRAY_INIT; 1227 bencWalk( val, &freeWalkFuncs, &a ); 1228 tr_ptrArrayDestruct( &a, tr_free ); 1229 } 1218 bencWalk( val, &freeWalkFuncs, NULL ); 1230 1219 } 1231 1220
Note: See TracChangeset
for help on using the changeset viewer.