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:
macournoyer (author)
Mon Apr 07 19:57:39 -0700 2008
commit  993e78f402183c0e58a5f2c9442d8565eaa53268
tree    4326eeabebf6706a180369a79742fb92c8627c3a
parent  747a41479340d52976aa067c3e93198a40d74627
thin / ext / thin_backend / buffer.c
100644 51 lines (41 sloc) 1.069 kb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
#include "buffer.h"
 
void buffer_init(buffer_t *buf, pool_t *p)
{
  buf->pool = p;
  buf->ptr = palloc(p, 1);
  assert(buf->ptr);
  buf->nalloc = 1;
  buf->salloc = p->size;
  buf->len = 0;
  buf->current = 0;
}
 
void buffer_free(buffer_t *buf)
{
  if (buf->ptr != NULL)
    pfree(buf->pool, buf->ptr);
  buf->salloc = 0;
  buf->nalloc = 0;
}
 
static inline void buffer_grow(buffer_t *buf, size_t size)
{
  char *new, *old;
  size_t num = (size_t) (size + 0.5) / (float) buf->pool->size;
  
  /* TODO store big body in tempfile */
  /* TODO if last alloc, just alloc next block */
  
  old = buf->ptr;
  new = (char *) palloc(buf->pool, buf->nalloc + num);
  assert(new);
  
  memcpy(new, old, buf->len);
 
  buf->ptr = new;
  buf->nalloc += num;
  buf->salloc += buf->pool->size * num;
  
  pfree(buf->pool, old);
}
 
void buffer_append(buffer_t *buf, const char *ptr, size_t len)
{
  /* alloc more mem when buffer full */
  if (buf->len + len > buf->salloc)
    buffer_grow(buf, len);
  
  memcpy(buf->ptr + buf->len, ptr, len);
  buf->len += len;
}