Home | History | Annotate | Download | only in utils
      1 // Copyright 2012 Google Inc. All Rights Reserved.
      2 //
      3 // Use of this source code is governed by a BSD-style license
      4 // that can be found in the COPYING file in the root of the source
      5 // tree. An additional intellectual property rights grant can be found
      6 // in the file PATENTS. All contributing project authors may
      7 // be found in the AUTHORS file in the root of the source tree.
      8 // -----------------------------------------------------------------------------
      9 //
     10 // Misc. common utility functions
     11 //
     12 // Author: Skal (pascal.massimino (at) gmail.com)
     13 
     14 #include <stdlib.h>
     15 #include <string.h>  // for memcpy()
     16 #include "../webp/decode.h"
     17 #include "../webp/encode.h"
     18 #include "./utils.h"
     19 
     20 // If PRINT_MEM_INFO is defined, extra info (like total memory used, number of
     21 // alloc/free etc) is printed. For debugging/tuning purpose only (it's slow,
     22 // and not multi-thread safe!).
     23 // An interesting alternative is valgrind's 'massif' tool:
     24 //    http://valgrind.org/docs/manual/ms-manual.html
     25 // Here is an example command line:
     26 /*    valgrind --tool=massif --massif-out-file=massif.out \
     27                --stacks=yes --alloc-fn=WebPSafeAlloc --alloc-fn=WebPSafeCalloc
     28       ms_print massif.out
     29 */
     30 // In addition:
     31 // * if PRINT_MEM_TRAFFIC is defined, all the details of the malloc/free cycles
     32 //   are printed.
     33 // * if MALLOC_FAIL_AT is defined, the global environment variable
     34 //   $MALLOC_FAIL_AT is used to simulate a memory error when calloc or malloc
     35 //   is called for the nth time. Example usage:
     36 //   export MALLOC_FAIL_AT=50 && ./examples/cwebp input.png
     37 // * if MALLOC_LIMIT is defined, the global environment variable $MALLOC_LIMIT
     38 //   sets the maximum amount of memory (in bytes) made available to libwebp.
     39 //   This can be used to emulate environment with very limited memory.
     40 //   Example: export MALLOC_LIMIT=64000000 && ./examples/dwebp picture.webp
     41 
     42 // #define PRINT_MEM_INFO
     43 // #define PRINT_MEM_TRAFFIC
     44 // #define MALLOC_FAIL_AT
     45 // #define MALLOC_LIMIT
     46 
     47 //------------------------------------------------------------------------------
     48 // Checked memory allocation
     49 
     50 #if defined(PRINT_MEM_INFO)
     51 
     52 #include <stdio.h>
     53 
     54 static int num_malloc_calls = 0;
     55 static int num_calloc_calls = 0;
     56 static int num_free_calls = 0;
     57 static int countdown_to_fail = 0;     // 0 = off
     58 
     59 typedef struct MemBlock MemBlock;
     60 struct MemBlock {
     61   void* ptr_;
     62   size_t size_;
     63   MemBlock* next_;
     64 };
     65 
     66 static MemBlock* all_blocks = NULL;
     67 static size_t total_mem = 0;
     68 static size_t total_mem_allocated = 0;
     69 static size_t high_water_mark = 0;
     70 static size_t mem_limit = 0;
     71 
     72 static int exit_registered = 0;
     73 
     74 static void PrintMemInfo(void) {
     75   fprintf(stderr, "\nMEMORY INFO:\n");
     76   fprintf(stderr, "num calls to: malloc = %4d\n", num_malloc_calls);
     77   fprintf(stderr, "              calloc = %4d\n", num_calloc_calls);
     78   fprintf(stderr, "              free   = %4d\n", num_free_calls);
     79   fprintf(stderr, "total_mem: %u\n", (uint32_t)total_mem);
     80   fprintf(stderr, "total_mem allocated: %u\n", (uint32_t)total_mem_allocated);
     81   fprintf(stderr, "high-water mark: %u\n", (uint32_t)high_water_mark);
     82   while (all_blocks != NULL) {
     83     MemBlock* b = all_blocks;
     84     all_blocks = b->next_;
     85     free(b);
     86   }
     87 }
     88 
     89 static void Increment(int* const v) {
     90   if (!exit_registered) {
     91 #if defined(MALLOC_FAIL_AT)
     92     {
     93       const char* const malloc_fail_at_str = getenv("MALLOC_FAIL_AT");
     94       if (malloc_fail_at_str != NULL) {
     95         countdown_to_fail = atoi(malloc_fail_at_str);
     96       }
     97     }
     98 #endif
     99 #if defined(MALLOC_LIMIT)
    100     {
    101       const char* const malloc_limit_str = getenv("MALLOC_LIMIT");
    102       if (malloc_limit_str != NULL) {
    103         mem_limit = atoi(malloc_limit_str);
    104       }
    105     }
    106 #endif
    107     (void)countdown_to_fail;
    108     (void)mem_limit;
    109     atexit(PrintMemInfo);
    110     exit_registered = 1;
    111   }
    112   ++*v;
    113 }
    114 
    115 static void AddMem(void* ptr, size_t size) {
    116   if (ptr != NULL) {
    117     MemBlock* const b = (MemBlock*)malloc(sizeof(*b));
    118     if (b == NULL) abort();
    119     b->next_ = all_blocks;
    120     all_blocks = b;
    121     b->ptr_ = ptr;
    122     b->size_ = size;
    123     total_mem += size;
    124     total_mem_allocated += size;
    125 #if defined(PRINT_MEM_TRAFFIC)
    126 #if defined(MALLOC_FAIL_AT)
    127     fprintf(stderr, "fail-count: %5d [mem=%u]\n",
    128             num_malloc_calls + num_calloc_calls, (uint32_t)total_mem);
    129 #else
    130     fprintf(stderr, "Mem: %u (+%u)\n", (uint32_t)total_mem, (uint32_t)size);
    131 #endif
    132 #endif
    133     if (total_mem > high_water_mark) high_water_mark = total_mem;
    134   }
    135 }
    136 
    137 static void SubMem(void* ptr) {
    138   if (ptr != NULL) {
    139     MemBlock** b = &all_blocks;
    140     // Inefficient search, but that's just for debugging.
    141     while (*b != NULL && (*b)->ptr_ != ptr) b = &(*b)->next_;
    142     if (*b == NULL) {
    143       fprintf(stderr, "Invalid pointer free! (%p)\n", ptr);
    144       abort();
    145     }
    146     {
    147       MemBlock* const block = *b;
    148       *b = block->next_;
    149       total_mem -= block->size_;
    150 #if defined(PRINT_MEM_TRAFFIC)
    151       fprintf(stderr, "Mem: %u (-%u)\n",
    152               (uint32_t)total_mem, (uint32_t)block->size_);
    153 #endif
    154       free(block);
    155     }
    156   }
    157 }
    158 
    159 #else
    160 #define Increment(v) do {} while (0)
    161 #define AddMem(p, s) do {} while (0)
    162 #define SubMem(p)    do {} while (0)
    163 #endif
    164 
    165 // Returns 0 in case of overflow of nmemb * size.
    166 static int CheckSizeArgumentsOverflow(uint64_t nmemb, size_t size) {
    167   const uint64_t total_size = nmemb * size;
    168   if (nmemb == 0) return 1;
    169   if ((uint64_t)size > WEBP_MAX_ALLOCABLE_MEMORY / nmemb) return 0;
    170   if (total_size != (size_t)total_size) return 0;
    171 #if defined(PRINT_MEM_INFO) && defined(MALLOC_FAIL_AT)
    172   if (countdown_to_fail > 0 && --countdown_to_fail == 0) {
    173     return 0;    // fake fail!
    174   }
    175 #endif
    176 #if defined(MALLOC_LIMIT)
    177   if (mem_limit > 0 && total_mem + total_size >= mem_limit) {
    178     return 0;   // fake fail!
    179   }
    180 #endif
    181 
    182   return 1;
    183 }
    184 
    185 void* WebPSafeMalloc(uint64_t nmemb, size_t size) {
    186   void* ptr;
    187   Increment(&num_malloc_calls);
    188   if (!CheckSizeArgumentsOverflow(nmemb, size)) return NULL;
    189   assert(nmemb * size > 0);
    190   ptr = malloc((size_t)(nmemb * size));
    191   AddMem(ptr, (size_t)(nmemb * size));
    192   return ptr;
    193 }
    194 
    195 void* WebPSafeCalloc(uint64_t nmemb, size_t size) {
    196   void* ptr;
    197   Increment(&num_calloc_calls);
    198   if (!CheckSizeArgumentsOverflow(nmemb, size)) return NULL;
    199   assert(nmemb * size > 0);
    200   ptr = calloc((size_t)nmemb, size);
    201   AddMem(ptr, (size_t)(nmemb * size));
    202   return ptr;
    203 }
    204 
    205 void WebPSafeFree(void* const ptr) {
    206   if (ptr != NULL) {
    207     Increment(&num_free_calls);
    208     SubMem(ptr);
    209   }
    210   free(ptr);
    211 }
    212 
    213 // Public API function.
    214 void WebPFree(void* ptr) {
    215   free(ptr);
    216 }
    217 
    218 //------------------------------------------------------------------------------
    219 
    220 void WebPCopyPlane(const uint8_t* src, int src_stride,
    221                    uint8_t* dst, int dst_stride, int width, int height) {
    222   assert(src != NULL && dst != NULL);
    223   assert(src_stride >= width && dst_stride >= width);
    224   while (height-- > 0) {
    225     memcpy(dst, src, width);
    226     src += src_stride;
    227     dst += dst_stride;
    228   }
    229 }
    230 
    231 void WebPCopyPixels(const WebPPicture* const src, WebPPicture* const dst) {
    232   assert(src != NULL && dst != NULL);
    233   assert(src->width == dst->width && src->height == dst->height);
    234   assert(src->use_argb && dst->use_argb);
    235   WebPCopyPlane((uint8_t*)src->argb, 4 * src->argb_stride, (uint8_t*)dst->argb,
    236                 4 * dst->argb_stride, 4 * src->width, src->height);
    237 }
    238 
    239 //------------------------------------------------------------------------------
    240