From 9b1a89e617562bf253dd20ac405446aea2075616 Mon Sep 17 00:00:00 2001 From: Nikolas Garofil Date: Thu, 18 Feb 2010 01:25:32 +0100 Subject: [PATCH 01/34] Fix current_mail_spool memleak --- src/conky.cc | 2 +- src/logging.h | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/conky.cc b/src/conky.cc index 9a12e233..1627c267 100644 --- a/src/conky.cc +++ b/src/conky.cc @@ -2344,7 +2344,7 @@ static void main_loop(void) #endif /* BUILD_LUA */ g_signal_pending = 0; } - clean_up(NULL, NULL); + clean_up(current_mail_spool, NULL); #ifdef HAVE_SYS_INOTIFY_H if (inotify_fd != -1) { diff --git a/src/logging.h b/src/logging.h index 609ba805..5455d545 100644 --- a/src/logging.h +++ b/src/logging.h @@ -27,6 +27,8 @@ * */ +#include "mail.h" + #ifdef __cplusplus extern "C" { #endif @@ -44,7 +46,7 @@ void clean_up(void *memtofree1, void* memtofree2); /* critical error */ #define CRIT_ERR(memtofree1, memtofree2, ...) \ - { NORM_ERR(__VA_ARGS__); clean_up(memtofree1, memtofree2); exit(EXIT_FAILURE); } + { NORM_ERR(__VA_ARGS__); clean_up(memtofree1, memtofree2); free(current_mail_spool); exit(EXIT_FAILURE); } /* debugging output */ extern int global_debug_level; From c7fa36a5f24431001449dc44d99ee2e68f6774c6 Mon Sep 17 00:00:00 2001 From: Caleb Spare Date: Thu, 18 Feb 2010 18:17:27 +0100 Subject: [PATCH 02/34] $memwithbuffers object and bar (sf.net #2954143) I've added $memwithbuffers to complement $mem, $memeasyfree, etc. This variable indicates the used system memory, regardless of buffers/caches. If the "no_buffers" option is set to "no", it will be the same as $mem. The reason I find this change useful is that I like to display both the total used system memory and the memory being used by applications. I've also included a memwithbuffersbar, which is the same as membar except that it uses $memwithbuffers as the size instead of $mem. Signed-off-by: Pavel Labath --- ChangeLog | 3 +++ doc/variables.xml | 19 +++++++++++++++++++ extras/nano/conky.nanorc | 2 +- extras/vim/syntax/conkyrc.vim | 2 +- src/common.cc | 8 ++++++++ src/common.h | 2 ++ src/conky.h | 3 ++- src/core.cc | 5 +++++ src/freebsd.cc | 1 + src/linux.cc | 6 +++--- src/netbsd.cc | 1 + src/openbsd.cc | 2 +- 12 files changed, 47 insertions(+), 7 deletions(-) diff --git a/ChangeLog b/ChangeLog index dbe133d0..fb645e34 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,6 @@ +2010-02-18 + * $memwithbuffers object and bar (sf.net #2954143), thanks kingfishr + 2009-12-09 * Replaced GHashTable with tr1::unordered_map in tcp_portmon. This means portmon no longer depends on glib. diff --git a/doc/variables.xml b/doc/variables.xml index ccc692dc..76bf77f6 100644 --- a/doc/variables.xml +++ b/doc/variables.xml @@ -1971,6 +1971,15 @@ Amount of memory in use + + + + + + + Amount of memory in use, including that used by system buffers and caches + + @@ -1981,6 +1990,16 @@ Bar that shows amount of memory in use + + + + + + + + Bar that shows amount of memory in use (including memory used by system buffers and caches) + + diff --git a/extras/nano/conky.nanorc b/extras/nano/conky.nanorc index beb60c73..70e9f2b5 100644 --- a/extras/nano/conky.nanorc +++ b/extras/nano/conky.nanorc @@ -11,7 +11,7 @@ color green "\<(alignment|append_file|background|border_inner_margin|border_oute color yellow "\<(above|below|bottom_left|bottom_right|bottom_middle|desktop|dock|no|none|normal|override|skip_pager|skip_taskbar|sticky|top_left|top_right|top_middle|middle_left|middle_right|middle_middle|undecorated|yes)\>" ## Variables -color brightblue "\<(acpiacadapter|acpifan|acpitemp|addr|addrs|alignc|alignr|apcupsd|apcupsd_cable|apcupsd_charge|apcupsd_lastxfer|apcupsd_linev|apcupsd_load|apcupsd_loadbar|apcupsd_loadgauge|apcupsd_loadgraph|apcupsd_model|apcupsd_name|apcupsd_status|apcupsd_temp|apcupsd_timeleft|apcupsd_upsmode|apm_adapter|apm_battery_life|apm_battery_time|audacious_bar|audacious_bitrate|audacious_channels|audacious_filename|audacious_frequency|audacious_length|audacious_length_seconds|audacious_main_volume|audacious_playlist_length|audacious_playlist_position|audacious_position|audacious_position_seconds|audacious_status|audacious_title|battery|battery_bar|battery_percent|battery_short|battery_time|blink|bmpx_album|bmpx_artist|bmpx_bitrate|bmpx_title|bmpx_track|bmpx_uri|buffers|cached|cmdline_to_pid|color|color0|color1|color2|color3|color4|color5|color6|color7|color8|color9|combine|conky_build_arch|conky_build_date|conky_version|cpu|cpubar|cpugauge|cpugraph|curl|desktop|desktop_name|desktop_number|disk_protect|diskio|diskio_read|diskio_write|diskiograph|diskiograph_read|diskiograph_write|downspeed|downspeedf|downspeedgraph|draft_mails|else|endif|entropy_avail|entropy_bar|entropy_perc|entropy_poolsize|eval|eve|exec|execbar|execgauge|execgraph|execi|execibar|execigauge|execigraph|execp|execpi|flagged_mails|font|format_time|forwarded_mails|freq|freq_g|fs_bar|fs_bar_free|fs_free|fs_free_perc|fs_size|fs_type|fs_used|fs_used_perc|goto|gw_iface|gw_ip|hddtemp|head|hr|hwmon|i2c|i8k_ac_status|i8k_bios|i8k_buttons_status|i8k_cpu_temp|i8k_left_fan_rpm|i8k_left_fan_status|i8k_right_fan_rpm|i8k_right_fan_status|i8k_serial|i8k_version|ibm_brightness|ibm_fan|ibm_temps|ibm_volume|iconv_start|iconv_stop|if_empty|if_existing|if_gw|if_match|if_mixer_mute|if_mounted|if_mpd_playing|if_running|if_smapi_bat_installed|if_up|if_updatenr|if_xmms2_connected|image|imap_messages|imap_unseen|include|ioscheduler|kernel|laptop_mode|lines|loadavg|loadgraph|lua|lua_bar|lua_gauge|lua_graph|lua_parse|machine|mails|mboxscan|mem|membar|memeasyfree|memfree|memgauge|memgraph|memmax|memperc|mixer|mixerbar|mixerl|mixerlbar|mixerr|mixerrbar|moc_album|moc_artist|moc_bitrate|moc_curtime|moc_file|moc_rate|moc_song|moc_state|moc_timeleft|moc_title|moc_totaltime|monitor|monitor_number|mpd_album|mpd_artist|mpd_bar|mpd_bitrate|mpd_elapsed|mpd_file|mpd_length|mpd_name|mpd_percent|mpd_random|mpd_repeat|mpd_smart|mpd_status|mpd_title|mpd_track|mpd_vol|nameserver|new_mails|nodename|nvidia|obsd_product|obsd_sensors_fan|obsd_sensors_temp|obsd_sensors_volt|obsd_vendor|offset|outlinecolor|pb_battery|pid_chroot|pid_cmdline|pid_cwd|pid_environ|pid_environ_list|pid_exe|pid_nice|pid_openfiles|pid_parent|pid_priority|pid_state|pid_state_short|pid_stderr|pid_stdin|pid_stdout|pid_threads|pid_thread_list|pid_time_kernelmode|pid_time_usermode|pid_time|pid_uid|pid_euid|pid_suid|pid_fsuid|pid_gid|pid_egid|pid_sgid|pid_fsgid|pid_read|pid_vmpeak|pid_vmsize|pid_vmlck|pid_vmhwm|pid_vmrss|pid_vmdata|pid_vmstk|pid_vmexe|pid_vmlib|pid_vmpte|pid_write|platform|pop3_unseen|pop3_used|pre_exec|processes|read_tcp|replied_mails|rss|running_processes|running_threads|scroll|seen_mails|shadecolor|smapi|smapi_bat_bar|smapi_bat_perc|smapi_bat_power|smapi_bat_temp|sony_fanspeed|stippled_hr|swap|swapbar|swapfree|swapmax|swapperc|sysname|tab|tail|tcp_portmon|template0|template1|template2|template3|template4|template5|template6|template7|template8|template9|texeci|texecpi|threads|time|to_bytes|top|top_io|top_mem|top_time|totaldown|totalup|trashed_mails|tztime|gid_name|uid_name|unflagged_mails|unforwarded_mails|unreplied_mails|unseen_mails|updates|upspeed|upspeedf|upspeedgraph|uptime|uptime_short|user_names|user_number|user_terms|user_times|user_time|utime|voffset|voltage_mv|voltage_v|weather|wireless_ap|wireless_bitrate|wireless_essid|wireless_link_bar|wireless_link_qual|wireless_link_qual_max|wireless_link_qual_perc|wireless_mode|words|xmms2_album|xmms2_artist|xmms2_bar|xmms2_bitrate|xmms2_comment|xmms2_date|xmms2_duration|xmms2_elapsed|xmms2_genre|xmms2_id|xmms2_percent|xmms2_playlist|xmms2_size|xmms2_smart|xmms2_status|xmms2_timesplayed|xmms2_title|xmms2_tracknr|xmms2_url)\>" +color brightblue "\<(acpiacadapter|acpifan|acpitemp|addr|addrs|alignc|alignr|apcupsd|apcupsd_cable|apcupsd_charge|apcupsd_lastxfer|apcupsd_linev|apcupsd_load|apcupsd_loadbar|apcupsd_loadgauge|apcupsd_loadgraph|apcupsd_model|apcupsd_name|apcupsd_status|apcupsd_temp|apcupsd_timeleft|apcupsd_upsmode|apm_adapter|apm_battery_life|apm_battery_time|audacious_bar|audacious_bitrate|audacious_channels|audacious_filename|audacious_frequency|audacious_length|audacious_length_seconds|audacious_main_volume|audacious_playlist_length|audacious_playlist_position|audacious_position|audacious_position_seconds|audacious_status|audacious_title|battery|battery_bar|battery_percent|battery_short|battery_time|blink|bmpx_album|bmpx_artist|bmpx_bitrate|bmpx_title|bmpx_track|bmpx_uri|buffers|cached|cmdline_to_pid|color|color0|color1|color2|color3|color4|color5|color6|color7|color8|color9|combine|conky_build_arch|conky_build_date|conky_version|cpu|cpubar|cpugauge|cpugraph|curl|desktop|desktop_name|desktop_number|disk_protect|diskio|diskio_read|diskio_write|diskiograph|diskiograph_read|diskiograph_write|downspeed|downspeedf|downspeedgraph|draft_mails|else|endif|entropy_avail|entropy_bar|entropy_perc|entropy_poolsize|eval|eve|exec|execbar|execgauge|execgraph|execi|execibar|execigauge|execigraph|execp|execpi|flagged_mails|font|format_time|forwarded_mails|freq|freq_g|fs_bar|fs_bar_free|fs_free|fs_free_perc|fs_size|fs_type|fs_used|fs_used_perc|goto|gw_iface|gw_ip|hddtemp|head|hr|hwmon|i2c|i8k_ac_status|i8k_bios|i8k_buttons_status|i8k_cpu_temp|i8k_left_fan_rpm|i8k_left_fan_status|i8k_right_fan_rpm|i8k_right_fan_status|i8k_serial|i8k_version|ibm_brightness|ibm_fan|ibm_temps|ibm_volume|iconv_start|iconv_stop|if_empty|if_existing|if_gw|if_match|if_mixer_mute|if_mounted|if_mpd_playing|if_running|if_smapi_bat_installed|if_up|if_updatenr|if_xmms2_connected|image|imap_messages|imap_unseen|include|ioscheduler|kernel|laptop_mode|lines|loadavg|loadgraph|lua|lua_bar|lua_gauge|lua_graph|lua_parse|machine|mails|mboxscan|mem|memwithbuffers|membar|memwithbuffersbar|memeasyfree|memfree|memgauge|memgraph|memmax|memperc|mixer|mixerbar|mixerl|mixerlbar|mixerr|mixerrbar|moc_album|moc_artist|moc_bitrate|moc_curtime|moc_file|moc_rate|moc_song|moc_state|moc_timeleft|moc_title|moc_totaltime|monitor|monitor_number|mpd_album|mpd_artist|mpd_bar|mpd_bitrate|mpd_elapsed|mpd_file|mpd_length|mpd_name|mpd_percent|mpd_random|mpd_repeat|mpd_smart|mpd_status|mpd_title|mpd_track|mpd_vol|nameserver|new_mails|nodename|nvidia|obsd_product|obsd_sensors_fan|obsd_sensors_temp|obsd_sensors_volt|obsd_vendor|offset|outlinecolor|pb_battery|pid_chroot|pid_cmdline|pid_cwd|pid_environ|pid_environ_list|pid_exe|pid_nice|pid_openfiles|pid_parent|pid_priority|pid_state|pid_state_short|pid_stderr|pid_stdin|pid_stdout|pid_threads|pid_thread_list|pid_time_kernelmode|pid_time_usermode|pid_time|pid_uid|pid_euid|pid_suid|pid_fsuid|pid_gid|pid_egid|pid_sgid|pid_fsgid|pid_read|pid_vmpeak|pid_vmsize|pid_vmlck|pid_vmhwm|pid_vmrss|pid_vmdata|pid_vmstk|pid_vmexe|pid_vmlib|pid_vmpte|pid_write|platform|pop3_unseen|pop3_used|pre_exec|processes|read_tcp|replied_mails|rss|running_processes|running_threads|scroll|seen_mails|shadecolor|smapi|smapi_bat_bar|smapi_bat_perc|smapi_bat_power|smapi_bat_temp|sony_fanspeed|stippled_hr|swap|swapbar|swapfree|swapmax|swapperc|sysname|tab|tail|tcp_portmon|template0|template1|template2|template3|template4|template5|template6|template7|template8|template9|texeci|texecpi|threads|time|to_bytes|top|top_io|top_mem|top_time|totaldown|totalup|trashed_mails|tztime|gid_name|uid_name|unflagged_mails|unforwarded_mails|unreplied_mails|unseen_mails|updates|upspeed|upspeedf|upspeedgraph|uptime|uptime_short|user_names|user_number|user_terms|user_times|user_time|utime|voffset|voltage_mv|voltage_v|weather|wireless_ap|wireless_bitrate|wireless_essid|wireless_link_bar|wireless_link_qual|wireless_link_qual_max|wireless_link_qual_perc|wireless_mode|words|xmms2_album|xmms2_artist|xmms2_bar|xmms2_bitrate|xmms2_comment|xmms2_date|xmms2_duration|xmms2_elapsed|xmms2_genre|xmms2_id|xmms2_percent|xmms2_playlist|xmms2_size|xmms2_smart|xmms2_status|xmms2_timesplayed|xmms2_title|xmms2_tracknr|xmms2_url)\>" color brightblue "\$\{?[0-9A-Z_!@#$*?-]+\}?" color cyan "(\{|\}|\(|\)|\;|\]|\[|`|\\|\$|<|>|!|=|&|\|)" diff --git a/extras/vim/syntax/conkyrc.vim b/extras/vim/syntax/conkyrc.vim index cd151a26..0ab80c97 100644 --- a/extras/vim/syntax/conkyrc.vim +++ b/extras/vim/syntax/conkyrc.vim @@ -50,7 +50,7 @@ syn region ConkyrcVar start=/\$\w\@=/ end=/\W\@=\|$/ contained contains=ConkyrcV syn match ConkyrcVarStuff /{\@<=/ms=s contained nextgroup=ConkyrcVarName -syn keyword ConkyrcVarName contained nextgroup=ConkyrcNumber,ConkyrcColour skipwhite acpiacadapter acpifan acpitemp addr addrs alignc alignr apcupsd apcupsd_cable apcupsd_charge apcupsd_lastxfer apcupsd_linev apcupsd_load apcupsd_loadbar apcupsd_loadgauge apcupsd_loadgraph apcupsd_model apcupsd_name apcupsd_status apcupsd_temp apcupsd_timeleft apcupsd_upsmode apm_adapter apm_battery_life apm_battery_time audacious_bar audacious_bitrate audacious_channels audacious_filename audacious_frequency audacious_length audacious_length_seconds audacious_main_volume audacious_playlist_length audacious_playlist_position audacious_position audacious_position_seconds audacious_status audacious_title battery battery_bar battery_percent battery_short battery_time blink bmpx_album bmpx_artist bmpx_bitrate bmpx_title bmpx_track bmpx_uri buffers cached cmdline_to_pid color color0 color1 color2 color3 color4 color5 color6 color7 color8 color9 combine conky_build_arch conky_build_date conky_version cpu cpubar cpugauge cpugraph curl desktop desktop_name desktop_number disk_protect diskio diskio_read diskio_write diskiograph diskiograph_read diskiograph_write downspeed downspeedf downspeedgraph draft_mails else endif entropy_avail entropy_bar entropy_perc entropy_poolsize eval eve exec execbar execgauge execgraph execi execibar execigauge execigraph execp execpi flagged_mails font format_time forwarded_mails freq freq_g fs_bar fs_bar_free fs_free fs_free_perc fs_size fs_type fs_used fs_used_perc goto gw_iface gw_ip hddtemp head hr hwmon i2c i8k_ac_status i8k_bios i8k_buttons_status i8k_cpu_temp i8k_left_fan_rpm i8k_left_fan_status i8k_right_fan_rpm i8k_right_fan_status i8k_serial i8k_version ibm_brightness ibm_fan ibm_temps ibm_volume iconv_start iconv_stop if_empty if_existing if_gw if_match if_mixer_mute if_mounted if_mpd_playing if_running if_smapi_bat_installed if_up if_updatenr if_xmms2_connected image imap_messages imap_unseen include ioscheduler kernel laptop_mode lines loadavg loadgraph lua lua_bar lua_gauge lua_graph lua_parse machine mails mboxscan mem membar memeasyfree memfree memgauge memgraph memmax memperc mixer mixerbar mixerl mixerlbar mixerr mixerrbar moc_album moc_artist moc_bitrate moc_curtime moc_file moc_rate moc_song moc_state moc_timeleft moc_title moc_totaltime monitor monitor_number mpd_album mpd_artist mpd_bar mpd_bitrate mpd_elapsed mpd_file mpd_length mpd_name mpd_percent mpd_random mpd_repeat mpd_smart mpd_status mpd_title mpd_track mpd_vol nameserver new_mails nodename nvidia obsd_product obsd_sensors_fan obsd_sensors_temp obsd_sensors_volt obsd_vendor offset outlinecolor pb_battery pid_chroot pid_cmdline pid_cwd pid_environ pid_environ_list pid_exe pid_nice pid_openfiles pid_parent pid_priority pid_state pid_state_short pid_stderr pid_stdin pid_stdout pid_threads pid_thread_list pid_time_kernelmode pid_time_usermode pid_time pid_uid pid_euid pid_suid pid_fsuid pid_gid pid_egid pid_sgid pid_fsgid pid_read pid_vmpeak pid_vmsize pid_vmlck pid_vmhwm pid_vmrss pid_vmdata pid_vmstk pid_vmexe pid_vmlib pid_vmpte pid_write platform pop3_unseen pop3_used pre_exec processes read_tcp replied_mails rss running_processes running_threads scroll seen_mails shadecolor smapi smapi_bat_bar smapi_bat_perc smapi_bat_power smapi_bat_temp sony_fanspeed stippled_hr swap swapbar swapfree swapmax swapperc sysname tab tail tcp_portmon template0 template1 template2 template3 template4 template5 template6 template7 template8 template9 texeci texecpi threads time to_bytes top top_io top_mem top_time totaldown totalup trashed_mails tztime gid_name uid_name unflagged_mails unforwarded_mails unreplied_mails unseen_mails updates upspeed upspeedf upspeedgraph uptime uptime_short user_names user_number user_terms user_times user_time utime voffset voltage_mv voltage_v weather wireless_ap wireless_bitrate wireless_essid wireless_link_bar wireless_link_qual wireless_link_qual_max wireless_link_qual_perc wireless_mode words xmms2_album xmms2_artist xmms2_bar xmms2_bitrate xmms2_comment xmms2_date xmms2_duration xmms2_elapsed xmms2_genre xmms2_id xmms2_percent xmms2_playlist xmms2_size xmms2_smart xmms2_status xmms2_timesplayed xmms2_title xmms2_tracknr xmms2_url +syn keyword ConkyrcVarName contained nextgroup=ConkyrcNumber,ConkyrcColour skipwhite acpiacadapter acpifan acpitemp addr addrs alignc alignr apcupsd apcupsd_cable apcupsd_charge apcupsd_lastxfer apcupsd_linev apcupsd_load apcupsd_loadbar apcupsd_loadgauge apcupsd_loadgraph apcupsd_model apcupsd_name apcupsd_status apcupsd_temp apcupsd_timeleft apcupsd_upsmode apm_adapter apm_battery_life apm_battery_time audacious_bar audacious_bitrate audacious_channels audacious_filename audacious_frequency audacious_length audacious_length_seconds audacious_main_volume audacious_playlist_length audacious_playlist_position audacious_position audacious_position_seconds audacious_status audacious_title battery battery_bar battery_percent battery_short battery_time blink bmpx_album bmpx_artist bmpx_bitrate bmpx_title bmpx_track bmpx_uri buffers cached cmdline_to_pid color color0 color1 color2 color3 color4 color5 color6 color7 color8 color9 combine conky_build_arch conky_build_date conky_version cpu cpubar cpugauge cpugraph curl desktop desktop_name desktop_number disk_protect diskio diskio_read diskio_write diskiograph diskiograph_read diskiograph_write downspeed downspeedf downspeedgraph draft_mails else endif entropy_avail entropy_bar entropy_perc entropy_poolsize eval eve exec execbar execgauge execgraph execi execibar execigauge execigraph execp execpi flagged_mails font format_time forwarded_mails freq freq_g fs_bar fs_bar_free fs_free fs_free_perc fs_size fs_type fs_used fs_used_perc goto gw_iface gw_ip hddtemp head hr hwmon i2c i8k_ac_status i8k_bios i8k_buttons_status i8k_cpu_temp i8k_left_fan_rpm i8k_left_fan_status i8k_right_fan_rpm i8k_right_fan_status i8k_serial i8k_version ibm_brightness ibm_fan ibm_temps ibm_volume iconv_start iconv_stop if_empty if_existing if_gw if_match if_mixer_mute if_mounted if_mpd_playing if_running if_smapi_bat_installed if_up if_updatenr if_xmms2_connected image imap_messages imap_unseen include ioscheduler kernel laptop_mode lines loadavg loadgraph lua lua_bar lua_gauge lua_graph lua_parse machine mails mboxscan mem memwithbuffers membar memwithbuffersbar memeasyfree memfree memgauge memgraph memmax memperc mixer mixerbar mixerl mixerlbar mixerr mixerrbar moc_album moc_artist moc_bitrate moc_curtime moc_file moc_rate moc_song moc_state moc_timeleft moc_title moc_totaltime monitor monitor_number mpd_album mpd_artist mpd_bar mpd_bitrate mpd_elapsed mpd_file mpd_length mpd_name mpd_percent mpd_random mpd_repeat mpd_smart mpd_status mpd_title mpd_track mpd_vol nameserver new_mails nodename nvidia obsd_product obsd_sensors_fan obsd_sensors_temp obsd_sensors_volt obsd_vendor offset outlinecolor pb_battery pid_chroot pid_cmdline pid_cwd pid_environ pid_environ_list pid_exe pid_nice pid_openfiles pid_parent pid_priority pid_state pid_state_short pid_stderr pid_stdin pid_stdout pid_threads pid_thread_list pid_time_kernelmode pid_time_usermode pid_time pid_uid pid_euid pid_suid pid_fsuid pid_gid pid_egid pid_sgid pid_fsgid pid_read pid_vmpeak pid_vmsize pid_vmlck pid_vmhwm pid_vmrss pid_vmdata pid_vmstk pid_vmexe pid_vmlib pid_vmpte pid_write platform pop3_unseen pop3_used pre_exec processes read_tcp replied_mails rss running_processes running_threads scroll seen_mails shadecolor smapi smapi_bat_bar smapi_bat_perc smapi_bat_power smapi_bat_temp sony_fanspeed stippled_hr swap swapbar swapfree swapmax swapperc sysname tab tail tcp_portmon template0 template1 template2 template3 template4 template5 template6 template7 template8 template9 texeci texecpi threads time to_bytes top top_io top_mem top_time totaldown totalup trashed_mails tztime gid_name uid_name unflagged_mails unforwarded_mails unreplied_mails unseen_mails updates upspeed upspeedf upspeedgraph uptime uptime_short user_names user_number user_terms user_times user_time utime voffset voltage_mv voltage_v weather wireless_ap wireless_bitrate wireless_essid wireless_link_bar wireless_link_qual wireless_link_qual_max wireless_link_qual_perc wireless_mode words xmms2_album xmms2_artist xmms2_bar xmms2_bitrate xmms2_comment xmms2_date xmms2_duration xmms2_elapsed xmms2_genre xmms2_id xmms2_percent xmms2_playlist xmms2_size xmms2_smart xmms2_status xmms2_timesplayed xmms2_title xmms2_tracknr xmms2_url hi def link ConkyrcComment Comment hi def link ConkyrcSetting Keyword diff --git a/src/common.cc b/src/common.cc index dc1cfa8e..08e88e6a 100644 --- a/src/common.cc +++ b/src/common.cc @@ -517,6 +517,7 @@ void print_##name(struct text_object *obj, char *p, int p_max_size) \ } PRINT_HR_GENERATOR(mem) +PRINT_HR_GENERATOR(memwithbuffers) PRINT_HR_GENERATOR(memeasyfree) PRINT_HR_GENERATOR(memfree) PRINT_HR_GENERATOR(memmax) @@ -538,6 +539,13 @@ double mem_barval(struct text_object *obj) return info.memmax ? ((double)info.mem / info.memmax) : 0; } +double mem_with_buffers_barval(struct text_object *obj) +{ + (void)obj; + + return info.memmax ? ((double)info.memwithbuffers / info.memmax) : 0; +} + uint8_t swap_percentage(struct text_object *obj) { (void)obj; diff --git a/src/common.h b/src/common.h index aa6519e6..42210bbf 100644 --- a/src/common.h +++ b/src/common.h @@ -105,6 +105,7 @@ uint8_t cpu_percentage(struct text_object *); double cpu_barval(struct text_object *); void print_mem(struct text_object *, char *, int); +void print_memwithbuffers(struct text_object *, char *, int); void print_memeasyfree(struct text_object *, char *, int); void print_memfree(struct text_object *, char *, int); void print_memmax(struct text_object *, char *, int); @@ -113,6 +114,7 @@ void print_swapfree(struct text_object *, char *, int); void print_swapmax(struct text_object *, char *, int); uint8_t mem_percentage(struct text_object *); double mem_barval(struct text_object *); +double mem_with_buffers_barval(struct text_object *); uint8_t swap_percentage(struct text_object *); double swap_barval(struct text_object *); diff --git a/src/conky.h b/src/conky.h index 306b3766..146fd8ab 100644 --- a/src/conky.h +++ b/src/conky.h @@ -209,7 +209,8 @@ struct information { double uptime; /* memory information in kilobytes */ - unsigned long long mem, memeasyfree, memfree, memmax, swap, swapfree, swapmax; + unsigned long long mem, memwithbuffers, memeasyfree, memfree, memmax; + unsigned long long swap, swapfree, swapmax; unsigned long long bufmem, buffers, cached; unsigned short procs; diff --git a/src/core.cc b/src/core.cc index 4277ad64..7e7fee16 100644 --- a/src/core.cc +++ b/src/core.cc @@ -839,6 +839,8 @@ struct text_object *construct_text_object(char *s, const char *arg, long obj->callbacks.free = &free_mboxscan; END OBJ(mem, &update_meminfo) obj->callbacks.print = &print_mem; + END OBJ(memwithbuffers, &update_meminfo) + obj->callbacks.print = &print_memwithbuffers; END OBJ(memeasyfree, &update_meminfo) obj->callbacks.print = &print_memeasyfree; END OBJ(memfree, &update_meminfo) @@ -853,6 +855,9 @@ struct text_object *construct_text_object(char *s, const char *arg, long END OBJ(membar, &update_meminfo) scan_bar(obj, arg, 1); obj->callbacks.barval = &mem_barval; + END OBJ(memwithbuffersbar, &update_meminfo) + scan_bar(obj, arg, 1); + obj->callbacks.barval = &mem_with_buffers_barval; #ifdef BUILD_X11 END OBJ(memgraph, &update_meminfo) char *buf = 0; diff --git a/src/freebsd.cc b/src/freebsd.cc index e53ec7b4..dfe18b71 100644 --- a/src/freebsd.cc +++ b/src/freebsd.cc @@ -177,6 +177,7 @@ void update_meminfo(void) info.memmax = total_pages * (pagesize >> 10); info.mem = (total_pages - free_pages - inactive_pages) * (pagesize >> 10); + info.memwithbuffers = info.mem; info.memeasyfree = info.memfree = info.memmax - info.mem; if ((swapmode(&swap_avail, &swap_free)) >= 0) { diff --git a/src/linux.cc b/src/linux.cc index ad3272cc..5b18f9fe 100644 --- a/src/linux.cc +++ b/src/linux.cc @@ -161,8 +161,8 @@ void update_meminfo(void) /* unsigned int a; */ char buf[256]; - info.mem = info.memmax = info.swap = info.swapfree = info.swapmax = info.bufmem = - info.buffers = info.cached = info.memfree = info.memeasyfree = 0; + info.mem = info.memwithbuffers = info.memmax = info.swap = info.swapfree = info.swapmax = + info.bufmem = info.buffers = info.cached = info.memfree = info.memeasyfree = 0; if (!(meminfo_fp = open_file("/proc/meminfo", &rep))) { return; @@ -188,7 +188,7 @@ void update_meminfo(void) } } - info.mem = info.memmax - info.memfree; + info.mem = info.memwithbuffers = info.memmax - info.memfree; info.memeasyfree = info.memfree; info.swap = info.swapmax - info.swapfree; diff --git a/src/netbsd.cc b/src/netbsd.cc index 909c622d..2d4db821 100644 --- a/src/netbsd.cc +++ b/src/netbsd.cc @@ -135,6 +135,7 @@ void update_meminfo() info.memmax = (total_pages * pagesize) >> 10; info.mem = ((total_pages - free_pages - inactive_pages) * pagesize) >> 10; + info.memwithbuffers = info.mem; info.memeasyfree = info.memfree = info.memmax - info.mem; if (swapmode(&swap_avail, &swap_free) >= 0) { diff --git a/src/openbsd.cc b/src/openbsd.cc index e9bb8189..4a53b4a1 100644 --- a/src/openbsd.cc +++ b/src/openbsd.cc @@ -183,7 +183,7 @@ void update_meminfo() } info.memmax = pagetok(vmtotal.t_rm) + pagetok(vmtotal.t_free); - info.mem = pagetok(vmtotal.t_rm); + info.mem = info.memwithbuffers = pagetok(vmtotal.t_rm); info.memeasyfree = info.memfree = info.memmax - info.mem; if ((swapmode(&swap_used, &swap_avail)) >= 0) { From f695ed58d9cab88470c54bd95b25251e19fcb6d4 Mon Sep 17 00:00:00 2001 From: Nikolas Garofil Date: Fri, 19 Feb 2010 00:09:24 +0100 Subject: [PATCH 03/34] Hide false memleaks in a debugbuild This makes sure valgrind doesn't show false memleaks that are caused by ncurses instead of our code when we do a 'Debug' build. There is a also a disadvantage: When your ncurses library is not build with the '--disable-leaks' option (this is the case in the default version of ncurses of most distro's) and you build conky with ncurses and debug turned on then you will see a segmentation fault when you stop conky. On the other hand people that want to build conky as a debug build with ncurses support will probably also have a ncurses with --disable-leaks ... --- src/conky.cc | 6 ++++++ src/nc.h | 11 +++++++++++ 2 files changed, 17 insertions(+) create mode 100644 src/nc.h diff --git a/src/conky.cc b/src/conky.cc index 1627c267..2f6ff6c4 100644 --- a/src/conky.cc +++ b/src/conky.cc @@ -66,6 +66,9 @@ #include #ifdef BUILD_NCURSES #include +#ifdef DEBUG +#include "nc.h" +#endif #endif #ifdef BUILD_WEATHER_XOAP #include @@ -4341,6 +4344,9 @@ int main(int argc, char **argv) kvm_close(kd); #endif +#if defined(BUILD_NCURSES) && defined(DEBUG) + _nc_free_and_exit(0); //hide false memleaks +#endif return 0; } diff --git a/src/nc.h b/src/nc.h new file mode 100644 index 00000000..73dddc9b --- /dev/null +++ b/src/nc.h @@ -0,0 +1,11 @@ +/* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*- */ + +#ifdef __cplusplus +extern "C" { +#endif + +void _nc_free_and_exit(int); + +#ifdef __cplusplus +} +#endif From 0627946ca13ebba2f7958ce67e8035531e82b7d6 Mon Sep 17 00:00:00 2001 From: Nikolas Garofil Date: Fri, 19 Feb 2010 13:21:40 +0100 Subject: [PATCH 04/34] Make hiding ncurses memleaks optional and disable it by default --- cmake/ConkyBuildOptions.cmake | 5 +++++ cmake/config.h.in | 1 + src/conky.cc | 4 ++-- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/cmake/ConkyBuildOptions.cmake b/cmake/ConkyBuildOptions.cmake index 35126468..0187d7f4 100644 --- a/cmake/ConkyBuildOptions.cmake +++ b/cmake/ConkyBuildOptions.cmake @@ -87,6 +87,11 @@ option(BUILD_BUILTIN_CONFIG "Enable builtin default configuration" true) option(BUILD_MATH "Enable math support" true) option(BUILD_NCURSES "Enable ncurses support" true) +if(BUILD_NCURSES) + option(LEAKFREE_NCURSES "Enable to hide false ncurses-memleaks in valgrind (works only when ncurses is compiled with --disable-leaks)" false) +else(BUILD_NCURSES) + set(LEAKFREE_NCURSES false CACHE BOOL "Enable to hide false ncurses-memleaks in valgrind (works only when ncurses is compiled with --disable-leaks)" FORCE) +endif(BUILD_NCURSES) option(BUILD_X11 "Build X11 support" true) if(BUILD_X11) diff --git a/cmake/config.h.in b/cmake/config.h.in index 070872b8..fad59201 100644 --- a/cmake/config.h.in +++ b/cmake/config.h.in @@ -78,6 +78,7 @@ #cmakedefine BUILD_BUILTIN_CONFIG 1 #cmakedefine BUILD_NCURSES 1 +#cmakedefine LEAKFREE_NCURSES 1 #cmakedefine BUILD_APCUPSD 1 diff --git a/src/conky.cc b/src/conky.cc index 2f6ff6c4..c9b0ac2c 100644 --- a/src/conky.cc +++ b/src/conky.cc @@ -66,7 +66,7 @@ #include #ifdef BUILD_NCURSES #include -#ifdef DEBUG +#ifdef LEAKFREE_NCURSES #include "nc.h" #endif #endif @@ -4344,7 +4344,7 @@ int main(int argc, char **argv) kvm_close(kd); #endif -#if defined(BUILD_NCURSES) && defined(DEBUG) +#ifdef LEAKFREE_NCURSES _nc_free_and_exit(0); //hide false memleaks #endif return 0; From 99b5352cff543662af02a7772a28dd2e21b9b414 Mon Sep 17 00:00:00 2001 From: Nikolas Garofil Date: Sat, 20 Feb 2010 02:25:38 +0100 Subject: [PATCH 05/34] Add --for-scripts support --- doc/command_options.xml | 13 +++++++++++++ src/conky.cc | 35 ++++++++++++++++++++++++++++++++--- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/doc/command_options.xml b/doc/command_options.xml index fd512a91..e920e3c2 100644 --- a/doc/command_options.xml +++ b/doc/command_options.xml @@ -106,6 +106,19 @@ Create own window to draw + + + + + + + + Prints TEXT on the standard output and exits. + (This option makes conky useful in scripts) + TEXT has to be enclosed by single quotes. Example: + conky -s 'Your uptime is: $uptime' + + diff --git a/src/conky.cc b/src/conky.cc index c9b0ac2c..4b2c5733 100644 --- a/src/conky.cc +++ b/src/conky.cc @@ -3944,6 +3944,7 @@ static void print_help(const char *prog_name) { " -x X x position\n" " -y Y y position\n" #endif /* BUILD_X11 */ + " -s, --for-scripts=TEXT render TEXT on stdout and exit, enclose TEXT by single quotes\n" " -t, --text=TEXT text to render, remember single quotes, like -t '$uptime'\n" " -u, --interval=SECS update interval\n" " -i COUNT number of times to update "PACKAGE_NAME" (and quit)\n" @@ -3953,7 +3954,7 @@ static void print_help(const char *prog_name) { } /* : means that character before that takes an argument */ -static const char *getopt_string = "vVqdDt:u:i:hc:p:" +static const char *getopt_string = "vVqdDs:t:u:i:hc:p:" #ifdef BUILD_X11 "x:y:w:a:f:X:" #ifdef OWN_WINDOW @@ -3989,6 +3990,7 @@ static const struct option longopts[] = { #endif { "window-id", 1, NULL, 'w' }, #endif /* BUILD_X11 */ + { "for-scripts", 1, NULL, 's' }, { "text", 1, NULL, 't' }, { "interval", 0, NULL, 'u' }, { "pause", 0, NULL, 'p' }, @@ -3997,10 +3999,37 @@ static const struct option longopts[] = { void initialisation(int argc, char **argv) { struct sigaction act, oact; + bool for_scripts = false; set_default_configurations(); - load_config_file(current_config); - currentconffile = conftree_add(currentconffile, current_config); +#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) \ + || defined(__NetBSD__) + optind = optreset = 1; +#else + optind = 0; +#endif + + while (1) { + int c = getopt_long(argc, argv, getopt_string, longopts, NULL); + + if (c == -1) { + break; + }else if (c == 's') { + if (global_text) { + free(global_text); + global_text = 0; + } + global_text = strndup(optarg, max_user_text); + convert_escapes(global_text); + total_run_times = 1; + output_methods = TO_STDOUT; + for_scripts = true; + } + } + if(for_scripts == false) { + load_config_file(current_config); + currentconffile = conftree_add(currentconffile, current_config); + } /* init specials array */ if ((specials = (special_t*)calloc(sizeof(struct special_t), max_specials)) == 0) { From a2a9ed054da2727d7f8c5e019c1c105804ae9cdb Mon Sep 17 00:00:00 2001 From: Nikolas Garofil Date: Sat, 20 Feb 2010 14:34:40 +0100 Subject: [PATCH 06/34] Shrink code by letting a separate function handle the resetting of optind --- src/conky.cc | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/conky.cc b/src/conky.cc index 4b2c5733..2496a0ce 100644 --- a/src/conky.cc +++ b/src/conky.cc @@ -3953,6 +3953,15 @@ static void print_help(const char *prog_name) { ); } +inline void reset_optind() { +#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) \ + || defined(__NetBSD__) + optind = optreset = 1; +#else + optind = 0; +#endif +} + /* : means that character before that takes an argument */ static const char *getopt_string = "vVqdDs:t:u:i:hc:p:" #ifdef BUILD_X11 @@ -4002,13 +4011,8 @@ void initialisation(int argc, char **argv) { bool for_scripts = false; set_default_configurations(); -#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) \ - || defined(__NetBSD__) - optind = optreset = 1; -#else - optind = 0; -#endif + reset_optind(); while (1) { int c = getopt_long(argc, argv, getopt_string, longopts, NULL); @@ -4050,12 +4054,7 @@ void initialisation(int argc, char **argv) { /* handle other command line arguments */ -#if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__OpenBSD__) \ - || defined(__NetBSD__) - optind = optreset = 1; -#else - optind = 0; -#endif + reset_optind(); #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) if ((kd = kvm_open("/dev/null", "/dev/null", "/dev/null", O_RDONLY, From cbc5aaad91907e7fd77dbc22fede822abae97c49 Mon Sep 17 00:00:00 2001 From: Nikolas Garofil Date: Sat, 20 Feb 2010 15:28:05 +0100 Subject: [PATCH 07/34] Shrink code by adding free_and_zero() --- src/conky.cc | 109 ++++++++++++--------------------------------------- 1 file changed, 24 insertions(+), 85 deletions(-) diff --git a/src/conky.cc b/src/conky.cc index 2496a0ce..bfdd71df 100644 --- a/src/conky.cc +++ b/src/conky.cc @@ -682,21 +682,14 @@ void conftree_empty(struct conftree* tree) { struct conftree *currentconffile; +#define free_and_zero(PTR) if(PTR) { free(PTR); PTR = NULL; } + static void extract_variable_text(const char *p) { free_text_objects(&global_root_object); - if (tmpstring1) { - free(tmpstring1); - tmpstring1 = 0; - } - if (tmpstring2) { - free(tmpstring2); - tmpstring2 = 0; - } - if (text_buffer) { - free(text_buffer); - text_buffer = 0; - } + free_and_zero(tmpstring1); + free_and_zero(tmpstring2); + free_and_zero(text_buffer); extract_variable_text_internal(&global_root_object, p); } @@ -2272,14 +2265,8 @@ static void main_loop(void) } } #endif /* BUILD_X11 */ - if(overwrite_file) { - free(overwrite_file); - overwrite_file = 0; - } - if(append_file) { - free(append_file); - append_file = 0; - } + free_and_zero(overwrite_file); + free_and_zero(append_file); break; default: /* Reaching here means someone set a signal @@ -2394,10 +2381,7 @@ void clean_up(void *memtofree1, void* memtofree2) } timed_thread::destroy_registered_threads(); - if (info.cpu_usage) { - free(info.cpu_usage); - info.cpu_usage = NULL; - } + free_and_zero(info.cpu_usage); #ifdef BUILD_X11 if (x_initialised == YES) { if(window_created == 1) { @@ -2414,14 +2398,8 @@ void clean_up(void *memtofree1, void* memtofree2) } XCloseDisplay(display); display = NULL; - if(info.x11.desktop.all_names) { - free(info.x11.desktop.all_names); - info.x11.desktop.all_names = NULL; - } - if (info.x11.desktop.name) { - free(info.x11.desktop.name); - info.x11.desktop.name = NULL; - } + free_and_zero(info.x11.desktop.all_names); + free_and_zero(info.x11.desktop.name); x_initialised = NO; }else{ free(fonts); //in set_default_configurations a font is set but not loaded @@ -2442,26 +2420,11 @@ void clean_up(void *memtofree1, void* memtofree2) #endif /* BUILD_X11 */ free_text_objects(&global_root_object); - if (tmpstring1) { - free(tmpstring1); - tmpstring1 = 0; - } - if (tmpstring2) { - free(tmpstring2); - tmpstring2 = 0; - } - if (text_buffer) { - free(text_buffer); - text_buffer = 0; - } - - if (global_text) { - free(global_text); - global_text = 0; - } - - free(current_config); - current_config = 0; + free_and_zero(tmpstring1); + free_and_zero(tmpstring2); + free_and_zero(text_buffer); + free_and_zero(global_text); + free_and_zero(current_config); #ifdef BUILD_PORT_MONITORS tcp_portmon_clear(); @@ -2493,16 +2456,12 @@ void clean_up(void *memtofree1, void* memtofree2) free(specials[i].graph); } } - free(specials); - specials = NULL; + free_and_zero(specials); } clear_net_stats(); clear_diskio_stats(); - if(global_cpu != NULL) { - free(global_cpu); - global_cpu = NULL; - } + free_and_zero(global_cpu); } static int string_to_bool(const char *s) @@ -3221,10 +3180,7 @@ char load_config_file(const char *f) } #endif CONF("overwrite_file") { - if(overwrite_file) { - free(overwrite_file); - overwrite_file = 0; - } + free_and_zero(overwrite_file); if (overwrite_works(value)) { overwrite_file = strdup(value); output_methods |= OVERWRITE_FILE; @@ -3232,10 +3188,7 @@ char load_config_file(const char *f) NORM_ERR("overwrite_file won't be able to create/overwrite '%s'", value); } CONF("append_file") { - if(append_file) { - free(append_file); - append_file = 0; - } + free_and_zero(append_file); if(append_works(value)) { append_file = strdup(value); output_methods |= APPEND_FILE; @@ -3558,10 +3511,7 @@ char load_config_file(const char *f) } #endif - if (global_text) { - free(global_text); - global_text = 0; - } + free_and_zero(global_text); global_text = (char *) malloc(1); global_text[0] = '\0'; @@ -4019,10 +3969,7 @@ void initialisation(int argc, char **argv) { if (c == -1) { break; }else if (c == 's') { - if (global_text) { - free(global_text); - global_text = 0; - } + free_and_zero(global_text); global_text = strndup(optarg, max_user_text); convert_escapes(global_text); total_run_times = 1; @@ -4095,10 +4042,7 @@ void initialisation(int argc, char **argv) { #endif #endif /* BUILD_X11 */ case 't': - if (global_text) { - free(global_text); - global_text = 0; - } + free_and_zero(global_text); global_text = strndup(optarg, max_user_text); convert_escapes(global_text); break; @@ -4145,11 +4089,7 @@ void initialisation(int argc, char **argv) { /* generate text and get initial size */ extract_variable_text(global_text); - if (global_text) { - free(global_text); - global_text = 0; - } - global_text = NULL; + free_and_zero(global_text); /* fork */ if (fork_to_background && first_pass) { int pid = fork(); @@ -4315,8 +4255,7 @@ int main(int argc, char **argv) if (stat(current_config, &sb) || (!S_ISREG(sb.st_mode) && !S_ISLNK(sb.st_mode))) { NORM_ERR("invalid configuration file '%s'\n", current_config); - free(current_config); - current_config = 0; + free_and_zero(current_config); } } From d91214e290166f7cd2c8bacf5a5c1ca46b658c8b Mon Sep 17 00:00:00 2001 From: Nikolas Garofil Date: Sat, 20 Feb 2010 17:59:51 +0100 Subject: [PATCH 08/34] Fix using the wireless-vars --- cmake/ConkyPlatformChecks.cmake | 2 +- src/linux.cc | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/cmake/ConkyPlatformChecks.cmake b/cmake/ConkyPlatformChecks.cmake index 6d26b971..e33284ad 100644 --- a/cmake/ConkyPlatformChecks.cmake +++ b/cmake/ConkyPlatformChecks.cmake @@ -89,7 +89,7 @@ if(BUILD_WLAN) if(NOT IWLIB_LIB) message(FATAL_ERROR "Unable to find libiw.so") endif(NOT IWLIB_LIB) - set(CMAKE_REQUIRED_LIBRARIES ${IWLIB_LIB}) + set(conky_libs ${conky_libs} ${IWLIB_LIB}) check_function_exists(iw_sockets_open IWLIB_SOCKETS_OPEN_FUNC) endif(BUILD_WLAN) diff --git a/src/linux.cc b/src/linux.cc index 5b18f9fe..c2272ea0 100644 --- a/src/linux.cc +++ b/src/linux.cc @@ -76,7 +76,7 @@ #define NBD_MAJOR 43 #endif -#ifdef HAVE_IWLIB +#ifdef BUILD_WLAN #include #endif @@ -356,7 +356,7 @@ void update_net_stats(void) char buf[256]; double delta; -#ifdef HAVE_IWLIB +#ifdef BUILD_WLAN // wireless info variables int skfd, has_bitrate = 0; struct wireless_info *winfo; @@ -496,9 +496,9 @@ void update_net_stats(void) } } -#ifdef HAVE_IWLIB +#ifdef BUILD_WLAN /* update wireless info */ - winfo = malloc(sizeof(struct wireless_info)); + winfo = (struct wireless_info *) malloc(sizeof(struct wireless_info)); memset(winfo, 0, sizeof(struct wireless_info)); skfd = iw_sockets_open(); From d184efb418e8b39a443bb7283fa06e0734c169df Mon Sep 17 00:00:00 2001 From: Nikolas Garofil Date: Sun, 21 Feb 2010 15:12:38 +0100 Subject: [PATCH 09/34] When using without argument, make sure that it shows the essid of the first device that has one instead of using the default network device (because most of the times this will be a non-wireless device) --- src/core.cc | 2 +- src/net_stat.cc | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/core.cc b/src/core.cc index 7e7fee16..671c7164 100644 --- a/src/core.cc +++ b/src/core.cc @@ -241,7 +241,7 @@ struct text_object *construct_text_object(char *s, const char *arg, long #ifdef BUILD_WLAN END OBJ(wireless_essid, &update_net_stats) - parse_net_stat_arg(obj, arg, free_at_crash); + obj->data.opaque = get_net_stat(arg, obj, free_at_crash); obj->callbacks.print = &print_wireless_essid; END OBJ(wireless_mode, &update_net_stats) parse_net_stat_arg(obj, arg, free_at_crash); diff --git a/src/net_stat.cc b/src/net_stat.cc index 603a8c58..38bfcd61 100644 --- a/src/net_stat.cc +++ b/src/net_stat.cc @@ -226,8 +226,15 @@ void print_wireless_essid(struct text_object *obj, char *p, int p_max_size) { struct net_stat *ns = (struct net_stat *)obj->data.opaque; - if (!ns) + if (!ns) { + for(unsigned int i = 0; *(netstats[i].dev) != 0; i++) { + if(*(netstats[i].essid) != 0) { + snprintf(p, p_max_size, "%s", netstats[i].essid); + return; + } + } return; + } snprintf(p, p_max_size, "%s", ns->essid); } From 31e22acb4b069a5d5023b2ec22ee90ad13863856 Mon Sep 17 00:00:00 2001 From: Nikolas Garofil Date: Sun, 21 Feb 2010 19:26:11 +0100 Subject: [PATCH 10/34] Fix crash with (for example): conky -s '${combine $uptime $uptime}' --- src/conky.cc | 1 - 1 file changed, 1 deletion(-) diff --git a/src/conky.cc b/src/conky.cc index bfdd71df..4c53ab37 100644 --- a/src/conky.cc +++ b/src/conky.cc @@ -2419,7 +2419,6 @@ void clean_up(void *memtofree1, void* memtofree2) free_desktop_info(); #endif /* BUILD_X11 */ - free_text_objects(&global_root_object); free_and_zero(tmpstring1); free_and_zero(tmpstring2); free_and_zero(text_buffer); From b88d69dbfc4f6d0ce28f01c8d26c14b356918d70 Mon Sep 17 00:00:00 2001 From: Nikolas Garofil Date: Sun, 21 Feb 2010 20:15:08 +0100 Subject: [PATCH 11/34] Implement fix from previous commit in another way to fix a leak that happens when conky closes --- src/conky.cc | 1 + src/core.cc | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/src/conky.cc b/src/conky.cc index 4c53ab37..bfdd71df 100644 --- a/src/conky.cc +++ b/src/conky.cc @@ -2419,6 +2419,7 @@ void clean_up(void *memtofree1, void* memtofree2) free_desktop_info(); #endif /* BUILD_X11 */ + free_text_objects(&global_root_object); free_and_zero(tmpstring1); free_and_zero(tmpstring2); free_and_zero(text_buffer); diff --git a/src/core.cc b/src/core.cc index 671c7164..55948581 100644 --- a/src/core.cc +++ b/src/core.cc @@ -1863,7 +1863,6 @@ void free_text_objects(struct text_object *root) } if(obj->sub) { free_text_objects(obj->sub); - free(obj->sub); } if(obj->special_data) free(obj->special_data); From e50ec9ded60a14cffd41911b6e3d54583004da71 Mon Sep 17 00:00:00 2001 From: Nikolas Garofil Date: Mon, 22 Feb 2010 13:02:24 +0100 Subject: [PATCH 12/34] Fix $combine reading unallocated memory --- src/combine.cc | 5 ++--- src/conky.cc | 2 -- src/core.h | 2 ++ 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/src/combine.cc b/src/combine.cc index f44f7dc6..40738480 100644 --- a/src/combine.cc +++ b/src/combine.cc @@ -181,7 +181,6 @@ void free_combine(struct text_object *obj) free(cd->seperation); free(cd->right); free_text_objects(obj->sub); - free(obj->sub); - free(obj->data.opaque); - obj->data.opaque = NULL; + free_and_zero(obj->sub); + free_and_zero(obj->data.opaque); } diff --git a/src/conky.cc b/src/conky.cc index bfdd71df..aec982d0 100644 --- a/src/conky.cc +++ b/src/conky.cc @@ -682,8 +682,6 @@ void conftree_empty(struct conftree* tree) { struct conftree *currentconffile; -#define free_and_zero(PTR) if(PTR) { free(PTR); PTR = NULL; } - static void extract_variable_text(const char *p) { free_text_objects(&global_root_object); diff --git a/src/core.h b/src/core.h index ed4ff091..89263228 100644 --- a/src/core.h +++ b/src/core.h @@ -28,6 +28,8 @@ * */ +#define free_and_zero(PTR) if(PTR) { free(PTR); PTR = NULL; } + #ifndef _CONKY_CORE_H_ #define _CONKY_CORE_H_ From 15e84fcfa178a9a99c16ddf989231f3c22395947 Mon Sep 17 00:00:00 2001 From: Nikolas Garofil Date: Mon, 22 Feb 2010 14:08:38 +0100 Subject: [PATCH 13/34] Fix last memleak in $combine --- src/combine.cc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/combine.cc b/src/combine.cc index 40738480..148f61a9 100644 --- a/src/combine.cc +++ b/src/combine.cc @@ -180,6 +180,8 @@ void free_combine(struct text_object *obj) free(cd->left); free(cd->seperation); free(cd->right); + free_text_objects(obj->sub->sub); + free_and_zero(obj->sub->sub); free_text_objects(obj->sub); free_and_zero(obj->sub); free_and_zero(obj->data.opaque); From ade4f3fc0c12b431853ee220d402dfad0eda97a7 Mon Sep 17 00:00:00 2001 From: Nikolas Garofil Date: Mon, 22 Feb 2010 14:22:24 +0100 Subject: [PATCH 14/34] Minimize change in free_text_objects introduced by b88d69dbfc4f6d0ce28f01c8d26c14b356918d70 --- src/core.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/src/core.cc b/src/core.cc index 55948581..d42e97ae 100644 --- a/src/core.cc +++ b/src/core.cc @@ -1863,6 +1863,7 @@ void free_text_objects(struct text_object *root) } if(obj->sub) { free_text_objects(obj->sub); + free_and_zero(obj->sub); } if(obj->special_data) free(obj->special_data); From 44a9e7529097eff25e1fc9150cb44856f5b5e895 Mon Sep 17 00:00:00 2001 From: Nikolas Garofil Date: Mon, 22 Feb 2010 16:30:37 +0100 Subject: [PATCH 15/34] Fix output of $combine when used in combination with $head --- src/combine.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/combine.cc b/src/combine.cc index 148f61a9..aac4cf15 100644 --- a/src/combine.cc +++ b/src/combine.cc @@ -124,7 +124,7 @@ void print_combine(struct text_object *obj, char *p, int p_max_size) generate_text_internal(&(buf[i][0]), max_user_text, *objsub); for(j=0; buf[i][j] != 0; j++) { if(buf[i][j] == '\t') buf[i][j] = ' '; - if(buf[i][j] == '\n') { + if(buf[i][j] == '\n' || buf[i][j] == 2) { //Some vars like $head use \002 instead of \n to separate lines buf[i][j] = 0; current[i]->row = strdup(&(buf[i][0])+nextstart); if(i==0 && (long)strlen(current[i]->row) > longest) longest = (long)strlen(current[i]->row); From ad1b72ed94a5d8ed9d5663bd884a7dc5cf69c0c9 Mon Sep 17 00:00:00 2001 From: Nikolas Garofil Date: Mon, 22 Feb 2010 19:27:05 +0100 Subject: [PATCH 16/34] Fix wrong output when nesting $combine-s --- src/combine.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/combine.cc b/src/combine.cc index aac4cf15..65189f41 100644 --- a/src/combine.cc +++ b/src/combine.cc @@ -124,7 +124,8 @@ void print_combine(struct text_object *obj, char *p, int p_max_size) generate_text_internal(&(buf[i][0]), max_user_text, *objsub); for(j=0; buf[i][j] != 0; j++) { if(buf[i][j] == '\t') buf[i][j] = ' '; - if(buf[i][j] == '\n' || buf[i][j] == 2) { //Some vars like $head use \002 instead of \n to separate lines + if(buf[i][j] == '\n') buf[i][j] = 0; //the vars inside combine may not have a \n at the end + if(buf[i][j] == 2) { // \002 is used instead of \n to separate lines inside a var buf[i][j] = 0; current[i]->row = strdup(&(buf[i][0])+nextstart); if(i==0 && (long)strlen(current[i]->row) > longest) longest = (long)strlen(current[i]->row); From c2c58cd8d7613b3ee5e8737564b09237c6c874db Mon Sep 17 00:00:00 2001 From: Nikolas Garofil Date: Mon, 22 Feb 2010 22:03:14 +0100 Subject: [PATCH 17/34] Use free_and_zero where possible in bmpx.cc --- src/bmpx.cc | 15 +++------------ src/core.h | 2 +- 2 files changed, 4 insertions(+), 13 deletions(-) diff --git a/src/bmpx.cc b/src/bmpx.cc index b9d6066d..f6dde9db 100644 --- a/src/bmpx.cc +++ b/src/bmpx.cc @@ -86,18 +86,9 @@ void update_bmpx() G_TYPE_INT, current_track, G_TYPE_INVALID, DBUS_TYPE_G_STRING_VALUE_HASHTABLE, &metadata, G_TYPE_INVALID)) { - if (current_info->bmpx.title) { - free(current_info->bmpx.title); - current_info->bmpx.title = 0; - } - if (current_info->bmpx.artist) { - free(current_info->bmpx.artist); - current_info->bmpx.artist = 0; - } - if (current_info->bmpx.album) { - free(current_info->bmpx.album); - current_info->bmpx.album = 0; - } + free_and_zero(current_info->bmpx.title); + free_and_zero(current_info->bmpx.artist); + free_and_zero(current_info->bmpx.album); current_info->bmpx.title = g_value_dup_string(g_hash_table_lookup(metadata, "title")); current_info->bmpx.artist = diff --git a/src/core.h b/src/core.h index 89263228..35df3324 100644 --- a/src/core.h +++ b/src/core.h @@ -28,7 +28,7 @@ * */ -#define free_and_zero(PTR) if(PTR) { free(PTR); PTR = NULL; } +#define free_and_zero(PTR) if(PTR) free(PTR); PTR = NULL; #ifndef _CONKY_CORE_H_ #define _CONKY_CORE_H_ From 8be18483e32b2f77a3d3134040a0bdcedd409cce Mon Sep 17 00:00:00 2001 From: Cesare Tirabassi Date: Mon, 22 Feb 2010 22:45:51 +0100 Subject: [PATCH 18/34] Fix LP: #525926; conky sigsegv if config file is deleted Basically, we only relod if the file is modified but still handle the case where the watch gets lost. Maybe we should just ignore IN_IGNORE altogether? (cherry picked from commit 085c39d39464642746a1883266d9dffcfc10e84c) --- src/conky.cc | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/conky.cc b/src/conky.cc index aec982d0..2c7fc2c9 100644 --- a/src/conky.cc +++ b/src/conky.cc @@ -2299,10 +2299,12 @@ static void main_loop(void) len = read(inotify_fd, inotify_buff, INOTIFY_BUF_LEN); while (len > 0 && idx < len) { struct inotify_event *ev = (struct inotify_event *) &inotify_buff[idx]; - if (ev->wd == inotify_config_wd && (ev->mask & IN_MODIFY || ev->mask & IN_IGNORED)) { - /* current_config should be reloaded */ - NORM_ERR("'%s' modified, reloading...", current_config); - reload_config(); + if (ev->wd == inotify_config_wd) { + if (ev->mask & IN_MODIFY) { + /* current_config should be reloaded */ + NORM_ERR("'%s' modified, reloading...", current_config); + reload_config(); + } if (ev->mask & IN_IGNORED) { /* for some reason we get IN_IGNORED here * sometimes, so we need to re-add the watch */ From 397a7a252a51a82a51947f9baeb0a0767bd92d0f Mon Sep 17 00:00:00 2001 From: Nikolas Garofil Date: Tue, 23 Feb 2010 02:58:44 +0100 Subject: [PATCH 19/34] Fix removing the config and sending a SIGUSR1 results in segfault This re-implements the fix of the previous commit because that one assumed that only overwriting the configfile can cause the config to reload --- src/conky.cc | 99 +++++++++++++++++++++++++++------------------------- 1 file changed, 51 insertions(+), 48 deletions(-) diff --git a/src/conky.cc b/src/conky.cc index 2c7fc2c9..8fafab72 100644 --- a/src/conky.cc +++ b/src/conky.cc @@ -2299,12 +2299,10 @@ static void main_loop(void) len = read(inotify_fd, inotify_buff, INOTIFY_BUF_LEN); while (len > 0 && idx < len) { struct inotify_event *ev = (struct inotify_event *) &inotify_buff[idx]; - if (ev->wd == inotify_config_wd) { - if (ev->mask & IN_MODIFY) { - /* current_config should be reloaded */ - NORM_ERR("'%s' modified, reloading...", current_config); - reload_config(); - } + if (ev->wd == inotify_config_wd && (ev->mask & IN_MODIFY || ev->mask & IN_IGNORED)) { + /* current_config should be reloaded */ + NORM_ERR("'%s' modified, reloading...", current_config); + reload_config(); if (ev->mask & IN_IGNORED) { /* for some reason we get IN_IGNORED here * sometimes, so we need to re-add the watch */ @@ -3956,6 +3954,51 @@ static const struct option longopts[] = { { 0, 0, 0, 0 } }; +void set_current_config() { + /* check if specified config file is valid */ + if (current_config) { + struct stat sb; + if (stat(current_config, &sb) || + (!S_ISREG(sb.st_mode) && !S_ISLNK(sb.st_mode))) { + NORM_ERR("invalid configuration file '%s'\n", current_config); + free_and_zero(current_config); + } + } + + /* load current_config, CONFIG_FILE or SYSTEM_CONFIG_FILE */ + + if (!current_config) { + /* load default config file */ + char buf[DEFAULT_TEXT_BUFFER_SIZE]; + FILE *fp; + + /* Try to use personal config file first */ + to_real_path(buf, CONFIG_FILE); + if (buf[0] && (fp = fopen(buf, "r"))) { + current_config = strndup(buf, max_user_text); + fclose(fp); + } + + /* Try to use system config file if personal config not readable */ + if (!current_config && (fp = fopen(SYSTEM_CONFIG_FILE, "r"))) { + current_config = strndup(SYSTEM_CONFIG_FILE, max_user_text); + fclose(fp); + } + + /* No readable config found */ + if (!current_config) { +#define NOCFGFILEFOUND "no readable personal or system-wide config file found" +#ifdef BUILD_BUILTIN_CONFIG + current_config = strdup("==builtin=="); + NORM_ERR(NOCFGFILEFOUND + ", using builtin default"); +#else + CRIT_ERR(NULL, NULL, NOCFGFILEFOUND); +#endif /* ! CONF_OUTPUT */ + } + } +} + void initialisation(int argc, char **argv) { struct sigaction act, oact; bool for_scripts = false; @@ -3978,6 +4021,7 @@ void initialisation(int argc, char **argv) { } } if(for_scripts == false) { + set_current_config(); load_config_file(current_config); currentconffile = conftree_add(currentconffile, current_config); } @@ -4249,48 +4293,7 @@ int main(int argc, char **argv) } } - /* check if specified config file is valid */ - if (current_config) { - struct stat sb; - if (stat(current_config, &sb) || - (!S_ISREG(sb.st_mode) && !S_ISLNK(sb.st_mode))) { - NORM_ERR("invalid configuration file '%s'\n", current_config); - free_and_zero(current_config); - } - } - - /* load current_config, CONFIG_FILE or SYSTEM_CONFIG_FILE */ - - if (!current_config) { - /* load default config file */ - char buf[DEFAULT_TEXT_BUFFER_SIZE]; - FILE *fp; - - /* Try to use personal config file first */ - to_real_path(buf, CONFIG_FILE); - if (buf[0] && (fp = fopen(buf, "r"))) { - current_config = strndup(buf, max_user_text); - fclose(fp); - } - - /* Try to use system config file if personal config not readable */ - if (!current_config && (fp = fopen(SYSTEM_CONFIG_FILE, "r"))) { - current_config = strndup(SYSTEM_CONFIG_FILE, max_user_text); - fclose(fp); - } - - /* No readable config found */ - if (!current_config) { -#define NOCFGFILEFOUND "no readable personal or system-wide config file found" -#ifdef BUILD_BUILTIN_CONFIG - current_config = strdup("==builtin=="); - NORM_ERR(NOCFGFILEFOUND - ", using builtin default"); -#else - CRIT_ERR(NULL, NULL, NOCFGFILEFOUND); -#endif /* ! CONF_OUTPUT */ - } - } + set_current_config(); #ifdef BUILD_WEATHER_XOAP /* Load xoap keys, if existing */ From 0e80e2908a9a622b2a228a25d163c5a21952a5c2 Mon Sep 17 00:00:00 2001 From: Nikolas Garofil Date: Tue, 23 Feb 2010 14:54:03 +0100 Subject: [PATCH 20/34] Use free_and_zero in ccurl_thread.cc where appropriate --- src/ccurl_thread.cc | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/ccurl_thread.cc b/src/ccurl_thread.cc index 33417146..82401dde 100644 --- a/src/ccurl_thread.cc +++ b/src/ccurl_thread.cc @@ -70,8 +70,8 @@ void ccurl_free_locations(ccurl_location_list &locations) { for (ccurl_location_list::iterator i = locations.begin(); i != locations.end(); i++) { - if ((*i)->uri) free((*i)->uri); - if ((*i)->result) free((*i)->result); + free_and_zero((*i)->uri); + free_and_zero((*i)->result); } locations.clear(); } @@ -239,8 +239,5 @@ void curl_print(struct text_object *obj, char *p, int p_max_size) void curl_obj_free(struct text_object *obj) { - if (obj->data.opaque) { - free(obj->data.opaque); - obj->data.opaque = NULL; - } + free_and_zero(obj->data.opaque); } From d7d8bb84ead9a23d76de486dab64ca37a6ded82e Mon Sep 17 00:00:00 2001 From: Pavel Labath Date: Tue, 23 Feb 2010 15:07:12 +0100 Subject: [PATCH 21/34] Add missing include --- src/ccurl_thread.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/src/ccurl_thread.cc b/src/ccurl_thread.cc index 82401dde..f25ea899 100644 --- a/src/ccurl_thread.cc +++ b/src/ccurl_thread.cc @@ -24,6 +24,7 @@ */ #include "conky.h" +#include "core.h" #include "logging.h" #include "ccurl_thread.h" #include "text_object.h" From 1e49ea9274190b180c51907bf506f62a4f48a950 Mon Sep 17 00:00:00 2001 From: Pavel Labath Date: Tue, 23 Feb 2010 15:13:49 +0100 Subject: [PATCH 22/34] Safer version of free_and_zero --- src/core.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/core.h b/src/core.h index 35df3324..a90ccafe 100644 --- a/src/core.h +++ b/src/core.h @@ -28,11 +28,11 @@ * */ -#define free_and_zero(PTR) if(PTR) free(PTR); PTR = NULL; - #ifndef _CONKY_CORE_H_ #define _CONKY_CORE_H_ +#define free_and_zero(PTR) do { if(PTR) free(PTR); (PTR) = NULL; } while(0) + #include "conky.h" struct text_object *construct_text_object(const char *s, const char *arg, long From c51972b5e25157dd8fc074c3ecb2169463df3c0f Mon Sep 17 00:00:00 2001 From: Nikolas Garofil Date: Tue, 23 Feb 2010 15:48:18 +0100 Subject: [PATCH 23/34] Use free_and_zero in ccurl_common.cc where appropriate --- src/ccurl_thread.cc | 1 - src/common.cc | 3 +-- src/conky.h | 2 ++ src/core.h | 2 -- 4 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/ccurl_thread.cc b/src/ccurl_thread.cc index f25ea899..82401dde 100644 --- a/src/ccurl_thread.cc +++ b/src/ccurl_thread.cc @@ -24,7 +24,6 @@ */ #include "conky.h" -#include "core.h" #include "logging.h" #include "ccurl_thread.h" #include "text_object.h" diff --git a/src/common.cc b/src/common.cc index 08e88e6a..71feee30 100644 --- a/src/common.cc +++ b/src/common.cc @@ -474,8 +474,7 @@ void scan_loadgraph_arg(struct text_object *obj, const char *arg) char *buf = 0; buf = scan_graph(obj, arg, 0); - if (buf) - free(buf); + free_and_zero(buf); } double loadgraphval(struct text_object *obj) diff --git a/src/conky.h b/src/conky.h index 146fd8ab..9c047d24 100644 --- a/src/conky.h +++ b/src/conky.h @@ -31,6 +31,8 @@ #ifndef _conky_h_ #define _conky_h_ +#define free_and_zero(PTR) do { if(PTR) free(PTR); (PTR) = NULL; } while(0) + #include /* defines */ #include "common.h" /* at least for struct dns_data */ #include /* struct uname_s */ diff --git a/src/core.h b/src/core.h index a90ccafe..ed4ff091 100644 --- a/src/core.h +++ b/src/core.h @@ -31,8 +31,6 @@ #ifndef _CONKY_CORE_H_ #define _CONKY_CORE_H_ -#define free_and_zero(PTR) do { if(PTR) free(PTR); (PTR) = NULL; } while(0) - #include "conky.h" struct text_object *construct_text_object(const char *s, const char *arg, long From 45dfe4994dd923629f377280d9e396086989bb70 Mon Sep 17 00:00:00 2001 From: Nikolas Garofil Date: Tue, 23 Feb 2010 16:31:17 +0100 Subject: [PATCH 24/34] Use template for free_and_zero --- src/conky.h | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/conky.h b/src/conky.h index 9c047d24..efbcb351 100644 --- a/src/conky.h +++ b/src/conky.h @@ -31,8 +31,6 @@ #ifndef _conky_h_ #define _conky_h_ -#define free_and_zero(PTR) do { if(PTR) free(PTR); (PTR) = NULL; } while(0) - #include /* defines */ #include "common.h" /* at least for struct dns_data */ #include /* struct uname_s */ @@ -370,4 +368,11 @@ extern enum x_initialiser_state x_initialised; #define UNUSED(a) (void)a #define UNUSED_ATTR __attribute__ ((unused)) +template void free_and_zero(T *ptr) { + if(ptr) { + free(ptr); + ptr = NULL; + } +} + #endif /* _conky_h_ */ From c9447ddb8e51434371aecb0fd69f2dac8ab536dd Mon Sep 17 00:00:00 2001 From: Pavel Labath Date: Tue, 23 Feb 2010 19:36:47 +0100 Subject: [PATCH 25/34] Put zero in free_and_zero --- src/conky.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/conky.h b/src/conky.h index efbcb351..3ac69b78 100644 --- a/src/conky.h +++ b/src/conky.h @@ -368,7 +368,8 @@ extern enum x_initialiser_state x_initialised; #define UNUSED(a) (void)a #define UNUSED_ATTR __attribute__ ((unused)) -template void free_and_zero(T *ptr) { +template +void free_and_zero(T *&ptr) { if(ptr) { free(ptr); ptr = NULL; From f236523f3055614781cfc70897004452a4df18f4 Mon Sep 17 00:00:00 2001 From: Nikolas Garofil Date: Tue, 23 Feb 2010 22:06:04 +0100 Subject: [PATCH 26/34] Use free_and_zero in conky.cc and core.cc where appropriate --- src/conky.cc | 28 ++++++++-------------------- src/core.cc | 9 ++++----- 2 files changed, 12 insertions(+), 25 deletions(-) diff --git a/src/conky.cc b/src/conky.cc index 8fafab72..d9354ab7 100644 --- a/src/conky.cc +++ b/src/conky.cc @@ -1487,7 +1487,7 @@ int draw_each_line_inner(char *s, int special_index, int last_special_applied) j++; } } - if (tmpcolour) free(tmpcolour); + free_and_zero(tmpcolour); if (h > cur_y_add && h > font_h) { cur_y_add = h; @@ -2258,9 +2258,7 @@ static void main_loop(void) XFixesDestroyRegion(display, x11_stuff.region2); XFixesDestroyRegion(display, x11_stuff.part); #endif /* BUILD_XDAMAGE */ - if (disp) { - free(disp); - } + free_and_zero(disp); } #endif /* BUILD_X11 */ free_and_zero(overwrite_file); @@ -2371,12 +2369,8 @@ void clean_up(void *memtofree1, void* memtofree2) #endif conftree_empty(currentconffile); currentconffile = NULL; - if(memtofree1) { - free(memtofree1); - } - if(memtofree2) { - free(memtofree2); - } + free_and_zero(memtofree1); + free_and_zero(memtofree2); timed_thread::destroy_registered_threads(); free_and_zero(info.cpu_usage); @@ -2931,8 +2925,7 @@ char load_config_file(const char *f) if (!value || x_initialised == YES) { CONF_ERR; } else { - if (disp) - free(disp); + free_and_zero(disp); disp = strdup(value); } } @@ -3279,9 +3272,7 @@ char load_config_file(const char *f) variable_substitute(value, buffer, 256); if (buffer[0] != '\0') { - if (current_mail_spool) { - free(current_mail_spool); - } + free_and_zero(current_mail_spool); current_mail_spool = strndup(buffer, text_buffer_size); } } else { @@ -4260,9 +4251,7 @@ int main(int argc, char **argv) case 'V': print_version(); /* doesn't return */ case 'c': - if (current_config) { - free(current_config); - } + free_and_zero(current_config); current_config = strndup(optarg, max_user_text); break; case 'q': @@ -4282,8 +4271,7 @@ int main(int argc, char **argv) window.window = strtol(optarg, 0, 0); break; case 'X': - if (disp) - free(disp); + free_and_zero(disp); disp = strdup(optarg); break; #endif /* BUILD_X11 */ diff --git a/src/core.cc b/src/core.cc index d42e97ae..71faf89b 100644 --- a/src/core.cc +++ b/src/core.cc @@ -442,7 +442,7 @@ struct text_object *construct_text_object(char *s, const char *arg, long SCAN_CPU(arg, obj->data.i); buf = scan_graph(obj, arg, 1); DBGP2("Adding $cpugraph for CPU %d", obj->data.i); - if (buf) free(buf); + free_and_zero(buf); obj->callbacks.graphval = &cpu_barval; END OBJ(loadgraph, &update_load_average) scan_loadgraph_arg(obj, arg); @@ -863,7 +863,7 @@ struct text_object *construct_text_object(char *s, const char *arg, long char *buf = 0; buf = scan_graph(obj, arg, 1); - if (buf) free(buf); + free_and_zero(buf); obj->callbacks.graphval = &mem_barval; #endif /* BUILD_X11*/ END OBJ(mixer, 0) @@ -1612,7 +1612,7 @@ struct text_object *construct_text_object(char *s, const char *arg, long END OBJ(apcupsd_loadgraph, &update_apcupsd) char* buf = 0; buf = scan_graph(obj, arg, 100); - if (buf) free(buf); + free_and_zero(buf); obj->callbacks.graphval = &apcupsd_loadbarval; #endif /* BUILD_X11 */ END OBJ(apcupsd_loadgauge, &update_apcupsd) @@ -1865,8 +1865,7 @@ void free_text_objects(struct text_object *root) free_text_objects(obj->sub); free_and_zero(obj->sub); } - if(obj->special_data) - free(obj->special_data); + free_and_zero(obj->special_data); free(obj); } From 72d581279a5c8c4ce351943e60061f0829aaba80 Mon Sep 17 00:00:00 2001 From: Nikolas Garofil Date: Tue, 23 Feb 2010 23:13:11 +0100 Subject: [PATCH 27/34] Use free_and_zero in [d-k]*.cc where appropriate --- src/diskio.cc | 6 ++---- src/eve.cc | 5 +---- src/exec.cc | 14 ++++---------- src/fonts.cc | 9 ++------- src/hddtemp.cc | 21 +++++---------------- 5 files changed, 14 insertions(+), 41 deletions(-) diff --git a/src/diskio.cc b/src/diskio.cc index c973ac91..bfbb4635 100644 --- a/src/diskio.cc +++ b/src/diskio.cc @@ -50,8 +50,7 @@ void clear_diskio_stats(void) while (stats.next) { cur = stats.next; stats.next = stats.next->next; - if (cur->dev) - free(cur->dev); + free_and_zero(cur->dev); free(cur); } } @@ -150,8 +149,7 @@ void parse_diskiograph_arg(struct text_object *obj, const char *arg) buf = scan_graph(obj, arg, 0); obj->data.opaque = prepare_diskio_stat(dev_name(buf)); - if (buf) - free(buf); + free_and_zero(buf); } double diskiographval(struct text_object *obj) diff --git a/src/eve.cc b/src/eve.cc index 04c9b80a..a9b4f682 100644 --- a/src/eve.cc +++ b/src/eve.cc @@ -437,8 +437,5 @@ void print_eve(struct text_object *obj, char *p, int p_max_size) void free_eve(struct text_object *obj) { - if (obj->data.opaque) { - free(obj->data.opaque); - obj->data.opaque = NULL; - } + free_and_zero(obj->data.opaque); } diff --git a/src/exec.cc b/src/exec.cc index dd297b4a..15038d73 100644 --- a/src/exec.cc +++ b/src/exec.cc @@ -204,8 +204,7 @@ static void threaded_exec(thread_handle &handle, struct text_object *obj) { std::lock_guard lock(handle.mutex()); - if (ed->buffer) - free(ed->buffer); + free_and_zero(ed->buffer); ed->buffer = buff; } if (handle.test(0)) { @@ -364,10 +363,7 @@ double execi_barval(struct text_object *obj) void free_exec(struct text_object *obj) { - if (obj->data.s) { - free(obj->data.s); - obj->data.s = NULL; - } + free_and_zero(obj->data.s); } void free_execi(struct text_object *obj) @@ -380,10 +376,8 @@ void free_execi(struct text_object *obj) if (ed->p_timed_thread) { ed->p_timed_thread.reset(); } - if (ed->cmd) - free(ed->cmd); - if (ed->buffer) - free(ed->buffer); + free_and_zero(ed->cmd); + free_and_zero(ed->buffer); delete ed; obj->data.opaque = NULL; } diff --git a/src/fonts.cc b/src/fonts.cc index b5c48c1e..9b5c5754 100644 --- a/src/fonts.cc +++ b/src/fonts.cc @@ -74,9 +74,7 @@ int add_font(const char *data_in) } font_count++; if (font_count == 0) { - if (fonts != NULL) { - free(fonts); - } + free_and_zero(fonts); if ((fonts = (struct font_list *) malloc(sizeof(struct font_list))) == NULL) { CRIT_ERR(NULL, NULL, "malloc"); @@ -148,10 +146,7 @@ void free_fonts(void) } } } - if (fonts) { - free(fonts); - fonts = 0; - } + free_and_zero(fonts); font_count = -1; selected_font = 0; #ifdef BUILD_XFT diff --git a/src/hddtemp.cc b/src/hddtemp.cc index d01c7e75..68173240 100644 --- a/src/hddtemp.cc +++ b/src/hddtemp.cc @@ -59,15 +59,13 @@ struct hdd_info hdd_info_head; void set_hddtemp_host(const char *host) { - if (hddtemp_host) - free(hddtemp_host); + free_and_zero(hddtemp_host); hddtemp_host = strdup(host); } void set_hddtemp_port(const char *port) { - if (hddtemp_port) - free(hddtemp_port); + free_and_zero(hddtemp_port); hddtemp_port = strdup(port); } @@ -237,18 +235,9 @@ void update_hddtemp(void) { void free_hddtemp(struct text_object *obj) { free_hddtemp_info(); - if (hddtemp_host) { - free(hddtemp_host); - hddtemp_host = NULL; - } - if (hddtemp_port) { - free(hddtemp_port); - hddtemp_port = NULL; - } - if (obj->data.s) { - free(obj->data.s); - obj->data.s = NULL; - } + free_and_zero(hddtemp_host); + free_and_zero(hddtemp_port); + free_and_zero(obj->data.s); } static int get_hddtemp_info(const char *dev, short *val, char *unit) From a2110d44d746d814aff5fef3d7b054b99755bfed Mon Sep 17 00:00:00 2001 From: Pavel Labath Date: Tue, 23 Feb 2010 23:59:03 +0100 Subject: [PATCH 28/34] Include guard fail --- src/logging.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/logging.h b/src/logging.h index 5455d545..cb6787b1 100644 --- a/src/logging.h +++ b/src/logging.h @@ -27,6 +27,9 @@ * */ +#ifndef _LOGGING_H +#define _LOGGING_H + #include "mail.h" #ifdef __cplusplus @@ -35,9 +38,6 @@ extern "C" { void clean_up(void *memtofree1, void* memtofree2); -#ifndef _LOGGING_H -#define _LOGGING_H - #define NORM_ERR(...) { \ fprintf(stderr, PACKAGE_NAME": "); \ fprintf(stderr, __VA_ARGS__); \ From 1fa465efb6b941c9a16b204584d35ae4c66c4f93 Mon Sep 17 00:00:00 2001 From: Nikolas Garofil Date: Wed, 24 Feb 2010 01:51:02 +0100 Subject: [PATCH 29/34] Use free_and_zero in l*.cc where appropriate --- src/libmpdclient.cc | 67 ++++++++++++--------------------------------- src/linux.cc | 14 ++++------ src/llua.cc | 24 ++++------------ 3 files changed, 28 insertions(+), 77 deletions(-) diff --git a/src/libmpdclient.cc b/src/libmpdclient.cc index 92156a13..ecc5ee42 100644 --- a/src/libmpdclient.cc +++ b/src/libmpdclient.cc @@ -485,12 +485,8 @@ void mpd_clearError(mpd_Connection *connection) void mpd_closeConnection(mpd_Connection *connection) { closesocket(connection->sock); - if (connection->returnElement) { - free(connection->returnElement); - } - if (connection->request) { - free(connection->request); - } + free_and_zero(connection->returnElement); + free_and_zero(connection->request); free(connection); WSACleanup(); } @@ -856,9 +852,7 @@ mpd_Status *mpd_getStatus(mpd_Connection *connection) void mpd_freeStatus(mpd_Status *status) { - if (status->error) { - free(status->error); - } + free_and_zero(status->error); free(status); } @@ -1011,39 +1005,17 @@ static void mpd_initSong(mpd_Song *song) static void mpd_finishSong(mpd_Song *song) { - if (song->file) { - free(song->file); - } - if (song->artist) { - free(song->artist); - } - if (song->album) { - free(song->album); - } - if (song->title) { - free(song->title); - } - if (song->track) { - free(song->track); - } - if (song->name) { - free(song->name); - } - if (song->date) { - free(song->date); - } - if (song->genre) { - free(song->genre); - } - if (song->composer) { - free(song->composer); - } - if (song->disc) { - free(song->disc); - } - if (song->comment) { - free(song->comment); - } + free_and_zero(song->file); + free_and_zero(song->artist); + free_and_zero(song->album); + free_and_zero(song->title); + free_and_zero(song->track); + free_and_zero(song->name); + free_and_zero(song->date); + free_and_zero(song->genre); + free_and_zero(song->composer); + free_and_zero(song->disc); + free_and_zero(song->comment); } mpd_Song *mpd_newSong(void) @@ -1112,9 +1084,7 @@ static void mpd_initDirectory(mpd_Directory *directory) static void mpd_finishDirectory(mpd_Directory *directory) { - if (directory->path) { - free(directory->path); - } + free_and_zero(directory->path); } mpd_Directory *mpd_newDirectory(void) @@ -1151,9 +1121,7 @@ static void mpd_initPlaylistFile(mpd_PlaylistFile *playlist) static void mpd_finishPlaylistFile(mpd_PlaylistFile *playlist) { - if (playlist->path) { - free(playlist->path); - } + free_and_zero(playlist->path); } mpd_PlaylistFile *mpd_newPlaylistFile(void) @@ -2084,8 +2052,7 @@ void mpd_commitSearch(mpd_Connection *connection) connection->request[len - 1] = '\0'; mpd_sendInfoCommand(connection, connection->request); - free(connection->request); - connection->request = NULL; + free_and_zero(connection->request); } /** diff --git a/src/linux.cc b/src/linux.cc index c2272ea0..8327345e 100644 --- a/src/linux.cc +++ b/src/linux.cc @@ -248,7 +248,6 @@ static struct { int count; } gw_info; -#define COND_FREE(x) if(x) free(x); x = 0 #define SAVE_SET_STRING(x, y) \ if (x && strcmp((char *)x, (char *)y)) { \ free(x); \ @@ -279,8 +278,8 @@ void update_gateway_info(void) unsigned long dest, gate, mask; unsigned int flags; - COND_FREE(gw_info.iface); - COND_FREE(gw_info.ip); + free_and_zero(gw_info.iface); + free_and_zero(gw_info.ip); gw_info.count = 0; if ((fp = fopen("/proc/net/route", "r")) == NULL) { @@ -315,10 +314,8 @@ void free_gateway_info(struct text_object *obj) { (void)obj; - if (gw_info.iface) - free(gw_info.iface); - if (gw_info.ip) - free(gw_info.ip); + free_and_zero(gw_info.iface); + free_and_zero(gw_info.ip); memset(&gw_info, 0, sizeof(gw_info)); } @@ -1116,8 +1113,7 @@ void free_sysfs_sensor(struct text_object *obj) return; close(sf->fd); - free(obj->data.opaque); - obj->data.opaque = NULL; + free_and_zero(obj->data.opaque); } #define CPUFREQ_PREFIX "/sys/devices/system/cpu" diff --git a/src/llua.cc b/src/llua.cc index 1ba00351..6cab830d 100644 --- a/src/llua.cc +++ b/src/llua.cc @@ -299,22 +299,10 @@ void llua_close(void) #ifdef HAVE_SYS_INOTIFY_H llua_rm_notifies(); #endif /* HAVE_SYS_INOTIFY_H */ - if (draw_pre_hook) { - free(draw_pre_hook); - draw_pre_hook = 0; - } - if (draw_post_hook) { - free(draw_post_hook); - draw_post_hook = 0; - } - if (startup_hook) { - free(startup_hook); - startup_hook = 0; - } - if (shutdown_hook) { - free(shutdown_hook); - shutdown_hook = 0; - } + free_and_zero(draw_pre_hook); + free_and_zero(draw_post_hook); + free_and_zero(startup_hook); + free_and_zero(shutdown_hook); if(!lua_L) return; lua_close(lua_L); lua_L = NULL; @@ -410,13 +398,13 @@ void llua_set_number(const char *key, double value) void llua_set_startup_hook(const char *args) { - if (startup_hook) free(startup_hook); + free_and_zero(startup_hook); startup_hook = strdup(args); } void llua_set_shutdown_hook(const char *args) { - if (shutdown_hook) free(shutdown_hook); + free_and_zero(shutdown_hook); shutdown_hook = strdup(args); } From 6092d063c9f04f2d67414dcaba3f4b595bbc4c56 Mon Sep 17 00:00:00 2001 From: Nikolas Garofil Date: Wed, 24 Feb 2010 02:14:20 +0100 Subject: [PATCH 30/34] Use free_and_zero in [m-o]*.cc where appropriate --- src/mail.cc | 6 ++---- src/mboxscan.cc | 9 +++------ src/moc.cc | 24 +++++++++++------------- src/mpd.cc | 14 ++++++-------- src/net_stat.cc | 9 ++------- src/nvidia.cc | 5 +---- 6 files changed, 25 insertions(+), 42 deletions(-) diff --git a/src/mail.cc b/src/mail.cc index 21ae7ddc..c1a76422 100644 --- a/src/mail.cc +++ b/src/mail.cc @@ -407,10 +407,8 @@ void free_local_mails(struct text_object *obj) if (!locmail) return; - if (locmail->mbox) - free(locmail->mbox); - free(obj->data.opaque); - obj->data.opaque = 0; + free_and_zero(locmail->mbox); + free_and_zero(obj->data.opaque); } #define MAXDATASIZE 1000 diff --git a/src/mboxscan.cc b/src/mboxscan.cc index 5415d85c..1aff91e6 100644 --- a/src/mboxscan.cc +++ b/src/mboxscan.cc @@ -411,11 +411,8 @@ void free_mboxscan(struct text_object *obj) if (!msd) return; - if (msd->args) - free(msd->args); - if (msd->output) - free(msd->output); - free(obj->data.opaque); - obj->data.opaque = NULL; + free_and_zero(msd->args); + free_and_zero(msd->output); + free_and_zero(obj->data.opaque); } diff --git a/src/moc.cc b/src/moc.cc index 09464894..157d67ba 100644 --- a/src/moc.cc +++ b/src/moc.cc @@ -31,8 +31,6 @@ #include #include -#define xfree(x) if (x) free(x); x = 0 - static struct { char *state; char *file; @@ -52,17 +50,17 @@ static timed_thread_ptr moc_thread; void free_moc(struct text_object *obj) { (void)obj; - xfree(moc.state); - xfree(moc.file); - xfree(moc.title); - xfree(moc.artist); - xfree(moc.song); - xfree(moc.album); - xfree(moc.totaltime); - xfree(moc.timeleft); - xfree(moc.curtime); - xfree(moc.bitrate); - xfree(moc.rate); + free_and_zero(moc.state); + free_and_zero(moc.file); + free_and_zero(moc.title); + free_and_zero(moc.artist); + free_and_zero(moc.song); + free_and_zero(moc.album); + free_and_zero(moc.totaltime); + free_and_zero(moc.timeleft); + free_and_zero(moc.curtime); + free_and_zero(moc.bitrate); + free_and_zero(moc.rate); } static void update_infos(void) diff --git a/src/mpd.cc b/src/mpd.cc index 40cf51f2..cddc8911 100644 --- a/src/mpd.cc +++ b/src/mpd.cc @@ -105,17 +105,15 @@ void init_mpd(void) static void clear_mpd(void) { -#define xfree(x) if (x) free(x) - xfree(mpd_info.title); - xfree(mpd_info.artist); - xfree(mpd_info.album); + free_and_zero(mpd_info.title); + free_and_zero(mpd_info.artist); + free_and_zero(mpd_info.album); /* do not free() the const char *status! */ /* do not free() the const char *random! */ /* do not free() the const char *repeat! */ - xfree(mpd_info.track); - xfree(mpd_info.name); - xfree(mpd_info.file); -#undef xfree + free_and_zero(mpd_info.track); + free_and_zero(mpd_info.name); + free_and_zero(mpd_info.file); memset(&mpd_info, 0, sizeof(mpd_info)); } diff --git a/src/net_stat.cc b/src/net_stat.cc index 38bfcd61..28308886 100644 --- a/src/net_stat.cc +++ b/src/net_stat.cc @@ -313,9 +313,7 @@ void clear_net_stats(void) { int i; for (i = 0; i < MAX_NET_INTERFACES; i++) { - if (netstats[i].dev) { - free(netstats[i].dev); - } + free_and_zero(netstats[i].dev); } memset(netstats, 0, sizeof(netstats)); } @@ -327,10 +325,7 @@ void parse_if_up_arg(struct text_object *obj, const char *arg) void free_if_up(struct text_object *obj) { - if (obj->data.opaque) { - free(obj->data.opaque); - obj->data.opaque = NULL; - } + free_and_zero(obj->data.opaque); } /* We should check if this is ok with OpenBSD and NetBSD as well. */ diff --git a/src/nvidia.cc b/src/nvidia.cc index 59866782..62473428 100644 --- a/src/nvidia.cc +++ b/src/nvidia.cc @@ -128,9 +128,6 @@ void print_nvidia_value(struct text_object *obj, char *p, int p_max_size) void free_nvidia(struct text_object *obj) { - if (obj->data.opaque) { - free(obj->data.opaque); - obj->data.opaque = NULL; - } + free_and_zero(obj->data.opaque); } From 4afe96b9ceecd14dab52262acd46a1faa6dcf974 Mon Sep 17 00:00:00 2001 From: Nikolas Garofil Date: Wed, 24 Feb 2010 02:46:45 +0100 Subject: [PATCH 31/34] cpp-ify prioqueue.c and use free_and_zero in [p-r]*.cc where appropriate --- src/CMakeLists.txt | 2 +- src/{prioqueue.c => prioqueue.cc} | 8 +++--- src/prioqueue.h | 7 ----- src/prss.cc | 47 ++++++++++++++----------------- src/read_tcp.cc | 6 ++-- src/rss.cc | 5 +--- 6 files changed, 29 insertions(+), 46 deletions(-) rename src/{prioqueue.c => prioqueue.cc} (96%) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 971a83ba..f3205f67 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -37,7 +37,7 @@ endif(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/build.h) set(conky_sources colours.cc combine.cc common.cc conky.cc core.cc diskio.cc entropy.cc exec.cc fs.cc mail.cc mixer.cc net_stat.cc template.cc mboxscan.cc read_tcp.cc scroll.cc specials.cc tailhead.cc - temphelper.cc text_object.cc timeinfo.cc top.cc algebra.cc prioqueue.c proc.cc + temphelper.cc text_object.cc timeinfo.cc top.cc algebra.cc prioqueue.cc proc.cc user.cc) # add timed thread library diff --git a/src/prioqueue.c b/src/prioqueue.cc similarity index 96% rename from src/prioqueue.c rename to src/prioqueue.cc index 0c9d9927..b546a6d5 100644 --- a/src/prioqueue.c +++ b/src/prioqueue.cc @@ -32,6 +32,7 @@ * */ +#include "conky.h" #include /* INT_MAX */ #include #include @@ -69,7 +70,7 @@ struct prio_queue *init_prio_queue(void) { struct prio_queue *retval; - retval = malloc(sizeof(struct prio_queue)); + retval = (struct prio_queue *) malloc(sizeof(struct prio_queue)); memset(retval, 0, sizeof(struct prio_queue)); /* use pq_free_nop by default */ @@ -110,7 +111,7 @@ static struct prio_elem *init_prio_elem(void *data) { struct prio_elem *retval; - retval = malloc(sizeof(struct prio_elem)); + retval = (struct prio_elem *) malloc(sizeof(struct prio_elem)); memset(retval, 0, sizeof(struct prio_elem)); retval->data = data; @@ -171,8 +172,7 @@ check_cur_size: queue->cur_size--; queue->tail = queue->tail->prev; (*queue->free)(queue->tail->next->data); - free(queue->tail->next); - queue->tail->next = NULL; + free_and_zero(queue->tail->next); } } diff --git a/src/prioqueue.h b/src/prioqueue.h index 7252a5f9..764be23b 100644 --- a/src/prioqueue.h +++ b/src/prioqueue.h @@ -25,9 +25,6 @@ #ifndef _PRIOQUEUE_H #define _PRIOQUEUE_H -#ifdef __cplusplus -extern "C" { -#endif /* forward-define for private data */ struct prio_queue; @@ -66,8 +63,4 @@ void *pop_prio_elem(prio_queue_t); /* clear and free the given queue */ void free_prio_queue(prio_queue_t); -#ifdef __cplusplus -} -#endif - #endif /* _PRIOQUEUE_H */ diff --git a/src/prss.cc b/src/prss.cc index 72a7e86c..2cbedcbe 100644 --- a/src/prss.cc +++ b/src/prss.cc @@ -49,19 +49,20 @@ void free_rss_items(PRSS *data) { int i; - for (i = 0; i < data->item_count; i++) { -#define CLEAR(a) if (data->items[i].a) { free(data->items[i].a); data->items[i].a = 0; } - CLEAR(title); - CLEAR(link); - CLEAR(description); - CLEAR(category); - CLEAR(pubDate); - CLEAR(guid); + if(data->items) { + for (i = 0; i < data->item_count; i++) { +#define CLEAR(a) free_and_zero(data->items[i].a); + CLEAR(title); + CLEAR(link); + CLEAR(description); + CLEAR(category); + CLEAR(pubDate); + CLEAR(guid); #undef CLEAR + } + free_and_zero(data->items); + data->item_count = 0; } - free(data->items); - data->item_count = 0; - data->items = 0; } void prss_free(PRSS *data) @@ -69,16 +70,10 @@ void prss_free(PRSS *data) if (!data) { return; } - if (data->version) { - free(data->version); - data->version = 0; - } - if (data->items) { - free_rss_items(data); - } + free_and_zero(data->version); + free_rss_items(data); data->version = 0; - data->items = 0; -#define CLEAR(a) if (data->a) { free(data->a); data->a = 0; } +#define CLEAR(a) free_and_zero(data->a); CLEAR(title); CLEAR(link); CLEAR(description); @@ -116,7 +111,7 @@ static inline void read_item(PRSS_Item *res, xmlNodePtr data) } #define ASSIGN(a) if (strcasecmp((const char*)data->name, #a) == EQUAL) { \ - if (res->a) free(res->a); \ + free_and_zero(res->a); \ res->a = strdup((const char*)child->content); \ continue; \ } @@ -143,7 +138,7 @@ static inline void read_element(PRSS *res, xmlNodePtr n) } #define ASSIGN(a) if (strcasecmp((const char*)n->name, #a) == EQUAL) { \ - if (res->a) free(res->a); \ + free_and_zero(res->a); \ res->a = strdup((const char*)child->content); \ return; \ } @@ -188,9 +183,9 @@ static inline int parse_rss_2_0(PRSS *res, xmlNodePtr root) } } - if (res->version) free(res->version); + free_and_zero(res->version); res->version = strndup("2.0", text_buffer_size); - if (res->items) free_rss_items(res); + free_rss_items(res); res->items = (PRSS_Item*) malloc(items * sizeof(PRSS_Item)); res->item_count = 0; @@ -221,9 +216,9 @@ static inline int parse_rss_1_0(PRSS *res, xmlNodePtr root) } } - if (res->version) free(res->version); + free_and_zero(res->version); res->version = strndup("1.0", text_buffer_size); - if (res->items) free_rss_items(res); + free_rss_items(res); res->items = (PRSS_Item*) malloc(items * sizeof(PRSS_Item)); res->item_count = 0; diff --git a/src/read_tcp.cc b/src/read_tcp.cc index 1b73489c..0577769d 100644 --- a/src/read_tcp.cc +++ b/src/read_tcp.cc @@ -109,8 +109,6 @@ void free_read_tcp(struct text_object *obj) if (!rtd) return; - if (rtd->host) - free(rtd->host); - free(obj->data.opaque); - obj->data.opaque = NULL; + free_and_zero(rtd->host); + free_and_zero(obj->data.opaque); } diff --git a/src/rss.cc b/src/rss.cc index c1f96ad2..61627ffd 100644 --- a/src/rss.cc +++ b/src/rss.cc @@ -183,8 +183,5 @@ void rss_print_info(struct text_object *obj, char *p, int p_max_size) void rss_free_obj_info(struct text_object *obj) { - if (obj->data.opaque) { - free(obj->data.opaque); - obj->data.opaque = NULL; - } + free_and_zero(obj->data.opaque); } From da763813000673a8d34b4d36cc13d95251684aeb Mon Sep 17 00:00:00 2001 From: Nikolas Garofil Date: Wed, 24 Feb 2010 11:52:59 +0100 Subject: [PATCH 32/34] Use free_and_zero in [st]*.cc where appropriate --- src/core.cc | 22 +++++++++------------- src/scroll.cc | 13 ++++--------- src/tailhead.cc | 12 ++++-------- src/tcp-portmon.cc | 5 +---- src/template.cc | 8 ++------ src/text_object.cc | 3 +-- src/timeinfo.cc | 14 ++++---------- src/top.cc | 21 ++++++--------------- 8 files changed, 31 insertions(+), 67 deletions(-) diff --git a/src/core.cc b/src/core.cc index 71faf89b..0c3410ff 100644 --- a/src/core.cc +++ b/src/core.cc @@ -1851,23 +1851,19 @@ void free_text_objects(struct text_object *root) { struct text_object *obj; - if (!root->prev) { - return; - } + if(root && root->prev) { + for (obj = root->prev; obj; obj = root->prev) { + root->prev = obj->prev; - for (obj = root->prev; obj; obj = root->prev) { - root->prev = obj->prev; - - if (obj->callbacks.free) { - (*obj->callbacks.free)(obj); - } - if(obj->sub) { + if (obj->callbacks.free) { + (*obj->callbacks.free)(obj); + } free_text_objects(obj->sub); free_and_zero(obj->sub); - } - free_and_zero(obj->special_data); + free_and_zero(obj->special_data); - free(obj); + free(obj); + } } } diff --git a/src/scroll.cc b/src/scroll.cc index 2255a7b3..89a04995 100644 --- a/src/scroll.cc +++ b/src/scroll.cc @@ -192,13 +192,8 @@ void free_scroll(struct text_object *obj) if (!sd) return; - if (sd->text) - free(sd->text); - if (obj->sub) { - free_text_objects(obj->sub); - free(obj->sub); - obj->sub = NULL; - } - free(obj->data.opaque); - obj->data.opaque = NULL; + free_and_zero(sd->text); + free_text_objects(obj->sub); + free_and_zero(obj->sub); + free_and_zero(obj->data.opaque); } diff --git a/src/tailhead.cc b/src/tailhead.cc index 8adaf8fe..fc059b89 100644 --- a/src/tailhead.cc +++ b/src/tailhead.cc @@ -74,12 +74,9 @@ void free_tailhead(struct text_object *obj) struct headtail *ht = (struct headtail *)obj->data.opaque; if (!ht) return; - if (ht->logfile) - free(ht->logfile); - if (ht->buffer) - free(ht->buffer); - free(obj->data.opaque); - obj->data.opaque = NULL; + free_and_zero(ht->logfile); + free_and_zero(ht->buffer); + free_and_zero(obj->data.opaque); } void init_tailhead(const char* type, const char* arg, struct text_object *obj, void* free_at_crash) { @@ -125,8 +122,7 @@ static void print_tailhead(const char* type, struct text_object *obj, char *p, i //empty the buffer and reset the counter if we used it the max number of times if(ht->buffer && ht->current_use >= ht->max_uses - 1) { - free(ht->buffer); - ht->buffer = NULL; + free_and_zero(ht->buffer); ht->current_use = 0; } //use the buffer if possible diff --git a/src/tcp-portmon.cc b/src/tcp-portmon.cc index 7677b2f6..433e37b3 100644 --- a/src/tcp-portmon.cc +++ b/src/tcp-portmon.cc @@ -156,8 +156,5 @@ int tcp_portmon_set_max_connections(int max) void tcp_portmon_free(struct text_object *obj) { - if (obj->data.opaque) { - free(obj->data.opaque); - obj->data.opaque = NULL; - } + free_and_zero(obj->data.opaque); } diff --git a/src/template.cc b/src/template.cc index e598517e..8359e4a1 100644 --- a/src/template.cc +++ b/src/template.cc @@ -53,10 +53,7 @@ void free_templates(void) } for (i = 0; i < MAX_TEMPLATES; i++) { - if (_template[i]) { - free(_template[i]); - _template[i] = NULL; - } + free_and_zero(_template[i]); } } @@ -67,8 +64,7 @@ int set_template(int n, const char *val) { if (n < 0 || n >= MAX_TEMPLATES || !val) return 1; - if (_template[n]) - free(_template[n]); + free_and_zero(_template[n]); _template[n] = strdup(val); return 0; } diff --git a/src/text_object.cc b/src/text_object.cc index 45a1a3c7..7734782e 100644 --- a/src/text_object.cc +++ b/src/text_object.cc @@ -35,8 +35,7 @@ void gen_free_opaque(struct text_object *obj) { - if (obj->data.opaque) - free(obj->data.opaque); + free_and_zero(obj->data.opaque); } int gen_false_iftest(struct text_object *obj) diff --git a/src/timeinfo.cc b/src/timeinfo.cc index 4dc17055..5e463d6f 100644 --- a/src/timeinfo.cc +++ b/src/timeinfo.cc @@ -133,10 +133,7 @@ void print_tztime(struct text_object *obj, char *p, int p_max_size) void free_time(struct text_object *obj) { - if (!obj->data.opaque) - return; - free(obj->data.opaque); - obj->data.opaque = NULL; + free_and_zero(obj->data.opaque); } void free_tztime(struct text_object *obj) @@ -146,13 +143,10 @@ void free_tztime(struct text_object *obj) if (!ts) return; - if (ts->tz) - free(ts->tz); - if (ts->fmt) - free(ts->fmt); + free_and_zero(ts->tz); + free_and_zero(ts->fmt); - free(obj->data.opaque); - obj->data.opaque = NULL; + free_and_zero(obj->data.opaque); } /* a safer asprintf() diff --git a/src/top.cc b/src/top.cc index 1c45d825..19f030bf 100644 --- a/src/top.cc +++ b/src/top.cc @@ -116,9 +116,7 @@ void free_all_processes(void) while (pr) { next = pr->next; - if (pr->name) { - free(pr->name); - } + free_and_zero(pr->name); free(pr); pr = next; } @@ -280,9 +278,7 @@ static int process_parse_stat(struct process *process) *q = 0; } - if (process->name) { - free(process->name); - } + free_and_zero(process->name); process->name = strndup(procname, text_buffer_size); process->rss *= getpagesize(); @@ -487,9 +483,7 @@ static void delete_process(struct process *p) else first_process = p->next; - if (p->name) { - free(p->name); - } + free_and_zero(p->name); /* remove the process from the hash table */ unhash_process(p); free(p); @@ -874,10 +868,8 @@ static void free_top(struct text_object *obj) if (!td) return; - if (td->s) - free(td->s); - free(obj->data.opaque); - obj->data.opaque = NULL; + free_and_zero(td->s); + free_and_zero(obj->data.opaque); } int parse_top_args(const char *s, const char *arg, struct text_object *obj) @@ -914,8 +906,7 @@ int parse_top_args(const char *s, const char *arg, struct text_object *obj) #else /* BUILD_IOSTATS */ NORM_ERR("Must be top, top_mem or top_time"); #endif /* BUILD_IOSTATS */ - free(obj->data.opaque); - obj->data.opaque = 0; + free_and_zero(obj->data.opaque); return 0; } From c1648e6558e10d26e4d2fa091902a535c08d8254 Mon Sep 17 00:00:00 2001 From: Nikolas Garofil Date: Wed, 24 Feb 2010 12:04:28 +0100 Subject: [PATCH 33/34] Use free_and_zero in [u-z]*.cc where appropriate --- src/tailhead.cc | 1 + src/text_object.cc | 1 + src/users.cc | 63 ++++++++++------------------------------------ src/weather.cc | 5 +--- src/x11.cc | 20 +++------------ src/xmms2.cc | 19 +++++++------- 6 files changed, 29 insertions(+), 80 deletions(-) diff --git a/src/tailhead.cc b/src/tailhead.cc index fc059b89..be135315 100644 --- a/src/tailhead.cc +++ b/src/tailhead.cc @@ -28,6 +28,7 @@ * */ +#include "conky.h" #include "config.h" #include "common.h" #include "text_object.h" diff --git a/src/text_object.cc b/src/text_object.cc index 7734782e..efb6dd4d 100644 --- a/src/text_object.cc +++ b/src/text_object.cc @@ -26,6 +26,7 @@ * along with this program. If not, see . * */ +#include "conky.h" #include "config.h" #include "text_object.h" #include "logging.h" diff --git a/src/users.cc b/src/users.cc index 9e3df172..ac36dab8 100644 --- a/src/users.cc +++ b/src/users.cc @@ -141,17 +141,11 @@ static void update_user_time(char *tty) tty_user_time(temp, tty); if (temp != NULL) { - if (current_info->users.ctime) { - free(current_info->users.ctime); - current_info->users.ctime = 0; - } + free_and_zero(current_info->users.ctime); current_info->users.ctime = (char*)malloc(text_buffer_size); strncpy(current_info->users.ctime, temp, text_buffer_size); } else { - if (current_info->users.ctime) { - free(current_info->users.ctime); - current_info->users.ctime = 0; - } + free_and_zero(current_info->users.ctime); current_info->users.ctime = (char*)malloc(text_buffer_size); strncpy(current_info->users.ctime, "broken", text_buffer_size); } @@ -165,17 +159,11 @@ void update_users(void) users_alloc(current_info); user_name(temp); if (temp != NULL) { - if (current_info->users.names) { - free(current_info->users.names); - current_info->users.names = 0; - } + free_and_zero(current_info->users.names); current_info->users.names = (char*)malloc(text_buffer_size); strncpy(current_info->users.names, temp, text_buffer_size); } else { - if (current_info->users.names) { - free(current_info->users.names); - current_info->users.names = 0; - } + free_and_zero(current_info->users.names); current_info->users.names = (char*)malloc(text_buffer_size); strncpy(current_info->users.names, "broken", text_buffer_size); } @@ -191,33 +179,21 @@ void update_users(void) temp[0] = 0; user_term(temp); if (temp != NULL) { - if (current_info->users.terms) { - free(current_info->users.terms); - current_info->users.terms = 0; - } + free_and_zero(current_info->users.terms); current_info->users.terms = (char*)malloc(text_buffer_size); strncpy(current_info->users.terms, temp, text_buffer_size); } else { - if (current_info->users.terms) { - free(current_info->users.terms); - current_info->users.terms = 0; - } + free_and_zero(current_info->users.terms); current_info->users.terms = (char*)malloc(text_buffer_size); strncpy(current_info->users.terms, "broken", text_buffer_size); } user_time(temp); if (temp != NULL) { - if (current_info->users.times) { - free(current_info->users.times); - current_info->users.times = 0; - } + free_and_zero(current_info->users.times); current_info->users.times = (char*)malloc(text_buffer_size); strncpy(current_info->users.times, temp, text_buffer_size); } else { - if (current_info->users.times) { - free(current_info->users.times); - current_info->users.times = 0; - } + free_and_zero(current_info->users.times); current_info->users.times = (char*)malloc(text_buffer_size); strncpy(current_info->users.times, "broken", text_buffer_size); } @@ -256,36 +232,23 @@ void print_user_number(struct text_object *obj, char *p, int p_max_size) void free_user_names(struct text_object *obj) { (void)obj; - if (info.users.names) { - free(info.users.names); - info.users.names = 0; - } + free_and_zero(info.users.names); } void free_user_terms(struct text_object *obj) { (void)obj; - if (info.users.terms) { - free(info.users.terms); - info.users.terms = 0; - } + free_and_zero(info.users.terms); } void free_user_times(struct text_object *obj) { (void)obj; - if (info.users.times) { - free(info.users.times); - info.users.times = 0; - } + free_and_zero(info.users.times); } void free_user_time(struct text_object *obj) { - if (info.users.ctime) { - free(info.users.ctime); - info.users.ctime = 0; - } - if (obj->data.s) - free(obj->data.s); + free_and_zero(info.users.ctime); + free_and_zero(obj->data.s); } diff --git a/src/weather.cc b/src/weather.cc index f2c2e4cd..32bf4e48 100644 --- a/src/weather.cc +++ b/src/weather.cc @@ -1010,8 +1010,5 @@ void print_weather(struct text_object *obj, char *p, int p_max_size) void free_weather(struct text_object *obj) { - if (obj->data.opaque) { - free(obj->data.opaque); - obj->data.opaque = NULL; - } + free_and_zero(obj->data.opaque); } diff --git a/src/x11.cc b/src/x11.cc index 12e9c454..8285b8b8 100644 --- a/src/x11.cc +++ b/src/x11.cc @@ -698,10 +698,7 @@ static inline void get_x11_desktop_names(Display *current_display, Window root, (actual_type == ATOM(UTF8_STRING)) && (nitems > 0L) && (actual_format == 8) ) { - if(current_info->x11.desktop.all_names) { - free(current_info->x11.desktop.all_names); - current_info->x11.desktop.all_names = NULL; - } + free_and_zero(current_info->x11.desktop.all_names); current_info->x11.desktop.all_names = (char*)malloc(nitems*sizeof(char)); memcpy(current_info->x11.desktop.all_names, prop, nitems); current_info->x11.desktop.nitems = nitems; @@ -721,10 +718,7 @@ static inline void get_x11_desktop_current_name(char *names) while ( i < current_info->x11.desktop.nitems ) { if ( names[i++] == '\0' ) { if ( ++k == current_info->x11.desktop.current ) { - if (current_info->x11.desktop.name) { - free(current_info->x11.desktop.name); - current_info->x11.desktop.name = NULL; - } + free_and_zero(current_info->x11.desktop.name); current_info->x11.desktop.name = (char*)malloc((i-j)*sizeof(char)); //desktop names can be empty but should always be not null strcpy( current_info->x11.desktop.name, (char *)&names[j] ); @@ -834,14 +828,8 @@ void print_desktop_name(struct text_object *obj, char *p, int p_max_size) void free_desktop_info(void) { - if(info.x11.desktop.name) { - free(info.x11.desktop.name); - info.x11.desktop.name = NULL; - } - if(info.x11.desktop.all_names) { - free(info.x11.desktop.all_names); - info.x11.desktop.all_names = NULL; - } + free_and_zero(info.x11.desktop.name); + free_and_zero(info.x11.desktop.all_names); } #ifdef OWN_WINDOW diff --git a/src/xmms2.cc b/src/xmms2.cc index 97ac9a01..86a2d194 100644 --- a/src/xmms2.cc +++ b/src/xmms2.cc @@ -84,20 +84,19 @@ static void xmms_alloc(struct information *ptr) ptr->xmms2.timesplayed = -1; } -#define xfree(x) if (x) { free(x); x = 0; } void free_xmms2(struct text_object *obj) { (void)obj; - xfree(info.xmms2.artist); - xfree(info.xmms2.album); - xfree(info.xmms2.title); - xfree(info.xmms2.genre); - xfree(info.xmms2.comment); - xfree(info.xmms2.url); - xfree(info.xmms2.date); - xfree(info.xmms2.status); - xfree(info.xmms2.playlist); + free_and_zero(info.xmms2.artist); + free_and_zero(info.xmms2.album); + free_and_zero(info.xmms2.title); + free_and_zero(info.xmms2.genre); + free_and_zero(info.xmms2.comment); + free_and_zero(info.xmms2.url); + free_and_zero(info.xmms2.date); + free_and_zero(info.xmms2.status); + free_and_zero(info.xmms2.playlist); } void connection_lost(void *p) From 63133c19d6f884134000aa0539cbc086fe8bc9ea Mon Sep 17 00:00:00 2001 From: Nikolas Garofil Date: Wed, 24 Feb 2010 15:37:20 +0100 Subject: [PATCH 34/34] cpp-ify conf_cookie.c --- src/CMakeLists.txt | 2 +- src/{conf_cookie.c => conf_cookie.cc} | 11 +++++------ src/conf_cookie.h | 8 -------- 3 files changed, 6 insertions(+), 15 deletions(-) rename src/{conf_cookie.c => conf_cookie.cc} (92%) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index f3205f67..8a1aa375 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -166,7 +166,7 @@ if(BUILD_ICONV) endif(BUILD_ICONV) if(BUILD_BUILTIN_CONFIG) - set(optional_sources ${optional_sources} conf_cookie.c) + set(optional_sources ${optional_sources} conf_cookie.cc) endif(BUILD_BUILTIN_CONFIG) add_executable(conky ${conky_sources} ${optional_sources}) diff --git a/src/conf_cookie.c b/src/conf_cookie.cc similarity index 92% rename from src/conf_cookie.c rename to src/conf_cookie.cc index 6dc9e3ff..14e40cfe 100644 --- a/src/conf_cookie.c +++ b/src/conf_cookie.cc @@ -61,14 +61,13 @@ conf_read(void *cookie, char *buf, COOKIE_LEN_T size) #endif /* defined(HAVE_FOPENCOOKIE) || defined(HAVE_FUNOPEN) */ #if defined(HAVE_FOPENCOOKIE) -static cookie_io_functions_t conf_cookie = { - .read = &conf_read, - .write = NULL, - .seek = NULL, - .close = NULL, -}; +static cookie_io_functions_t conf_cookie; FILE *conf_cookie_open(void) { + conf_cookie.read = &conf_read; + conf_cookie.write = NULL; + conf_cookie.seek = NULL; + conf_cookie.close = NULL; return fopencookie(NULL, "r", conf_cookie); } #elif defined(HAVE_FUNOPEN) diff --git a/src/conf_cookie.h b/src/conf_cookie.h index b58606fd..5458b465 100644 --- a/src/conf_cookie.h +++ b/src/conf_cookie.h @@ -3,14 +3,6 @@ #ifndef _CONF_COOKIE_H #define _CONF_COOKIE_H -#ifdef __cplusplus -extern "C" { -#endif - FILE *conf_cookie_open(void); -#ifdef __cplusplus -} -#endif - #endif /* _CONF_COOKIE_H */