summaryrefslogtreecommitdiff
path: root/desktop
diff options
context:
space:
mode:
authorJames Bursa <james@netsurf-browser.org>2002-11-02 22:28:05 +0000
committerJames Bursa <james@netsurf-browser.org>2002-11-02 22:28:05 +0000
commit24080add5c78f09e0604b7ae180ec52209c86d1e (patch)
tree56c08122691d2c35ee50bdd490d7b92290e75c1f /desktop
parentc009c7111c99ff78c5116f874a57a9ac7daf960d (diff)
downloadnetsurf-24080add5c78f09e0604b7ae180ec52209c86d1e.tar.gz
netsurf-24080add5c78f09e0604b7ae180ec52209c86d1e.tar.bz2
[project @ 2002-11-02 22:28:05 by bursa]
Memory cache. svn path=/import/netsurf/; revision=47
Diffstat (limited to 'desktop')
-rw-r--r--desktop/browser.c52
-rw-r--r--desktop/browser.h3
-rw-r--r--desktop/cache.c128
-rw-r--r--desktop/cache.h32
4 files changed, 193 insertions, 22 deletions
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 <string.h>
#include <stdlib.h>
#include <assert.h>
@@ -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 <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#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);
+