Browse code

webkernel: integrate the in/out bufs into the client structures

Dario Rodriguez authored on 15/06/2014 15:13:14
Showing 1 changed files
... ...
@@ -100,6 +100,7 @@ wk_free(wk *paramweb)
100 100
         _wk *web=(_wk *)paramweb;
101 101
         wk_clientblock *cb;
102 102
         wk_client *client;
103
+        wk_bufblock *bb;
103 104
         if(web==NULL)
104 105
                 return;
105 106
         /* release server fds */
... ...
@@ -136,6 +137,19 @@ wk_free(wk *paramweb)
136 137
         web->sizeclientblocks=0;
137 138
         if(web->clientblocks!=NULL)
138 139
                 free(web->clientblocks),web->clientblocks=NULL;
140
+	/* release the used sbuf */
141
+        for(i=0;i<web->usedbufblocks;i++) {
142
+                bb=web->bufblocks+i;
143
+                if(bb->bufs==NULL)
144
+                        continue;
145
+                bb->usedbufs=0;
146
+                bb->sizebufs=0;
147
+                free(bb->bufs),bb->bufs=NULL;
148
+        }
149
+        web->usedbufblocks=0;
150
+        web->sizebufblocks=0;
151
+        if(web->bufblocks!=NULL)
152
+                free(web->bufblocks),web->bufblocks=NULL;
139 153
         /* free the main struct */
140 154
         free(web),web=NULL;
141 155
 }
... ...
@@ -335,7 +349,7 @@ wk_bufrelease(wk *paramweb, int sbufid)
335 349
         sbuf *buf;
336 350
         int numblock,j,bit;
337 351
         _wk *web=(_wk *)paramweb;
338
-        if(web==NULL)
352
+        if(web==NULL || sbufid<0)
339 353
                 return(-1);
340 354
         numblock=sbufid/BUFBLOCK;
341 355
         j=sbufid%BUFBLOCK;
... ...
@@ -393,6 +407,7 @@ wk_clientacquire(_wk *web)
393 407
         int i,j,k;
394 408
         wk_clientblock *cb;
395 409
         wk_client *client;
410
+        int w;
396 411
         /* make sure there are free clientblocks */
397 412
         if(web->usedclientblocks==web->sizeclientblocks) {
398 413
                 wk_clientblock *newcb;
... ...
@@ -439,6 +454,17 @@ wk_clientacquire(_wk *web)
439 454
         client->web=(wk *)web;
440 455
         client->connid=i*CLIENTBLOCK+j;
441 456
         client->fd=-1;
457
+        if((client->inbufid=wk_sbufacquire((wk *)web))==-1) {
458
+                wk_clientrelease(web,client->connid);
459
+                return(NULL); /* insufficient memory */
460
+        }
461
+        for(w=0;w<MAXOUTBUF;w++)
462
+                client->outbufids[w]=-1;
463
+        if((client->outbufids[0]=wk_sbufacquire((wk *)web))==-1) {
464
+                wk_clientrelease(web,client->connid);
465
+                return(NULL); /* insufficient memory */
466
+        }
467
+        client->usedoutbufids=1;
442 468
         return(client);
443 469
 }
444 470
 
... ...
@@ -447,6 +473,7 @@ wk_clientrelease(_wk *web, int connid)
447 473
 {
448 474
         wk_client *client;
449 475
         int numblock,j,bit;
476
+        int w;
450 477
         numblock=connid/CLIENTBLOCK;
451 478
         j=connid%CLIENTBLOCK;
452 479
         bit=0x1<<(j&3);
... ...
@@ -454,6 +481,10 @@ wk_clientrelease(_wk *web, int connid)
454 481
                 return(-1);
455 482
         web->clientblocks[numblock].usedclients--;
456 483
         web->clientblocks[numblock].acquired[j>>3]&=(~bit);
484
+        if(client->inbufid!=-1)
485
+                wk_sbufrelease((wk *)web,client->inbufid),client->inbufid=-1;
486
+        for(w=0;w<client->usedoutbufids;w++) 
487
+                wk_sbufrelease((wk *)web,client->outbufids[w]),client->outbufids[w]=-1;
457 488
         memset(client,0,sizeof(wk_client));
458 489
         return(0);
459 490
 }