OSDN Git Service

*** empty log message ***
[drdeamon64/drdeamon64.git] / deamon / drd64_server_recvstatus.c
1 /*DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64
2
3                          D r . D e a m o n  6 4
4                         for INTEL64(R), AMD64(R)
5         
6    Copyright(C) 2007-2009 Koine Yuusuke(koinec). All rights reserved.
7
8 Redistribution and use in source and binary forms, with or without
9 modification, are permitted provided that the following conditions are met:
10
11  1. Redistributions of source code must retain the above copyright notice,
12     this list of conditions and the following disclaimer.
13  2. Redistributions in binary form must reproduce the above copyright
14     notice, this list of conditions and the following disclaimer in the
15     documentation and/or other materials provided with the distribution.
16
17 THIS SOFTWARE IS PROVIDED BY Koine Yuusuke(koinec) ``AS IS'' AND ANY
18 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 DISCLAIMED. IN NO EVENT SHALL Koine Yuusuke(koinec) OR CONTRIBUTORS BE
21 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
27 OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64DrDeAmOn64*/
30
31 /* File Info -----------------------------------------------------------
32 File: drd64_.c
33 Function: 
34 Comment: 
35 ----------------------------------------------------------------------*/
36
37 #include"drd64_server.h"
38 //#include"drd64_debugd.h"
39 //#include"drd64_debugd_info.h"
40 #define DRD64_SRC_SERVER_RECVSTATUS
41 #include"drd64_server_recvstatus.h"
42
43 Drd64_Server_RecvStatus         **gpp_recvstat;
44 Drd64_Server_RecvStatus         *gp_recvalloc;
45 int                                                     gi_recvalloc_max;
46 int                                                     gi_recvalloc_now;
47 int                                                     gi_recvstat_max;
48
49
50 EXTERN_SERVER_RECVSTATUS
51 int
52         Drd64_Server_RecvStatus_Init(
53                 int i_alloc_units )
54 {
55         int             i_cnt;
56         void    *pv_temp;
57
58         gpp_recvstat    = (Drd64_Server_RecvStatus **)malloc(
59                                         sizeof( Drd64_Server_RecvStatus *) * FD_SETSIZE );
60         if( NULL == gpp_recvstat )      {
61                 return 0x01;
62         }
63
64         gi_recvstat_max = 0;
65
66         gp_recvalloc = (Drd64_Server_RecvStatus *)malloc(
67                                                 sizeof( Drd64_Server_RecvStatus )
68                                                         * i_alloc_units );
69         if( NULL == gp_recvalloc )      {
70                 free( gpp_recvstat );
71                 gpp_recvstat    = NULL;
72                 return 0x02;
73         }
74
75         memset( gpp_recvstat, 0x00, 
76                                         sizeof( Drd64_Server_RecvStatus *) * FD_SETSIZE );
77         
78         memset( gp_recvalloc, 0x00, 
79                                                 sizeof( Drd64_Server_RecvStatus )
80                                                         * i_alloc_units );
81         for( i_cnt = 0; i_cnt < i_alloc_units; i_cnt++ )        {
82                 (gp_recvalloc + i_cnt)->i_fds_id        = -1;
83                 pv_temp = malloc( DRD64_MAX_PACKET_LENGTH );
84                 if( NULL == pv_temp )   { break; }
85                 
86                 (gp_recvalloc + i_cnt)->pv_buf  = pv_temp;
87         }
88
89         if( NULL == pv_temp     )       {
90                 for( ; i_cnt >= 0; i_cnt-- )    {
91                         free( (gp_recvalloc + i_cnt)->pv_buf );
92                         (gp_recvalloc + i_cnt)->pv_buf  = NULL;
93                 }
94                 free( gp_recvalloc );
95                 gi_recvalloc_max        = 0;
96                 gi_recvalloc_now        = 0;
97                 gp_recvalloc            = NULL;
98         
99                 free( gpp_recvstat );
100                 gpp_recvstat            = NULL;
101                 
102                 return 0x01;
103         }
104
105         gi_recvalloc_max        = i_alloc_units;
106         gi_recvalloc_now        = 0;
107
108         return 0x00;
109 }
110
111
112 EXTERN_SERVER_RECVSTATUS
113 int
114         Drd64_Server_RecvStatus_Term(
115                 void )
116 {
117         int             i_cnt;
118
119         for( i_cnt = 0; i_cnt < gi_recvalloc_max; i_cnt++ )     {
120                         free( (gp_recvalloc + i_cnt)->pv_buf );
121                         (gp_recvalloc + i_cnt)->pv_buf  = NULL;
122         }
123
124         free( gp_recvalloc );
125         gi_recvalloc_max        = 0;
126         gi_recvalloc_now        = 0;
127         gp_recvalloc            = NULL;
128
129         free( gpp_recvstat );
130         gpp_recvstat            = NULL;
131
132         return;
133 }
134
135
136 EXTERN_SERVER_RECVSTATUS
137 Drd64_Server_RecvStatus *
138         Drd64_Server_RecvStatus_AllocRecvStatus(
139                 int     i_fds,
140                 int     i_alloc_units )
141 {
142         int             i_cnt;
143         Drd64_Server_RecvStatus         *p_recv;
144         Drd64_Server_RecvStatus         *p_temp;
145
146         p_recv  = NULL;
147
148         if( gi_recvalloc_now >= gi_recvalloc_max )      {
149                 for( i_cnt = 0; i_cnt < gi_recvalloc_max; i_cnt++ )             {
150                         if( -1 == (gp_recvalloc + i_cnt)->i_fds_id )    {
151                                 p_recv  = (gp_recvalloc + i_cnt);
152                                 break;
153                         }
154                 }
155
156                 if( NULL == p_recv )    {
157                         p_temp  = realloc( gp_recvalloc,
158                                         gi_recvalloc_max + i_alloc_units );
159                         if( NULL == p_temp )    { return NULL; }
160                 
161                         for( i_cnt = gi_recvalloc_max;
162                                         i_cnt < i_alloc_units + gi_recvalloc_max;
163                                         i_cnt++ )       {
164                                 (gp_recvalloc + i_cnt)->i_fds_id        = -1;
165                         }
166
167                         gp_recvalloc    = p_temp;
168                         gi_recvalloc_max        += i_alloc_units;
169                         p_recv  = gp_recvalloc + gi_recvalloc_now++;
170                 }
171         }
172         else    {
173                 p_recv  = gp_recvalloc + gi_recvalloc_now++;
174         }
175
176         p_recv->i_fds_id        = i_fds;
177         *(gpp_recvstat + i_fds) = p_recv;
178         if( i_fds + 1 > gi_recvstat_max )
179                 { gi_recvstat_max       = i_fds + 1; }
180
181         return p_recv;
182 }
183
184
185 EXTERN_SERVER_RECVSTATUS
186 void
187         Drd64_Server_RecvStatus_FreeRecvStatus(
188                 int i_fds )
189 {
190         int             i_cnt;
191         Drd64_Server_RecvStatus *p_recv_now;
192         Drd64_Server_RecvStatus *p_recv_temp;
193
194         p_recv_now      = *(gpp_recvstat + i_fds);
195
196         if( NULL != p_recv_now )        {
197                 *(gpp_recvstat + i_fds) = NULL;
198
199                 if( i_fds + 1 == gi_recvstat_max )      {
200                         for( i_cnt = i_fds+1; i_cnt > 0; i_cnt-- )              {
201                                 p_recv_temp     = *(gpp_recvstat + i_cnt);
202                                 if( NULL != p_recv_temp )       {
203                                         gi_recvstat_max = i_cnt + 1;
204                                         break;
205                                 }
206                         }
207
208                         if( 0 == i_cnt ) { gi_recvstat_max      = 0; }
209                 }
210
211                 p_recv_now->i_fds_id    = -1;
212         }
213         
214         return;
215 }
216
217
218 EXTERN_SERVER_RECVSTATUS
219 Drd64_Server_RecvStatus *
220         Drd64_Server_RecvStatus_GetRecvStatus(
221                 int             i_fds )
222 {
223         return *(gpp_recvstat + i_fds);
224 }
225
226
227 EXTERN_SERVER_RECVSTATUS
228 int
229         Drd64_Server_RecvStatus_GetRecvStatMax(
230                 void )
231 {
232         return gi_recvstat_max;
233 }
234
235 /* EOF of drd64_.c ----------------------------------- */