diff ffstools/tiffs-mkfs/output.c @ 708:e2e4aed24522

tiffs-mkfs: malloc file chunk buffer only once
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 23 May 2020 02:50:06 +0000
parents 805885936f62
children 178ed445021d
line wrap: on
line diff
--- a/ffstools/tiffs-mkfs/output.c	Wed May 20 17:32:35 2020 +0000
+++ b/ffstools/tiffs-mkfs/output.c	Sat May 23 02:50:06 2020 +0000
@@ -37,6 +37,12 @@
 	data_block[8] = 0xBD;
 	data_fill_level = 0x10;
 	objects_in_block = 0;
+
+	chunk_buffer = malloc(chunk_size_max);
+	if (!chunk_buffer) {
+		perror("malloc of file chunk buffer");
+		exit(1);
+	}
 }
 
 void
@@ -137,7 +143,6 @@
 	struct tree_object *to;
 {
 	int fd, cc;
-	u_char *data;
 	int head, seg;
 	struct tiffs_inode *inp;
 
@@ -146,12 +151,7 @@
 		perror(to->u.f.host_pathname);
 		exit(1);
 	}
-	data = malloc(chunk_size_max);
-	if (!data) {
-		perror("malloc of file chunk buffer");
-		exit(1);
-	}
-	cc = read(fd, data, chunk_size_max);
+	cc = read(fd, chunk_buffer, chunk_size_max);
 	if (cc < 0) {
 read_err:	perror("error reading file content");
 		exit(1);
@@ -159,23 +159,22 @@
 	if (cc == 0) {
 		/* zero length file */
 		close(fd);
-		free(data);
 		return create_object(to->name, OBJTYPE_FILE, (u_char *) 0, 0);
 	}
-	head = create_object(to->name, OBJTYPE_FILE, data, cc);
+	head = create_object(to->name, OBJTYPE_FILE, chunk_buffer, cc);
 	inp = inode_array + head;
 	for (;;) {
-		cc = read(fd, data, chunk_size_max);
+		cc = read(fd, chunk_buffer, chunk_size_max);
 		if (cc < 0)
 			goto read_err;
 		if (cc == 0)
 			break;
-		seg = create_object((char *) 0, OBJTYPE_SEGMENT, data, cc);
+		seg = create_object((char *) 0, OBJTYPE_SEGMENT, chunk_buffer,
+					cc);
 		inp->child = htole16(seg);
 		inp = inode_array + seg;
 	}
 	close(fd);
-	free(data);
 	return head;
 }