883a9bc62e81e0a8f8bebfae2549219b15476ddf
[mirror/winof/.git] / ulp / wsd / user / sockinfo.c
1 /*\r
2  * Copyright (c) 2005 SilverStorm Technologies.  All rights reserved.\r
3  *\r
4  * This software is available to you under the OpenIB.org BSD license\r
5  * below:\r
6  *\r
7  *     Redistribution and use in source and binary forms, with or\r
8  *     without modification, are permitted provided that the following\r
9  *     conditions are met:\r
10  *\r
11  *      - Redistributions of source code must retain the above\r
12  *        copyright notice, this list of conditions and the following\r
13  *        disclaimer.\r
14  *\r
15  *      - Redistributions in binary form must reproduce the above\r
16  *        copyright notice, this list of conditions and the following\r
17  *        disclaimer in the documentation and/or other materials\r
18  *        provided with the distribution.\r
19  *\r
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,\r
21  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF\r
22  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND\r
23  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS\r
24  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN\r
25  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN\r
26  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\r
27  * SOFTWARE.\r
28  *\r
29  * $Id$\r
30  */\r
31 \r
32 #include "ibspdll.h"\r
33 \r
34 \r
35 /* \r
36  * Function: create_socket_info\r
37  * \r
38  *  Description:\r
39  *    Allocates a new socket info context structure and initializes some fields.\r
40 */\r
41 struct ibsp_socket_info *\r
42 create_socket_info(void)\r
43 {\r
44         struct ibsp_socket_info *socket_info;\r
45 \r
46         IBSP_ENTER( IBSP_DBG_SI );\r
47 \r
48         socket_info = HeapAlloc( g_ibsp.heap,\r
49                 HEAP_ZERO_MEMORY, sizeof(struct ibsp_socket_info) );\r
50         if( socket_info == NULL )\r
51         {\r
52                 IBSP_TRACE_EXIT( IBSP_DBG_SI,\r
53                         ("HeapAlloc() failed: %d\n", GetLastError()) );\r
54                 return NULL;\r
55         }\r
56 \r
57         cl_spinlock_init( &socket_info->mutex );\r
58 \r
59         cl_qlist_init( &socket_info->buf_mem_list.list );\r
60         cl_spinlock_init( &socket_info->buf_mem_list.mutex );\r
61 \r
62         cl_qlist_init( &socket_info->listen.list );\r
63 \r
64         cl_spinlock_init( &socket_info->send_lock );\r
65         cl_spinlock_init( &socket_info->recv_lock );\r
66 \r
67 #ifdef _DEBUG_\r
68         memset( socket_info->recv_wr, 0x38, sizeof(socket_info->send_wr) );\r
69         memset( socket_info->recv_wr, 0x38, sizeof(socket_info->recv_wr) );\r
70         memset( socket_info->recv_wr, 0x38, sizeof(socket_info->dup_wr) );\r
71 #endif\r
72 \r
73         socket_info->switch_socket = INVALID_SOCKET;\r
74         /*\r
75          * Preset to 1, IBSPCloseSocket will decrement it, and switch socket\r
76          * will be freed once it goes to zero.\r
77          */\r
78         socket_info->ref_cnt = 1;\r
79 \r
80         IBSP_TRACE( IBSP_DBG_SI, ("socket_info (%p)\n", socket_info) );\r
81 \r
82         IBSP_EXIT( IBSP_DBG_SI );\r
83         return socket_info;\r
84 }\r
85 \r
86 \r
87 /* \r
88  * Function: deref_sock_info\r
89  * \r
90  * Description:\r
91  *      This routine decrements a socket context's reference count, and if\r
92  *      it reaches zero, frees the socket context structure.\r
93  */\r
94 void AL_API\r
95 deref_socket_info(\r
96         IN                              struct ibsp_socket_info         *p_socket )\r
97 {\r
98         int             ret, error;\r
99 \r
100         IBSP_ENTER( IBSP_DBG_SI );\r
101 \r
102         if( !cl_atomic_dec( &p_socket->ref_cnt ) )\r
103         {\r
104                 if( p_socket->switch_socket != INVALID_SOCKET )\r
105                 {\r
106                         /* ref_cnt hit zero - destroy the switch socket. */\r
107                         IBSP_TRACE1( IBSP_DBG_SI,\r
108                                 ("socket=0x%p calling lpWPUCloseSocketHandle=0x%p\n",\r
109                                 p_socket, p_socket->switch_socket) );\r
110 \r
111                         ret = g_ibsp.up_call_table.lpWPUCloseSocketHandle(\r
112                                 p_socket->switch_socket, &error );\r
113                         if( ret == SOCKET_ERROR )\r
114                         {\r
115                                 IBSP_ERROR( ("WPUCloseSocketHandle failed: %d\n", error) );\r
116                         }\r
117                         else\r
118                         {\r
119                                 STAT_DEC( wpusocket_num );\r
120                         }\r
121 \r
122                         p_socket->switch_socket = INVALID_SOCKET;\r
123                 }\r
124 \r
125                 CL_ASSERT( !p_socket->qp );\r
126                 CL_ASSERT( !p_socket->conn_item.p_map );\r
127 \r
128                 cl_spinlock_destroy( &p_socket->buf_mem_list.mutex );\r
129                 cl_spinlock_destroy( &p_socket->mutex );\r
130 \r
131                 cl_spinlock_destroy( &p_socket->send_lock );\r
132                 cl_spinlock_destroy( &p_socket->recv_lock );\r
133 \r
134                 HeapFree( g_ibsp.heap, 0, p_socket );\r
135 \r
136                 IBSP_TRACE( IBSP_DBG_SI, ("Freed socket_info (%p)\n", p_socket) );\r
137         }\r
138         IBSP_EXIT( IBSP_DBG_SI );\r
139 }\r