changeset 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 9b0ad72184e3
files ffstools/tiffs-mkfs/globals.c ffstools/tiffs-mkfs/globals.h ffstools/tiffs-mkfs/output.c
diffstat 3 files changed, 13 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/ffstools/tiffs-mkfs/globals.c	Wed May 20 17:32:35 2020 +0000
+++ b/ffstools/tiffs-mkfs/globals.c	Sat May 23 02:50:06 2020 +0000
@@ -10,7 +10,7 @@
 unsigned ffs_sector_size, ffs_nsectors;
 char *format_name;
 unsigned chunk_size_max, block_files_max;
-u_char *inode_block, *data_block;
+u_char *inode_block, *data_block, *chunk_buffer;
 struct tiffs_inode *inode_array;
 unsigned inode_fill_level, data_fill_level, objects_in_block;
 unsigned blocks_written;
--- a/ffstools/tiffs-mkfs/globals.h	Wed May 20 17:32:35 2020 +0000
+++ b/ffstools/tiffs-mkfs/globals.h	Sat May 23 02:50:06 2020 +0000
@@ -6,7 +6,7 @@
 extern unsigned ffs_nsectors;
 extern char *format_name;
 extern unsigned chunk_size_max, block_files_max;
-extern u_char *inode_block, *data_block;
+extern u_char *inode_block, *data_block, *chunk_buffer;
 extern struct tiffs_inode *inode_array;
 extern unsigned inode_fill_level, data_fill_level, objects_in_block;
 extern unsigned blocks_written;
--- 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;
 }