diff options
author | David Robillard <d@drobilla.net> | 2011-01-25 17:03:53 +0000 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2011-01-25 17:03:53 +0000 |
commit | 5b0c4fec31b17b92d01f4eeccfd4bc4af8484865 (patch) | |
tree | 24c8a2b3a74678ce9d6d59860a2a7525a844c378 | |
parent | 2e3ec18723e534dd1e6600148a2e24216138414d (diff) | |
download | serd-5b0c4fec31b17b92d01f4eeccfd4bc4af8484865.tar.gz serd-5b0c4fec31b17b92d01f4eeccfd4bc4af8484865.tar.bz2 serd-5b0c4fec31b17b92d01f4eeccfd4bc4af8484865.zip |
Fix memory leaks.
Push builtin URIs (rdf:type, etc) on parser creation/deletion, rather than at each parse.
git-svn-id: http://svn.drobilla.net/serd/trunk@67 490d8e77-9747-427b-9fa3-0b8f29cee8a0
-rw-r--r-- | src/env.c | 6 | ||||
-rw-r--r-- | src/reader.c | 71 | ||||
-rw-r--r-- | src/serd_internal.h | 6 | ||||
-rw-r--r-- | src/serdi.c | 15 |
4 files changed, 55 insertions, 43 deletions
@@ -42,12 +42,6 @@ serd_node_copy(const SerdNode* node) return copy; } -static void -serd_node_free(SerdNode* node) -{ - free((uint8_t*)node->buf); // FIXME: const cast -} - SERD_API SerdEnv serd_env_new() diff --git a/src/reader.c b/src/reader.c index 04891658..bd82ec9d 100644 --- a/src/reader.c +++ b/src/reader.c @@ -1342,33 +1342,41 @@ serd_reader_new(SerdSyntax syntax, SerdStatementSink statement_sink, SerdEndSink end_sink) { - const Cursor cur = { NULL, 0, 0 }; - SerdReader reader = malloc(sizeof(struct SerdReaderImpl)); - reader->handle = handle; - reader->base_sink = base_sink; - reader->prefix_sink = prefix_sink; - reader->statement_sink = statement_sink; - reader->end_sink = end_sink; - reader->fd = 0; - reader->stack = serd_stack_new(STACK_PAGE_SIZE); - reader->cur = cur; - reader->next_id = 1; - reader->read_buf = (uint8_t*)malloc(READ_BUF_LEN * 2); - reader->read_head = 0; - reader->eof = false; + const Cursor cur = { NULL, 0, 0 }; + SerdReader me = malloc(sizeof(struct SerdReaderImpl)); + me->handle = handle; + me->base_sink = base_sink; + me->prefix_sink = prefix_sink; + me->statement_sink = statement_sink; + me->end_sink = end_sink; + me->fd = 0; + me->stack = serd_stack_new(STACK_PAGE_SIZE); + me->cur = cur; + me->next_id = 1; + me->read_buf = (uint8_t*)malloc(READ_BUF_LEN * 2); + me->read_head = 0; + me->eof = false; #ifdef STACK_DEBUG - reader->alloc_stack = 0; - reader->n_allocs = 0; + me->alloc_stack = 0; + me->n_allocs = 0; #endif - memset(reader->read_buf, '\0', READ_BUF_LEN * 2); + memset(me->read_buf, '\0', READ_BUF_LEN * 2); /* Read into the second page of the buffer. Occasionally peek_string will move the read_head to before this point when readahead causes a page fault. */ - reader->read_buf += READ_BUF_LEN; // Read 1 page in - return reader; + me->read_buf += READ_BUF_LEN; // Read 1 page in + +#define RDF_FIRST NS_RDF "first" +#define RDF_REST NS_RDF "rest" +#define RDF_NIL NS_RDF "nil" + me->rdf_first = make_node(SERD_URI, push_string(me, RDF_FIRST, 49), 0, 0); + me->rdf_rest = make_node(SERD_URI, push_string(me, RDF_REST, 48), 0, 0); + me->rdf_nil = make_node(SERD_URI, push_string(me, RDF_NIL, 47), 0, 0); + + return me; } SERD_API @@ -1376,6 +1384,10 @@ void serd_reader_free(SerdReader reader) { SerdReader const me = (SerdReader)reader; + pop_string(me, me->rdf_nil.value); + pop_string(me, me->rdf_rest.value); + pop_string(me, me->rdf_first.value); + #ifdef STACK_DEBUG free(me->alloc_stack); #endif @@ -1386,28 +1398,17 @@ serd_reader_free(SerdReader reader) SERD_API bool -serd_reader_read_file(SerdReader reader, FILE* file, const uint8_t* name) +serd_reader_read_file(SerdReader me, FILE* file, const uint8_t* name) { - #define RDF_FIRST NS_RDF "first" - #define RDF_REST NS_RDF "rest" - #define RDF_NIL NS_RDF "nil" - SerdReader const me = (SerdReader)reader; - const Cursor cur = { name, 1, 1 }; + const Cursor cur = { name, 1, 1 }; - me->fd = file; - me->cur = cur; - me->rdf_first = make_node(SERD_URI, push_string(me, RDF_FIRST, 49), 0, 0); - me->rdf_rest = make_node(SERD_URI, push_string(me, RDF_REST, 48), 0, 0); - me->rdf_nil = make_node(SERD_URI, push_string(me, RDF_NIL, 47), 0, 0); + me->fd = file; + me->cur = cur; page(me); const bool ret = read_turtleDoc(me); - pop_string(me, me->rdf_nil.value); - pop_string(me, me->rdf_rest.value); - pop_string(me, me->rdf_first.value); - me->cur = cur; - me->fd = 0; + me->fd = 0; return ret; } diff --git a/src/serd_internal.h b/src/serd_internal.h index 7adf1567..4fa2a2c5 100644 --- a/src/serd_internal.h +++ b/src/serd_internal.h @@ -121,4 +121,10 @@ serd_strlen(const uint8_t* utf8, size_t* out_n_bytes) return n_chars; } +static inline void +serd_node_free(SerdNode* node) +{ + free((uint8_t*)node->buf); // FIXME: ick, const cast +} + #endif // SERD_INTERNAL_H diff --git a/src/serdi.c b/src/serdi.c index 467c3275..5ab5e440 100644 --- a/src/serdi.c +++ b/src/serdi.c @@ -56,6 +56,7 @@ string_sink(const void* buf, size_t len, void* stream) return len; } +// FIXME: doesn't belong here static SerdNode serd_node_new_uri(const SerdURI* uri, SerdURI* out) { @@ -86,6 +87,13 @@ serd_node_new_uri(const SerdURI* uri, SerdURI* out) return node; } +// FIXME: doesn't belong here +static void +serd_node_free(SerdNode* node) +{ + free((uint8_t*)node->buf); // FIXME: ick, const cast +} + static uint8_t* copy_string(const uint8_t* str, size_t* n_bytes) { @@ -113,7 +121,8 @@ event_base(void* handle, return false; } base_uri_node = serd_node_new_uri(&abs_base_uri, &base_uri); - + + serd_node_free(&state->base_uri_node); state->base_uri_node = base_uri_node; state->base_uri = base_uri; serd_writer_set_base_uri(state->writer, &base_uri); @@ -138,6 +147,7 @@ event_prefix(void* handle, SerdURI base_uri; SerdNode base_uri_node = serd_node_new_uri(&abs_uri, &base_uri); serd_env_add(state->env, name, &base_uri_node); + serd_node_free(&base_uri_node); } else { serd_env_add(state->env, name, uri_node); } @@ -288,7 +298,8 @@ main(int argc, char** argv) serd_writer_free(state.writer); serd_env_free(state.env); - free(base_uri_str); + + serd_node_free(&state.base_uri_node); if (success) { return 0; |