remove one malloc() by making color_cache non dynamic

Change-Id: I7c71a056f79a79bfacfe64a263f1eb8476c05456
This commit is contained in:
Pascal Massimino 2012-07-16 19:13:58 -07:00
parent 8130c4cc64
commit 7f0c178e46
4 changed files with 9 additions and 15 deletions

View File

@ -565,13 +565,14 @@ static int DecodeImageData(VP8LDecoder* const dec,
int col = 0, row = 0; int col = 0, row = 0;
VP8LBitReader* const br = &dec->br_; VP8LBitReader* const br = &dec->br_;
VP8LMetadata* const hdr = &dec->hdr_; VP8LMetadata* const hdr = &dec->hdr_;
VP8LColorCache* const color_cache = hdr->color_cache_;
HTreeGroup* htree_group = hdr->htree_groups_; HTreeGroup* htree_group = hdr->htree_groups_;
uint32_t* src = data; uint32_t* src = data;
uint32_t* last_cached = data; uint32_t* last_cached = data;
uint32_t* const src_end = data + width * height; uint32_t* const src_end = data + width * height;
const int len_code_limit = NUM_LITERAL_CODES + NUM_LENGTH_CODES; const int len_code_limit = NUM_LITERAL_CODES + NUM_LENGTH_CODES;
const int color_cache_limit = len_code_limit + hdr->color_cache_size_; 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_; const int mask = hdr->huffman_mask_;
assert(htree_group != NULL); assert(htree_group != NULL);
@ -784,7 +785,7 @@ static void ClearMetadata(VP8LMetadata* const hdr) {
free(hdr->huffman_image_); free(hdr->huffman_image_);
DeleteHtreeGroups(hdr->htree_groups_, hdr->num_htree_groups_); DeleteHtreeGroups(hdr->htree_groups_, hdr->num_htree_groups_);
VP8LColorCacheDelete(hdr->color_cache_); VP8LColorCacheClear(&hdr->color_cache_);
InitMetadata(hdr); InitMetadata(hdr);
} }
@ -875,13 +876,13 @@ static int DecodeImageStream(int xsize, int ysize,
// Finish setting up the color-cache // Finish setting up the color-cache
if (color_cache_bits > 0) { if (color_cache_bits > 0) {
hdr->color_cache_size_ = 1 << color_cache_bits; hdr->color_cache_size_ = 1 << color_cache_bits;
hdr->color_cache_ = (VP8LColorCache*)malloc(sizeof(*hdr->color_cache_)); if (!VP8LColorCacheInit(&hdr->color_cache_, color_cache_bits)) {
if (hdr->color_cache_ == NULL ||
!VP8LColorCacheInit(hdr->color_cache_, color_cache_bits)) {
dec->status_ = VP8_STATUS_OUT_OF_MEMORY; dec->status_ = VP8_STATUS_OUT_OF_MEMORY;
ok = 0; ok = 0;
goto End; goto End;
} }
} else {
hdr->color_cache_size_ = 0;
} }
UpdateDecoder(dec, transform_xsize, transform_ysize); UpdateDecoder(dec, transform_xsize, transform_ysize);

View File

@ -45,7 +45,7 @@ typedef struct {
typedef struct { typedef struct {
int color_cache_size_; int color_cache_size_;
VP8LColorCache *color_cache_; VP8LColorCache color_cache_;
int huffman_mask_; int huffman_mask_;
int huffman_subsample_bits_; int huffman_subsample_bits_;

View File

@ -35,14 +35,10 @@ int VP8LColorCacheInit(VP8LColorCache* const cc, int hash_bits) {
void VP8LColorCacheClear(VP8LColorCache* const cc) { void VP8LColorCacheClear(VP8LColorCache* const cc) {
if (cc != NULL) { if (cc != NULL) {
free(cc->colors_); free(cc->colors_);
cc->colors_ = NULL;
} }
} }
void VP8LColorCacheDelete(VP8LColorCache* const cc) {
VP8LColorCacheClear(cc);
free(cc);
}
#if defined(__cplusplus) || defined(c_plusplus) #if defined(__cplusplus) || defined(c_plusplus)
} }
#endif #endif

View File

@ -56,12 +56,9 @@ static WEBP_INLINE int VP8LColorCacheContains(const VP8LColorCache* const cc,
// Returns false in case of memory error. // Returns false in case of memory error.
int VP8LColorCacheInit(VP8LColorCache* const color_cache, int hash_bits); 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); void VP8LColorCacheClear(VP8LColorCache* const color_cache);
// Delete the color_cache object.
void VP8LColorCacheDelete(VP8LColorCache* const color_cache);
//------------------------------------------------------------------------------ //------------------------------------------------------------------------------
#if defined(__cplusplus) || defined(c_plusplus) #if defined(__cplusplus) || defined(c_plusplus)