This is the mail archive of the gdb-patches@sourceware.org mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[PATCH] Remote FILEIO: newly allocated file descriptors have to be initialized.


This patch has to be approved before I can commit it.

Extra file descriptor array entries don't get initialized when increasing array size.

On the test case :

#define FILENAME "test" /* name of a file that exists */

#include <stdio.h>
#include <fcntl.h>

main()
{
   int i;
   printf( "Opening many files...\n" );
   for (i = 0; i < 20; i++) {
       int fd = open(FILENAME, O_RDONLY);
       printf( "(%02d)  Got fd = %d\n", i, fd);
   }
}

we end up wasting more memory space than we need :

Opening many files...
(00)  Got fd = 3
(01)  Got fd = 4
(02)  Got fd = 5
(03)  Got fd = 6
(04)  Got fd = 7
(05)  Got fd = 8
(06)  Got fd = 9
(07)  Got fd = 10
(08)  Got fd = 14
(09)  Got fd = 19
(10)  Got fd = 20
(11)  Got fd = 26
(12)  Got fd = 30
(13)  Got fd = 40
(14)  Got fd = 50
(15)  Got fd = 60
(16)  Got fd = 70
(17)  Got fd = 80
(18)  Got fd = 90
(19)  Got fd = 97

With this fix, it looks better:
Opening many files...
(00)  Got fd = 3
(01)  Got fd = 4
(02)  Got fd = 5
(03)  Got fd = 6
(04)  Got fd = 7
(05)  Got fd = 8
(06)  Got fd = 9
(07)  Got fd = 10
(08)  Got fd = 11
(09)  Got fd = 12
(10)  Got fd = 13
(11)  Got fd = 14
(12)  Got fd = 15
(13)  Got fd = 16
(14)  Got fd = 17
(15)  Got fd = 18
(16)  Got fd = 19
(17)  Got fd = 20
(18)  Got fd = 21
(19)  Got fd = 22

Thanks,
-- Maxim


2007-08-09  Maxim Grigoriev  <maxim2405@gmail.com>

	* remote-fileio.c (remote_fileio_resize_fd_map): Initialize newly
	allocated file descriptors.

Index: remote-fileio.c
===================================================================
RCS file: /cvs/src/src/gdb/remote-fileio.c,v
retrieving revision 1.24
diff -u -r1.24 remote-fileio.c
--- remote-fileio.c	5 Aug 2007 01:04:31 -0000	1.24
+++ remote-fileio.c	9 Aug 2007 23:14:43 -0000
@@ -70,12 +70,16 @@
 static int
 remote_fileio_resize_fd_map (void)
 {
+  int i = remote_fio_data.fd_map_size;
+
   if (!remote_fio_data.fd_map)
     return remote_fileio_init_fd_map ();
   remote_fio_data.fd_map_size += 10;
   remote_fio_data.fd_map =
     (int *) xrealloc (remote_fio_data.fd_map,
 		      remote_fio_data.fd_map_size * sizeof (int));
+  for (; i < remote_fio_data.fd_map_size; i++)
+    remote_fio_data.fd_map[i] = FIO_FD_INVALID;
   return remote_fio_data.fd_map_size - 10;
 }
 

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]