From 24080add5c78f09e0604b7ae180ec52209c86d1e Mon Sep 17 00:00:00 2001 From: James Bursa Date: Sat, 2 Nov 2002 22:28:05 +0000 Subject: [project @ 2002-11-02 22:28:05 by bursa] Memory cache. svn path=/import/netsurf/; revision=47 --- desktop/browser.c | 52 +++++++++++++--------- desktop/browser.h | 3 +- desktop/cache.c | 128 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ desktop/cache.h | 32 ++++++++++++++ makefile | 11 +++-- 5 files changed, 200 insertions(+), 26 deletions(-) create mode 100644 desktop/cache.c create mode 100644 desktop/cache.h diff --git a/desktop/browser.c b/desktop/browser.c index 5f61161cc..441197fc4 100644 --- a/desktop/browser.c +++ b/desktop/browser.c @@ -1,5 +1,5 @@ /** - * $Id: browser.c,v 1.3 2002/10/15 10:41:12 monkeyson Exp $ + * $Id: browser.c,v 1.4 2002/11/02 22:28:05 bursa Exp $ */ #include "netsurf/riscos/font.h" @@ -8,6 +8,7 @@ #include "netsurf/render/css.h" #include "netsurf/desktop/browser.h" #include "netsurf/render/utils.h" +#include "netsurf/desktop/cache.h" #include #include #include @@ -124,13 +125,10 @@ void content_html_reformat(struct content* c, int width) Log("content_html_reformat", "Starting stuff"); if (c->data.html.layout != NULL) { - fprintf(stderr, "content_reformat box_free\n"); -// box_free(c->data.html.layout); - fprintf(stderr, "content_reformat free box\n"); -// xfree(c->data.html.layout); - c->data.html.layout = NULL; /* should be a freeing operation here */ + /* TODO: skip if width is unchanged */ + layout_document(c->data.html.layout->children, (unsigned long)width); + return; } - /* free other things too... */ Log("content_html_reformat", "Setting document to myDoc"); c->data.html.document = c->data.html.parser->myDoc; @@ -225,7 +223,7 @@ void browser_window_reformat(struct browser_window* bw) { Log("browser_window_reformat", "This isn't html"); browser_window_set_status(bw, "This is not HTML!"); - content_destroy(bw->current_content); + cache_free(bw->current_content); bw->current_content = NULL; } break; @@ -333,8 +331,8 @@ void browser_window_destroy(struct browser_window* bw) if (bw == NULL) return; - content_destroy(bw->current_content); - content_destroy(bw->future_content); + cache_free(bw->current_content); + cache_free(bw->future_content); if (bw->history != NULL) { @@ -370,22 +368,34 @@ void browser_window_destroy(struct browser_window* bw) void browser_window_open_location_historical(struct browser_window* bw, char* url) { - struct fetch_request* req; - if (bw == NULL) return; if (bw->future_content != NULL) - content_destroy(bw->future_content); + cache_free(bw->future_content); + + bw->future_content = cache_get(url); + if (bw->future_content == 0) + { + /* not in cache: start fetch */ + struct fetch_request* req; + + req = xcalloc(1, sizeof(struct fetch_request)); + req->type = REQUEST_FROM_BROWSER; + req->requestor.browser = bw; - req = xcalloc(1, sizeof(struct fetch_request)); - req->type = REQUEST_FROM_BROWSER; - req->requestor.browser = bw; + bw->future_content = (struct content*) xcalloc(1, sizeof(struct content)); + bw->future_content->main_fetch = create_fetch(url, bw->url, 0, req); - bw->future_content = (struct content*) xcalloc(1, sizeof(struct content)); - bw->future_content->main_fetch = create_fetch(url, bw->url, 0, req); + cache_put(url, bw->future_content, 1000); - browser_window_start_throbber(bw); + browser_window_start_throbber(bw); + } + else + { + /* in cache: reformat page and display */ + browser_window_reformat(bw); + } return; } @@ -444,7 +454,7 @@ int browser_window_message(struct browser_window* bw, struct browser_message* ms { browser_window_stop_throbber(bw); bw->future_content->main_fetch = NULL; - content_destroy(bw->future_content); + cache_free(bw->future_content); bw->future_content = NULL; } break; @@ -465,7 +475,7 @@ int browser_window_message(struct browser_window* bw, struct browser_message* ms htmlParseChunk(bw->future_content->data.html.parser, "", 0, 1); bw->future_content->main_fetch = NULL; previous_safety = gui_window_set_redraw_safety(bw->window, UNSAFE); - content_destroy(bw->current_content); + cache_free(bw->current_content); bw->current_content = bw->future_content; bw->future_content = NULL; browser_window_reformat(bw); diff --git a/desktop/browser.h b/desktop/browser.h index 5cb97d999..50fc3faf7 100644 --- a/desktop/browser.h +++ b/desktop/browser.h @@ -1,5 +1,5 @@ /** - * $Id: browser.h,v 1.3 2002/10/15 10:41:12 monkeyson Exp $ + * $Id: browser.h,v 1.4 2002/11/02 22:28:05 bursa Exp $ */ #ifndef _NETSURF_DESKTOP_BROWSER_H_ @@ -63,6 +63,7 @@ struct content } html; } data; struct fetch* main_fetch; + unsigned int ref_count; }; diff --git a/desktop/cache.c b/desktop/cache.c new file mode 100644 index 000000000..012683ebe --- /dev/null +++ b/desktop/cache.c @@ -0,0 +1,128 @@ +/** + * $Id: cache.c,v 1.1 2002/11/02 22:28:05 bursa Exp $ + */ + +#include +#include +#include +#include "netsurf/desktop/cache.h" +#include "netsurf/render/utils.h" +#include "netsurf/utils/log.h" +#include "curl/curl.h" +#include "ubiqx/ubi_Cache.h" + +/** + * internal structures and declarations + */ + +ubi_cacheRoot memcache; + +struct memcache_entry { + ubi_cacheEntry Node; + char * url; + struct content * content; +}; + +static int memcache_compare(ubi_trItemPtr item, ubi_trNode * node); +void memcache_free(ubi_trNode * node); + + +/** + * cache_init -- initialise the cache manager + */ + +void cache_init(void) +{ + /* memory cache */ + ubi_cacheInit(&memcache, memcache_compare, memcache_free, 40, 100*1024); +} + + +/** + * cache_quit -- terminate the cache manager + */ + +void cache_quit(void) +{ + ubi_cacheClear(&memcache); +} + + +/** + * cache_get -- retrieve url from memory cache or disc cache + */ + +struct content * cache_get(char * const url) +{ + struct memcache_entry * entry; + + entry = (struct memcache_entry *) ubi_cacheGet(&memcache, url); + if (entry != 0) { + LOG(("url %s in cache, node %p", url, entry)); + entry->content->ref_count++; + return entry->content; + } + + LOG(("url %s not cached", url)); + + /* TODO: check disc cache */ + + return 0; +} + + +/** + * cache_put -- place content in the memory cache + */ + +void cache_put(char * const url, struct content * content, unsigned long size) +{ + struct memcache_entry * entry; + + entry = xcalloc(1, sizeof(struct memcache_entry)); + entry->url = xstrdup(url); + entry->content = content; + content->ref_count = 2; /* cache, caller */ + ubi_cachePut(&memcache, size, + (ubi_cacheEntry *) entry, entry->url); +} + + +/** + * cache_free -- free a cache object if it is no longer used + */ + +void cache_free(struct content * content) +{ + LOG(("content %p, ref_count %u", content, content->ref_count)); + if (--content->ref_count == 0) { + LOG(("ref count 0, freeing")); + content_destroy(content); + } +} + + +/** + * memory cache + */ + +static int memcache_compare(ubi_trItemPtr item, ubi_trNode * node) +{ + return strcmp((char *) item, ((struct memcache_entry *) node)->url); +} + + +void memcache_free(ubi_trNode * node) +{ + struct memcache_entry * entry = (struct memcache_entry *) node; + + LOG(("node %p, node->url %s", node, entry->url)); + + cache_free(entry->content); + free(entry->url); + free(entry); + + /* TODO: place the object in a disc cache */ +} + + diff --git a/desktop/cache.h b/desktop/cache.h new file mode 100644 index 000000000..90131d8c2 --- /dev/null +++ b/desktop/cache.h @@ -0,0 +1,32 @@ +/** + * $Id: cache.h,v 1.1 2002/11/02 22:28:05 bursa Exp $ + */ + +/** + * Using the cache: + * + * cache_init(); + * ... + * c = cache_get(url); + * if (c == 0) { + * ... (create c) ... + * cache_put(url, c, size); + * } + * ... + * cache_free(c); + * ... + * cache_quit(); + * + * cache_free informs the cache that the content is no longer being used, so + * it can be deleted from the cache if necessary. There must be a call to + * cache_free for each cache_get or cache_put. + */ + +#include "netsurf/desktop/browser.h" + +void cache_init(void); +void cache_quit(void); +struct content * cache_get(char * const url); +void cache_put(char * const url, struct content * content, unsigned long size); +void cache_free(struct content * content); + diff --git a/makefile b/makefile index 229cc8e70..390488ddc 100644 --- a/makefile +++ b/makefile @@ -1,4 +1,4 @@ -# $Id: makefile,v 1.8 2002/10/15 11:09:56 bursa Exp $ +# $Id: makefile,v 1.9 2002/11/02 22:28:05 bursa Exp $ all: !NetSurf/!RunImage,ff8 clean: @@ -9,6 +9,7 @@ FLAGS = -g -Wall -W -Wundef -Wpointer-arith -Wbad-function-cast -Wcast-qual \ -Wmissing-declarations -Wredundant-decls -Wnested-externs -Winline -std=c9x \ -I.. -I../../Tools/libxml2/include -I../../Tools/oslib \ -I../../Tools/curl/include -I../../Tools \ + -I../../Tools/ubiqx \ -Dfd_set=long -mpoke-function-name -DNETSURF_DUMP CC = riscos-gcc OBJECTS = render/objs-riscos/utils.o render/objs-riscos/css.o \ @@ -17,13 +18,15 @@ OBJECTS = render/objs-riscos/utils.o render/objs-riscos/css.o \ riscos/objs-riscos/gui.o riscos/objs-riscos/font.o \ riscos/objs-riscos/theme.o \ desktop/objs-riscos/browser.o desktop/objs-riscos/fetch.o \ - desktop/objs-riscos/netsurf.o + desktop/objs-riscos/netsurf.o desktop/objs-riscos/cache.o HEADERS = render/box.h render/css.h render/css_enum.h \ render/layout.h render/utils.h riscos/font.h riscos/gui.h \ riscos/theme.h \ - desktop/browser.h desktop/fetch.h desktop/gui.h desktop/netsurf.h + desktop/browser.h desktop/fetch.h desktop/gui.h desktop/netsurf.h \ + desktop/cache.h LIBS = ../../Tools/libxml2/libxml.ro ../../Tools/oslib/oslib.o \ - ../../Tools/curl/libcurl.ro ../../Tools/libutf-8/libutf-8.ro + ../../Tools/curl/libcurl.ro ../../Tools/libutf-8/libutf-8.ro \ + ../../Tools/ubiqx/libubiqx.ro !NetSurf/!RunImage,ff8: $(OBJECTS) $(CC) $(FLAGS) -o !NetSurf/!RunImage,ff8 $(OBJECTS) $(LIBS) -- cgit v1.2.3