diff --git a/library/changes b/library/changes index f8cf1b5..5e22fe9 100644 --- a/library/changes +++ b/library/changes @@ -96,6 +96,14 @@ harder to break code by accidentally forgetting to call InitSemaphore() after having allocated the memory for it. +- Rewrote the code that allocates the file descriptor and file + buffer tables so that all the memory allocations are in one + place and it's possible to specify exactly how many table + entries are required at a time. + +- Creation and initialization of semaphores now uses the AmigaOS4 + specific functions for this purpose, if available. + c.lib 1.188 (7.2.2005) diff --git a/library/fcntl_fcntl.c b/library/fcntl_fcntl.c index 89f6243..935f43d 100644 --- a/library/fcntl_fcntl.c +++ b/library/fcntl_fcntl.c @@ -1,5 +1,5 @@ /* - * $Id: fcntl_fcntl.c,v 1.11 2005-02-28 13:22:53 obarthel Exp $ + * $Id: fcntl_fcntl.c,v 1.12 2005-03-04 09:07:09 obarthel Exp $ * * :ts=4 * @@ -216,30 +216,18 @@ fcntl(int file_descriptor, int cmd, ... /* int arg */ ) goto out; } - __stdio_lock(); - - /* Check if we have that many fd's already */ - while(fdbase >= __num_fd) - { - __stdio_unlock(); - - if(__check_abort_enabled) - __check_abort(); - - /* No; enlarge it */ - if(__grow_fd_table() < 0) - goto out; - - __stdio_lock(); - } - - __stdio_unlock(); + /* Make sure that we have the required number of file + descriptors available. */ + if(__grow_fd_table(fdbase + 1) < 0) + goto out; vacant_slot = -1; /* Guaranteed to have enough here */ do { + __stdio_unlock(); + if(__check_abort_enabled) __check_abort(); @@ -254,10 +242,8 @@ fcntl(int file_descriptor, int cmd, ... /* int arg */ ) } } - __stdio_unlock(); - /* Didn't really find any, grow the table further */ - if(vacant_slot < 0 && __grow_fd_table() < 0) + if(vacant_slot < 0 && __grow_fd_table(0) < 0) goto out; } while(vacant_slot < 0); diff --git a/library/fcntl_open.c b/library/fcntl_open.c index da1e18c..7b2a137 100644 --- a/library/fcntl_open.c +++ b/library/fcntl_open.c @@ -1,5 +1,5 @@ /* - * $Id: fcntl_open.c,v 1.13 2005-03-03 14:20:55 obarthel Exp $ + * $Id: fcntl_open.c,v 1.14 2005-03-04 09:07:09 obarthel Exp $ * * :ts=4 * @@ -144,7 +144,7 @@ open(const char *path_name, int open_flag, ... /* mode_t mode */ ) fd_slot_number = __find_vacant_fd_entry(); if(fd_slot_number < 0) { - if(__grow_fd_table() < 0) + if(__grow_fd_table(0) < 0) { SHOWMSG("couldn't find a vacant file descriptor, and couldn't allocate one either"); goto out; diff --git a/library/socket_accept.c b/library/socket_accept.c index b6342e9..51f9419 100644 --- a/library/socket_accept.c +++ b/library/socket_accept.c @@ -1,5 +1,5 @@ /* - * $Id: socket_accept.c,v 1.8 2005-03-03 14:20:55 obarthel Exp $ + * $Id: socket_accept.c,v 1.9 2005-03-04 09:07:09 obarthel Exp $ * * :ts=4 * @@ -94,7 +94,7 @@ accept(int sockfd,struct sockaddr *cliaddr,int *addrlen) new_fd_slot_number = __find_vacant_fd_entry(); if(new_fd_slot_number < 0) { - if(__grow_fd_table() < 0) + if(__grow_fd_table(0) < 0) { SHOWMSG("couldn't find a vacant fd slot and no memory to create one"); goto out; diff --git a/library/socket_socket.c b/library/socket_socket.c index 78d81a3..c79200c 100644 --- a/library/socket_socket.c +++ b/library/socket_socket.c @@ -1,5 +1,5 @@ /* - * $Id: socket_socket.c,v 1.6 2005-03-03 14:20:55 obarthel Exp $ + * $Id: socket_socket.c,v 1.7 2005-03-04 09:07:09 obarthel Exp $ * * :ts=4 * @@ -61,7 +61,7 @@ socket(int domain,int type,int protocol) fd_slot_number = __find_vacant_fd_entry(); if(fd_slot_number < 0) { - if(__grow_fd_table() < 0) + if(__grow_fd_table(0) < 0) { SHOWMSG("couldn't find a vacant fd slot and no memory to create one"); goto out; diff --git a/library/stdio_fopen.c b/library/stdio_fopen.c index 27243f2..e40336d 100644 --- a/library/stdio_fopen.c +++ b/library/stdio_fopen.c @@ -1,5 +1,5 @@ /* - * $Id: stdio_fopen.c,v 1.5 2005-02-27 21:58:21 obarthel Exp $ + * $Id: stdio_fopen.c,v 1.6 2005-03-04 09:07:09 obarthel Exp $ * * :ts=4 * @@ -76,7 +76,7 @@ fopen(const char *filename, const char *mode) slot_number = __find_vacant_iob_entry(); if(slot_number < 0) { - if(__grow_iob_table() < 0) + if(__grow_iob_table(0) < 0) { SHOWMSG("couldn't find a free file table, and no memory for a new one"); goto out; diff --git a/library/stdio_growfdtable.c b/library/stdio_growfdtable.c index 343f50a..7801de1 100644 --- a/library/stdio_growfdtable.c +++ b/library/stdio_growfdtable.c @@ -1,5 +1,5 @@ /* - * $Id: stdio_growfdtable.c,v 1.3 2005-02-03 16:56:16 obarthel Exp $ + * $Id: stdio_growfdtable.c,v 1.4 2005-03-04 09:07:09 obarthel Exp $ * * :ts=4 * @@ -44,54 +44,64 @@ /****************************************************************************/ int -__grow_fd_table(void) +__grow_fd_table(int max_fd) { const int granularity = 10; - struct fd ** new_fd; int new_num_fd; int result = -1; - int i; - new_num_fd = __num_fd + granularity; + if(max_fd == 0) + new_num_fd = __num_fd + granularity; + else + new_num_fd = max_fd; - new_fd = malloc(sizeof(*new_fd) * new_num_fd); - if(new_fd == NULL) + if(new_num_fd > __num_fd) { - SHOWMSG("not enough memory for new file descriptor table"); + struct fd ** new_fd; + int i; - __set_errno(ENOMEM); - goto out; - } - - for(i = __num_fd ; i < new_num_fd ; i++) - { - new_fd[i] = malloc(sizeof(*new_fd[i])); - if(new_fd[i] == NULL) + new_fd = malloc(sizeof(*new_fd) * new_num_fd); + if(new_fd == NULL) { - int j; - - SHOWMSG("not enough memory for new file descriptor table entry"); - - for(j = __num_fd ; j < i ; j++) - free(new_fd[j]); - - free(new_fd); + SHOWMSG("not enough memory for new file descriptor table"); __set_errno(ENOMEM); goto out; } - memset(new_fd[i],0,sizeof(*new_fd[i])); + for(i = __num_fd ; i < new_num_fd ; i++) + { + new_fd[i] = malloc(sizeof(*new_fd[i])); + if(new_fd[i] == NULL) + { + int j; + + SHOWMSG("not enough memory for new file descriptor table entry"); + + for(j = __num_fd ; j < i ; j++) + free(new_fd[j]); + + free(new_fd); + + __set_errno(ENOMEM); + goto out; + } + + memset(new_fd[i],0,sizeof(*new_fd[i])); + } + + if(__fd != NULL) + { + for(i = 0 ; i < __num_fd ; i++) + new_fd[i] = __fd[i]; + + free(__fd); + } + + __fd = new_fd; + __num_fd = new_num_fd; } - for(i = 0 ; i < __num_fd ; i++) - new_fd[i] = __fd[i]; - - free(__fd); - - __fd = new_fd; - __num_fd = new_num_fd; - result = 0; out: diff --git a/library/stdio_growiobtable.c b/library/stdio_growiobtable.c index e93d7e1..40f58d0 100644 --- a/library/stdio_growiobtable.c +++ b/library/stdio_growiobtable.c @@ -1,5 +1,5 @@ /* - * $Id: stdio_growiobtable.c,v 1.3 2005-02-03 16:56:16 obarthel Exp $ + * $Id: stdio_growiobtable.c,v 1.4 2005-03-04 09:07:09 obarthel Exp $ * * :ts=4 * @@ -44,54 +44,64 @@ /****************************************************************************/ int -__grow_iob_table(void) +__grow_iob_table(int max_iob) { const int granularity = 10; - struct iob ** new_iob; int new_num_iob; int result = -1; - int i; - new_num_iob = __num_iob + granularity; + if(max_iob == 0) + new_num_iob = __num_iob + granularity; + else + new_num_iob = max_iob; - new_iob = malloc(sizeof(*new_iob) * new_num_iob); - if(new_iob == NULL) + if(new_num_iob > __num_iob) { - SHOWMSG("not enough memory for file table"); + struct iob ** new_iob; + int i; - __set_errno(ENOMEM); - goto out; - } - - for(i = __num_iob ; i < new_num_iob ; i++) - { - new_iob[i] = malloc(sizeof(*new_iob[i])); - if(new_iob[i] == NULL) + new_iob = malloc(sizeof(*new_iob) * new_num_iob); + if(new_iob == NULL) { - int j; - - SHOWMSG("not enough memory for file table entry"); - - for(j = __num_iob ; j < i ; j++) - free(new_iob[j]); - - free(new_iob); + SHOWMSG("not enough memory for file table"); __set_errno(ENOMEM); goto out; } - memset(new_iob[i],0,sizeof(*new_iob[i])); + for(i = __num_iob ; i < new_num_iob ; i++) + { + new_iob[i] = malloc(sizeof(*new_iob[i])); + if(new_iob[i] == NULL) + { + int j; + + SHOWMSG("not enough memory for file table entry"); + + for(j = __num_iob ; j < i ; j++) + free(new_iob[j]); + + free(new_iob); + + __set_errno(ENOMEM); + goto out; + } + + memset(new_iob[i],0,sizeof(*new_iob[i])); + } + + if(__iob != NULL) + { + for(i = 0 ; i < __num_iob ; i++) + new_iob[i] = __iob[i]; + + free(__iob); + } + + __iob = new_iob; + __num_iob = new_num_iob; } - for(i = 0 ; i < __num_iob ; i++) - new_iob[i] = __iob[i]; - - free(__iob); - - __iob = new_iob; - __num_iob = new_num_iob; - result = 0; out: diff --git a/library/stdio_init_exit.c b/library/stdio_init_exit.c index 96d8251..ce6da23 100644 --- a/library/stdio_init_exit.c +++ b/library/stdio_init_exit.c @@ -1,5 +1,5 @@ /* - * $Id: stdio_init_exit.c,v 1.22 2005-03-03 14:20:55 obarthel Exp $ + * $Id: stdio_init_exit.c,v 1.23 2005-03-04 09:07:09 obarthel Exp $ * * :ts=4 * @@ -131,36 +131,12 @@ __stdio_init(void) if(__stdio_lock_init() < 0) goto out; - __iob = malloc(sizeof(*__iob) * num_standard_files); - if(__iob == NULL) + if(__grow_iob_table(num_standard_files) < 0) goto out; - for(i = 0 ; i < num_standard_files ; i++) - { - __iob[i] = malloc(sizeof(*__iob[i])); - if(__iob[i] == NULL) - goto out; - - memset(__iob[i],0,sizeof(*__iob[i])); - } - - __num_iob = num_standard_files; - - __fd = malloc(sizeof(*__fd) * num_standard_files); - if(__fd == NULL) + if(__grow_fd_table(num_standard_files) < 0) goto out; - for(i = 0 ; i < num_standard_files ; i++) - { - __fd[i] = malloc(sizeof(*__fd[i])); - if(__fd[i] == NULL) - goto out; - - memset(__fd[i],0,sizeof(*__fd[i])); - } - - __num_fd = num_standard_files; - /* Now initialize the standard I/O streams (input, output, error). */ for(i = STDIN_FILENO ; i <= STDERR_FILENO ; i++) { diff --git a/library/stdio_protos.h b/library/stdio_protos.h index e4ee6ab..6d1665b 100644 --- a/library/stdio_protos.h +++ b/library/stdio_protos.h @@ -1,5 +1,5 @@ /* - * $Id: stdio_protos.h,v 1.11 2005-02-28 13:22:53 obarthel Exp $ + * $Id: stdio_protos.h,v 1.12 2005-03-04 09:07:09 obarthel Exp $ * * :ts=4 * @@ -114,12 +114,12 @@ extern int __find_vacant_iob_entry(void); /****************************************************************************/ /* stdio_growfdtable.c */ -extern int __grow_fd_table(void); +extern int __grow_fd_table(int max_fd); /****************************************************************************/ /* stdio_growiobtable.c */ -extern int __grow_iob_table(void); +extern int __grow_iob_table(int max_fd); /****************************************************************************/ diff --git a/library/stdio_record_locking.c b/library/stdio_record_locking.c index 68afe82..919dd21 100644 --- a/library/stdio_record_locking.c +++ b/library/stdio_record_locking.c @@ -1,5 +1,5 @@ /* - * $Id: stdio_record_locking.c,v 1.4 2005-02-28 10:07:31 obarthel Exp $ + * $Id: stdio_record_locking.c,v 1.5 2005-03-04 09:07:09 obarthel Exp $ * * :ts=4 * @@ -147,20 +147,35 @@ obtain_file_lock_semaphore(BOOL shared) { SHOWMSG("didn't find it; we're going to make our own"); - FileLockSemaphore = AllocMem(sizeof(*FileLockSemaphore) + strlen(__file_lock_semaphore_name)+1,MEMF_ANY|MEMF_PUBLIC); + #if defined(__amigaos4__) + { + FileLockSemaphore = AllocSysObjectTags(ASOT_SEMAPHORE, + ASOSEM_Size, sizeof(*FileLockSemaphore), + ASOSEM_Name, __file_lock_semaphore_name, + ASOSEM_Pri, 1, + TAG_END); + } + #else + { + FileLockSemaphore = AllocMem(sizeof(*FileLockSemaphore) + strlen(__file_lock_semaphore_name)+1,MEMF_ANY|MEMF_PUBLIC); + if(FileLockSemaphore != NULL) + { + memset(FileLockSemaphore,0,sizeof(*FileLockSemaphore)); + + InitSemaphore(&FileLockSemaphore->fls_Semaphore); + + FileLockSemaphore->fls_Semaphore.ss_Link.ln_Name = (char *)(FileLockSemaphore + 1); + strcpy(FileLockSemaphore->fls_Semaphore.ss_Link.ln_Name,__file_lock_semaphore_name); + + FileLockSemaphore->fls_Semaphore.ss_Link.ln_Pri = 1; + } + } + #endif /* __amigaos4__ */ + if(FileLockSemaphore != NULL) { SHOWMSG("adding our own semaphore"); - memset(FileLockSemaphore,0,sizeof(*FileLockSemaphore)); - - InitSemaphore(&FileLockSemaphore->fls_Semaphore); - - FileLockSemaphore->fls_Semaphore.ss_Link.ln_Name = (char *)(FileLockSemaphore + 1); - strcpy(FileLockSemaphore->fls_Semaphore.ss_Link.ln_Name,__file_lock_semaphore_name); - - FileLockSemaphore->fls_Semaphore.ss_Link.ln_Pri = 1; - FileLockSemaphore->fls_Size = sizeof(*FileLockSemaphore); NewList((struct List *)&FileLockSemaphore->fls_LockList); diff --git a/library/unistd_dup2.c b/library/unistd_dup2.c index cf5ac0e..ca24b44 100644 --- a/library/unistd_dup2.c +++ b/library/unistd_dup2.c @@ -1,5 +1,5 @@ /* - * $Id: unistd_dup2.c,v 1.7 2005-02-27 21:58:21 obarthel Exp $ + * $Id: unistd_dup2.c,v 1.8 2005-03-04 09:07:09 obarthel Exp $ * * :ts=4 * @@ -75,7 +75,7 @@ dup2(int file_descriptor1, int file_descriptor2) if(file_descriptor2 < 0) { /* No free space, so let's grow the table. */ - if(__grow_fd_table() < 0) + if(__grow_fd_table(0) < 0) { SHOWMSG("not enough memory for new file descriptor"); goto out; @@ -88,14 +88,8 @@ dup2(int file_descriptor1, int file_descriptor2) else if (file_descriptor1 != file_descriptor2) { /* Make sure the requested duplicate exists. */ - while(file_descriptor2 >= __num_fd) - { - if(__check_abort_enabled) - __check_abort(); - - if(__grow_fd_table() < 0) - goto out; - } + if(__grow_fd_table(file_descriptor2 + 1) < 0) + goto out; assert( file_descriptor2 >= 0 && file_descriptor2 < __num_fd ); assert( __fd[file_descriptor2] != NULL ); diff --git a/library/unistd_fdopen.c b/library/unistd_fdopen.c index 2a7bf5c..80a81be 100644 --- a/library/unistd_fdopen.c +++ b/library/unistd_fdopen.c @@ -1,5 +1,5 @@ /* - * $Id: unistd_fdopen.c,v 1.5 2005-02-27 21:58:21 obarthel Exp $ + * $Id: unistd_fdopen.c,v 1.6 2005-03-04 09:07:09 obarthel Exp $ * * :ts=4 * @@ -80,7 +80,7 @@ fdopen(int file_descriptor, const char * type) slot_number = __find_vacant_iob_entry(); if(slot_number < 0) { - if(__grow_iob_table() < 0) + if(__grow_iob_table(0) < 0) { SHOWMSG("not enough memory for a file buffer slot"); goto out;