From cc8949d42722e10d3750dc6d2c7a906d9ac1120a4b83417657d53eca493829d7 Mon Sep 17 00:00:00 2001 From: ADAM David Alan Martin Date: Wed, 6 May 2026 02:35:54 -0400 Subject: [PATCH] Start C++ RAII conversions for dicache. --- src/dicache.cc | 50 ++++++++++++++++++++++++++------------------------ src/dicache.hh | 17 +++++++++-------- 2 files changed, 35 insertions(+), 32 deletions(-) diff --git a/src/dicache.cc b/src/dicache.cc index 4c9ae9c..fadcfbc 100644 --- a/src/dicache.cc +++ b/src/dicache.cc @@ -60,7 +60,7 @@ static int Dicache_entry_cmp(const void *v1, const void *v2) { const DICacheEntry *e1 = reinterpret_cast< const DICacheEntry * >( v1 ), *e2 = reinterpret_cast< const DICacheEntry * >( v2 ); - int st = a_Url_cmp(e1->url, e2->url); + int st = a_Url_cmp(e1->url.get(), e2->url.get()); if (st == 0) { if (e2->version == DIC_Last) st = (e1->Flags & DIF_Last ? 0 : -1); @@ -93,7 +93,7 @@ void a_Dicache_init(void) */ static DICacheEntry *Dicache_entry_new(void) { - DICacheEntry *entry = dNew(DICacheEntry, 1); + DICacheEntry *entry = std::make_unique< DICacheEntry >().release(); entry->width = 0; entry->height = 0; @@ -125,7 +125,7 @@ static DICacheEntry *Dicache_add_entry(const DilloUrl *Url) DICacheEntry e, *entry, *last; entry = Dicache_entry_new(); - e.url = (DilloUrl*)Url; + e.url = a_Url_dup( Url ); e.version = DIC_Last; last = reinterpret_cast< DICacheEntry * >( dList_find_sorted(CachedIMGs, &e, Dicache_entry_cmp) ); if (last) { @@ -133,7 +133,7 @@ static DICacheEntry *Dicache_add_entry(const DilloUrl *Url) last->Flags &= ~DIF_Last; entry->version = last->version + 1; } - entry->url = a_Url_dup(Url).release(); + entry->url = a_Url_dup(Url); entry->Flags |= DIF_Last; dList_insert_sorted(CachedIMGs, entry, Dicache_entry_cmp); @@ -153,7 +153,7 @@ DICacheEntry *a_Dicache_get_entry(const DilloUrl *Url, int version) DICacheEntry *entry = NULL; dReturn_val_if_fail(version != 0, NULL); - e.url = const_cast< DilloUrl * >( Url ); + e.url = a_Url_dup( Url ); e.version = version; entry = reinterpret_cast< DICacheEntry * >( dList_find_sorted(CachedIMGs, &e, Dicache_entry_cmp) ); if (entry && !(entry->Flags & DIF_Valid) && version == DIC_Last) @@ -169,7 +169,7 @@ static void Dicache_remove(const DilloUrl *Url, int version) DICacheEntry e, *entry; _MSG("Dicache_remove url=%s\n", URL_STR(Url)); - e.url = const_cast< DilloUrl * >( Url ); + e.url = a_Url_dup( Url ); e.version = version; entry = reinterpret_cast< DICacheEntry * >( dList_find_sorted(CachedIMGs, &e, Dicache_entry_cmp) ); dReturn_if (entry == NULL); @@ -180,15 +180,18 @@ static void Dicache_remove(const DilloUrl *Url, int version) dList_remove(CachedIMGs, entry); dicache_size_total -= entry->TotalSize; + delete entry; +} + +DICacheEntry::~DICacheEntry() +{ /* entry cleanup */ - delete entry->url; - dFree(entry->cmap); - delete entry->BitVec; - a_Imgbuf_unref(entry->v_imgbuf); - if (entry->Decoder) { - entry->Decoder(CA_Abort, reinterpret_cast< CacheClient * >( entry->DecoderData )); + dFree(cmap); + delete BitVec; + if(v_imgbuf) a_Imgbuf_unref(v_imgbuf); + if (Decoder) { + Decoder(CA_Abort, reinterpret_cast< CacheClient * >( DecoderData )); } - dFree(entry); } /** @@ -410,27 +413,27 @@ static void *Dicache_image(int ImgType, const char *MimeType, void *Ptr, if (ImgType == DIC_Jpeg) { DicEntry->Decoder = (CA_Callback_t)a_Jpeg_callback; DicEntry->DecoderData = - a_Jpeg_new(web->Image.get(), DicEntry->url, DicEntry->version); + a_Jpeg_new(web->Image.get(), DicEntry->url.get(), DicEntry->version); } else if (ImgType == DIC_Gif) { DicEntry->Decoder = (CA_Callback_t)a_Gif_callback; DicEntry->DecoderData = - a_Gif_new(web->Image.get(), DicEntry->url, DicEntry->version); + a_Gif_new(web->Image.get(), DicEntry->url.get(), DicEntry->version); } else if (ImgType == DIC_Webp) { DicEntry->Decoder = (CA_Callback_t)a_Webp_callback; DicEntry->DecoderData = - a_Webp_new(web->Image.get(), DicEntry->url, DicEntry->version); + a_Webp_new(web->Image.get(), DicEntry->url.get(), DicEntry->version); } else if (ImgType == DIC_Png) { DicEntry->Decoder = (CA_Callback_t)a_Png_callback; DicEntry->DecoderData = - a_Png_new(web->Image.get(), DicEntry->url, DicEntry->version); + a_Png_new(web->Image.get(), DicEntry->url.get(), DicEntry->version); } else if (ImgType == DIC_Svg) { DicEntry->Decoder = (CA_Callback_t)a_Svg_callback; DicEntry->DecoderData = - a_Svg_new(web->Image.get(), DicEntry->url, DicEntry->version); + a_Svg_new(web->Image.get(), DicEntry->url.get(), DicEntry->version); } } else { /* Repeated image */ - a_Dicache_ref(DicEntry->url, DicEntry->version); + a_Dicache_ref(DicEntry->url.get(), DicEntry->version); } /* Survive three cleanup passes (set to zero = old behaviour). */ DicEntry->SurvCleanup = 3; @@ -511,7 +514,7 @@ void a_Dicache_callback(int Op, CacheClient_t *Client) if (DicEntry->State < DIC_Close) { DicEntry->Decoder(Op, Client); } else { - a_Dicache_close(DicEntry->url, DicEntry->version, Client); + a_Dicache_close(DicEntry->url.get(), DicEntry->version, Client); } } @@ -520,7 +523,7 @@ void a_Dicache_callback(int Op, CacheClient_t *Client) if (Image->height == 0 && DicEntry->State >= DIC_SetParms) { /* Set parms */ a_Image_set_parms( - Image, DicEntry->v_imgbuf, DicEntry->url, + Image, DicEntry->v_imgbuf, DicEntry->url.get(), DicEntry->version, DicEntry->width, DicEntry->height, DicEntry->type); } @@ -570,7 +573,7 @@ void a_Dicache_cleanup(void) continue; /* keep the entry one more pass */ /* free this unused entry */ - Dicache_remove(entry->url, entry->version); + Dicache_remove(entry->url.get(), entry->version); --i; /* adjust counter */ } } @@ -590,12 +593,11 @@ void a_Dicache_freeall(void) /* Remove all the dicache entries */ while ((entry = reinterpret_cast< DICacheEntry * >( dList_nth_data(CachedIMGs, dList_length(CachedIMGs)-1) ))) { dList_remove_fast(CachedIMGs, entry); - delete entry->url; dFree(entry->cmap); delete entry->BitVec; a_Imgbuf_unref(entry->v_imgbuf); dicache_size_total -= entry->TotalSize; - dFree(entry); + delete entry; } dList_free(CachedIMGs); } diff --git a/src/dicache.hh b/src/dicache.hh index f4ec7cf..d0315e3 100644 --- a/src/dicache.hh +++ b/src/dicache.hh @@ -27,26 +27,27 @@ typedef enum { DIC_Abort /**< Image transfer aborted */ } DicEntryState; -typedef struct DICacheEntry { - DilloUrl *url; /**< Image URL for this entry */ +struct DICacheEntry { + std::unique_ptr< DilloUrl > url; /**< Image URL for this entry */ DilloImgType type; /**< Image type */ uint_t width, height; /**< As taken from image data */ short Flags; /**< See Flags */ short SurvCleanup; /**< Cleanup-pass survival for unused images */ - uchar_t *cmap; /**< Color map */ - void *v_imgbuf; /**< Void pointer to an Imgbuf object */ + uchar_t *cmap= nullptr; /**< Color map */ + void *v_imgbuf= nullptr; /**< Void pointer to an Imgbuf object */ uint_t TotalSize; /**< Amount of memory the image takes up */ uint_t ScanNumber; /**< Current decoding scan */ - bitvec_t *BitVec; /**< Bit vector for decoded rows */ + bitvec_t *BitVec= nullptr; /**< Bit vector for decoded rows */ DicEntryState State; /**< Current status for this entry */ int RefCount; /**< Reference Counter */ int version; /**< Version number, used for different versions of the same URL image */ uint_t DecodedSize; /**< Size of already decoded data */ - CA_Callback_t Decoder; /**< Client function */ - void *DecoderData; /**< Client function data */ -} DICacheEntry; + CA_Callback_t Decoder= nullptr; /**< Client function */ + void *DecoderData= nullptr; /**< Client function data */ + ~DICacheEntry(); +}; void a_Dicache_init (void);