Skip to content

Commit

Permalink
tds: Use inline for tds_socket_init and tds_socket_done
Browse files Browse the repository at this point in the history
For historical reasons a lot of macros were used instead of
inline functions.
We already use in other places inline functions so it's no
more a portable issue.
The implementation of these function is small and we don't
need additional headers.
This allows reuse in code not linking all libtds library (like
ODBC tests).

Signed-off-by: Frediano Ziglio <[email protected]>
  • Loading branch information
freddy77 committed Dec 11, 2024
1 parent 5af8fae commit ef2e944
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 43 deletions.
36 changes: 25 additions & 11 deletions include/freetds/sysdep_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,19 @@ typedef int pid_t;
#define CLOSESOCKET(a) closesocket((a))
#define IOCTLSOCKET(a,b,c) ioctlsocket((a), (b), (c))
#define SOCKLEN_T int
TDS_EXTERN_C int tds_socket_init(void);
#define INITSOCKET() tds_socket_init()
TDS_EXTERN_C void tds_socket_done(void);
#define DONESOCKET() tds_socket_done()
static inline int
tds_socket_init(void)
{
WSADATA wsadata;

return WSAStartup(MAKEWORD(2, 2), &wsadata);
}

static inline void
tds_socket_done(void)
{
WSACleanup();
}
#define NETDB_REENTRANT 1 /* BSD-style netdb interface is reentrant */

#define TDSSOCK_EINTR WSAEINTR
Expand Down Expand Up @@ -153,13 +162,18 @@ typedef DWORD pid_t;
#define TDSSOCK_ECONNRESET ECONNRESET
#endif

#ifndef INITSOCKET
#define INITSOCKET() 0
#endif /* !INITSOCKET */

#ifndef DONESOCKET
#define DONESOCKET() do { } while(0)
#endif /* !DONESOCKET */
#ifndef _WIN32
static inline int
tds_socket_init(void)
{
return 0;
}

static inline void
tds_socket_done(void)
{
}
#endif

#ifndef READSOCKET
# ifdef MSG_NOSIGNAL
Expand Down
4 changes: 2 additions & 2 deletions src/apps/tsql.c
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,7 @@ main(int argc, char **argv)

istty = isatty(0);

if (INITSOCKET()) {
if (tds_socket_init()) {
fprintf(stderr, "Unable to initialize sockets\n");
return 1;
}
Expand Down Expand Up @@ -957,7 +957,7 @@ main(int argc, char **argv)
tds_free_socket(tds);
tds_free_login(login);
tds_free_context(context);
DONESOCKET();
tds_socket_done();

return 0;
}
5 changes: 1 addition & 4 deletions src/odbc/unittests/freeclose.c
Original file line number Diff line number Diff line change
Expand Up @@ -284,10 +284,7 @@ main(void)
const int num_inserts = 20;
int is_freetds;

#ifdef _WIN32
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);
#endif
tds_socket_init();

if (tds_mutex_init(&mtx))
return 1;
Expand Down
5 changes: 1 addition & 4 deletions src/odbc/unittests/timeout3.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,7 @@ main(void)
int port;
time_t start_time, end_time;

#ifdef _WIN32
WSADATA wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);
#endif
tds_socket_init();

if (tds_mutex_init(&mtx))
return 1;
Expand Down
6 changes: 3 additions & 3 deletions src/odbc/winsetup.c
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ ConfigDSN(HWND hwndParent, WORD fRequest, LPCSTR lpszDriver, LPCSTR lpszAttribut
* ConfigDSN() only looks up addresses and names, and never actually
* uses any sockets.
*/
INITSOCKET();
tds_socket_init();

/* Create a blank login struct */
di = alloc_dsninfo();
Expand Down Expand Up @@ -378,12 +378,12 @@ ConfigDSN(HWND hwndParent, WORD fRequest, LPCSTR lpszDriver, LPCSTR lpszAttribut

/* Clean up and return TRUE, indicating that the change took place */
free_dsninfo(di);
DONESOCKET();
tds_socket_done();
return TRUE;

Fail:
free_dsninfo(di);
DONESOCKET();
tds_socket_done();
return FALSE;
}

Expand Down
3 changes: 1 addition & 2 deletions src/tds/mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,6 @@ winsock_initialized(void)
static bool initialized = false;
static tds_mutex mtx = TDS_MUTEX_INITIALIZER;

WSADATA wsa_data;
int erc;

if (initialized)
Expand All @@ -701,7 +700,7 @@ winsock_initialized(void)
}

/* initialize the socket layer */
erc = WSAStartup(MAKEWORD(2, 2), &wsa_data);
erc = tds_socket_init();
initialized = (erc == 0);
tds_mutex_unlock(&mtx);

Expand Down
16 changes: 0 additions & 16 deletions src/tds/net.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,22 +106,6 @@ static void tds_check_cancel(TDSCONNECTION *conn);
* @{
*/

#ifdef _WIN32
int
tds_socket_init(void)
{
WSADATA wsadata;

return WSAStartup(MAKEWORD(2, 2), &wsadata);
}

void
tds_socket_done(void)
{
WSACleanup();
}
#endif

#if !defined(SOL_TCP) && (defined(IPPROTO_TCP) || defined(_WIN32))
/* fix incompatibility between MS headers */
# ifndef IPPROTO_TCP
Expand Down
2 changes: 1 addition & 1 deletion win32/initnet.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved TDS_UNUSED)
_CrtSetDbgFlag(_CRTDBG_CHECK_ALWAYS_DF | _CRTDBG_LEAK_CHECK_DF | _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG));
#endif

if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0)
if (tds_socket_init() != 0)
return FALSE;

DisableThreadLibraryCalls(hinstDLL);
Expand Down

0 comments on commit ef2e944

Please sign in to comment.