public
Description: A very fast & simple Ruby web server
Homepage: http://code.macournoyer.com/thin/
Clone URL: git://github.com/macournoyer/thin.git
Search Repo:
Grow buffer when sending headers
macournoyer (author)
Mon Apr 07 20:27:34 -0700 2008
commit  c9f36a52fccbd001b378f4769d07301498686c5e
tree    9b8a0cfd78c09e8b0c51c207f80bef89e56eed83
parent  05fc8d27d9f6edc6d03c1ba841a2aa04dbbe5205
...
19
20
21
22
 
23
 
 
 
 
 
24
25
 
26
27
28
 
29
30
31
 
32
33
34
35
36
37
38
 
 
 
39
40
41
42
43
44
45
46
47
 
48
49
50
...
19
20
21
 
22
23
24
25
26
27
28
29
 
30
31
32
 
33
34
35
 
36
37
38
39
40
 
 
 
41
42
43
44
45
46
47
48
49
 
 
 
50
51
52
53
0
@@ -19,32 +19,35 @@
0
   buf->nalloc = 0;
0
 }
0
 
0
-static inline void buffer_grow(buffer_t *buf, size_t size)
0
+void buffer_grow(buffer_t *buf, size_t len)
0
 {
0
+ size_t new_len = buf->len + len;
0
+
0
+ if (new_len <= buf->salloc)
0
+ return;
0
+
0
   char *new, *old;
0
- size_t num = (size_t) (size + 0.5) / (float) buf->pool->size;
0
+ size_t num = (size_t) (new_len + 0.5) / (float) buf->pool->size;
0
   
0
   /* TODO store big body in tempfile */
0
- /* TODO if last alloc, just alloc next block */
0
+ /* TODO if free space in next blocks, alloc more and don't memcpy */
0
   
0
   old = buf->ptr;
0
- new = (char *) palloc(buf->pool, buf->nalloc + num);
0
+ new = (char *) palloc(buf->pool, num);
0
   assert(new);
0
   
0
   memcpy(new, old, buf->len);
0
 
0
- buf->ptr = new;
0
- buf->nalloc += num;
0
- buf->salloc += buf->pool->size * num;
0
+ buf->ptr = new;
0
+ buf->nalloc = num;
0
+ buf->salloc = buf->pool->size * num;
0
   
0
   pfree(buf->pool, old);
0
 }
0
 
0
 void buffer_append(buffer_t *buf, const char *ptr, size_t len)
0
 {
0
- /* alloc more mem when buffer full */
0
- if (buf->len + len > buf->salloc)
0
- buffer_grow(buf, len);
0
+ buffer_grow(buf, len);
0
   
0
   memcpy(buf->ptr + buf->len, ptr, len);
0
   buf->len += len;
...
16
17
18
 
19
20
21
...
16
17
18
19
20
21
22
0
@@ -16,6 +16,7 @@
0
 
0
 void buffer_init(buffer_t *buf, pool_t *p);
0
 void buffer_free(buffer_t *buf);
0
+void buffer_grow(buffer_t *buf, size_t len);
0
 void buffer_append(buffer_t *buf, const char *ptr, size_t len);
0
 
0
 #endif /* _BUFFER_H_ */
...
185
186
187
 
188
189
190
...
185
186
187
188
189
190
191
0
@@ -185,6 +185,7 @@
0
     key = RARRAY_PTR(keys)[i];
0
     value = rb_hash_aref(headers, key);
0
     /* FIXME possible buffer overflow, replace w/ buffer_append or something */
0
+ buffer_grow(&c->write_buffer, n);
0
     n += sprintf((char *) c->write_buffer.ptr + c->write_buffer.len + n,
0
                  "%s: %s" CRLF,
0
                  RSTRING_PTR(key),

Comments

    No one has commented yet.