diff options
author | Ludovic Courtès <ludo@gnu.org> | 2014-10-31 22:53:04 +0100 |
---|---|---|
committer | Ludovic Courtès <ludo@gnu.org> | 2014-11-01 00:48:11 +0100 |
commit | a7bbba05838cabe2294f498e7008e1c51db6d664 (patch) | |
tree | 8f174dd1a994bfebfa7cb4482429669a1bb1acfe | |
parent | 30c5982a9548a0ca0ea46111beb490f06d74a40a (diff) | |
download | guile-a7bbba05838cabe2294f498e7008e1c51db6d664.tar.gz |
Use on-stack or GC-managed memory in 'search-path'.
* libguile/load.c (stringbuf_free): Remove.
(stringbuf_grow): Use 'scm_gc_malloc_pointerless' instead of 'scm_realloc'.
(search_path): Use stack-allocated INITIAL_BUFFER instead of
'scm_malloc'. Remove use of 'stringbuf_free'.
-rw-r--r-- | libguile/load.c | 28 |
1 files changed, 14 insertions, 14 deletions
diff --git a/libguile/load.c b/libguile/load.c index 74ccd088f..79d711cc1 100644 --- a/libguile/load.c +++ b/libguile/load.c @@ -403,24 +403,24 @@ SCM scm_listofnullstr; /* Utility functions for assembling C strings in a buffer. */ -struct stringbuf { +struct stringbuf +{ char *buf, *ptr; size_t buf_len; }; static void -stringbuf_free (void *data) -{ - struct stringbuf *buf = (struct stringbuf *)data; - free (buf->buf); -} - -static void stringbuf_grow (struct stringbuf *buf) { - size_t ptroff = buf->ptr - buf->buf; - buf->buf_len *= 2; - buf->buf = scm_realloc (buf->buf, buf->buf_len); + size_t ptroff, prev_len; + void *prev_buf = buf->buf; + + prev_len = buf->buf_len; + ptroff = buf->ptr - buf->buf; + + buf->buf_len *= 2; + buf->buf = scm_gc_malloc_pointerless (buf->buf_len, "search-path"); + memcpy (buf->buf, prev_buf, prev_len); buf->ptr = buf->buf + ptroff; } @@ -558,6 +558,7 @@ search_path (SCM path, SCM filename, SCM extensions, SCM require_exts, char *filename_chars; size_t filename_len; SCM result = SCM_BOOL_F; + char initial_buffer[256]; if (scm_ilength (path) < 0) scm_misc_error ("%search-path", "path is not a proper list: ~a", @@ -619,9 +620,8 @@ search_path (SCM path, SCM filename, SCM extensions, SCM require_exts, if (scm_is_null (extensions)) extensions = scm_listofnullstr; - buf.buf_len = 512; - buf.buf = scm_malloc (buf.buf_len); - scm_dynwind_unwind_handler (stringbuf_free, &buf, SCM_F_WIND_EXPLICITLY); + buf.buf_len = sizeof initial_buffer; + buf.buf = initial_buffer; /* Try every path element. */ |