Commit 3efb82f4 authored by Uwe Schulzweida's avatar Uwe Schulzweida
Browse files

file.c: cleanup

parent 4642287e
......@@ -5,7 +5,7 @@
#include <assert.h>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <string.h>
#include <errno.h>
#include <ctype.h>
#include <unistd.h>
......@@ -19,8 +19,6 @@ size_t getpagesize(void);
#include "error.h"
#include "file.h"
/* begin deike */
#ifdef USE_MPI
#include "cdi.h"
#include "namespace.h"
......@@ -29,8 +27,6 @@ size_t getpagesize(void);
#include "pio_util.h"
#endif
/* end deike */
#if ! defined(O_BINARY)
#define O_BINARY 0
#endif
......@@ -80,17 +76,17 @@ static int _file_init = FALSE;
static pthread_once_t _file_init_thread = PTHREAD_ONCE_INIT;
static pthread_mutex_t _file_mutex;
# define FILE_LOCK pthread_mutex_lock(&_file_mutex);
# define FILE_UNLOCK pthread_mutex_unlock(&_file_mutex);
# define FILE_INIT \
if ( _file_init == FALSE ) pthread_once(&_file_init_thread, file_initialize);
# define FILE_LOCK() pthread_mutex_lock(&_file_mutex)
# define FILE_UNLOCK() pthread_mutex_unlock(&_file_mutex)
# define FILE_INIT() \
if ( _file_init == FALSE ) pthread_once(&_file_init_thread, file_initialize)
#else
# define FILE_LOCK
# define FILE_UNLOCK
# define FILE_INIT \
if ( _file_init == FALSE ) file_initialize();
# define FILE_LOCK()
# define FILE_UNLOCK()
# define FILE_INIT() \
if ( _file_init == FALSE ) file_initialize()
#endif
......@@ -157,7 +153,7 @@ static void file_table_print(void);
* A version string.
*/
#undef LIBVERSION
#define LIBVERSION 1.7.1
#define LIBVERSION 1.8.0
#define XSTRING(x) #x
#define STRING(x) XSTRING(x)
const char file_libvers[] = STRING(LIBVERSION) " of "__DATE__" "__TIME__;
......@@ -176,6 +172,7 @@ const char file_libvers[] = STRING(LIBVERSION) " of "__DATE__" "__TIME__;
use HAVE_STRUCT_STAT_ST_BLKSIZE
22/08/2010 1.7.0 refactor
11/11/2010 1.7.1 update for changed interface of error.h
02/02/2012 1.8.0 cleanup
*/
......@@ -197,21 +194,21 @@ void file_list_new(void)
_fileList = (filePtrToIdx *) malloc(_file_max*sizeof(filePtrToIdx));
}
static
void file_list_delete(void)
{
if ( _fileList )
{
free(_fileList);
_fileList = NULL;
}
if ( _fileList )
{
free(_fileList);
_fileList = NULL;
}
}
static
void file_init_pointer(void)
{
int i;
for ( i = 0; i < _file_max; i++ )
{
_fileList[i].next = _fileList + i + 1;
......@@ -229,15 +226,15 @@ bfile_t *file_to_pointer(int idx)
{
bfile_t *fileptr = NULL;
FILE_INIT
FILE_INIT();
if ( idx >= 0 && idx < _file_max )
{
FILE_LOCK
FILE_LOCK();
fileptr = _fileList[idx].ptr;
FILE_UNLOCK
FILE_UNLOCK();
}
else
Error("file index %d undefined!", idx);
......@@ -254,7 +251,7 @@ int file_from_pointer(bfile_t *ptr)
if ( ptr )
{
FILE_LOCK
FILE_LOCK();
if ( _fileAvail )
{
......@@ -263,14 +260,14 @@ int file_from_pointer(bfile_t *ptr)
newptr->next = 0;
idx = newptr->idx;
newptr->ptr = ptr;
if ( FILE_Debug )
Message("Pointer %p has idx %d from file list", ptr, idx);
}
else
Warning("Too many open files (limit is %d)!", _file_max);
FILE_UNLOCK
FILE_UNLOCK();
}
else
Error("Internal problem (pointer %p undefined)", ptr);
......@@ -324,7 +321,7 @@ void file_delete_entry(bfile_t *fileptr)
idx = fileptr->self;
FILE_LOCK
FILE_LOCK();
free(fileptr);
......@@ -332,7 +329,7 @@ void file_delete_entry(bfile_t *fileptr)
_fileList[idx].ptr = 0;
_fileAvail = &_fileList[idx];
FILE_UNLOCK
FILE_UNLOCK();
if ( FILE_Debug )
Message("Removed idx %d from file list", idx);
......@@ -563,7 +560,7 @@ int fileSetPos(int fileID, off_t offset, int whence)
if ( FILE_Debug )
Message("Reset buffer pos from %ld to %ld",
fileptr->bufferPos, fileptr->bufferEnd + 1);
fileptr->bufferPos = fileptr->bufferEnd + 1;
}
fileptr->bufferCnt = fileptr->bufferEnd - position + 1;
......@@ -597,7 +594,7 @@ int fileSetPos(int fileID, off_t offset, int whence)
if ( FILE_Debug )
Message("Reset buffer pos from %ld to %ld",
fileptr->bufferPos, fileptr->bufferEnd + 1);
fileptr->bufferPos = fileptr->bufferEnd + 1;
}
fileptr->bufferCnt -= offset;
......@@ -798,12 +795,13 @@ void file_initialize(void)
}
file_list_new();
atexit(file_list_delete);
FILE_LOCK
FILE_LOCK();
file_init_pointer();
FILE_UNLOCK
FILE_UNLOCK();
if ( FILE_Debug ) atexit(file_table_print);
......@@ -867,7 +865,7 @@ void file_set_buffer(bfile_t *fileptr)
fileptr->buffer = (char *) malloc(buffersize);
if ( fileptr->buffer == NULL )
SysError("Allocation of file buffer failed!");
}
}
if ( fileptr->type == FILE_TYPE_FOPEN )
if ( setvbuf(fileptr->fp, fileptr->buffer, _IOFBF, buffersize) )
......@@ -884,14 +882,14 @@ int file_fill_buffer(bfile_t *fileptr)
int ret;
long offset = 0;
off_t retseek;
if ( FILE_Debug )
Message("file ptr = %p Cnt = %ld", fileptr, fileptr->bufferCnt);
if ( (fileptr->flag & FILE_EOF) != 0 ) return (EOF);
if ( fileptr->buffer == NULL ) file_set_buffer(fileptr);
if ( fileptr->bufferSize == 0 ) return (EOF);
fd = fileptr->fd;
......@@ -934,7 +932,7 @@ int file_fill_buffer(bfile_t *fileptr)
retseek = lseek(fileptr->fd, fileptr->bufferPos, SEEK_SET);
if ( retseek == (off_t)-1 )
SysError("lseek error at pos %ld file %s", (long) fileptr->bufferPos, fileptr->name);
nread = (long) read(fd, fileptr->buffer, fileptr->bufferSize);
}
......@@ -1057,8 +1055,7 @@ void fileSetBufferSize(int fileID, long buffersize)
if ( fileptr ) fileptr->bufferSize = buffersize;
}
/*
/*
* Open a file. Returns file ID, or -1 on error
*/
int fileOpen(const char *filename, const char *mode)
......@@ -1070,15 +1067,12 @@ int fileOpen(const char *filename, const char *mode)
struct stat filestat;
bfile_t *fileptr = NULL;
/* begin deike */
#ifdef USE_MPI
if ( memcmp ( mode, "w", 1 ) == 0 && commInqIOMode () != PIO_NONE )
return pioFileOpenW ( filename );
#endif
/* end deike */
FILE_INIT
FILE_INIT();
fmode = tolower((int) mode[0]);
......@@ -1149,14 +1143,13 @@ int fileOpen(const char *filename, const char *mode)
if ( fileptr->type == FILE_TYPE_FOPEN ) file_set_buffer(fileptr);
if ( FILE_Debug )
Message("File %s opened with ID %d", filename, fileID);
Message("File %s opened with ID %d", filename, fileID);
}
return (fileID);
}
/*
/*
* Close a file.
*/
int fileClose(int fileID)
......@@ -1167,13 +1160,10 @@ int fileClose(int fileID)
char *ftname[] = {"unknown", "open", "fopen"};
bfile_t *fileptr;
/* begin deike */
#ifdef USE_MPI
if ( commInqIOMode () != PIO_NONE )
return pioFileClose ( fileID );
return pioFileClose ( fileID );
#endif
/* end deike */
fileptr = file_to_pointer(fileID);
......
......@@ -77,9 +77,9 @@ void namespaceInit ( int nspn, int * argHasLocalFile )
if ( nspn >= 1 )
{
hasLocalFiles = xmalloc ( nspn * sizeof ( hasLocalFiles[0] ));
for ( nspID = 0; nspID < nspn; nspID++ )
for ( nspID = 0; nspID < nspn; nspID++ )
hasLocalFiles[nspID] = argHasLocalFile[nspID];
resStatus = xmalloc ( nspn * sizeof ( resStatus[0] ));
resStatus = xmalloc ( nspn * sizeof ( resStatus[0] ));
}
#endif
}
......@@ -87,7 +87,7 @@ void namespaceInit ( int nspn, int * argHasLocalFile )
void namespaceCleanup ( void )
{
if ( nNamespaces > 1 )
if ( nNamespaces > 1 )
{
free ( hasLocalFiles );
hasLocalFiles = NULL;
......@@ -123,7 +123,7 @@ int namespaceHasLocalFile ( int nId )
{
if ( nId >= nNamespaces || nId < 0 )
abort ();
return hasLocalFiles ? hasLocalFiles[nId] : 0;
}
......@@ -141,7 +141,7 @@ int namespaceAdaptKey ( int key, int nspTarget )
xassert ( tin.nsp == nspTarget );
nsp = namespaceGetActive ();
return namespaceIdxEncode2 ( nsp, tin.idx );
}
......@@ -157,7 +157,7 @@ int namespaceAdaptKey2 ( int key )
tin.nsp = (int)(((unsigned)( key & nspmask )) >> idxbits);
nsp = namespaceGetActive ();
return namespaceIdxEncode2 ( nsp, tin.idx );
}
......
......@@ -11,7 +11,7 @@ typedef struct {
int idx;
int nsp;
statusCode resStatus;
}namespaceTuple_t;
} namespaceTuple_t;
void namespaceCleanup ( void );
void namespaceInit ( int, int * );
......
......@@ -14,8 +14,6 @@
#include "pio_rpc.h"
extern void file_list_delete ( void );
enum { MIN_ARRAY_SIZE = 128, MIN_BUFF_SIZE = 4096 };
static int *arraySizeAllocated;
......@@ -154,8 +152,6 @@ void arrayDestroy ( void )
free ( arraySizeAllocated );
arraySizeAllocated = NULL;
}
file_list_delete ();
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment