chunkd: make error paths more regular
PATCH 3/4 chunkd: make error paths more regular
1. Don't try to save a call in a function where normal and error unfolding sequences are different.
2. Use exception labels linked to what caused them, not to what cleanup has to be done.
3. Balance fs_open <-> fs_close.
--- server/be-fs.c | 18 ++++++++---------- server/chunkd.h | 1 - server/server.c | 14 ++++++-------- 3 files changed, 14 insertions(+), 19 deletions(-)
Jeff, I generally do not interfere into the style, such as the offset for continuation arguments etc. In this case I had to puzzle out if error paths were correct and figured it's cheaper to clean them. For fs_free case, I had to think again what you meant by such an unusual arrangement. Apparently it's just a random artefact.
commit d10b113308ac5bda13d524ebb3f42f610a9ff8c8
Author: Master <[email protected]> Date: Fri Dec 25 22:25:41 2009 -0700
Cleanup.
server/be-fs.c +8 -10
@@ -75,7 +75,7 @@ int fs_open(void) }if (!tchdbsetmutex (hdb)) - goto out_hdb; + goto out_mut; omode =HDBOREADER |HDBONOLCK |HDBOWRITER |HDBOCREAT |HDBOTSYNC ;if (!tchdbopen (hdb, db_fn, omode)) {
@@ -86,24 +86,22 @@ int fs_open(void) chunkd_srv.tbl_master = hdb; -out:free (db_fn); -return rc; +return 0 ; +out_mut: out_hdb:tchdbdel (hdb); - goto out; +out: +free (db_fn); +return rc; }void fs_close (void ) {tchdbclose (chunkd_srv.tbl_master); -} - -void fs_free (void ) -{ -if (chunkd_srv.tbl_master) -tchdbdel (chunkd_srv.tbl_master); +tchdbdel (chunkd_srv.tbl_master); + chunkd_srv.tbl_master =NULL ; } bool fs_table_open(const char *user, const void *kbuf, size_t klen,
server/chunkd.h +0 -1
@@ -220,7 +220,6 @@ extern struct hail_log cldu_hail_log;/* be-fs.c */ extern int fs_open (void );extern void fs_close (void ); -extern void fs_free (void );extern struct backend_obj *fs_obj_new (uint32_t table_id,const void *kbuf,size_t klen,enum chunk_errcode *err_code); extern struct backend_obj *fs_obj_open(uint32_t table_id, const char *user,
server/server.c +6 -8
@@ -1664,13 +1664,13 @@ int main (int argc, char *argv[])if (fs_open ()) { rc =1 ; - goto err_out_session; + goto err_out_fs; }if (cld_begin (chunkd_srv.ourhost, chunkd_srv.cell, chunkd_srv.nid, &chunkd_srv.loc,NULL )) { rc =1 ; - goto err_out_fs; + goto err_out_cld; }/* set up server networking */
@@ -1686,19 +1686,17 @@ int main (int argc, char *argv[])applog (LOG_INFO ,"shutting down" ); +/* net_close(); */ err_out_listen:cld_end (); -err_out_fs: +err_out_cld:fs_close (); +err_out_fs: +g_hash_table_destroy (chunkd_srv.fd_info); err_out_session: -/* net_close(); */ unlink (chunkd_srv.pid_file);close (chunkd_srv.pid_fd); err_out: -if (strict_free) { -fs_free (); -g_hash_table_destroy (chunkd_srv.fd_info); - }closelog ();return rc; }
Quoted 21 lines · Pete Zaitcev
server/be-fs.c +2
-3@@ -75,7 +75,7 @@ int fs_open(void) }if (!tchdbsetmutex (hdb)) - goto out_hdb; + goto out_mut; omode =HDBOREADER |HDBONOLCK |HDBOWRITER |HDBOCREAT |HDBOTSYNC ;if (!tchdbopen (hdb, db_fn, omode)) {@@ -86,24 +86,22 @@ int fs_open(void) chunkd_srv.tbl_master = hdb; -out:free (db_fn); -return rc; +return 0 ;
server/be-fs.c int fs_open(void) · line 99
Quoted 10 lines
server/be-fs.c +4
-1+out_mut: out_hdb:tchdbdel (hdb); - goto out; +out: +free (db_fn); +return rc; }
This part is OK.
Quoted 52 lines
server/be-fs.c +2
-4-void fs_free (void ) -{ -if (chunkd_srv.tbl_master) -tchdbdel (chunkd_srv.tbl_master); +tchdbdel (chunkd_srv.tbl_master); + chunkd_srv.tbl_master =NULL ; } bool fs_table_open(const char *user, const void *kbuf, size_t klen,server/chunkd.h +0
-1@@ -220,7 +220,6 @@ extern struct hail_log cldu_hail_log;/* be-fs.c */ extern int fs_open (void );extern void fs_close (void ); -extern void fs_free (void );extern struct backend_obj *fs_obj_new (uint32_t table_id,const void *kbuf,size_t klen,enum chunk_errcode *err_code); extern struct backend_obj *fs_obj_open(uint32_t table_id, const char *user,server/server.c +5
-3@@ -1664,13 +1664,13 @@ int main (int argc, char *argv[])if (fs_open ()) { rc =1 ; - goto err_out_session; + goto err_out_fs; }if (cld_begin (chunkd_srv.ourhost, chunkd_srv.cell, chunkd_srv.nid, &chunkd_srv.loc,NULL )) { rc =1 ; - goto err_out_fs; + goto err_out_cld; }/* set up server networking */ @@ -1686,19 +1686,17 @@ int main (int argc, char *argv[])applog (LOG_INFO ,"shutting down" ); +/* net_close(); */ err_out_listen:cld_end (); -err_out_fs: +err_out_cld:fs_close (); +err_out_fs:
server/server.c int main (int argc, char *argv[]) · line 1701
Quoted 10 lines
server/server.c +1
-5+g_hash_table_destroy (chunkd_srv.fd_info); err_out_session: -/* net_close(); */ unlink (chunkd_srv.pid_file);close (chunkd_srv.pid_fd); err_out: -if (strict_free) { -fs_free (); -g_hash_table_destroy (chunkd_srv.fd_info); - }
I think it was misunderstood what is going on here. Read first what strict_free does, the docs are in "chunkd --help" output.
server/server.c int main (int argc, char *argv[]) · line 1702
Quoted 3 lines
server/server.c +0
-0{"strict-free" ,1001 ,NULL ,0 ,"For memory-checker runs. When shutting down server, free local " "heap, rather than simply exit(2)ing and letting OS clean up." },
It can be significantly faster to exit() rather than needlessly walk data structures and free them, when the OS can do the same thing, only faster and with more efficiency.
Therefore the end code in main() handles both normal shutdown, after possibly considerable runtime has elapsed, and error shutdown. Toss into the mix memory-checker code, and the result can be quite confusing, I admit.
The only time we need to free heap objects is the rare, non-default case of memory checker (valgrind, here) runs.
Jeff