From 16dbc995ecb7f1f4d67c30ea462cff88365bf506 Mon Sep 17 00:00:00 2001 From: Jean Delvare Date: Sun, 15 Feb 2009 17:15:31 +0000 Subject: [PATCH] Exit the configuration file parser as soon as we are done with it. This makes the peak memory consumption significantly lower (-15% using "sensors" in my tests). git-svn-id: http://lm-sensors.org/svn/lm-sensors/trunk@5644 7894878c-1315-0410-8ee3-d5d059ff63e0 --- CHANGES | 1 + lib/init.c | 31 +++++++++++++++++++++---------- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/CHANGES b/CHANGES index 20ddef17..3cd15592 100644 --- a/CHANGES +++ b/CHANGES @@ -10,6 +10,7 @@ SVN-HEAD Skip non-file sysfs entries for speed Fix memory leak in scanner when using flex 2.5.9 or later Report configuration file read errors + Exit the configuration file parser sooner lm_sensors.init: Support new format of /etc/sysconfig/lm_sensors (#2246) Drop support for kernels 2.4 and earlier lm_sensors.init.suse: Delete (actual SuSE script is much different) diff --git a/lib/init.c b/lib/init.c index a7b2c7ea..060d045d 100644 --- a/lib/init.c +++ b/lib/init.c @@ -61,6 +61,20 @@ static int sensors_parse(void) return res; } +static int parse_config(FILE *input) +{ + int err; + + if (sensors_scanner_init(input)) + return -SENSORS_ERR_PARSE; + err = sensors_parse(); + sensors_scanner_exit(); + if (err) + return -SENSORS_ERR_PARSE; + + return 0; +} + int sensors_init(FILE *input) { int res; @@ -71,10 +85,9 @@ int sensors_init(FILE *input) (res = sensors_read_sysfs_chips())) goto exit_cleanup; - res = -SENSORS_ERR_PARSE; if (input) { - if (sensors_scanner_init(input) || - sensors_parse()) + res = parse_config(input); + if (res) goto exit_cleanup; } else { /* No configuration provided, use default */ @@ -82,14 +95,14 @@ int sensors_init(FILE *input) if (!input && errno == ENOENT) input = fopen(ALT_CONFIG_FILE, "r"); if (input) { - if (sensors_scanner_init(input) || - sensors_parse()) { - fclose(input); - goto exit_cleanup; - } + res = parse_config(input); fclose(input); + if (res) + goto exit_cleanup; + } else if (errno != ENOENT) { sensors_parse_error(strerror(errno), 0); + res = -SENSORS_ERR_PARSE; goto exit_cleanup; } } @@ -197,8 +210,6 @@ void sensors_cleanup(void) { int i; - sensors_scanner_exit(); - for (i = 0; i < sensors_proc_chips_count; i++) { free_chip_name(&sensors_proc_chips[i].chip); free_chip_features(&sensors_proc_chips[i]);