@matheusbsbs / matheus-bagliano / commits / deadcef4c1
Fork 0

vcs-svn: replace buffer_read_string memory pool with a strbuf

obj_pool is inherently global and does not use the standard growing factor alloc_nr, which makes it feel out of place in the git codebase. Plus it is overkill for this application: all that is needed is a buffer that can grow between requests to accomodate larger strings. Use a strbuf instead. As a side effect, this improves the error handling: allocation failures will result in a clean exit instead of segfaults. It would be nice to add a test case (using ulimit or failmalloc) but that can wait for another day. Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>

Jonathan Nieder committed Nov 6, 2010 at 12:01 UTC deadcef4c15d54d0a397180a1783ae8939254188
1 file changed +6 -10
vcs-svn/line_buffer.c
+6 -10
@@ -5,15 +5,13 @@
5
6 #include "git-compat-util.h"
7 #include "line_buffer.h"
8 -#include "obj_pool.h"
8 +#include "strbuf.h"
9
10 #define LINE_BUFFER_LEN 10000
11 #define COPY_BUFFER_LEN 4096
12
13 -/* Create memory pool for char sequence of known length */
14 -obj_pool_gen(blob, char, 4096)
15 -
13 static char line_buffer[LINE_BUFFER_LEN];
14 +static struct strbuf blob_buffer = STRBUF_INIT;
15 static FILE *infile;
16
17 int buffer_init(const char *filename)
@@ -58,11 +56,9 @@ char *buffer_read_line(void)
56
57 char *buffer_read_string(uint32_t len)
58 {
61 - char *s;
62 - blob_free(blob_pool.size);
63 - s = blob_pointer(blob_alloc(len + 1));
64 - s[fread(s, 1, len, infile)] = '\0';
65 - return ferror(infile) ? NULL : s;
59 + strbuf_reset(&blob_buffer);
60 + strbuf_fread(&blob_buffer, len, infile);
61 + return ferror(infile) ? NULL : blob_buffer.buf;
62 }
63
64 void buffer_copy_bytes(uint32_t len)
@@ -94,5 +90,5 @@ void buffer_skip_bytes(uint32_t len)
90
91 void buffer_reset(void)
92 {
97 - blob_reset();
93 + strbuf_release(&blob_buffer);
94 }