public inbox for cygwin-cvs@sourceware.org
help / color / mirror / Atom feed
From: Corinna Vinschen <corinna@sourceware.org>
To: cygwin-cvs@sourceware.org
Subject: [newlib-cygwin] Cygwin: net.cc: convert wsock_errmap into a simple array of error codes
Date: Thu,  4 Aug 2022 10:56:37 +0000 (GMT)	[thread overview]
Message-ID: <20220804105637.31FCF385736F@sourceware.org> (raw)

https://sourceware.org/git/gitweb.cgi?p=newlib-cygwin.git;h=70c7e8c1baaf5aa3a1e7327cd21463f4fa5b336b

commit 70c7e8c1baaf5aa3a1e7327cd21463f4fa5b336b
Author: Corinna Vinschen <corinna@vinschen.de>
Date:   Thu Aug 4 11:45:58 2022 +0200

    Cygwin: net.cc: convert wsock_errmap into a simple array of error codes
    
    Avoid linear searches for error codes by converting wsock_errmap
    to a ordered array, indexed by WinSock error code (subtracted by
    WSABASEERR.
    
    Signed-off-by: Corinna Vinschen <corinna@vinschen.de>

Diff:
---
 winsup/cygwin/net.cc | 134 +++++++++++++++++++++++++++++++--------------------
 1 file changed, 82 insertions(+), 52 deletions(-)

diff --git a/winsup/cygwin/net.cc b/winsup/cygwin/net.cc
index 83ea37f6e..8840d5ead 100644
--- a/winsup/cygwin/net.cc
+++ b/winsup/cygwin/net.cc
@@ -134,63 +134,93 @@ inet_makeaddr (int net, int lna)
   return in;
 }
 
