Browse code

tcp: minor optimization

- tcp_req complete & has_content_len transformed into flags
(4 bytes saved per connection)

Andrei Pelinescu-Onciul authored on 20/03/2009 19:59:14
Showing 2 changed files
... ...
@@ -114,13 +114,18 @@ struct tcp_req{
114 114
 	char* body; /* body position */
115 115
 	unsigned int b_size; /* buffer size-1 (extra space for 0-term)*/
116 116
 	int content_len;
117
-	int has_content_len; /* 1 if content_length was parsed ok*/
118
-	int complete; /* 1 if one req has been fully read, 0 otherwise*/
117
+	unsigned short flags; /* F_TCP_REQ_HAS_CLEN | F_TCP_REQ_COMPLETE */
119 118
 	int bytes_to_go; /* how many bytes we have still to read from the body*/
120 119
 	enum tcp_req_errors error;
121 120
 	enum tcp_req_states state;
122 121
 };
123 122
 
123
+/* tcp_req flags */
124
+#define F_TCP_REQ_HAS_CLEN 1
125
+#define F_TCP_REQ_COMPLETE 2
126
+
127
+#define TCP_REQ_HAS_CLEN(tr)  ((tr)->flags & F_TCP_REQ_HAS_CLEN)
128
+#define TCP_REQ_COMPLETE(tr)  ((tr)->flags & F_TCP_REQ_COMPLETE)
124 129
 
125 130
 
126 131
 struct tcp_connection;
... ...
@@ -218,19 +218,19 @@ int tcp_read_headers(struct tcp_connection *c, int* read_flags)
218 218
 	#define content_len_beg_case \
219 219
 					case ' ': \
220 220
 					case '\t': \
221
-						if (!r->has_content_len) r->state=H_STARTWS; \
221
+						if (!TCP_REQ_HAS_CLEN(r)) r->state=H_STARTWS; \
222 222
 						else r->state=H_SKIP; \
223 223
 							/* not interested if we already found one */ \
224 224
 						break; \
225 225
 					case 'C': \
226 226
 					case 'c': \
227
-						if(!r->has_content_len) r->state=H_CONT_LEN1; \
227
+						if(!TCP_REQ_HAS_CLEN(r)) r->state=H_CONT_LEN1; \
228 228
 						else r->state=H_SKIP; \
229 229
 						break; \
230 230
 					case 'l': \
231 231
 					case 'L': \
232 232
 						/* short form for Content-Length */ \
233
-						if (!r->has_content_len) r->state=H_L_COLON; \
233
+						if (!TCP_REQ_HAS_CLEN(r)) r->state=H_L_COLON; \
234 234
 						else r->state=H_SKIP; \
235 235
 						break
236 236
 						
... ...
@@ -272,7 +272,7 @@ int tcp_read_headers(struct tcp_connection *c, int* read_flags)
272 272
 				r->bytes_to_go-=remaining;
273 273
 				p+=remaining;
274 274
 				if (r->bytes_to_go==0){
275
-					r->complete=1;
275
+					r->flags|=F_TCP_REQ_COMPLETE;
276 276
 					goto skip;
277 277
 				}
278 278
 				break;
... ...
@@ -298,11 +298,11 @@ int tcp_read_headers(struct tcp_connection *c, int* read_flags)
298 298
 					case '\n':
299 299
 						/* found LF LF */
300 300
 						r->state=H_BODY;
