"Fossies" - the Fresh Open Source Software Archive  

Source code changes of the file "cache.c" between
links-1.03.tar.gz and links-1.04.tar.gz

About: Links is a Lynx-like text WWW browser (table support and pop-up dialog boxes).

cache.c  (links-1.03):cache.c  (links-1.04)
#include "links.h" #include "links.h"
struct list_head cache = {&cache, &cache}; struct list_head cache = {&cache, &cache};
long cache_size; my_uintptr_t cache_size = 0;
int cache_count = 0; int cache_count = 0;
long cache_info(int type) my_uintptr_t cache_info(int type)
{ {
int i = 0; my_uintptr_t i = 0;
struct cache_entry *ce; struct cache_entry *ce;
switch (type) { switch (type) {
case CI_BYTES: case CI_BYTES:
return cache_size; return cache_size;
case CI_FILES: case CI_FILES:
foreach(ce, cache) i++; foreach(ce, cache) i++;
return i; return i;
case CI_LOCKED: case CI_LOCKED:
foreach(ce, cache) i += !!ce->refcount; foreach(ce, cache) i += !!ce->refcount;
return i; return i;
case CI_LOADING: case CI_LOADING:
foreach(ce, cache) i += is_entry_used(ce); foreach(ce, cache) i += is_entry_used(ce);
return i; return i;
case CI_LIST: case CI_LIST:
return (long) &cache; return (my_uintptr_t) &cache;
default: default:
internal("cache_info: bad request"); internal("cache_info: bad request");
} }
return 0; return 0;
} }
unsigned char *extract_proxy(unsigned char *url) unsigned char *extract_proxy(unsigned char *url)
{ {
char *a; char *a;
if (strlen(url) < 8 || casecmp(url, "proxy://", 8)) return url; if (strlen(url) < 8 || casecmp(url, "proxy://", 8)) return url;
skipping to change at line 86 skipping to change at line 86
#define sf(x) e->data_size += (x), cache_size += (x) #define sf(x) e->data_size += (x), cache_size += (x)
int page_size = 4096; int page_size = 4096;
#define C_ALIGN(x) ((((x) + sizeof(struct fragment) + 64) | (page_size - 1)) - s izeof(struct fragment) - 64) #define C_ALIGN(x) ((((x) + sizeof(struct fragment) + 64) | (page_size - 1)) - s izeof(struct fragment) - 64)
int add_fragment(struct cache_entry *e, off_t offset, unsigned char *data, off_t length) int add_fragment(struct cache_entry *e, off_t offset, unsigned char *data, off_t length)
{ {
struct fragment *f; struct fragment *f;
struct fragment *nf; struct fragment *nf;
int a = 0;
int trunc = 0; int trunc = 0;
volatile off_t ca; volatile off_t ca;
if (!length) return 0; if (!length) return 0;
e->incomplete = 1; e->incomplete = 1;
if (offset + length < 0 || offset + length < offset) overalloc(); if ((off_t)(0UL + offset + length) < 0 || (off_t)(0UL + offset + length)
if (offset + (off_t)C_ALIGN(length) < 0 || offset + (off_t)C_ALIGN(length < offset) overalloc();
) < offset) overalloc(); if ((off_t)(0UL + offset + (off_t)C_ALIGN(length)) < 0 || (off_t)(0UL + o
ffset + (off_t)C_ALIGN(length)) < offset) overalloc();
if (e->length < offset + length) e->length = offset + length; if (e->length < offset + length) e->length = offset + length;
e->count = cache_count++; e->count = cache_count++;
if (!list_empty(e->frag)) {
f = e->frag.prev;
if (f->offset + f->length == offset) goto have_f;
}
foreach(f, e->frag) { foreach(f, e->frag) {
have_f:
if (f->offset > offset) break; if (f->offset > offset) break;
if (f->offset <= offset && f->offset+f->length >= offset) { if (f->offset <= offset && f->offset+f->length >= offset) {
if (offset+length > f->offset+f->length) { if (offset+length > f->offset+f->length) {
if (memcmp(f->data+offset-f->offset, data, f->off set+f->length-offset)) trunc = 1; if (memcmp(f->data+offset-f->offset, data, f->off set+f->length-offset)) trunc = 1;
a = 1; /* !!! FIXME */
if (offset-f->offset+length <= f->real_length) { if (offset-f->offset+length <= f->real_length) {
sf((offset+length) - (f->offset+f->length )); sf((offset+length) - (f->offset+f->length ));
f->length = offset-f->offset+length; f->length = offset-f->offset+length;
} } else {
else {
sf(-(f->offset+f->length-offset)); sf(-(f->offset+f->length-offset));
f->length = offset-f->offset; f->length = offset-f->offset;
f = f->next; f = f->next;
break; break;
} }
} else { } else {
if (memcmp(f->data+offset-f->offset, data, length )) trunc = 1; if (memcmp(f->data+offset-f->offset, data, length )) trunc = 1;
} }
memcpy(f->data+offset-f->offset, data, length); memcpy(f->data+offset-f->offset, data, length);
goto ch_o; goto ch_o;
} }
} }
/* Intel C 9 has a bug and miscompiles this statement (< 0 test is true) */ /* Intel C 9 has a bug and miscompiles this statement (< 0 test is true) */
/*if (C_ALIGN(length) > MAXINT - sizeof(struct fragment) || C_ALIGN(lengt h) < 0) overalloc();*/ /*if (C_ALIGN(length) > MAXINT - sizeof(struct fragment) || C_ALIGN(lengt h) < 0) overalloc();*/
ca = C_ALIGN(length); ca = C_ALIGN(length);
if (ca > MAXINT - (int)sizeof(struct fragment) || ca < 0) overalloc(); if (ca > MAXINT - (int)sizeof(struct fragment) || ca < 0) overalloc();
nf = mem_alloc(sizeof(struct fragment) + ca); nf = mem_alloc(sizeof(struct fragment) + ca);
a = 1;
sf(length); sf(length);
nf->offset = offset; nf->offset = offset;
nf->length = length; nf->length = length;
nf->real_length = C_ALIGN(length); nf->real_length = C_ALIGN(length);
memcpy(nf->data, data, length); memcpy(nf->data, data, length);
add_at_pos(f->prev, nf); add_at_pos(f->prev, nf);
f = nf; f = nf;
ch_o: ch_o:
while ((void *)f->next != &e->frag && f->offset+f->length > f->next->offs et) { while ((void *)f->next != &e->frag && f->offset+f->length > f->next->offs et) {
if (f->offset+f->length < f->next->offset+f->next->length) { if (f->offset+f->length < f->next->offset+f->next->length) {
skipping to change at line 155 skipping to change at line 156
nf = f->next; nf = f->next;
del_from_list(nf); del_from_list(nf);
sf(-nf->length); sf(-nf->length);
mem_free(nf); mem_free(nf);
} }
if (trunc) truncate_entry(e, offset + length, 0); if (trunc) truncate_entry(e, offset + length, 0);
/*{ /*{
foreach(f, e->frag) fprintf(stderr, "%d, %d, %d\n", f->offset, f- >length, f->real_length); foreach(f, e->frag) fprintf(stderr, "%d, %d, %d\n", f->offset, f- >length, f->real_length);
debug("a-"); debug("a-");
}*/ }*/
return a; if (e->length > e->max_length) {
e->max_length = e->length;
return 1;
}
return 0;
} }
void defrag_entry(struct cache_entry *e) void defrag_entry(struct cache_entry *e)
{ {
struct fragment *f, *g, *h, *n, *x; struct fragment *f, *g, *h, *n, *x;
off_t l; off_t l;
if (list_empty(e->frag)) return; if (list_empty(e->frag)) return;
f = e->frag.next; f = e->frag.next;
if (f->offset) return; if (f->offset) return;
for (g = f->next; g != (void *)&e->frag && g->offset <= g->prev->offset+g ->prev->length; g = g->next) if (g->offset < g->prev->offset+g->prev->length) { for (g = f->next; g != (void *)&e->frag && g->offset <= g->prev->offset+g ->prev->length; g = g->next) if (g->offset < g->prev->offset+g->prev->length) {
internal("fragments overlay"); internal("fragments overlay");
return; return;
} }
if (g == f->next && f->length == f->real_length) return; if (g == f->next && f->length == f->real_length) return;
for (l = 0, h = f; h != g; h = h->next) l += h->length; for (l = 0, h = f; h != g; h = h->next) {
if (l > MAXINT - (int)sizeof(struct fragment) || l < 0) overalloc(); if ((off_t)(0UL + l + h->length) < 0 || (off_t)(0UL + l + h->leng
th) < l) overalloc();
l += h->length;
}
if (l > MAXINT - (int)sizeof(struct fragment)) overalloc();
n = mem_alloc(sizeof(struct fragment) + l); n = mem_alloc(sizeof(struct fragment) + l);
n->offset = 0; n->offset = 0;
n->length = l; n->length = l;
n->real_length = l; n->real_length = l;
/*{ /*{
struct fragment *f; struct fragment *f;
foreach(f, e->frag) fprintf(stderr, "%d, %d, %d\n", f->offset, f- >length, f->real_length); foreach(f, e->frag) fprintf(stderr, "%d, %d, %d\n", f->offset, f- >length, f->real_length);
debug("d1-"); debug("d1-");
}*/ }*/
for (l = 0, h = f; h != g; h = h->next) { for (l = 0, h = f; h != g; h = h->next) {
skipping to change at line 260 skipping to change at line 268
} else break; } else break;
} }
} }
void delete_entry_content(struct cache_entry *e) void delete_entry_content(struct cache_entry *e)
{ {
e->count = cache_count++; e->count = cache_count++;
free_list(e->frag); free_list(e->frag);
e->length = 0; e->length = 0;
e->incomplete = 1; e->incomplete = 1;
if ((cache_size -= e->data_size) < 0) { if (cache_size < (my_uintptr_t)e->data_size) {
internal("cache_size underflow: %ld", cache_size); internal("cache_size underflow: %lu, %lu", (unsigned long)cache_s
cache_size = 0; ize, (unsigned long)e->data_size);
} }
cache_size -= e->data_size;
e->data_size = 0; e->data_size = 0;
if (e->last_modified) { if (e->last_modified) {
mem_free(e->last_modified); mem_free(e->last_modified);
e->last_modified = NULL; e->last_modified = NULL;
} }
} }
void delete_cache_entry(struct cache_entry *e) void delete_cache_entry(struct cache_entry *e)
{ {
if (e->refcount) internal("deleteing locked cache entry"); if (e->refcount) internal("deleting locked cache entry");
#ifdef DEBUG #ifdef DEBUG
if (is_entry_used(e)) internal("deleting loading cache entry"); if (is_entry_used(e)) internal("deleting loading cache entry");
#endif #endif
delete_entry_content(e); delete_entry_content(e);
del_from_list(e); del_from_list(e);
mem_free(e->url); mem_free(e->url);
if (e->head) mem_free(e->head); if (e->head) mem_free(e->head);
if (e->last_modified) mem_free(e->last_modified); if (e->last_modified) mem_free(e->last_modified);
if (e->redirect) mem_free(e->redirect); if (e->redirect) mem_free(e->redirect);
#ifdef HAVE_SSL #ifdef HAVE_SSL
if (e->ssl_info) mem_free(e->ssl_info); if (e->ssl_info) mem_free(e->ssl_info);
#endif #endif
mem_free(e); mem_free(e);
} }
void garbage_collection(int u) void garbage_collection(int u)
{ {
struct cache_entry *e, *f; struct cache_entry *e, *f;
long ncs = cache_size; my_uintptr_t ncs = cache_size;
long ccs = 0; my_uintptr_t ccs = 0;
if (!u && cache_size <= memory_cache_size) return; if (!u && cache_size <= (my_uintptr_t)memory_cache_size) return;
foreach(e, cache) { foreach(e, cache) {
if (e->refcount || is_entry_used(e)) if ((ncs -= e->data_size) < if (e->refcount || is_entry_used(e)) {
0) { if (ncs < (my_uintptr_t)e->data_size) {
internal("cache_size underflow: %ld", ncs); internal("cache_size underflow: %lu, %lu", (unsig
ncs = 0; ned long)ncs, (unsigned long)e->data_size);
}
ncs -= e->data_size;
} }
ccs += e->data_size; ccs += e->data_size;
} }
if (ccs != cache_size) internal("cache size badly computed: %ld != %ld", if (ccs != cache_size) internal("cache size badly computed: %lu != %lu",
cache_size, ccs), cache_size = ccs; (unsigned long)cache_size, (unsigned long)ccs), cache_size = ccs;
if (!u && ncs <= memory_cache_size) return; if (!u && ncs <= (my_uintptr_t)memory_cache_size) return;
for (e = cache.prev; (void *)e != &cache; e = e->prev) { for (e = cache.prev; (void *)e != &cache; e = e->prev) {
if (!u && ncs <= (longlong)memory_cache_size * MEMORY_CACHE_GC_PE RCENT) goto g; if (!u && (longlong)ncs <= (longlong)memory_cache_size * MEMORY_C ACHE_GC_PERCENT) goto g;
if (e->refcount || is_entry_used(e)) { if (e->refcount || is_entry_used(e)) {
e->tgc = 0; e->tgc = 0;
continue; continue;
} }
e->tgc = 1; e->tgc = 1;
if ((ncs -= e->data_size) < 0) { if (ncs < (my_uintptr_t)e->data_size) {
internal("cache_size underflow: %ld", ncs); internal("cache_size underflow: %lu, %lu", (unsigned long
ncs = 0; )ncs, (unsigned long)e->data_size);
} }
ncs -= e->data_size;
} }
if (/*!no &&*/ ncs) internal("cache_size(%ld) is larger than size of all objects(%ld)", cache_size, cache_size - ncs); if (ncs) internal("cache_size(%lu) is larger than size of all objects(%lu )", (unsigned long)cache_size, (unsigned long)(cache_size - ncs));
g: g:
if ((void *)(e = e->next) == &cache) return; if ((void *)(e = e->next) == &cache) return;
if (!u) for (f = e; (void *)f != &cache; f = f->next) { if (!u) for (f = e; (void *)f != &cache; f = f->next) {
if (ncs + f->data_size <= (longlong)memory_cache_size * MEMORY_CA CHE_GC_PERCENT) { if (f->data_size && (longlong)ncs + f->data_size <= (longlong)mem ory_cache_size * MEMORY_CACHE_GC_PERCENT) {
ncs += f->data_size; ncs += f->data_size;
f->tgc = 0; f->tgc = 0;
} }
} }
for (f = e; (void *)f != &cache;) { for (f = e; (void *)f != &cache;) {
f = f->next; f = f->next;
if (f->prev->tgc) delete_cache_entry(f->prev); if (f->prev->tgc) delete_cache_entry(f->prev);
} }
/*if (!no && cache_size > (longlong)memory_cache_size * MEMORY_CACHE_GC_P
ERCENT) {
internal("garbage collection doesn't work, cache size %ld", cache
_size);
}*/
} }
void init_cache(void) void init_cache(void)
{ {
#ifdef HAVE_GETPAGESIZE #ifdef HAVE_GETPAGESIZE
int getpg = getpagesize(); int getpg;
EINTRLOOP(getpg, getpagesize());
if (getpg > 0 && getpg < 0x10000 && !(getpg & (getpg - 1))) page_size = g etpg; if (getpg > 0 && getpg < 0x10000 && !(getpg & (getpg - 1))) page_size = g etpg;
#endif #endif
} }
 End of changes. 26 change blocks. 
41 lines changed or deleted 51 lines changed or added

Home  |  About  |  Features  |  All  |  Newest  |  Dox  |  Diffs  |  RSS Feeds  |  Screenshots  |  Comments  |  Imprint  |  Privacy  |  HTTP(S)