OSDN Git Service

Fix aliasing bug that also caused memory usage problems.
[pf3gnuchains/gcc-fork.git] / gcc / ada / socket.c
1 /****************************************************************************
2  *                                                                          *
3  *                         GNAT COMPILER COMPONENTS                         *
4  *                                                                          *
5  *                              S O C K E T                                 *
6  *                                                                          *
7  *                          C Implementation File                           *
8  *                                                                          *
9  *            Copyright (C) 2003, Free Software Foundation, Inc.            *
10  *                                                                          *
11  * GNAT is free software;  you can  redistribute it  and/or modify it under *
12  * terms of the  GNU General Public License as published  by the Free Soft- *
13  * ware  Foundation;  either version 2,  or (at your option) any later ver- *
14  * sion.  GNAT is distributed in the hope that it will be useful, but WITH- *
15  * OUT ANY WARRANTY;  without even the  implied warranty of MERCHANTABILITY *
16  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License *
17  * for  more details.  You should have  received  a copy of the GNU General *
18  * Public License  distributed with GNAT;  see file COPYING.  If not, write *
19  * to  the Free Software Foundation,  59 Temple Place - Suite 330,  Boston, *
20  * MA 02111-1307, USA.                                                      *
21  *                                                                          *
22  * As a  special  exception,  if you  link  this file  with other  files to *
23  * produce an executable,  this file does not by itself cause the resulting *
24  * executable to be covered by the GNU General Public License. This except- *
25  * ion does not  however invalidate  any other reasons  why the  executable *
26  * file might be covered by the  GNU Public License.                        *
27  *                                                                          *
28  * GNAT was originally developed  by the GNAT team at  New York University. *
29  * Extensive contributions were provided by Ada Core Technologies Inc.      *
30  *                                                                          *
31  ****************************************************************************/
32
33 /*  This file provides a portable binding to the fd set functions           */
34
35 #ifdef __vxworks
36 #include "vxWorks.h"
37 #endif
38
39 #ifdef IN_RTS
40 #include "tconfig.h"
41 #include "tsystem.h"
42
43 #if defined (WINNT)
44 #define FD_SETSIZE 1024
45 #include <windows.h>
46
47 #ifdef __MINGW32__
48 #include "mingw32.h"
49 #if STD_MINGW
50 #include <winsock.h>
51 #else
52 #include <windows32/sockets.h>
53 #endif
54 #endif
55 #endif
56
57 #if defined (VMS)
58 #define FD_SETSIZE 4096
59 #include <sys/time.h>
60 #endif
61
62 #else
63 #include "config.h"
64 #include "system.h"
65 #endif
66
67 #include "raise.h"
68
69 extern void __gnat_free_socket_set (fd_set *);
70 extern void __gnat_last_socket_in_set (fd_set *, int *);
71 extern void __gnat_get_socket_from_set (fd_set *, int *, int *);
72 extern void __gnat_insert_socket_in_set (fd_set *, int);
73 extern int __gnat_is_socket_in_set (fd_set *, int);
74 extern fd_set *__gnat_new_socket_set (fd_set *);
75 extern void __gnat_remove_socket_from_set (fd_set *, int);
76 \f
77 /* Free socket set. */
78
79 void
80 __gnat_free_socket_set (fd_set *set)
81 {
82   __gnat_free (set);
83 }
84
85 /* Find the largest socket in the socket set SET. This is needed for
86    `select'.  LAST is the maximum value for the largest socket. This hint is
87    used to avoid scanning very large socket sets.  On return, LAST is the
88    actual largest socket in the socket set. */
89
90 void
91 __gnat_last_socket_in_set (fd_set *set, int *last)
92 {
93   int s;
94   int l;
95   l = -1;
96
97 #ifdef WINNT
98   /* More efficient method for NT. */
99   for (s = 0; s < set->fd_count; s++)
100     if ((int) set->fd_array[s] > l)
101       l = set->fd_array[s];
102
103 #else
104
105   for (s = *last; s != -1; s--)
106     if (FD_ISSET (s, set))
107       {
108         l = s;
109         break;
110       }
111 #endif
112
113   *last = l;
114 }
115
116 /* Get last socket and remove it from the socket set SET.  LAST is the
117    maximum value of the largest socket.  This hint is used to avoid scanning
118    very large socket sets.  On return, LAST is set to the actual largest
119    socket in the socket set. */
120
121 void
122 __gnat_get_socket_from_set (fd_set *set, int *last, int *socket)
123 {
124   *socket = *last;
125   FD_CLR (*socket, set);
126   __gnat_last_socket_in_set (set, last);
127 }
128
129 /* Insert SOCKET in the socket set SET. */
130
131 void
132 __gnat_insert_socket_in_set (fd_set *set, int socket)
133 {
134   FD_SET (socket, set);
135 }
136
137 /* Check whether a given SOCKET is in the socket set SET. */
138
139 int
140 __gnat_is_socket_in_set (fd_set *set, int socket)
141 {
142   return FD_ISSET (socket, set);
143 }
144
145 /* Allocate a new socket set and set it as empty.  */
146
147 fd_set *
148 __gnat_new_socket_set (fd_set *set)
149 {
150   fd_set *new;
151
152   new = (fd_set *) __gnat_malloc (sizeof (fd_set));
153
154   if (set)
155     memcpy (new, set, sizeof (fd_set));
156   else
157     FD_ZERO (new);
158
159   return new;
160 }
161
162 /* Remove SOCKET from the socket set SET. */
163
164 void
165 __gnat_remove_socket_from_set (fd_set *set, int socket)
166 {
167   FD_CLR (socket, set);
168 }