summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTobias Klauser <tklauser@distanz.ch>2006-11-09 22:53:12 +0100
committerTobias Klauser <tklauser@xenon.tklauser.home>2006-11-09 22:53:12 +0100
commite3a3f534ae230670eba09264a37626932f6cd39f (patch)
treea7a8d0de95c61ac3bb920c8dcb4a3fc0e92384e1
parenta3f1b217c76165d4e743a01cc22692774093868d (diff)
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.
-rw-r--r--inotail.c11
1 files 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;