From e3a3f534ae230670eba09264a37626932f6cd39f Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Thu, 9 Nov 2006 22:53:12 +0100 Subject: Dynamically allocate memory for the inotify event buffer By using malloc(3) depending on the number of files we have, the memory footprint can be reduced. Before this change, the buffer size was 32 * sizeof(struct inotify_event) which was an overhead when just tailing some few files. Now the memory is allocated depending on the number of files to tail. I know of no occassion where I received more than one event at a time per file, so the '2 *' can probably be dropped too. --- inotail.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/inotail.c b/inotail.c index fefde0d..8a9e1a9 100644 --- a/inotail.c +++ b/inotail.c @@ -323,8 +323,9 @@ ignore: static int watch_files(struct file_struct *f, int n_files) { int ifd, i, n_ignored = 0; + unsigned buflen = 2 * n_files * sizeof(struct inotify_event); struct inotify_event *inev; - char buf[sizeof(struct inotify_event) * 32]; /* Let's hope we don't get more than 32 events at a time */ + char *buf; ifd = inotify_init(); if (ifd < 0) { @@ -340,13 +341,14 @@ static int watch_files(struct file_struct *f, int n_files) n_ignored++; } - memset(&buf, 0, sizeof(buf)); + buf = malloc(buflen); + memset(buf, 0, buflen); while (n_ignored < n_files) { int len; - len = read(ifd, buf, sizeof(buf)); - inev = (struct inotify_event *) &buf; + len = read(ifd, buf, buflen); + inev = (struct inotify_event *) buf; while (len > 0) { struct file_struct *fil = NULL; @@ -368,6 +370,7 @@ static int watch_files(struct file_struct *f, int n_files) } } + free(buf); close(ifd); return n_ignored; -- cgit v1.2.3-54-g00ecf