301
-						if (r->has_content_len){
301
+						if (TCP_REQ_HAS_CLEN(r)){
302 302
 							r->body=p+1;
303 303
 							r->bytes_to_go=r->content_len;
304 304
 							if (r->bytes_to_go==0){
305
-								r->complete=1;
305
+								r->flags|=F_TCP_REQ_COMPLETE;
306 306
 								p++;
307 307
 								goto skip;
308 308
 							}
... ...
@@ -322,11 +322,11 @@ int tcp_read_headers(struct tcp_connection *c, int* read_flags)
322 322
 				if (*p=='\n'){
323 323
 					/* found LF CR LF */
324 324
 					r->state=H_BODY;
325
-					if (r->has_content_len){
325
+					if (TCP_REQ_HAS_CLEN(r)){
326 326
 						r->body=p+1;
327 327
 						r->bytes_to_go=r->content_len;
328 328
 						if (r->bytes_to_go==0){
329
-							r->complete=1;
329
+							r->flags|=F_TCP_REQ_COMPLETE;
330 330
 							p++;
331 331
 							goto skip;
332 332
 						}
... ...
@@ -410,8 +410,8 @@ int tcp_read_headers(struct tcp_connection *c, int* read_flags)
410 410
 			case H_SKIP_EMPTY_CRLFCR_FOUND:
411 411
 				if (*p=='\n'){
412 412
 					r->state = H_PING_CRLF;
413
-					r->complete = 1;
414
-					r->has_content_len = 1; /* hack to avoid error check */
413
+					r->flags |= F_TCP_REQ_HAS_CLEN |
414
+							F_TCP_REQ_COMPLETE; /* hack to avoid error check */
415 415
 					p++;
416 416
 					goto skip;
417 417
 				}else{
... ...
@@ -437,7 +437,7 @@ int tcp_read_headers(struct tcp_connection *c, int* read_flags)
437 437
 					/* using has_content_len as a flag if there should be
438 438
 					 * fingerprint or no
439 439
 					 */
440
-					r->has_content_len = (mc == MAGIC_COOKIE) ? 1 : 0;
440
+					r->flags |= (mc == MAGIC_COOKIE) ? F_TCP_REQ_HAS_CLEN : 0;
441 441
 					
442 442
 					r->body += sizeof(struct stun_hdr);
443 443
 					p = r->body; 
... ...
@@ -485,8 +485,8 @@ int tcp_read_headers(struct tcp_connection *c, int* read_flags)
485 485
 					r->body += body_len;
486 486
 					p = r->body;
487 487
 					r->state = H_STUN_END;
488
-					r->complete = 1;
489
-					r->has_content_len = 1; /* hack to avoid error check */
488
+					r->flags |= F_TCP_REQ_COMPLETE |
489
+						F_TCP_REQ_HAS_CLEN; /* hack to avoid error check */
490 490
 					goto skip;
491 491
 				}
492 492
 				else {
... ...
@@ -563,12 +563,12 @@ int tcp_read_headers(struct tcp_connection *c, int* read_flags)
563 563
 					case ' ':
564 564
 					case '\t': /* FIXME: check if line contains only WS */
565 565
 						r->state=H_SKIP;
566
-						r->has_content_len=1;
566
+						r->flags|=F_TCP_REQ_HAS_CLEN;
567 567
 						break;
568 568
 					case '\n':
569 569
 						/* end of line, parse successful */
570 570
 						r->state=H_LF;
571
-						r->has_content_len=1;
571
+						r->flags|=F_TCP_REQ_HAS_CLEN;
572 572
 						break;
573 573
 					default:
574 574
 						LOG(L_ERR, "ERROR: tcp_read_headers: bad "
... ...
@@ -643,7 +643,8 @@ again:
643 643
 			 * if req. is complete we might have a second unparsed
644 644
 			 * request after it, so postpone release_with_eof
645 645
 			 */
646
-			if (unlikely((con->state==S_CONN_EOF) && (req->complete==0))) {
646
+			if (unlikely((con->state==S_CONN_EOF) && 
647
+						(! TCP_REQ_COMPLETE(req)))) {
647 648
 				DBG( "tcp_read_req: EOF\n");
648 649
 				resp=CONN_EOF;
649 650
 				goto end_req;
... ...
@@ -660,7 +661,7 @@ again:
660 660
 			resp=CONN_ERROR;
661 661
 			goto end_req;
662 662
 		}
663
-		if (likely(req->complete)){
663
+		if (likely(TCP_REQ_COMPLETE(req))){
664 664
 #ifdef EXTRA_DEBUG
665 665
 			DBG("tcp_read_req: end of header part\n");
666 666
 			DBG("- received from: port %d\n", con->rcv.src_port);
... ...
@@ -668,7 +669,7 @@ again:
668 668
 			DBG("tcp_read_req: headers:\n%.*s.\n",
669 669
 					(int)(req->body-req->start), req->start);
670 670
 #endif
671
-			if (likely(req->has_content_len)){
671
+			if (likely(TCP_REQ_HAS_CLEN(req))){
672 672
 				DBG("tcp_read_req: content-length= %d\n", req->content_len);
673 673
 #ifdef EXTRA_DEBUG
674 674
 				DBG("tcp_read_req: body:\n%.*s\n", req->content_len,req->body);
... ...
@@ -733,7 +734,8 @@ again:
733 733
 			req->body=0;
734 734
 			req->error=TCP_REQ_OK;
735 735
 			req->state=H_SKIP_EMPTY;
736
-			req->complete=req->content_len=req->has_content_len=0;
736
+			req->flags=0;
737
+			req->content_len=0;
737 738
 			req->bytes_to_go=0;
738 739
 			req->pos=req->buf+size;
739 740
 			
... ...
@@ -1085,15 +1087,15 @@ error:
1085 1085
 #ifdef USE_STUN
1086 1086
 int is_msg_complete(struct tcp_req* r)
1087 1087
 {
1088
-	if (r->has_content_len == 1) {
1088
+	if (TCP_REQ_HAS_CLEN(r)) {
1089 1089
 		r->state = H_STUN_FP;
1090 1090
 		return 0;
1091 1091
 	}
1092 1092
 	else {
1093 1093
 		/* STUN message is complete */
1094 1094
 		r->state = H_STUN_END;
1095
-		r->complete = 1;
1096
-		r->has_content_len = 1; /* hack to avoid error check */
1095
+		r->flags |= F_TCP_REQ_COMPLETE |
1096
+					F_TCP_REQ_HAS_CLEN; /* hack to avoid error check */
1097 1097
 		return 1;
1098 1098
 	}
1099 1099
 }