From eec1716bbd8a692aa260c24db57c0465fcb9fb43 Mon Sep 17 00:00:00 2001 From: Daniel Silverstone Date: Sat, 7 Jul 2012 10:56:51 +0100 Subject: HTML: Make Document memoise more generically and port HTMLElement to that interface --- src/html/html_document.c | 59 +++++++++++++++++++++++----------------- src/html/html_document.h | 10 +++---- src/html/html_document_strings.h | 43 +++++++++++++++++++++++++++++ src/html/html_element.c | 4 +-- 4 files changed, 83 insertions(+), 33 deletions(-) create mode 100644 src/html/html_document_strings.h diff --git a/src/html/html_document.c b/src/html/html_document.c index a18218b..e618deb 100644 --- a/src/html/html_document.c +++ b/src/html/html_document.c @@ -64,6 +64,7 @@ dom_exception _dom_html_document_initialise(dom_html_document *doc, dom_events_default_action_fetcher daf) { dom_exception error; + int sidx; error = _dom_document_initialise(&doc->base, daf); if (error != DOM_NO_ERR) @@ -75,47 +76,55 @@ dom_exception _dom_html_document_initialise(dom_html_document *doc, doc->url = NULL; doc->cookie = NULL; - doc->_memo_id = doc->_memo_title = doc->_memo_lang = - doc->_memo_dir = doc->_memo_class = NULL; + doc->memoised = calloc(sizeof(dom_string *), hds_COUNT); + if (doc->memoised == NULL) { + error = DOM_NO_MEM_ERR; + goto out; + } -#define MEMOISE(attr) \ +#define HTML_DOCUMENT_STRINGS_ACTION(attr) \ error = dom_string_create_interned((const uint8_t *) #attr, \ - SLEN(#attr), &doc->_memo_##attr); \ + SLEN(#attr), &doc->memoised[hds_##attr]); \ if (error != DOM_NO_ERR) { \ - if (doc->_memo_id != NULL) \ - dom_string_unref(doc->_memo_id); \ - if (doc->_memo_title != NULL) \ - dom_string_unref(doc->_memo_title); \ - if (doc->_memo_lang != NULL) \ - dom_string_unref(doc->_memo_lang); \ - if (doc->_memo_dir != NULL) \ - dom_string_unref(doc->_memo_dir); \ - return error; \ + goto out; \ + } + +#include "html_document_strings.h" +#undef HTML_DOCUMENT_STRINGS_ACTION + +out: + if (doc->memoised != NULL && error != DOM_NO_ERR) { + for(sidx = 0; sidx < hds_COUNT; ++sidx) { + if (doc->memoised[sidx] != NULL) { + dom_string_unref(doc->memoised[sidx]); + } + } + free(doc->memoised); + doc->memoised = NULL; } - - MEMOISE(id) - MEMOISE(title) - MEMOISE(lang) - MEMOISE(dir) - MEMOISE(class) - return error; } /* Finalise a HTMLDocument */ void _dom_html_document_finalise(dom_html_document *doc) { + int sidx; + dom_string_unref(doc->cookie); dom_string_unref(doc->url); dom_string_unref(doc->domain); dom_string_unref(doc->referrer); dom_string_unref(doc->title); - dom_string_unref(doc->_memo_id); - dom_string_unref(doc->_memo_title); - dom_string_unref(doc->_memo_lang); - dom_string_unref(doc->_memo_dir); - dom_string_unref(doc->_memo_class); + if (doc->memoised != NULL) { + for(sidx = 0; sidx < hds_COUNT; ++sidx) { + if (doc->memoised[sidx] != NULL) { + dom_string_unref(doc->memoised[sidx]); + } + } + free(doc->memoised); + doc->memoised = NULL; + } _dom_document_finalise(&doc->base); } diff --git a/src/html/html_document.h b/src/html/html_document.h index 57ec1ec..7f9bdb8 100644 --- a/src/html/html_document.h +++ b/src/html/html_document.h @@ -24,14 +24,12 @@ struct dom_html_document { dom_string *url; /**< HTML document URL */ dom_string *cookie; /**< HTML document cookie */ - /* Cached strings for html objects to use */ - dom_string *_memo_id; /**< Memoised 'id' */ - dom_string *_memo_title;/**< Memoised 'title' */ - dom_string *_memo_lang; /**< Memoised 'lang' */ - dom_string *_memo_dir; /**< Memoised 'dir' */ - dom_string *_memo_class;/**< Memoised 'class' */ + /** Cached strings for html objects to use */ + dom_string **memoised; }; +#include "html_document_strings.h" + /* Create a HTMLDocument */ dom_exception _dom_html_document_create( dom_events_default_action_fetcher daf, diff --git a/src/html/html_document_strings.h b/src/html/html_document_strings.h new file mode 100644 index 0000000..a45ed0d --- /dev/null +++ b/src/html/html_document_strings.h @@ -0,0 +1,43 @@ +/* + * This file is part of libdom. + * Licensed under the MIT License, + * http://www.opensource.org/licenses/mit-license.php + * Copyright 2012 Daniel Silverstone + */ + +/* Note, this file deliberately lacks guards since it's included many times + * in many places in order to correctly handle the loading of the strings. + */ + +#ifndef HTML_DOCUMENT_STRINGS_ACTION +#define HTML_DOCUMENT_STRINGS_INTERNAL_ACTION 1 +#define HTML_DOCUMENT_STRINGS_PREFIX \ + typedef enum { +#define HTML_DOCUMENT_STRINGS_SUFFIX \ + hds_COUNT \ + } html_document_memo_string_e; +#define HTML_DOCUMENT_STRINGS_ACTION(tag) \ + hds_##tag, +#endif + +#ifdef HTML_DOCUMENT_STRINGS_PREFIX +HTML_DOCUMENT_STRINGS_PREFIX +#endif + +HTML_DOCUMENT_STRINGS_ACTION(id) +HTML_DOCUMENT_STRINGS_ACTION(title) +HTML_DOCUMENT_STRINGS_ACTION(lang) +HTML_DOCUMENT_STRINGS_ACTION(dir) +HTML_DOCUMENT_STRINGS_ACTION(class) + +#ifdef HTML_DOCUMENT_STRINGS_SUFFIX +HTML_DOCUMENT_STRINGS_SUFFIX +#endif + + +#ifdef HTML_DOCUMENT_STRINGS_INTERNAL_ACTION +#undef HTML_DOCUMENT_STRINGS_INTERNAL_ACTION +#undef HTML_DOCUMENT_STRINGS_PREFIX +#undef HTML_DOCUMENT_STRINGS_SUFFIX +#undef HTML_DOCUMENT_STRINGS_ACTION +#endif diff --git a/src/html/html_element.c b/src/html/html_element.c index 18607d7..be8066f 100644 --- a/src/html/html_element.c +++ b/src/html/html_element.c @@ -112,7 +112,7 @@ dom_exception _dom_html_element_get_##fattr(dom_html_element *element, \ \ _memo_##attr = \ ((struct dom_html_document *) \ - ((struct dom_node_internal *)element)->owner)->_memo_##attr; \ + ((struct dom_node_internal *)element)->owner)->memoised[hds_##attr]; \ \ ret = dom_element_get_attribute(element, _memo_##attr, fattr); \ \ @@ -127,7 +127,7 @@ dom_exception _dom_html_element_set_##fattr(dom_html_element *element, \ \ _memo_##attr = \ ((struct dom_html_document *) \ - ((struct dom_node_internal *)element)->owner)->_memo_##attr; \ + ((struct dom_node_internal *)element)->owner)->memoised[hds_##attr]; \ \ ret = dom_element_set_attribute(element, _memo_##attr, fattr); \ \ -- cgit v1.2.3