@matheusbsbs / matheus-bagliano / commits / 2b87c45ba6
Fork 0

sha1_file.c: cleanup "offset" usage

First there are too many offsets there and it is getting confusing. So 'offset' is now 'curpos' to distinguish from other offsets like 'obj_offset'. Then structures like x = foo(x, &y) are now done as y = foo(&x). It looks more natural that the result y be returned directly and x be passed as reference to be updated in place. This has the effect of reducing some line length and removing a few, needing a bit less stack space, and it even reduces the compiled code size. Signed-off-by: Nicolas Pitre <nico@cam.org> Signed-off-by: Junio C Hamano <junkio@cox.net>

Nicolas Pitre committed Feb 26, 2007 at 14:55 UTC 2b87c45ba6f8950ffc44cb6a3c861ad2e5f176a8
1 file changed +51 -59
sha1_file.c
+51 -59
@@ -1043,12 +1043,11 @@ void * unpack_sha1_file(void *map, unsigned long mapsize, char *type, unsigned l
1043
1044 static unsigned long get_delta_base(struct packed_git *p,
1045 struct pack_window **w_curs,
1046 - unsigned long offset,
1046 + unsigned long *curpos,
1047 enum object_type kind,
1048 - unsigned long delta_obj_offset,
1049 - unsigned long *base_obj_offset)
1048 + unsigned long delta_obj_offset)
1049 {
1051 - unsigned char *base_info = use_pack(p, w_curs, offset, NULL);
1050 + unsigned char *base_info = use_pack(p, w_curs, *curpos, NULL);
1051 unsigned long base_offset;
1052
1053 /* use_pack() assured us we have [base_info, base_info + 20)
@@ -1071,18 +1070,17 @@ static unsigned long get_delta_base(struct packed_git *p,
1070 base_offset = delta_obj_offset - base_offset;
1071 if (base_offset >= delta_obj_offset)
1072 die("delta base offset out of bound");
1074 - offset += used;
1073 + *curpos += used;
1074 } else if (kind == OBJ_REF_DELTA) {
1075 /* The base entry _must_ be in the same pack */
1076 base_offset = find_pack_entry_one(base_info, p);
1077 if (!base_offset)
1078 die("failed to find delta-pack base object %s",
1079 sha1_to_hex(base_info));
1081 - offset += 20;
1080 + *curpos += 20;
1081 } else
1082 die("I am totally screwed");
1084 - *base_obj_offset = base_offset;
1085 - return offset;
1083 + return base_offset;
1084 }
1085
1086 /* forward declaration for a mutually recursive function */
@@ -1091,7 +1089,7 @@ static int packed_object_info(struct packed_git *p, unsigned long offset,
1089
1090 static int packed_delta_info(struct packed_git *p,
1091 struct pack_window **w_curs,
1094 - unsigned long offset,
1092 + unsigned long curpos,
1093 enum object_type kind,
1094 unsigned long obj_offset,
1095 char *type,
@@ -1099,8 +1097,7 @@ static int packed_delta_info(struct packed_git *p,
1097 {
1098 unsigned long base_offset;
1099
1102 - offset = get_delta_base(p, w_curs, offset, kind,
1103 - obj_offset, &base_offset);
1100 + base_offset = get_delta_base(p, w_curs, &curpos, kind, obj_offset);
1101
1102 /* We choose to only get the type of the base object and
1103 * ignore potentially corrupt pack file that expects the delta
@@ -1113,7 +1110,6 @@ static int packed_delta_info(struct packed_git *p,
1110 if (sizep) {
1111 const unsigned char *data;
1112 unsigned char delta_head[20], *in;
1116 - unsigned long result_size;
1113 z_stream stream;
1114 int st;
1115
@@ -1123,10 +1119,10 @@ static int packed_delta_info(struct packed_git *p,
1119
1120 inflateInit(&stream);
1121 do {
1126 - in = use_pack(p, w_curs, offset, &stream.avail_in);
1122 + in = use_pack(p, w_curs, curpos, &stream.avail_in);
1123 stream.next_in = in;
1124 st = inflate(&stream, Z_FINISH);
1129 - offset += stream.next_in - in;
1125 + curpos += stream.next_in - in;
1126 } while ((st == Z_OK || st == Z_BUF_ERROR)
1127 && stream.total_out < sizeof(delta_head));
1128 inflateEnd(&stream);
@@ -1143,21 +1139,20 @@ static int packed_delta_info(struct packed_git *p,
1139 get_delta_hdr_size(&data, delta_head+sizeof(delta_head));
1140
1141 /* Read the result size */
1146 - result_size = get_delta_hdr_size(&data, delta_head+sizeof(delta_head));
1147 - *sizep = result_size;
1142 + *sizep = get_delta_hdr_size(&data, delta_head+sizeof(delta_head));
1143 }
1144 return 0;
1145 }
1146
1152 -static unsigned long unpack_object_header(struct packed_git *p,
1153 - struct pack_window **w_curs,
1154 - unsigned long offset,
1155 - enum object_type *type,
1156 - unsigned long *sizep)
1147 +static int unpack_object_header(struct packed_git *p,
1148 + struct pack_window **w_curs,
1149 + unsigned long *curpos,
1150 + unsigned long *sizep)
1151 {
1152 unsigned char *base;
1153 unsigned int left;
1154 unsigned long used;
1155 + enum object_type type;
1156
1157 /* use_pack() assures us we have [base, base + 20) available
1158 * as a range that we can look at at. (Its actually the hash
@@ -1165,16 +1160,17 @@ static unsigned long unpack_object_header(struct packed_git *p,
1160 * the maximum deflated object size is 2^137, which is just
1161 * insane, so we know won't exceed what we have been given.
1162 */
1168 - base = use_pack(p, w_curs, offset, &left);
1169 - used = unpack_object_header_gently(base, left, type, sizep);
1163 + base = use_pack(p, w_curs, *curpos, &left);
1164 + used = unpack_object_header_gently(base, left, &type, sizep);
1165 if (!used)
1166 die("object offset outside of pack file");
1167 + *curpos += used;
1168
1173 - return offset + used;
1169 + return type;
1170 }
1171
1172 void packed_object_info_detail(struct packed_git *p,
1177 - unsigned long offset,
1173 + unsigned long obj_offset,
1174 char *type,
1175 unsigned long *size,
1176 unsigned long *store_size,
@@ -1182,13 +1178,13 @@ void packed_object_info_detail(struct packed_git *p,
1178 unsigned char *base_sha1)
1179 {
1180 struct pack_window *w_curs = NULL;
1185 - unsigned long obj_offset, val;
1181 + unsigned long curpos, dummy;
1182 unsigned char *next_sha1;
1183 enum object_type kind;
1184
1185 *delta_chain_length = 0;
1190 - obj_offset = offset;
1191 - offset = unpack_object_header(p, &w_curs, offset, &kind, size);
1186 + curpos = obj_offset;
1187 + kind = unpack_object_header(p, &w_curs, &curpos, size);
1188
1189 for (;;) {
1190 switch (kind) {
@@ -1204,61 +1200,58 @@ void packed_object_info_detail(struct packed_git *p,
1200 unuse_pack(&w_curs);
1201 return;
1202 case OBJ_OFS_DELTA:
1207 - get_delta_base(p, &w_curs, offset, kind,
1208 - obj_offset, &offset);
1203 + obj_offset = get_delta_base(p, &w_curs, &curpos, kind, obj_offset);
1204 if (*delta_chain_length == 0) {
1210 - /* TODO: find base_sha1 as pointed by offset */
1205 + /* TODO: find base_sha1 as pointed by curpos */
1206 }
1207 break;
1208 case OBJ_REF_DELTA:
1214 - next_sha1 = use_pack(p, &w_curs, offset, NULL);
1209 + next_sha1 = use_pack(p, &w_curs, curpos, NULL);
1210 if (*delta_chain_length == 0)
1211 hashcpy(base_sha1, next_sha1);
1217 - offset = find_pack_entry_one(next_sha1, p);
1212 + obj_offset = find_pack_entry_one(next_sha1, p);
1213 break;
1214 }
1220 - obj_offset = offset;
1221 - offset = unpack_object_header(p, &w_curs, offset, &kind, &val);
1215 (*delta_chain_length)++;
1216 + curpos = obj_offset;
1217 + kind = unpack_object_header(p, &w_curs, &curpos, &dummy);
1218 }
1219 }
1220
1226 -static int packed_object_info(struct packed_git *p, unsigned long offset,
1221 +static int packed_object_info(struct packed_git *p, unsigned long obj_offset,
1222 char *type, unsigned long *sizep)
1223 {
1224 struct pack_window *w_curs = NULL;
1230 - unsigned long size, obj_offset = offset;
1225 + unsigned long size, curpos = obj_offset;
1226 enum object_type kind;
1232 - int r;
1227
1234 - offset = unpack_object_header(p, &w_curs, offset, &kind, &size);
1228 + kind = unpack_object_header(p, &w_curs, &curpos, &size);
1229
1230 switch (kind) {
1231 case OBJ_OFS_DELTA:
1232 case OBJ_REF_DELTA:
1239 - r = packed_delta_info(p, &w_curs, offset, kind,
1240 - obj_offset, type, sizep);
1241 - unuse_pack(&w_curs);
1242 - return r;
1233 + packed_delta_info(p, &w_curs, curpos, kind,
1234 + obj_offset, type, sizep);
1235 + break;
1236 case OBJ_COMMIT:
1237 case OBJ_TREE:
1238 case OBJ_BLOB:
1239 case OBJ_TAG:
1240 strcpy(type, type_names[kind]);
1248 - unuse_pack(&w_curs);
1241 + if (sizep)
1242 + *sizep = size;
1243 break;
1244 default:
1245 die("pack %s contains unknown object type %d",
1246 p->pack_name, kind);
1247 }
1254 - if (sizep)
1255 - *sizep = size;
1248 + unuse_pack(&w_curs);
1249 return 0;
1250 }
1251
1252 static void *unpack_compressed_entry(struct packed_git *p,
1253 struct pack_window **w_curs,
1261 - unsigned long offset,
1254 + unsigned long curpos,
1255 unsigned long size)
1256 {
1257 int st;
@@ -1273,10 +1266,10 @@ static void *unpack_compressed_entry(struct packed_git *p,
1266
1267 inflateInit(&stream);
1268 do {
1276 - in = use_pack(p, w_curs, offset, &stream.avail_in);
1269 + in = use_pack(p, w_curs, curpos, &stream.avail_in);
1270 stream.next_in = in;
1271 st = inflate(&stream, Z_FINISH);
1279 - offset += stream.next_in - in;
1272 + curpos += stream.next_in - in;
1273 } while (st == Z_OK || st == Z_BUF_ERROR);
1274 inflateEnd(&stream);
1275 if ((st != Z_STREAM_END) || stream.total_out != size) {
@@ -1289,7 +1282,7 @@ static void *unpack_compressed_entry(struct packed_git *p,
1282
1283 static void *unpack_delta_entry(struct packed_git *p,
1284 struct pack_window **w_curs,
1292 - unsigned long offset,
1285 + unsigned long curpos,
1286 unsigned long delta_size,
1287 enum object_type kind,
1288 unsigned long obj_offset,
@@ -1299,14 +1292,13 @@ static void *unpack_delta_entry(struct packed_git *p,
1292 void *delta_data, *result, *base;
1293 unsigned long result_size, base_size, base_offset;
1294
1302 - offset = get_delta_base(p, w_curs, offset, kind,
1303 - obj_offset, &base_offset);
1295 + base_offset = get_delta_base(p, w_curs, &curpos, kind, obj_offset);
1296 base = unpack_entry(p, base_offset, type, &base_size);
1297 if (!base)
1298 die("failed to read delta base object at %lu from %s",
1299 base_offset, p->pack_name);
1300
1309 - delta_data = unpack_compressed_entry(p, w_curs, offset, delta_size);
1301 + delta_data = unpack_compressed_entry(p, w_curs, curpos, delta_size);
1302 result = patch_delta(base, base_size,
1303 delta_data, delta_size,
1304 &result_size);
@@ -1318,19 +1310,19 @@ static void *unpack_delta_entry(struct packed_git *p,
1310 return result;
1311 }
1312
1321 -void *unpack_entry(struct packed_git *p, unsigned long offset,
1322 - char *type, unsigned long *sizep)
1313 +void *unpack_entry(struct packed_git *p, unsigned long obj_offset,
1314 + char *type, unsigned long *sizep)
1315 {
1316 struct pack_window *w_curs = NULL;
1325 - unsigned long size, obj_offset = offset;
1317 + unsigned long size, curpos = obj_offset;
1318 enum object_type kind;
1319 void *retval;
1320
1329 - offset = unpack_object_header(p, &w_curs, offset, &kind, &size);
1321 + kind = unpack_object_header(p, &w_curs, &curpos, &size);
1322 switch (kind) {
1323 case OBJ_OFS_DELTA:
1324 case OBJ_REF_DELTA:
1333 - retval = unpack_delta_entry(p, &w_curs, offset, size,
1325 + retval = unpack_delta_entry(p, &w_curs, curpos, size,
1326 kind, obj_offset, type, sizep);
1327 break;
1328 case OBJ_COMMIT:
@@ -1339,7 +1331,7 @@ void *unpack_entry(struct packed_git *p, unsigned long offset,
1331 case OBJ_TAG:
1332 strcpy(type, type_names[kind]);
1333 *sizep = size;
1342 - retval = unpack_compressed_entry(p, &w_curs, offset, size);
1334 + retval = unpack_compressed_entry(p, &w_curs, curpos, size);
1335 break;
1336 default:
1337 die("unknown object type %i in %s", kind, p->pack_name);