Path: blob/master/thirdparty/libwebp/src/utils/utils.c
9912 views
// Copyright 2012 Google Inc. All Rights Reserved.1//2// Use of this source code is governed by a BSD-style license3// that can be found in the COPYING file in the root of the source4// tree. An additional intellectual property rights grant can be found5// in the file PATENTS. All contributing project authors may6// be found in the AUTHORS file in the root of the source tree.7// -----------------------------------------------------------------------------8//9// Misc. common utility functions10//11// Author: Skal ([email protected])1213#include "src/utils/utils.h"1415#include <stdlib.h>16#include <string.h> // for memcpy()1718#include "src/utils/palette.h"19#include "src/webp/encode.h"2021// If PRINT_MEM_INFO is defined, extra info (like total memory used, number of22// alloc/free etc) is printed. For debugging/tuning purpose only (it's slow,23// and not multi-thread safe!).24// An interesting alternative is valgrind's 'massif' tool:25// https://valgrind.org/docs/manual/ms-manual.html26// Here is an example command line:27/* valgrind --tool=massif --massif-out-file=massif.out \28--stacks=yes --alloc-fn=WebPSafeMalloc --alloc-fn=WebPSafeCalloc29ms_print massif.out30*/31// In addition:32// * if PRINT_MEM_TRAFFIC is defined, all the details of the malloc/free cycles33// are printed.34// * if MALLOC_FAIL_AT is defined, the global environment variable35// $MALLOC_FAIL_AT is used to simulate a memory error when calloc or malloc36// is called for the nth time. Example usage:37// export MALLOC_FAIL_AT=50 && ./examples/cwebp input.png38// * if MALLOC_LIMIT is defined, the global environment variable $MALLOC_LIMIT39// sets the maximum amount of memory (in bytes) made available to libwebp.40// This can be used to emulate environment with very limited memory.41// Example: export MALLOC_LIMIT=64000000 && ./examples/dwebp picture.webp4243// #define PRINT_MEM_INFO44// #define PRINT_MEM_TRAFFIC45// #define MALLOC_FAIL_AT46// #define MALLOC_LIMIT4748//------------------------------------------------------------------------------49// Checked memory allocation5051#if defined(PRINT_MEM_INFO)5253#include <stdio.h>5455static int num_malloc_calls = 0;56static int num_calloc_calls = 0;57static int num_free_calls = 0;58static int countdown_to_fail = 0; // 0 = off5960typedef struct MemBlock MemBlock;61struct MemBlock {62void* ptr_;63size_t size_;64MemBlock* next_;65};6667static MemBlock* all_blocks = NULL;68static size_t total_mem = 0;69static size_t total_mem_allocated = 0;70static size_t high_water_mark = 0;71static size_t mem_limit = 0;7273static int exit_registered = 0;7475static void PrintMemInfo(void) {76fprintf(stderr, "\nMEMORY INFO:\n");77fprintf(stderr, "num calls to: malloc = %4d\n", num_malloc_calls);78fprintf(stderr, " calloc = %4d\n", num_calloc_calls);79fprintf(stderr, " free = %4d\n", num_free_calls);80fprintf(stderr, "total_mem: %u\n", (uint32_t)total_mem);81fprintf(stderr, "total_mem allocated: %u\n", (uint32_t)total_mem_allocated);82fprintf(stderr, "high-water mark: %u\n", (uint32_t)high_water_mark);83while (all_blocks != NULL) {84MemBlock* b = all_blocks;85all_blocks = b->next_;86free(b);87}88}8990static void Increment(int* const v) {91if (!exit_registered) {92#if defined(MALLOC_FAIL_AT)93{94const char* const malloc_fail_at_str = getenv("MALLOC_FAIL_AT");95if (malloc_fail_at_str != NULL) {96countdown_to_fail = atoi(malloc_fail_at_str);97}98}99#endif100#if defined(MALLOC_LIMIT)101{102const char* const malloc_limit_str = getenv("MALLOC_LIMIT");103#if MALLOC_LIMIT > 1104mem_limit = (size_t)MALLOC_LIMIT;105#endif106if (malloc_limit_str != NULL) {107mem_limit = atoi(malloc_limit_str);108}109}110#endif111(void)countdown_to_fail;112(void)mem_limit;113atexit(PrintMemInfo);114exit_registered = 1;115}116++*v;117}118119static void AddMem(void* ptr, size_t size) {120if (ptr != NULL) {121MemBlock* const b = (MemBlock*)malloc(sizeof(*b));122if (b == NULL) abort();123b->next_ = all_blocks;124all_blocks = b;125b->ptr_ = ptr;126b->size_ = size;127total_mem += size;128total_mem_allocated += size;129#if defined(PRINT_MEM_TRAFFIC)130#if defined(MALLOC_FAIL_AT)131fprintf(stderr, "fail-count: %5d [mem=%u]\n",132num_malloc_calls + num_calloc_calls, (uint32_t)total_mem);133#else134fprintf(stderr, "Mem: %u (+%u)\n", (uint32_t)total_mem, (uint32_t)size);135#endif136#endif137if (total_mem > high_water_mark) high_water_mark = total_mem;138}139}140141static void SubMem(void* ptr) {142if (ptr != NULL) {143MemBlock** b = &all_blocks;144// Inefficient search, but that's just for debugging.145while (*b != NULL && (*b)->ptr_ != ptr) b = &(*b)->next_;146if (*b == NULL) {147fprintf(stderr, "Invalid pointer free! (%p)\n", ptr);148abort();149}150{151MemBlock* const block = *b;152*b = block->next_;153total_mem -= block->size_;154#if defined(PRINT_MEM_TRAFFIC)155fprintf(stderr, "Mem: %u (-%u)\n",156(uint32_t)total_mem, (uint32_t)block->size_);157#endif158free(block);159}160}161}162163#else164#define Increment(v) do {} while (0)165#define AddMem(p, s) do {} while (0)166#define SubMem(p) do {} while (0)167#endif168169// Returns 0 in case of overflow of nmemb * size.170static int CheckSizeArgumentsOverflow(uint64_t nmemb, size_t size) {171const uint64_t total_size = nmemb * size;172if (nmemb == 0) return 1;173if ((uint64_t)size > WEBP_MAX_ALLOCABLE_MEMORY / nmemb) return 0;174if (!CheckSizeOverflow(total_size)) return 0;175#if defined(PRINT_MEM_INFO) && defined(MALLOC_FAIL_AT)176if (countdown_to_fail > 0 && --countdown_to_fail == 0) {177return 0; // fake fail!178}179#endif180#if defined(PRINT_MEM_INFO) && defined(MALLOC_LIMIT)181if (mem_limit > 0) {182const uint64_t new_total_mem = (uint64_t)total_mem + total_size;183if (!CheckSizeOverflow(new_total_mem) ||184new_total_mem > mem_limit) {185return 0; // fake fail!186}187}188#endif189190return 1;191}192193void* WebPSafeMalloc(uint64_t nmemb, size_t size) {194void* ptr;195Increment(&num_malloc_calls);196if (!CheckSizeArgumentsOverflow(nmemb, size)) return NULL;197assert(nmemb * size > 0);198ptr = malloc((size_t)(nmemb * size));199AddMem(ptr, (size_t)(nmemb * size));200return ptr;201}202203void* WebPSafeCalloc(uint64_t nmemb, size_t size) {204void* ptr;205Increment(&num_calloc_calls);206if (!CheckSizeArgumentsOverflow(nmemb, size)) return NULL;207assert(nmemb * size > 0);208ptr = calloc((size_t)nmemb, size);209AddMem(ptr, (size_t)(nmemb * size));210return ptr;211}212213void WebPSafeFree(void* const ptr) {214if (ptr != NULL) {215Increment(&num_free_calls);216SubMem(ptr);217}218free(ptr);219}220221// Public API functions.222223void* WebPMalloc(size_t size) {224return WebPSafeMalloc(1, size);225}226227void WebPFree(void* ptr) {228WebPSafeFree(ptr);229}230231//------------------------------------------------------------------------------232233void WebPCopyPlane(const uint8_t* src, int src_stride,234uint8_t* dst, int dst_stride, int width, int height) {235assert(src != NULL && dst != NULL);236assert(abs(src_stride) >= width && abs(dst_stride) >= width);237while (height-- > 0) {238memcpy(dst, src, width);239src += src_stride;240dst += dst_stride;241}242}243244void WebPCopyPixels(const WebPPicture* const src, WebPPicture* const dst) {245assert(src != NULL && dst != NULL);246assert(src->width == dst->width && src->height == dst->height);247assert(src->use_argb && dst->use_argb);248WebPCopyPlane((uint8_t*)src->argb, 4 * src->argb_stride, (uint8_t*)dst->argb,2494 * dst->argb_stride, 4 * src->width, src->height);250}251252//------------------------------------------------------------------------------253254int WebPGetColorPalette(const WebPPicture* const pic, uint32_t* const palette) {255return GetColorPalette(pic, palette);256}257258//------------------------------------------------------------------------------259260#if defined(WEBP_NEED_LOG_TABLE_8BIT)261const uint8_t WebPLogTable8bit[256] = { // 31 ^ clz(i)2620, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3,2634, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,2645, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,2655, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,2666, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,2676, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,2686, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,2696, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,2707, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,2717, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,2727, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,2737, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,2747, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,2757, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,2767, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7,2777, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7278};279#endif280281//------------------------------------------------------------------------------282283284