[project @ 2002-11-02 22:28:05 by bursa]

Memory cache.

svn path=/import/netsurf/; revision=47
This commit is contained in:
James Bursa 2002-11-02 22:28:05 +00:00
parent c009c7111c
commit 24080add5c
5 changed files with 200 additions and 26 deletions

View File

@ -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);
req = xcalloc(1, sizeof(struct fetch_request));
req->type = REQUEST_FROM_BROWSER;
req->requestor.browser = bw;
bw->future_content = cache_get(url);
if (bw->future_content == 0)
{
/* not in cache: start fetch */
struct fetch_request* req;
bw->future_content = (struct content*) xcalloc(1, sizeof(struct content));
bw->future_content->main_fetch = create_fetch(url, bw->url, 0, req);
req = xcalloc(1, sizeof(struct fetch_request));
req->type = REQUEST_FROM_BROWSER;
req->requestor.browser = bw;
browser_window_start_throbber(bw);
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);
}
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);

View File

@ -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;
};

128
desktop/cache.c Normal file
View File

@ -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 */
}

32
desktop/cache.h Normal file
View File

@ -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);

View File

@ -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)