-static const errmap_t wsock_errmap[] = {
-  {WSA_INVALID_HANDLE, "WSA_INVALID_HANDLE", EBADF},
-  {WSA_NOT_ENOUGH_MEMORY, "WSA_NOT_ENOUGH_MEMORY", ENOMEM},
-  {WSA_INVALID_PARAMETER, "WSA_INVALID_PARAMETER", EINVAL},
-  {WSAEINTR, "WSAEINTR", EINTR},
-  {WSAEWOULDBLOCK, "WSAEWOULDBLOCK", EWOULDBLOCK},
-  {WSAEINPROGRESS, "WSAEINPROGRESS", EINPROGRESS},
-  {WSAEALREADY, "WSAEALREADY", EALREADY},
-  {WSAENOTSOCK, "WSAENOTSOCK", ENOTSOCK},
-  {WSAEDESTADDRREQ, "WSAEDESTADDRREQ", EDESTADDRREQ},
-  {WSAEMSGSIZE, "WSAEMSGSIZE", EMSGSIZE},
-  {WSAEPROTOTYPE, "WSAEPROTOTYPE", EPROTOTYPE},
-  {WSAENOPROTOOPT, "WSAENOPROTOOPT", ENOPROTOOPT},
-  {WSAEPROTONOSUPPORT, "WSAEPROTONOSUPPORT", EPROTONOSUPPORT},
-  {WSAESOCKTNOSUPPORT, "WSAESOCKTNOSUPPORT", ESOCKTNOSUPPORT},
-  {WSAEOPNOTSUPP, "WSAEOPNOTSUPP", EOPNOTSUPP},
-  {WSAEPFNOSUPPORT, "WSAEPFNOSUPPORT", EPFNOSUPPORT},
-  {WSAEAFNOSUPPORT, "WSAEAFNOSUPPORT", EAFNOSUPPORT},
-  {WSAEADDRINUSE, "WSAEADDRINUSE", EADDRINUSE},
-  {WSAEADDRNOTAVAIL, "WSAEADDRNOTAVAIL", EADDRNOTAVAIL},
-  {WSAENETDOWN, "WSAENETDOWN", ENETDOWN},
-  {WSAENETUNREACH, "WSAENETUNREACH", ENETUNREACH},
-  {WSAENETRESET, "WSAENETRESET", ENETRESET},
-  {WSAECONNABORTED, "WSAECONNABORTED", ECONNABORTED},
-  {WSAECONNRESET, "WSAECONNRESET", ECONNRESET},
-  {WSAENOBUFS, "WSAENOBUFS", ENOBUFS},
-  {WSAEISCONN, "WSAEISCONN", EISCONN},
-  {WSAENOTCONN, "WSAENOTCONN", ENOTCONN},
-  {WSAESHUTDOWN, "WSAESHUTDOWN", ESHUTDOWN},
-  {WSAETOOMANYREFS, "WSAETOOMANYREFS", ETOOMANYREFS},
-  {WSAETIMEDOUT, "WSAETIMEDOUT", ETIMEDOUT},
-  {WSAECONNREFUSED, "WSAECONNREFUSED", ECONNREFUSED},
-  {WSAELOOP, "WSAELOOP", ELOOP},
-  {WSAENAMETOOLONG, "WSAENAMETOOLONG", ENAMETOOLONG},
-  {WSAEHOSTDOWN, "WSAEHOSTDOWN", EHOSTDOWN},
-  {WSAEHOSTUNREACH, "WSAEHOSTUNREACH", EHOSTUNREACH},
-  {WSAENOTEMPTY, "WSAENOTEMPTY", ENOTEMPTY},
-  {WSAEPROCLIM, "WSAEPROCLIM", EPROCLIM},
-  {WSAEUSERS, "WSAEUSERS", EUSERS},
-  {WSAEDQUOT, "WSAEDQUOT", EDQUOT},
-  {WSAESTALE, "WSAESTALE", ESTALE},
-  {WSAEREMOTE, "WSAEREMOTE", EREMOTE},
-  {WSAEINVAL, "WSAEINVAL", EINVAL},
-  {WSAEFAULT, "WSAEFAULT", EFAULT},
-  {WSAEBADF, "WSAEBADF", EBADF},
-  {WSAEACCES, "WSAEACCES", EACCES},
-  {WSAEMFILE, "WSAEMFILE", EMFILE},
-  {0, "NOERROR", 0},
-  {0, NULL, 0}
+static const int wsock_errmap[] =
+{
+  0,			/* WSABASEERR (10000) */
+  0,			/* 10001 */
+  0,			/* 10002 */
+  0,			/* 10003 */
+  EINTR,		/* WSAEINTR */
+  0,			/* 10005 */
+  0,			/* 10006 */
+  0,			/* 10007 */
+  0,			/* 10008 */
+  EBADF,		/* WSAEBADF */
+  0,			/* 10010 */
+  0,			/* 10011 */
+  0,			/* 10012 */
+  EACCES,		/* WSAEACCES */
+  EFAULT,		/* WSAEFAULT */
+  0,			/* 10015 */
+  0,			/* 10016 */
+  0,			/* 10017 */
+  0,			/* 10018 */
+  0,			/* 10019 */
+  0,			/* 10020 */
+  0,			/* 10021 */
+  EINVAL,		/* WSAEINVAL */
+  0,			/* 10023 */
+  EMFILE,		/* WSAEMFILE */
+  0,			/* 10025 */
+  0,			/* 10026 */
+  0,			/* 10027 */
+  0,			/* 10028 */
+  0,			/* 10029 */
+  0,			/* 10030 */
+  0,			/* 10031 */
+  0,			/* 10032 */
+  0,			/* 10033 */
+  0,			/* 10034 */
+  EWOULDBLOCK,		/* WSAEWOULDBLOCK */
+  EINPROGRESS,		/* WSAEINPROGRESS */
+  EALREADY,		/* WSAEALREADY */
+  ENOTSOCK,		/* WSAENOTSOCK */
+  EDESTADDRREQ,		/* WSAEDESTADDRREQ */
+  EMSGSIZE,		/* WSAEMSGSIZE */
+  EPROTOTYPE,		/* WSAEPROTOTYPE */
+  ENOPROTOOPT,		/* WSAENOPROTOOPT */
+  EPROTONOSUPPORT,	/* WSAEPROTONOSUPPORT */
+  ESOCKTNOSUPPORT,	/* WSAESOCKTNOSUPPORT */
+  EOPNOTSUPP,		/* WSAEOPNOTSUPP */
+  EPFNOSUPPORT,		/* WSAEPFNOSUPPORT */
+  EAFNOSUPPORT,		/* WSAEAFNOSUPPORT */
+  EADDRINUSE,		/* WSAEADDRINUSE */
+  EADDRNOTAVAIL,	/* WSAEADDRNOTAVAIL */
+  ENETDOWN,		/* WSAENETDOWN */
+  ENETUNREACH,		/* WSAENETUNREACH */
+  ENETRESET,		/* WSAENETRESET */
+  ECONNABORTED,		/* WSAECONNABORTED */
+  ECONNRESET,		/* WSAECONNRESET */
+  ENOBUFS,		/* WSAENOBUFS */
+  EISCONN,		/* WSAEISCONN */
+  ENOTCONN,		/* WSAENOTCONN */
+  ESHUTDOWN,		/* WSAESHUTDOWN */
+  ETOOMANYREFS,		/* WSAETOOMANYREFS */
+  ETIMEDOUT,		/* WSAETIMEDOUT */
+  ECONNREFUSED,		/* WSAECONNREFUSED */
+  ELOOP,		/* WSAELOOP */
+  ENAMETOOLONG,		/* WSAENAMETOOLONG */
+  EHOSTDOWN,		/* WSAEHOSTDOWN */
+  EHOSTUNREACH,		/* WSAEHOSTUNREACH */
+  ENOTEMPTY,		/* WSAENOTEMPTY */
+  EPROCLIM,		/* WSAEPROCLIM */
+  EUSERS,		/* WSAEUSERS */
+  EDQUOT,		/* WSAEDQUOT */
+  ESTALE,		/* WSAESTALE */
+  EREMOTE,		/* WSAEREMOTE */
 };
 
 int
 find_winsock_errno (DWORD why)
 {
-  for (int i = 0; wsock_errmap[i].s != NULL; ++i)
-    if (why == wsock_errmap[i].w)
-      return wsock_errmap[i].e;
+  if (!why)
+    return 0;
+  if (why < WSABASEERR)
+    geterrno_from_win_error (why, EACCES);
+
+  why -= WSABASEERR;
+  if (why < sizeof wsock_errmap / sizeof wsock_errmap[0])
+    return wsock_errmap[why];
 
   return EACCES;
 }


                 reply	other threads:[~2022-08-04 10:56 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220804105637.31FCF385736F@sourceware.org \
    --to=corinna@sourceware.org \
    --cc=cygwin-cvs@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).