From 7f0c178e462b0d9d57edf8a08c26a2c478a3fd35 Mon Sep 17 00:00:00 2001 From: Pascal Massimino Date: Mon, 16 Jul 2012 19:13:58 -0700 Subject: [PATCH] remove one malloc() by making color_cache non dynamic Change-Id: I7c71a056f79a79bfacfe64a263f1eb8476c05456 --- src/dec/vp8l.c | 11 ++++++----- src/dec/vp8li.h | 2 +- src/utils/color_cache.c | 6 +----- src/utils/color_cache.h | 5 +---- 4 files changed, 9 insertions(+), 15 deletions(-) diff --git a/src/dec/vp8l.c b/src/dec/vp8l.c index 473bb300..8fa211ec 100644 --- a/src/dec/vp8l.c +++ b/src/dec/vp8l.c @@ -565,13 +565,14 @@ static int DecodeImageData(VP8LDecoder* const dec, int col = 0, row = 0; VP8LBitReader* const br = &dec->br_; VP8LMetadata* const hdr = &dec->hdr_; - VP8LColorCache* const color_cache = hdr->color_cache_; HTreeGroup* htree_group = hdr->htree_groups_; uint32_t* src = data; uint32_t* last_cached = data; uint32_t* const src_end = data + width * height; const int len_code_limit = NUM_LITERAL_CODES + NUM_LENGTH_CODES; const int color_cache_limit = len_code_limit + hdr->color_cache_size_; + VP8LColorCache* const color_cache = + (hdr->color_cache_size_ > 0) ? &hdr->color_cache_ : NULL; const int mask = hdr->huffman_mask_; assert(htree_group != NULL); @@ -784,7 +785,7 @@ static void ClearMetadata(VP8LMetadata* const hdr) { free(hdr->huffman_image_); DeleteHtreeGroups(hdr->htree_groups_, hdr->num_htree_groups_); - VP8LColorCacheDelete(hdr->color_cache_); + VP8LColorCacheClear(&hdr->color_cache_); InitMetadata(hdr); } @@ -875,13 +876,13 @@ static int DecodeImageStream(int xsize, int ysize, // Finish setting up the color-cache if (color_cache_bits > 0) { hdr->color_cache_size_ = 1 << color_cache_bits; - hdr->color_cache_ = (VP8LColorCache*)malloc(sizeof(*hdr->color_cache_)); - if (hdr->color_cache_ == NULL || - !VP8LColorCacheInit(hdr->color_cache_, color_cache_bits)) { + if (!VP8LColorCacheInit(&hdr->color_cache_, color_cache_bits)) { dec->status_ = VP8_STATUS_OUT_OF_MEMORY; ok = 0; goto End; } + } else { + hdr->color_cache_size_ = 0; } UpdateDecoder(dec, transform_xsize, transform_ysize); diff --git a/src/dec/vp8li.h b/src/dec/vp8li.h index 82d943da..542dbb71 100644 --- a/src/dec/vp8li.h +++ b/src/dec/vp8li.h @@ -45,7 +45,7 @@ typedef struct { typedef struct { int color_cache_size_; - VP8LColorCache *color_cache_; + VP8LColorCache color_cache_; int huffman_mask_; int huffman_subsample_bits_; diff --git a/src/utils/color_cache.c b/src/utils/color_cache.c index 9b1be7a5..7d3ebea6 100644 --- a/src/utils/color_cache.c +++ b/src/utils/color_cache.c @@ -35,14 +35,10 @@ int VP8LColorCacheInit(VP8LColorCache* const cc, int hash_bits) { void VP8LColorCacheClear(VP8LColorCache* const cc) { if (cc != NULL) { free(cc->colors_); + cc->colors_ = NULL; } } -void VP8LColorCacheDelete(VP8LColorCache* const cc) { - VP8LColorCacheClear(cc); - free(cc); -} - #if defined(__cplusplus) || defined(c_plusplus) } #endif diff --git a/src/utils/color_cache.h b/src/utils/color_cache.h index d37425c2..e99b9dfd 100644 --- a/src/utils/color_cache.h +++ b/src/utils/color_cache.h @@ -56,12 +56,9 @@ static WEBP_INLINE int VP8LColorCacheContains(const VP8LColorCache* const cc, // Returns false in case of memory error. int VP8LColorCacheInit(VP8LColorCache* const color_cache, int hash_bits); -// Delete the color cache. +// Delete the memory associated to color cache. void VP8LColorCacheClear(VP8LColorCache* const color_cache); -// Delete the color_cache object. -void VP8LColorCacheDelete(VP8LColorCache* const color_cache); - //------------------------------------------------------------------------------ #if defined(__cplusplus) || defined(c_plusplus)