1 // Copyright 2012 Google Inc. All Rights Reserved.
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 // -----------------------------------------------------------------------------
10 // Misc. common utility functions
12 // Author: Skal (pascal.massimino@gmail.com)
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
28 // * if PRINT_MEM_TRAFFIC is defined, all the details of the malloc/free cycles
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)
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
;
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_
;
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
);
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
);
105 (void)countdown_to_fail
;
107 atexit(PrintMemInfo
);
113 static void AddMem(void* ptr
, size_t size
) {
115 MemBlock
* const b
= (MemBlock
*)malloc(sizeof(*b
));
116 if (b
== NULL
) abort();
117 b
->next_
= all_blocks
;
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
);
128 fprintf(stderr
, "Mem: %u (+%u)\n", (uint32_t)total_mem
, (uint32_t)size
);
131 if (total_mem
> high_water_mark
) high_water_mark
= total_mem
;
135 static void SubMem(void* ptr
) {
137 MemBlock
** b
= &all_blocks
;
138 // Inefficient search, but that's just for debugging.
139 while (*b
!= NULL
&& (*b
)->ptr_
!= ptr
) b
= &(*b
)->next_
;
141 fprintf(stderr
, "Invalid pointer free! (%p)\n", ptr
);
145 MemBlock
* const block
= *b
;
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_
);
158 #define Increment(v) do {} while (0)
159 #define AddMem(p, s) do {} while (0)
160 #define SubMem(p) do {} while (0)
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!
174 #if defined(MALLOC_LIMIT)
175 if (mem_limit
> 0 && total_mem
+ total_size
>= mem_limit
) {
176 return 0; // fake fail!
183 void* WebPSafeMalloc(uint64_t nmemb
, size_t size
) {
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
));
193 void* WebPSafeCalloc(uint64_t nmemb
, size_t size
) {
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
));
203 void WebPSafeFree(void* const ptr
) {
205 Increment(&num_free_calls
);
211 //------------------------------------------------------------------------------