changeset 710:43e46533be39

imported patch dicache-cleanup5
author Jorge Arellano Cid <jcid@dillo.org>
date Thu, 01 Jan 2009 10:06:48 -0300
parents 2e5f159ae6df
children b5716fb282ed 6a590c3e0db9
files src/cache.c src/cache.h src/dicache.c src/dicache.h src/gif.c src/html.cc src/html_common.hh src/imgbuf.cc src/jpeg.c src/nav.c src/png.c
diffstat 11 files changed, 69 insertions(+), 58 deletions(-) [+]
line wrap: on
line diff
--- a/src/cache.c	Thu Jan 01 10:06:44 2009 -0300
+++ b/src/cache.c	Thu Jan 01 10:06:48 2009 -0300
@@ -161,6 +161,7 @@
    ClientKey = Cache_client_make_key();
    NewClient->Key = ClientKey;
    NewClient->Url = Url;
+   NewClient->Version = 0;
    NewClient->Buf = NULL;
    NewClient->Callback = Callback;
    NewClient->CbData = CbData;
@@ -1258,9 +1259,14 @@
 void a_Cache_stop_client(int Key)
 {
    CacheClient_t *Client;
+   DICacheEntry *DicEntry;
 
    if ((Client = dList_find_custom(ClientQueue, INT2VOIDP(Key),
                                    Cache_client_by_key_cmp))) {
+      DicEntry = a_Dicache_get_entry(Client->Url, Client->Version);
+      if (DicEntry) {
+         a_Dicache_unref(Client->Url, Client->Version);
+      }
       Cache_client_dequeue(Client, NULLKey);
    } else {
       _MSG("WARNING: Cache_stop_client, nonexistent client\n");
--- a/src/cache.h	Thu Jan 01 10:06:44 2009 -0300
+++ b/src/cache.h	Thu Jan 01 10:06:48 2009 -0300
@@ -46,6 +46,7 @@
 struct _CacheClient {
    int Key;                 /* Primary Key for this client */
    const DilloUrl *Url;     /* Pointer to a cache entry Url */
+   int Version;             /* Dicache version of this Url (0 if not used) */
    void *Buf;               /* Pointer to cache-data */
    uint_t BufSize;          /* Valid size of cache-data */
    CA_Callback_t Callback;  /* Client function */
--- a/src/dicache.c	Thu Jan 01 10:06:44 2009 -0300
+++ b/src/dicache.c	Thu Jan 01 10:06:48 2009 -0300
@@ -87,7 +87,7 @@
    entry->ScanNumber = 0;
    entry->BitVec = NULL;
    entry->State = DIC_Empty;
-   entry->version = 0;
+   entry->version = 1;
 
    entry->Decoder = NULL;
    entry->DecoderData = NULL;
@@ -133,41 +133,31 @@
 }
 
 /*
- * Search an entry in the dicache (given the Url).
- * Return value: a pointer to the entry of the _newest_ (i.e. highest)
- *               version if found; NULL otherwise.
+ * Search a particular version of a URL in the Dicache.
+ * Return value: a pointer to the entry if found; NULL otherwise.
+ *
+ * Notes: DIC_Last means last version of the image.
+ *        version zero is not allowed.
  */
-DICacheEntry *a_Dicache_get_entry(const DilloUrl *Url)
+DICacheEntry *a_Dicache_get_entry(const DilloUrl *Url, int version)
 {
    DICacheNode *node;
-   DICacheEntry *entry;
+   DICacheEntry *entry = NULL;
+
+   dReturn_val_if_fail(version != 0, NULL);
 
    node = dList_find_sorted(CachedIMGs, Url, Dicache_node_by_url_cmp);
-
-   if (!node || !node->valid)
-      return NULL;
-
-   for (entry = node->first; (entry && entry->next); entry = entry->next);
-
-   return entry;
-}
-
-/*
- * Search a particular version of a URL in the Dicache.
- * Return value: a pointer to the entry if found; NULL otherwise.
- */
-static DICacheEntry *Dicache_get_entry_version(const DilloUrl *Url,
-                                               int version)
-{
-   DICacheNode *node;
-   DICacheEntry *entry;
-
-   node = dList_find_sorted(CachedIMGs, Url, Dicache_node_by_url_cmp);
-   entry = (node) ? node->first : NULL;
-
-   while (entry && entry->version != version)
-      entry = entry->next;
-
+   if (node) {
+      if (version == DIC_Last) {
+         if (node->valid) {
+            entry = node->first;
+            for ( ;  (entry && entry->next); entry = entry->next);
+         }
+      } else {
+         entry = node->first;
+         for ( ; entry && entry->version != version; entry = entry->next) ;
+      }
+   }
    return entry;
 }
 
@@ -217,7 +207,7 @@
 {
    DICacheEntry *entry;
 
-   if ((entry = Dicache_get_entry_version(Url, version))) {
+   if ((entry = a_Dicache_get_entry(Url, version))) {
       if (--entry->RefCount == 0) {
          Dicache_remove(Url, version);
       }
@@ -231,7 +221,7 @@
 {
    DICacheEntry *entry;
 
-   if ((entry = Dicache_get_entry_version(Url, version))) {
+   if ((entry = a_Dicache_get_entry(Url, version))) {
       ++entry->RefCount;
    }
    return entry;
@@ -239,7 +229,8 @@
 
 /*
  * Invalidate this entry. This is used for the reloading mechanism.
- * Can't erase current versions, but a_Dicache_get_entry must return NULL.
+ * Can't erase current versions, but a_Dicache_get_entry(url, DIC_Last)
+ * must return NULL.
  */
 void a_Dicache_invalidate_entry(const DilloUrl *Url)
 {
@@ -261,10 +252,14 @@
    uint_t i;
    DilloWeb *Web = Client->Web;
    DilloImage *Image = Web->Image;
-   DICacheEntry *DicEntry = a_Dicache_get_entry(Web->url);
+   DICacheEntry *DicEntry = a_Dicache_get_entry(Web->url, DIC_Last);
 
    dReturn_if_fail ( DicEntry != NULL );
 
+   /* Copy the version number in the Client */
+   if (Client->Version == 0)
+      Client->Version = DicEntry->version;
+
    /* Only call the decoder when necessary */
    if (Op == CA_Send && DicEntry->State < DIC_Close &&
        DicEntry->DecodedSize < Client->BufSize) {
@@ -322,7 +317,7 @@
    MSG("a_Dicache_set_parms (%s)\n", URL_STR(url));
    dReturn_if_fail ( Image != NULL && width && height );
    /* Find the DicEntry for this Image */
-   DicEntry = Dicache_get_entry_version(url, version);
+   DicEntry = a_Dicache_get_entry(url, version);
    dReturn_if_fail ( DicEntry != NULL );
    /* Parameters already set? */
    dReturn_if_fail ( DicEntry->State < DIC_SetParms );
@@ -350,7 +345,7 @@
                         const uchar_t *cmap, uint_t num_colors,
                         int num_colors_max, int bg_index)
 {
-   DICacheEntry *DicEntry = Dicache_get_entry_version(url, version);
+   DICacheEntry *DicEntry = a_Dicache_get_entry(url, version);
 
    dReturn_if_fail ( DicEntry != NULL );
 
@@ -375,7 +370,7 @@
 
    MSG("a_Dicache_new_scan\n");
    dReturn_if_fail ( url != NULL );
-   DicEntry = Dicache_get_entry_version(url, version);
+   DicEntry = a_Dicache_get_entry(url, version);
    dReturn_if_fail ( DicEntry != NULL );
    if (DicEntry->State < DIC_SetParms) {
       MSG("a_Dicache_new_scan before DIC_SetParms\n");
@@ -398,7 +393,7 @@
 
    _MSG("a_Dicache_write\n");
    dReturn_if_fail ( Image != NULL );
-   DicEntry = Dicache_get_entry_version(url, version);
+   DicEntry = a_Dicache_get_entry(url, version);
    dReturn_if_fail ( DicEntry != NULL );
    dReturn_if_fail ( DicEntry->width > 0 && DicEntry->height > 0 );
 
@@ -417,10 +412,10 @@
 void a_Dicache_close(DilloUrl *url, int version, CacheClient_t *Client)
 {
    DilloWeb *Web = Client->Web;
-   DICacheEntry *DicEntry = Dicache_get_entry_version(url, version);
+   DICacheEntry *DicEntry = a_Dicache_get_entry(url, version);
 
+   dReturn_if_fail ( DicEntry != NULL );
    MSG("a_Dicache_close RefCount=%d\n", DicEntry->RefCount);
-   dReturn_if_fail ( DicEntry != NULL );
 
    DicEntry->State = DIC_Close;
    dFree(DicEntry->cmap);
--- a/src/dicache.h	Thu Jan 01 10:06:44 2009 -0300
+++ b/src/dicache.h	Thu Jan 01 10:06:48 2009 -0300
@@ -10,6 +10,10 @@
 #include "image.hh"
 #include "cache.h"
 
+/* Symbolic name to request the last version of an image */
+#define DIC_Last  -1
+
+
 /* These will reflect the entry's "state" */
 typedef enum {
    DIC_Empty,      /* Just created the entry */
@@ -47,7 +51,7 @@
 
 void a_Dicache_init (void);
 
-DICacheEntry *a_Dicache_get_entry(const DilloUrl *Url);
+DICacheEntry *a_Dicache_get_entry(const DilloUrl *Url, int version);
 DICacheEntry *a_Dicache_add_entry(const DilloUrl *Url);
 
 void a_Dicache_callback(int Op, CacheClient_t *Client);
--- a/src/gif.c	Thu Jan 01 10:06:44 2009 -0300
+++ b/src/gif.c	Thu Jan 01 10:06:48 2009 -0300
@@ -157,7 +157,8 @@
 
 /*
  * MIME handler for "image/gif" type
- * (Sets Gif_callback as cache-client)
+ * Sets a_Dicache_callback as the cache-client,
+ * and Gif_callback as the image decoder.
  */
 void *a_Gif_image(const char *Type, void *Ptr, CA_Callback_t *Call,
                   void **Data)
@@ -172,7 +173,7 @@
    /* Add an extra reference to the Image (for dicache usage) */
    a_Image_ref(web->Image);
 
-   DicEntry = a_Dicache_get_entry(web->url);
+   DicEntry = a_Dicache_get_entry(web->url, DIC_Last);
    if (!DicEntry) {
       /* Let's create an entry for this image... */
       DicEntry = a_Dicache_add_entry(web->url);
@@ -261,7 +262,7 @@
 {
    int i;
 
-   _MSG("destroy gif %p\n", gif);
+   MSG("Gif_close: destroy gif %p\n", gif);
 
    a_Dicache_close(gif->url, gif->version, Client);
 
--- a/src/html.cc	Thu Jan 01 10:06:44 2009 -0300
+++ b/src/html.cc	Thu Jan 01 10:06:48 2009 -0300
@@ -467,7 +467,6 @@
    a_Misc_parse_content_type(content_type, NULL, NULL, &charset);
 
    stop_parser = false;
-   stop_parser_after_head = false;
    repush_after_head = false;
 
    CurrTagOfs = 0;
@@ -1607,10 +1606,10 @@
 
       html->InFlags &= ~IN_HEAD;
 
-      if (html->stop_parser_after_head)
+      if (html->repush_after_head) {
          html->stop_parser = true;
-      if (html->repush_after_head)
          a_Nav_repush(html->bw);
+      }
    }
 }
 
@@ -2937,7 +2936,6 @@
              * this code in another bw might have already changed it for us.
              */
             if (a_Misc_content_type_cmp(html->content_type, new_content)) {
-               html->stop_parser_after_head = true;
                html->repush_after_head = true;
             }
          }
--- a/src/html_common.hh	Thu Jan 01 10:06:44 2009 -0300
+++ b/src/html_common.hh	Thu Jan 01 10:06:48 2009 -0300
@@ -172,7 +172,6 @@
    char *content_type, *charset;
    bool stop_parser;
 
-   bool stop_parser_after_head;
    bool repush_after_head;
 
    size_t CurrTagOfs;
--- a/src/imgbuf.cc	Thu Jan 01 10:06:44 2009 -0300
+++ b/src/imgbuf.cc	Thu Jan 01 10:06:48 2009 -0300
@@ -70,7 +70,8 @@
  */
 void a_Imgbuf_unref(void *v_imgbuf)
 {
-   ((Imgbuf*)v_imgbuf)->unref();
+   if (v_imgbuf)
+      ((Imgbuf*)v_imgbuf)->unref();
 }
 
 /*
--- a/src/jpeg.c	Thu Jan 01 10:06:44 2009 -0300
+++ b/src/jpeg.c	Thu Jan 01 10:06:48 2009 -0300
@@ -103,7 +103,8 @@
 
 /*
  * MIME handler for "image/jpeg" type
- * (Sets Jpeg_callback or a_Dicache_callback as the cache-client)
+ * Sets a_Dicache_callback as the cache-client,
+ * and Jpeg_callback as the image decoder.
  */
 void *a_Jpeg_image(const char *Type, void *P, CA_Callback_t *Call,
                    void **Data)
@@ -117,7 +118,7 @@
    /* Add an extra reference to the Image (for dicache usage) */
    a_Image_ref(web->Image);
 
-   DicEntry = a_Dicache_get_entry(web->url);
+   DicEntry = a_Dicache_get_entry(web->url, DIC_Last);
    if (!DicEntry) {
       /* Let's create an entry for this image... */
       DicEntry = a_Dicache_add_entry(web->url);
--- a/src/nav.c	Thu Jan 01 10:06:44 2009 -0300
+++ b/src/nav.c	Thu Jan 01 10:06:48 2009 -0300
@@ -378,6 +378,9 @@
  * Repush current URL: not an end-to-end reload but from cache.
  * - Currently used to switch to a charset decoder given by the META element.
  * - Delayed to let dillo finish the call flow into a known state.
+ *
+ * There's no need to stop the parser before calling this function:
+ * When the timeout activates, a_Bw_stop_clients will stop the data feed.
  */
 void a_Nav_repush(BrowserWindow *bw)
 {
--- a/src/png.c	Thu Jan 01 10:06:44 2009 -0300
+++ b/src/png.c	Thu Jan 01 10:06:48 2009 -0300
@@ -304,12 +304,13 @@
  */
 static void Png_close(DilloPng *png, CacheClient_t *Client)
 {
+   /* Let dicache know decoding is over */
+   a_Dicache_close(png->url, png->version, Client);
+
    /* Free up the resources for this image */
-   a_Dicache_close(png->url, png->version, Client);
    dFree(png->image_data);
    dFree(png->row_pointers);
    dFree(png->linebuf);
-
    if (setjmp(png->jmpbuf))
       MSG_WARN("PNG: can't destroy read structure\n");
    else if (png->png_ptr)
@@ -444,8 +445,9 @@
 }
 
 /*
- * MIME handler for "image/png" type
- * (Sets Png_callback or a_Dicache_callback as the cache-client)
+ * MIME handler for "image/png" type.
+ * Sets a_Dicache_callback as the cache-client,
+ * and Png_callback as the image decoder.
  *
  * Parameters:
  *   Type: MIME type
@@ -465,7 +467,7 @@
    /* Add an extra reference to the Image (for dicache usage) */
    a_Image_ref(web->Image);
 
-   DicEntry = a_Dicache_get_entry(web->url);
+   DicEntry = a_Dicache_get_entry(web->url, DIC_Last);
    if (!DicEntry) {
       /* Let's create an entry for this image... */
       DicEntry = a_Dicache_add_entry(web->url);