| OLD | NEW |
| 1 // Copyright 2012 Google Inc. All Rights Reserved. | 1 // Copyright 2012 Google Inc. All Rights Reserved. |
| 2 // | 2 // |
| 3 // Use of this source code is governed by a BSD-style license | 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 | 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 | 5 // tree. An additional intellectual property rights grant can be found |
| 6 // in the file PATENTS. All contributing project authors may | 6 // in the file PATENTS. All contributing project authors may |
| 7 // be found in the AUTHORS file in the root of the source tree. | 7 // be found in the AUTHORS file in the root of the source tree. |
| 8 // ----------------------------------------------------------------------------- | 8 // ----------------------------------------------------------------------------- |
| 9 // | 9 // |
| 10 // Misc. common utility functions | 10 // Misc. common utility functions |
| 11 // | 11 // |
| 12 // Author: Skal (pascal.massimino@gmail.com) | 12 // Author: Skal (pascal.massimino@gmail.com) |
| 13 | 13 |
| 14 #include <stdlib.h> | 14 #include <stdlib.h> |
| 15 #include <string.h> // for memcpy() |
| 16 #include "../webp/decode.h" |
| 17 #include "../webp/encode.h" |
| 15 #include "./utils.h" | 18 #include "./utils.h" |
| 16 | 19 |
| 17 // If PRINT_MEM_INFO is defined, extra info (like total memory used, number of | 20 // 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, | 21 // alloc/free etc) is printed. For debugging/tuning purpose only (it's slow, |
| 19 // and not multi-thread safe!). | 22 // and not multi-thread safe!). |
| 20 // An interesting alternative is valgrind's 'massif' tool: | 23 // An interesting alternative is valgrind's 'massif' tool: |
| 21 // http://valgrind.org/docs/manual/ms-manual.html | 24 // http://valgrind.org/docs/manual/ms-manual.html |
| 22 // Here is an example command line: | 25 // Here is an example command line: |
| 23 /* valgrind --tool=massif --massif-out-file=massif.out \ | 26 /* valgrind --tool=massif --massif-out-file=massif.out \ |
| 24 --stacks=yes --alloc-fn=WebPSafeAlloc --alloc-fn=WebPSafeCalloc | 27 --stacks=yes --alloc-fn=WebPSafeAlloc --alloc-fn=WebPSafeCalloc |
| (...skipping 15 matching lines...) Expand all Loading... |
| 40 // #define PRINT_MEM_TRAFFIC | 43 // #define PRINT_MEM_TRAFFIC |
| 41 // #define MALLOC_FAIL_AT | 44 // #define MALLOC_FAIL_AT |
| 42 // #define MALLOC_LIMIT | 45 // #define MALLOC_LIMIT |
| 43 | 46 |
| 44 //------------------------------------------------------------------------------ | 47 //------------------------------------------------------------------------------ |
| 45 // Checked memory allocation | 48 // Checked memory allocation |
| 46 | 49 |
| 47 #if defined(PRINT_MEM_INFO) | 50 #if defined(PRINT_MEM_INFO) |
| 48 | 51 |
| 49 #include <stdio.h> | 52 #include <stdio.h> |
| 50 #include <stdlib.h> // for abort() | |
| 51 | 53 |
| 52 static int num_malloc_calls = 0; | 54 static int num_malloc_calls = 0; |
| 53 static int num_calloc_calls = 0; | 55 static int num_calloc_calls = 0; |
| 54 static int num_free_calls = 0; | 56 static int num_free_calls = 0; |
| 55 static int countdown_to_fail = 0; // 0 = off | 57 static int countdown_to_fail = 0; // 0 = off |
| 56 | 58 |
| 57 typedef struct MemBlock MemBlock; | 59 typedef struct MemBlock MemBlock; |
| 58 struct MemBlock { | 60 struct MemBlock { |
| 59 void* ptr_; | 61 void* ptr_; |
| 60 size_t size_; | 62 size_t size_; |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 201 } | 203 } |
| 202 | 204 |
| 203 void WebPSafeFree(void* const ptr) { | 205 void WebPSafeFree(void* const ptr) { |
| 204 if (ptr != NULL) { | 206 if (ptr != NULL) { |
| 205 Increment(&num_free_calls); | 207 Increment(&num_free_calls); |
| 206 SubMem(ptr); | 208 SubMem(ptr); |
| 207 } | 209 } |
| 208 free(ptr); | 210 free(ptr); |
| 209 } | 211 } |
| 210 | 212 |
| 213 // Public API function. |
| 214 void WebPFree(void* ptr) { |
| 215 free(ptr); |
| 216 } |
| 217 |
| 211 //------------------------------------------------------------------------------ | 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 //------------------------------------------------------------------------------ |
| OLD | NEW |