diff options
author | Quincey Koziol <koziol@hdfgroup.org> | 2002-08-08 16:52:55 (GMT) |
---|---|---|
committer | Quincey Koziol <koziol@hdfgroup.org> | 2002-08-08 16:52:55 (GMT) |
commit | d8397a6f426227d09d20e647ce8b12b8c6295b2d (patch) | |
tree | 2943fbfd2bfb66cf167eb642835fdb4deb3afd3c /src/H5FDsec2.c | |
parent | 573307786a1f5f7ce597e5191ea08c3bbd95b66c (diff) | |
download | hdf5-d8397a6f426227d09d20e647ce8b12b8c6295b2d.zip hdf5-d8397a6f426227d09d20e647ce8b12b8c6295b2d.tar.gz hdf5-d8397a6f426227d09d20e647ce8b12b8c6295b2d.tar.bz2 |
[svn-r5842] Purpose:
Code cleanup
Description:
Change most (all?) HRETURN_ERROR macros to HGOTO_ERROR macros, along with
HRETURN macros to HGOTO_DONE macros. This unifies the error return path
from functions and reduces the size of the library by up to 10% on some
platforms.
Additionally, I improved a lot of the error cleanup code in many routines.
Platforms tested:
FreeBSD 4.6 (sleipnir) serial & parallel and IRIX64 6.5 (modi4) serial &
parallel.
Diffstat (limited to 'src/H5FDsec2.c')
-rw-r--r-- | src/H5FDsec2.c | 138 |
1 files changed, 77 insertions, 61 deletions
diff --git a/src/H5FDsec2.c b/src/H5FDsec2.c index f888852..bd2ca96 100644 --- a/src/H5FDsec2.c +++ b/src/H5FDsec2.c @@ -231,16 +231,17 @@ herr_t H5Pset_fapl_sec2(hid_t fapl_id) { H5P_genplist_t *plist; /* Property list pointer */ - herr_t ret_value=FAIL; + herr_t ret_value; FUNC_ENTER_API(H5Pset_fapl_sec2, FAIL); H5TRACE1("e","i",fapl_id); if(TRUE!=H5P_isa_class(fapl_id,H5P_FILE_ACCESS) || NULL == (plist = H5I_object(fapl_id))) - HRETURN_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "not a file access property list"); + HGOTO_ERROR(H5E_ARGS, H5E_BADTYPE, FAIL, "not a file access property list"); ret_value= H5P_set_driver(plist, H5FD_SEC2, NULL); +done: FUNC_LEAVE(ret_value); } @@ -268,7 +269,7 @@ H5FD_sec2_open(const char *name, unsigned flags, hid_t UNUSED fapl_id, haddr_t maxaddr) { int o_flags; - int fd; + int fd=(-1); H5FD_sec2_t *file=NULL; #ifdef WIN32 HFILE filehandle; @@ -276,16 +277,17 @@ H5FD_sec2_open(const char *name, unsigned flags, hid_t UNUSED fapl_id, int results; #endif h5_stat_t sb; + H5FD_t *ret_value; FUNC_ENTER_NOAPI(H5FD_sec2_open, NULL); /* Check arguments */ if (!name || !*name) - HRETURN_ERROR(H5E_ARGS, H5E_BADVALUE, NULL, "invalid file name"); + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, NULL, "invalid file name"); if (0==maxaddr || HADDR_UNDEF==maxaddr) - HRETURN_ERROR(H5E_ARGS, H5E_BADRANGE, NULL, "bogus maxaddr"); + HGOTO_ERROR(H5E_ARGS, H5E_BADRANGE, NULL, "bogus maxaddr"); if (ADDR_OVERFLOW(maxaddr)) - HRETURN_ERROR(H5E_ARGS, H5E_OVERFLOW, NULL, "bogus maxaddr"); + HGOTO_ERROR(H5E_ARGS, H5E_OVERFLOW, NULL, "bogus maxaddr"); /* Build the open flags */ o_flags = (H5F_ACC_RDWR & flags) ? O_RDWR : O_RDONLY; @@ -295,16 +297,14 @@ H5FD_sec2_open(const char *name, unsigned flags, hid_t UNUSED fapl_id, /* Open the file */ if ((fd=HDopen(name, o_flags, 0666))<0) - HRETURN_ERROR(H5E_FILE, H5E_CANTOPENFILE, NULL, "unable to open file"); - if (HDfstat(fd, &sb)<0) { - HDclose(fd); - HRETURN_ERROR(H5E_FILE, H5E_BADFILE, NULL, "unable to fstat file"); - } + HGOTO_ERROR(H5E_FILE, H5E_CANTOPENFILE, NULL, "unable to open file"); + if (HDfstat(fd, &sb)<0) + HGOTO_ERROR(H5E_FILE, H5E_BADFILE, NULL, "unable to fstat file"); /* Create the new file struct */ if (NULL==(file=H5FL_ALLOC(H5FD_sec2_t,1))) - HRETURN_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, - "unable to allocate file struct"); + HGOTO_ERROR(H5E_RESOURCE, H5E_NOSPACE, NULL, "unable to allocate file struct"); + file->fd = fd; file->eof = sb.st_size; file->pos = HADDR_UNDEF; @@ -318,7 +318,17 @@ H5FD_sec2_open(const char *name, unsigned flags, hid_t UNUSED fapl_id, file->device = sb.st_dev; file->inode = sb.st_ino; #endif - FUNC_LEAVE((H5FD_t*)file); + + /* Set return value */ + ret_value=(H5FD_t*)file; + +done: + if(ret_value==NULL) { + if(fd>=0) + HDclose(fd); + } /* end if */ + + FUNC_LEAVE(ret_value); } @@ -342,15 +352,17 @@ static herr_t H5FD_sec2_close(H5FD_t *_file) { H5FD_sec2_t *file = (H5FD_sec2_t*)_file; + herr_t ret_value=SUCCEED; /* Return value */ FUNC_ENTER_NOAPI(H5FD_sec2_close, FAIL); if (HDclose(file->fd)<0) - HRETURN_ERROR(H5E_IO, H5E_CANTCLOSEFILE, FAIL, "unable to close file"); + HGOTO_ERROR(H5E_IO, H5E_CANTCLOSEFILE, FAIL, "unable to close file"); H5FL_FREE(H5FD_sec2_t,file); - FUNC_LEAVE(SUCCEED); +done: + FUNC_LEAVE(ret_value); } @@ -382,29 +394,30 @@ H5FD_sec2_cmp(const H5FD_t *_f1, const H5FD_t *_f2) FUNC_ENTER_NOAPI(H5FD_sec2_cmp, H5FD_VFD_DEFAULT); #ifdef WIN32 - if (f1->fileindexhi < f2->fileindexhi) ret_value= -1; - if (f1->fileindexhi > f2->fileindexhi) ret_value= 1; + if (f1->fileindexhi < f2->fileindexhi) HGOTO_DONE(-1); + if (f1->fileindexhi > f2->fileindexhi) HGOTO_DONE(1); - if (f1->fileindexlo < f2->fileindexlo) ret_value= -1; - if (f1->fileindexlo > f2->fileindexlo) ret_value= 1; + if (f1->fileindexlo < f2->fileindexlo) HGOTO_DONE(-1); + if (f1->fileindexlo > f2->fileindexlo) HGOTO_DONE(1); #else #ifdef H5_DEV_T_IS_SCALAR - if (f1->device < f2->device) ret_value= -1; - if (f1->device > f2->device) ret_value= 1; + if (f1->device < f2->device) HGOTO_DONE(-1); + if (f1->device > f2->device) HGOTO_DONE(1); #else /* H5_DEV_T_IS_SCALAR */ /* If dev_t isn't a scalar value on this system, just use memcmp to * determine if the values are the same or not. The actual return value * shouldn't really matter... */ - if(HDmemcmp(&(f1->device),&(f2->device),sizeof(dev_t))<0) ret_value= -1; - if(HDmemcmp(&(f1->device),&(f2->device),sizeof(dev_t))>0) ret_value= 1; + if(HDmemcmp(&(f1->device),&(f2->device),sizeof(dev_t))<0) HGOTO_DONE(-1); + if(HDmemcmp(&(f1->device),&(f2->device),sizeof(dev_t))>0) HGOTO_DONE(1); #endif /* H5_DEV_T_IS_SCALAR */ - if (f1->inode < f2->inode) ret_value= -1; - if (f1->inode > f2->inode) ret_value= 1; + if (f1->inode < f2->inode) HGOTO_DONE(-1); + if (f1->inode > f2->inode) HGOTO_DONE(1); #endif +done: FUNC_LEAVE(ret_value); } @@ -562,6 +575,7 @@ H5FD_sec2_read(H5FD_t *_file, H5FD_mem_t UNUSED type, hid_t UNUSED dxpl_id, hadd { H5FD_sec2_t *file = (H5FD_sec2_t*)_file; ssize_t nbytes; + herr_t ret_value=SUCCEED; /* Return value */ FUNC_ENTER_NOAPI(H5FD_sec2_read, FAIL); @@ -570,20 +584,16 @@ H5FD_sec2_read(H5FD_t *_file, H5FD_mem_t UNUSED type, hid_t UNUSED dxpl_id, hadd /* Check for overflow conditions */ if (HADDR_UNDEF==addr) - HRETURN_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "addr undefined"); + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "addr undefined"); if (REGION_OVERFLOW(addr, size)) - HRETURN_ERROR(H5E_ARGS, H5E_OVERFLOW, FAIL, "addr overflow"); + HGOTO_ERROR(H5E_ARGS, H5E_OVERFLOW, FAIL, "addr overflow"); if (addr+size>file->eoa) - HRETURN_ERROR(H5E_ARGS, H5E_OVERFLOW, FAIL, "addr overflow"); + HGOTO_ERROR(H5E_ARGS, H5E_OVERFLOW, FAIL, "addr overflow"); /* Seek to the correct location */ if ((addr!=file->pos || OP_READ!=file->op) && - file_seek(file->fd, (file_offset_t)addr, SEEK_SET)<0) { - file->pos = HADDR_UNDEF; - file->op = OP_UNKNOWN; - HRETURN_ERROR(H5E_IO, H5E_SEEKERROR, FAIL, - "unable to seek to proper position"); - } + file_seek(file->fd, (file_offset_t)addr, SEEK_SET)<0) + HGOTO_ERROR(H5E_IO, H5E_SEEKERROR, FAIL, "unable to seek to proper position"); /* * Read data, being careful of interrupted system calls, partial results, @@ -593,12 +603,8 @@ H5FD_sec2_read(H5FD_t *_file, H5FD_mem_t UNUSED type, hid_t UNUSED dxpl_id, hadd do { nbytes = HDread(file->fd, buf, size); } while (-1==nbytes && EINTR==errno); - if (-1==nbytes) { - /* error */ - file->pos = HADDR_UNDEF; - file->op = OP_UNKNOWN; - HRETURN_ERROR(H5E_IO, H5E_READERROR, FAIL, "file read failed"); - } + if (-1==nbytes) /* error */ + HGOTO_ERROR(H5E_IO, H5E_READERROR, FAIL, "file read failed"); if (0==nbytes) { /* end of file but not end of format address space */ HDmemset(buf, 0, size); @@ -614,7 +620,15 @@ H5FD_sec2_read(H5FD_t *_file, H5FD_mem_t UNUSED type, hid_t UNUSED dxpl_id, hadd /* Update current position */ file->pos = addr; file->op = OP_READ; - FUNC_LEAVE(SUCCEED); + +done: + if(ret_value<0) { + /* Reset last file I/O information */ + file->pos = HADDR_UNDEF; + file->op = OP_UNKNOWN; + } /* end if */ + + FUNC_LEAVE(ret_value); } @@ -642,6 +656,7 @@ H5FD_sec2_write(H5FD_t *_file, H5FD_mem_t UNUSED type, hid_t UNUSED dxpl_id, had { H5FD_sec2_t *file = (H5FD_sec2_t*)_file; ssize_t nbytes; + herr_t ret_value=SUCCEED; /* Return value */ FUNC_ENTER_NOAPI(H5FD_sec2_write, FAIL); @@ -650,20 +665,16 @@ H5FD_sec2_write(H5FD_t *_file, H5FD_mem_t UNUSED type, hid_t UNUSED dxpl_id, had /* Check for overflow conditions */ if (HADDR_UNDEF==addr) - HRETURN_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "addr undefined"); + HGOTO_ERROR(H5E_ARGS, H5E_BADVALUE, FAIL, "addr undefined"); if (REGION_OVERFLOW(addr, size)) - HRETURN_ERROR(H5E_ARGS, H5E_OVERFLOW, FAIL, "addr overflow"); + HGOTO_ERROR(H5E_ARGS, H5E_OVERFLOW, FAIL, "addr overflow"); if (addr+size>file->eoa) - HRETURN_ERROR(H5E_ARGS, H5E_OVERFLOW, FAIL, "addr overflow"); + HGOTO_ERROR(H5E_ARGS, H5E_OVERFLOW, FAIL, "addr overflow"); /* Seek to the correct location */ if ((addr!=file->pos || OP_WRITE!=file->op) && - file_seek(file->fd, (file_offset_t)addr, SEEK_SET)<0) { - file->pos = HADDR_UNDEF; - file->op = OP_UNKNOWN; - HRETURN_ERROR(H5E_IO, H5E_SEEKERROR, FAIL, - "unable to seek to proper position"); - } + file_seek(file->fd, (file_offset_t)addr, SEEK_SET)<0) + HGOTO_ERROR(H5E_IO, H5E_SEEKERROR, FAIL, "unable to seek to proper position"); /* * Write the data, being careful of interrupted system calls and partial @@ -673,12 +684,8 @@ H5FD_sec2_write(H5FD_t *_file, H5FD_mem_t UNUSED type, hid_t UNUSED dxpl_id, had do { nbytes = HDwrite(file->fd, buf, size); } while (-1==nbytes && EINTR==errno); - if (-1==nbytes) { - /* error */ - file->pos = HADDR_UNDEF; - file->op = OP_UNKNOWN; - HRETURN_ERROR(H5E_IO, H5E_WRITEERROR, FAIL, "file write failed"); - } + if (-1==nbytes) /* error */ + HGOTO_ERROR(H5E_IO, H5E_WRITEERROR, FAIL, "file write failed"); assert(nbytes>0); assert((size_t)nbytes<=size); size -= nbytes; @@ -692,7 +699,14 @@ H5FD_sec2_write(H5FD_t *_file, H5FD_mem_t UNUSED type, hid_t UNUSED dxpl_id, had if (file->pos>file->eof) file->eof = file->pos; - FUNC_LEAVE(SUCCEED); +done: + if(ret_value<0) { + /* Reset last file I/O information */ + file->pos = HADDR_UNDEF; + file->op = OP_UNKNOWN; + } /* end if */ + + FUNC_LEAVE(ret_value); } @@ -721,6 +735,7 @@ H5FD_sec2_flush(H5FD_t *_file, unsigned UNUSED closing) HFILE filehandle; /* Windows file handle */ LARGE_INTEGER li; /* 64-bit integer for SetFilePointer() call */ #endif /* WIN32 */ + herr_t ret_value=SUCCEED; /* Return value */ FUNC_ENTER_NOAPI(H5FD_sec2_flush, FAIL); @@ -737,10 +752,10 @@ H5FD_sec2_flush(H5FD_t *_file, unsigned UNUSED closing) li.QuadPart = file->eoa; SetFilePointer((HANDLE)filehandle,li.LowPart,&li.HighPart,FILE_BEGIN); if(SetEndOfFile((HANDLE)filehandle)==0) - HRETURN_ERROR(H5E_IO, H5E_SEEKERROR, FAIL, "unable to extend file properly"); + HGOTO_ERROR(H5E_IO, H5E_SEEKERROR, FAIL, "unable to extend file properly"); #else /* WIN32 */ if (-1==file_truncate(file->fd, (file_offset_t)file->eoa)) - HRETURN_ERROR(H5E_IO, H5E_SEEKERROR, FAIL, "unable to extend file properly"); + HGOTO_ERROR(H5E_IO, H5E_SEEKERROR, FAIL, "unable to extend file properly"); #endif /* WIN32 */ /* Update the eof value */ @@ -751,5 +766,6 @@ H5FD_sec2_flush(H5FD_t *_file, unsigned UNUSED closing) file->op = OP_UNKNOWN; } - FUNC_LEAVE(SUCCEED); +done: + FUNC_LEAVE(ret_value); } |