Update mojo sdk to rev 1dc8a9a5db73d3718d99917fadf31f5fb2ebad4f
[chromium-blink-merge.git] / third_party / libwebp / utils / utils.c
blob8ff7f12fac7f283ab933af1f5cfe68e149cbc305
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
12 // Author: Skal (pascal.massimino@gmail.com)
14 #include <stdlib.h>
15 #include "./utils.h"
17 // If PRINT_MEM_INFO is defined, extra info (like total memory used, number of
18 // alloc/free etc) is printed. For debugging/tuning purpose only (it's slow,
19 // and not multi-thread safe!).
20 // An interesting alternative is valgrind's 'massif' tool:
21 // http://valgrind.org/docs/manual/ms-manual.html
22 // Here is an example command line:
23 /* valgrind --tool=massif --massif-out-file=massif.out \
24 --stacks=yes --alloc-fn=WebPSafeAlloc --alloc-fn=WebPSafeCalloc
25 ms_print massif.out
27 // In addition:
28 // * if PRINT_MEM_TRAFFIC is defined, all the details of the malloc/free cycles
29 // are printed.
30 // * if MALLOC_FAIL_AT is defined, the global environment variable
31 // $MALLOC_FAIL_AT is used to simulate a memory error when calloc or malloc
32 // is called for the nth time. Example usage:
33 // export MALLOC_FAIL_AT=50 && ./examples/cwebp input.png
34 // * if MALLOC_LIMIT is defined, the global environment variable $MALLOC_LIMIT
35 // sets the maximum amount of memory (in bytes) made available to libwebp.
36 // This can be used to emulate environment with very limited memory.
37 // Example: export MALLOC_LIMIT=64000000 && ./examples/dwebp picture.webp
39 // #define PRINT_MEM_INFO
40 // #define PRINT_MEM_TRAFFIC
41 // #define MALLOC_FAIL_AT
42 // #define MALLOC_LIMIT
44 //------------------------------------------------------------------------------
45 // Checked memory allocation
47 #if defined(PRINT_MEM_INFO)
49 #include <stdio.h>
50 #include <stdlib.h> // for abort()
52 static int num_malloc_calls = 0;
53 static int num_calloc_calls = 0;
54 static int num_free_calls = 0;
55 static int countdown_to_fail = 0; // 0 = off
57 typedef struct MemBlock MemBlock;
58 struct MemBlock {
59 void* ptr_;
60 size_t size_;
61 MemBlock* next_;
64 static MemBlock* all_blocks = NULL;
65 static size_t total_mem = 0;
66 static size_t total_mem_allocated = 0;
67 static size_t high_water_mark = 0;
68 static size_t mem_limit = 0;
70 static int exit_registered = 0;
72 static void PrintMemInfo(void) {
73 fprintf(stderr, "\nMEMORY INFO:\n");
74 fprintf(stderr, "num calls to: malloc = %4d\n", num_malloc_calls);
75 fprintf(stderr, " calloc = %4d\n", num_calloc_calls);
76 fprintf(stderr, " free = %4d\n", num_free_calls);
77 fprintf(stderr, "total_mem: %u\n", (uint32_t)total_mem);
78 fprintf(stderr, "total_mem allocated: %u\n", (uint32_t)total_mem_allocated);
79 fprintf(stderr, "high-water mark: %u\n", (uint32_t)high_water_mark);
80 while (all_blocks != NULL) {
81 MemBlock* b = all_blocks;
82 all_blocks = b->next_;
83 free(b);
87 static void Increment(int* const v) {
88 if (!exit_registered) {
89 #if defined(MALLOC_FAIL_AT)
91 const char* const malloc_fail_at_str = getenv("MALLOC_FAIL_AT");
92 if (malloc_fail_at_str != NULL) {
93 countdown_to_fail = atoi(malloc_fail_at_str);
96 #endif
97 #if defined(MALLOC_LIMIT)
99 const char* const malloc_limit_str = getenv("MALLOC_LIMIT");
100 if (malloc_limit_str != NULL) {
101 mem_limit = atoi(malloc_limit_str);
104 #endif
105 (void)countdown_to_fail;
106 (void)mem_limit;
107 atexit(PrintMemInfo);
108 exit_registered = 1;
110 ++*v;
113 static void AddMem(void* ptr, size_t size) {
114 if (ptr != NULL) {
115 MemBlock* const b = (MemBlock*)malloc(sizeof(*b));
116 if (b == NULL) abort();
117 b->next_ = all_blocks;
118 all_blocks = b;
119 b->ptr_ = ptr;
120 b->size_ = size;
121 total_mem += size;
122 total_mem_allocated += size;
123 #if defined(PRINT_MEM_TRAFFIC)
124 #if defined(MALLOC_FAIL_AT)
125 fprintf(stderr, "fail-count: %5d [mem=%u]\n",
126 num_malloc_calls + num_calloc_calls, (uint32_t)total_mem);
127 #else
128 fprintf(stderr, "Mem: %u (+%u)\n", (uint32_t)total_mem, (uint32_t)size);
129 #endif
130 #endif
131 if (total_mem > high_water_mark) high_water_mark = total_mem;
135 static void SubMem(void* ptr) {
136 if (ptr != NULL) {
137 MemBlock** b = &all_blocks;
138 // Inefficient search, but that's just for debugging.
139 while (*b != NULL && (*b)->ptr_ != ptr) b = &(*b)->next_;
140 if (*b == NULL) {
141 fprintf(stderr, "Invalid pointer free! (%p)\n", ptr);
142 abort();
145 MemBlock* const block = *b;
146 *b = block->next_;
147 total_mem -= block->size_;
148 #if defined(PRINT_MEM_TRAFFIC)
149 fprintf(stderr, "Mem: %u (-%u)\n",
150 (uint32_t)total_mem, (uint32_t)block->size_);
151 #endif
152 free(block);
157 #else
158 #define Increment(v) do {} while (0)
159 #define AddMem(p, s) do {} while (0)
160 #define SubMem(p) do {} while (0)
161 #endif
163 // Returns 0 in case of overflow of nmemb * size.
164 static int CheckSizeArgumentsOverflow(uint64_t nmemb, size_t size) {
165 const uint64_t total_size = nmemb * size;
166 if (nmemb == 0) return 1;
167 if ((uint64_t)size > WEBP_MAX_ALLOCABLE_MEMORY / nmemb) return 0;
168 if (total_size != (size_t)total_size) return 0;
169 #if defined(PRINT_MEM_INFO) && defined(MALLOC_FAIL_AT)
170 if (countdown_to_fail > 0 && --countdown_to_fail == 0) {
171 return 0; // fake fail!
173 #endif
174 #if defined(MALLOC_LIMIT)
175 if (mem_limit > 0 && total_mem + total_size >= mem_limit) {
176 return 0; // fake fail!
178 #endif
180 return 1;
183 void* WebPSafeMalloc(uint64_t nmemb, size_t size) {
184 void* ptr;
185 Increment(&num_malloc_calls);
186 if (!CheckSizeArgumentsOverflow(nmemb, size)) return NULL;
187 assert(nmemb * size > 0);
188 ptr = malloc((size_t)(nmemb * size));
189 AddMem(ptr, (size_t)(nmemb * size));
190 return ptr;
193 void* WebPSafeCalloc(uint64_t nmemb, size_t size) {
194 void* ptr;
195 Increment(&num_calloc_calls);
196 if (!CheckSizeArgumentsOverflow(nmemb, size)) return NULL;
197 assert(nmemb * size > 0);
198 ptr = calloc((size_t)nmemb, size);
199 AddMem(ptr, (size_t)(nmemb * size));
200 return ptr;
203 void WebPSafeFree(void* const ptr) {
204 if (ptr != NULL) {
205 Increment(&num_free_calls);
206 SubMem(ptr);
208 free(ptr);
211 //------------------------------------------------------------------------------