From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2210) id 984A5396ECFE; Fri, 8 May 2020 11:30:17 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 984A5396ECFE Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Ken Brown To: cygwin-cvs@sourceware.org Subject: [newlib-cygwin] Cygwin: FIFO: allow fc_handler list to grow dynamically X-Act-Checkin: newlib-cygwin X-Git-Author: Ken Brown X-Git-Refname: refs/heads/master X-Git-Oldrev: 606baf5566d683ac68e3414c457d124c7c7bdcc5 X-Git-Newrev: c76ded2ca0bb6136205e69caa073bf19944ae509 Message-Id: <20200508113017.984A5396ECFE@sourceware.org> Date: Fri, 8 May 2020 11:30:17 +0000 (GMT) X-BeenThere: cygwin-cvs@cygwin.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Cygwin core component git logs List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 08 May 2020 11:30:17 -0000 https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=c76ded2ca0bb6136205e69caa073bf19944ae509 commit c76ded2ca0bb6136205e69caa073bf19944ae509 Author: Ken Brown Date: Thu Apr 2 13:47:18 2020 -0400 Cygwin: FIFO: allow fc_handler list to grow dynamically Make fc_handler a pointer to malloc'd memory instead of a fixed-size array. The size is now a new data member 'shandlers'. Call realloc in add_client_handler if we need to grow the array. free fc_handler in close. As long as we're touching that code, also remove an unneeded lock. Diff: --- winsup/cygwin/fhandler.h | 6 +++--- winsup/cygwin/fhandler_fifo.cc | 41 +++++++++++++++++++++++------------------ 2 files changed, 26 insertions(+), 21 deletions(-) diff --git a/winsup/cygwin/fhandler.h b/winsup/cygwin/fhandler.h index bd44da5cd..4f42cf1b8 100644 --- a/winsup/cygwin/fhandler.h +++ b/winsup/cygwin/fhandler.h @@ -1268,7 +1268,6 @@ public: }; #define CYGWIN_FIFO_PIPE_NAME_LEN 47 -#define MAX_CLIENTS 64 /* The last three are the ones we try to read from. */ enum fifo_client_connect_state @@ -1351,8 +1350,9 @@ class fhandler_fifo: public fhandler_base UNICODE_STRING pipe_name; WCHAR pipe_name_buf[CYGWIN_FIFO_PIPE_NAME_LEN + 1]; bool _maybe_eof; - fifo_client_handler fc_handler[MAX_CLIENTS]; - int nhandlers; + fifo_client_handler *fc_handler; /* Dynamically growing array. */ + int shandlers; /* Size (capacity) of the array. */ + int nhandlers; /* Number of elements in the array. */ af_unix_spinlock_t _fifo_client_lock; bool reader, writer, duplexer; size_t max_atomic_write; diff --git a/winsup/cygwin/fhandler_fifo.cc b/winsup/cygwin/fhandler_fifo.cc index 0b9b33785..595e55ad9 100644 --- a/winsup/cygwin/fhandler_fifo.cc +++ b/winsup/cygwin/fhandler_fifo.cc @@ -70,7 +70,8 @@ static NO_COPY fifo_reader_id_t null_fr_id = { .winpid = 0, .fh = NULL }; fhandler_fifo::fhandler_fifo (): fhandler_base (), read_ready (NULL), write_ready (NULL), writer_opening (NULL), - cancel_evt (NULL), thr_sync_evt (NULL), _maybe_eof (false), nhandlers (0), + cancel_evt (NULL), thr_sync_evt (NULL), _maybe_eof (false), + fc_handler (NULL), shandlers (0), nhandlers (0), reader (false), writer (false), duplexer (false), max_atomic_write (DEFAULT_PIPEBUFSIZE), me (null_fr_id), shmem_handle (NULL), shmem (NULL) @@ -287,27 +288,28 @@ fhandler_fifo::wait_open_pipe (HANDLE& ph) int fhandler_fifo::add_client_handler () { - int ret = -1; fifo_client_handler fc; HANDLE ph = NULL; - if (nhandlers == MAX_CLIENTS) + if (nhandlers >= shandlers) { - set_errno (EMFILE); - goto out; + void *temp = realloc (fc_handler, + (shandlers += 64) * sizeof (fc_handler[0])); + if (!temp) + { + shandlers -= 64; + set_errno (ENOMEM); + return -1; + } + fc_handler = (fifo_client_handler *) temp; } ph = create_pipe_instance (); if (!ph) - goto out; - else - { - ret = 0; - fc.h = ph; - fc.state = fc_listening; - fc_handler[nhandlers++] = fc; - } -out: - return ret; + return -1; + fc.h = ph; + fc.state = fc_listening; + fc_handler[nhandlers++] = fc; + return 0; } void @@ -1067,10 +1069,10 @@ fhandler_fifo::close () NtClose (write_ready); if (writer_opening) NtClose (writer_opening); - fifo_client_lock (); for (int i = 0; i < nhandlers; i++) fc_handler[i].close (); - fifo_client_unlock (); + if (fc_handler) + free (fc_handler); return fhandler_base::close (); } @@ -1130,7 +1132,8 @@ fhandler_fifo::dup (fhandler_base *child, int flags) fhf->fifo_client_unlock (); /* Clear fc_handler list; the child never starts as owner. */ - fhf->nhandlers = 0; + fhf->nhandlers = fhf->shandlers = 0; + fhf->fc_handler = NULL; if (!DuplicateHandle (GetCurrentProcess (), shmem_handle, GetCurrentProcess (), &fhf->shmem_handle, @@ -1206,6 +1209,8 @@ fhandler_fifo::fixup_after_exec () if (reopen_shmem () < 0) api_fatal ("Can't reopen shared memory during exec, %E"); + fc_handler = NULL; + nhandlers = shandlers = 0; me.winpid = GetCurrentProcessId (); if (!(cancel_evt = create_event ())) api_fatal ("Can't create reader thread cancel event during exec, %E");