<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -9,6 +9,8 @@ ext/*/vc60.pdb
 ext/*/*.so
 lib/*.bundle
 lib/*.so
+test/*.o
+test/*_test
 log
 spec/rails_app/log
 doc/rdoc/*</diff>
      <filename>.gitignore</filename>
    </modified>
    <modified>
      <diff>@@ -11,4 +11,8 @@ Dir['tasks/**/*.rake'].each { |rake| load rake }
 task :default =&gt; :spec
 
 ext_task :thin_parser
-ext_task :thin_backend
\ No newline at end of file
+ext_task :thin_backend
+
+task :test do
+  cd 'test' { sh 'make test' }
+end
\ No newline at end of file</diff>
      <filename>Rakefile</filename>
    </modified>
    <modified>
      <diff>@@ -39,23 +39,22 @@ int thin_print_headers(thin_connection_t *connection, char *str)
 void thin_connection_writable_cb(EV_P_ struct ev_io *watcher, int revents)
 {
   thin_connection_t *connection = get_ev_data(connection, watcher, write);
-  char              *msg;
-  int                n;
+  char              *resp;
+  int                n, sent;
   
   /* TODO write in chunk */
 
-  /* TODO get rid of that malloc or do something less stupid! */
-  msg = (char *) malloc(connection-&gt;body.len + 1024);
+  resp = (char *) palloc(connection-&gt;buffer_pool,
+                         connection-&gt;body.len / connection-&gt;buffer_pool-&gt;size + 1);
   
-  n  = sprintf(msg, &quot;HTTP/1.1 %d OK\r\n&quot;, connection-&gt;status);
-  n += thin_print_headers(connection, (char *) msg + n);
-  n += sprintf((char *) msg + n, &quot;\r\n%s&quot;, connection-&gt;body.ptr);
+  n  = sprintf(resp, &quot;HTTP/1.1 %d OK\r\n&quot;, connection-&gt;status);
+  n += thin_print_headers(connection, (char *) resp + n);
+  n += sprintf((char *) resp + n, &quot;\r\n%s&quot;, connection-&gt;body.ptr);
   
-  if (send(connection-&gt;fd, msg, n, 0) &lt; 0)
-    rb_sys_fail(&quot;send&quot;); 
-  
-  free(msg); /* see, doesn't make sense... */
+  sent = send(connection-&gt;fd, resp, n, 0);
+  /* TODO do something w/ sent, maybe buffer, not all sent? */
   
+  pfree(connection-&gt;buffer_pool, resp);
   watcher-&gt;cb = thin_connection_closable_cb;
 }
 
@@ -67,21 +66,22 @@ void thin_connection_readable_cb(EV_P_ struct ev_io *watcher, int revents)
   char              *new, *old;
   
   /* alloc more mem when buffer full */
-  if (connection-&gt;read_buffer.len == connection-&gt;read_buffer.nalloc) {
-    /* TODO refactor this into a buffer.c file? */
+  if (connection-&gt;read_buffer.len &gt;= connection-&gt;read_buffer.salloc) {
     old = connection-&gt;read_buffer.ptr;
-    new = (char *) malloc(connection-&gt;read_buffer.nalloc * 2);
+    new = (char *) palloc(connection-&gt;buffer_pool,
+                          connection-&gt;read_buffer.nalloc * 2);
     if (new == NULL)
-      rb_sys_fail(&quot;malloc&quot;);
+      rb_sys_fail(&quot;palloc&quot;);
     
     connection-&gt;read_buffer.ptr = new;
     connection-&gt;read_buffer.nalloc *= 2;
-    free(old);
+    connection-&gt;read_buffer.salloc *= 2;
+    pfree(connection-&gt;buffer_pool, old);
   }
 
   len = recv(connection-&gt;fd,
              connection-&gt;read_buffer.ptr + connection-&gt;read_buffer.len,
-             THIN_BUFFER_SIZE - connection-&gt;read_buffer.len,
+             connection-&gt;read_buffer.salloc - connection-&gt;read_buffer.len,
              0);
   
   connection-&gt;read_buffer.len += len;
@@ -160,6 +160,7 @@ void thin_connection_start(thin_backend_t *backend, int fd, struct sockaddr_in r
   if (connection-&gt;read_buffer.ptr == NULL)
     rb_sys_fail(&quot;palloc&quot;);
   connection-&gt;read_buffer.nalloc = 1;
+  connection-&gt;read_buffer.salloc = connection-&gt;buffer_pool-&gt;size;
   connection-&gt;read_buffer.len = 0;
   
   /* reinit parser */
@@ -191,6 +192,8 @@ void thin_connection_close(thin_connection_t *connection)
   
   if (connection-&gt;read_buffer.ptr != NULL)
     pfree(connection-&gt;buffer_pool, connection-&gt;read_buffer.ptr);
+  connection-&gt;read_buffer.salloc = 0;
+  connection-&gt;read_buffer.nalloc = 0;
   
   connection-&gt;open = 0; 
 }
\ No newline at end of file</diff>
      <filename>ext/thin_backend/connection.c</filename>
    </modified>
    <modified>
      <diff>@@ -38,8 +38,9 @@ typedef struct thin_buffer_s thin_buffer_t;
 typedef struct thin_connection_s thin_connection_t;
 
 struct thin_buffer_s {
+  size_t  nalloc; /* num slices alloced */
+  size_t  salloc;   /* total size alloced */
   size_t  len;
-  size_t  nalloc;
   char   *ptr;
 };
 </diff>
      <filename>ext/thin_backend/thin.h</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>ade03b50f8adf4d388a1471611246084d314e4f0</id>
    </parent>
  </parents>
  <author>
    <name>macournoyer</name>
    <email>macournoyer@gmail.com</email>
  </author>
  <url>http://github.com/macournoyer/thin-turbo/commit/0a03eb0848d17fea7afe276670c5ecd94489fec6</url>
  <id>0a03eb0848d17fea7afe276670c5ecd94489fec6</id>
  <committed-date>2008-03-26T19:00:01-07:00</committed-date>
  <authored-date>2008-03-26T19:00:01-07:00</authored-date>
  <message>Use pool to allocate read buffers</message>
  <tree>bd2ff88b38300db1bc00c774f5937cf2da4b2190</tree>
  <committer>
    <name>macournoyer</name>
    <email>macournoyer@gmail.com</email>
  </committer>
</commit>
