Download 3rdparty/libwebp/src/utils/utils.c from tgollogly/test: direct link, hf CLI and curl.
- Browser
- Download file 8.86 kB
-
https://huggingface.co/tgollogly/test/resolve/main/3rdparty/libwebp/src/utils/utils.c
- Command line
-
hf download hf://tgollogly/test/3rdparty/libwebp/src/utils/utils.c
-
curl -L -o utils.c https://huggingface.co/tgollogly/test/resolve/main/3rdparty/libwebp/src/utils/utils.c
8.86 kB
| // Copyright 2012 Google Inc. All Rights Reserved. | |
| // | |
| // Use of this source code is governed by a BSD-style license | |
| // that can be found in the COPYING file in the root of the source | |
| // tree. An additional intellectual property rights grant can be found | |
| // in the file PATENTS. All contributing project authors may | |
| // be found in the AUTHORS file in the root of the source tree. | |
| // ----------------------------------------------------------------------------- | |
| // | |
| // Misc. common utility functions | |
| // | |
| // Author: Skal (pascal.massimino@gmail.com) | |
| // If PRINT_MEM_INFO is defined, extra info (like total memory used, number of | |
| // alloc/free etc) is printed. For debugging/tuning purpose only (it's slow, | |
| // and not multi-thread safe!). | |
| // An interesting alternative is valgrind's 'massif' tool: | |
| // https://valgrind.org/docs/manual/ms-manual.html | |
| // Here is an example command line: | |
| /* valgrind --tool=massif --massif-out-file=massif.out \ | |
| --stacks=yes --alloc-fn=WebPSafeMalloc --alloc-fn=WebPSafeCalloc | |
| ms_print massif.out | |
| */ | |
| // In addition: | |
| // * if PRINT_MEM_TRAFFIC is defined, all the details of the malloc/free cycles | |
| // are printed. | |
| // * if MALLOC_FAIL_AT is defined, the global environment variable | |
| // $MALLOC_FAIL_AT is used to simulate a memory error when calloc or malloc | |
| // is called for the nth time. Example usage: | |
| // export MALLOC_FAIL_AT=50 && ./examples/cwebp input.png | |
| // * if MALLOC_LIMIT is defined, the global environment variable $MALLOC_LIMIT | |
| // sets the maximum amount of memory (in bytes) made available to libwebp. | |
| // This can be used to emulate environment with very limited memory. | |
| // Example: export MALLOC_LIMIT=64000000 && ./examples/dwebp picture.webp | |
| // #define PRINT_MEM_INFO | |
| // #define PRINT_MEM_TRAFFIC | |
| // #define MALLOC_FAIL_AT | |
| // #define MALLOC_LIMIT | |
| //------------------------------------------------------------------------------ | |
| // Checked memory allocation | |
| static int num_malloc_calls = 0; | |
| static int num_calloc_calls = 0; | |
| static int num_free_calls = 0; | |
| static int countdown_to_fail = 0; // 0 = off | |
| typedef struct MemBlock MemBlock; | |
| struct MemBlock { | |
| void* ptr_; | |
| size_t size_; | |
| MemBlock* next_; | |
| }; | |
| static MemBlock* all_blocks = NULL; | |
| static size_t total_mem = 0; | |
| static size_t total_mem_allocated = 0; | |
| static size_t high_water_mark = 0; | |
| static size_t mem_limit = 0; | |
| static int exit_registered = 0; | |
| static void PrintMemInfo(void) { | |
| fprintf(stderr, "\nMEMORY INFO:\n"); | |
| fprintf(stderr, "num calls to: malloc = %4d\n", num_malloc_calls); | |
| fprintf(stderr, " calloc = %4d\n", num_calloc_calls); | |
| fprintf(stderr, " free = %4d\n", num_free_calls); | |
| fprintf(stderr, "total_mem: %u\n", (uint32_t)total_mem); | |
| fprintf(stderr, "total_mem allocated: %u\n", (uint32_t)total_mem_allocated); | |
| fprintf(stderr, "high-water mark: %u\n", (uint32_t)high_water_mark); | |
| while (all_blocks != NULL) { | |
| MemBlock* b = all_blocks; | |
| all_blocks = b->next_; | |
| free(b); | |
| } | |
| } | |
| static void Increment(int* const v) { | |
| if (!exit_registered) { | |
| { | |
| const char* const malloc_fail_at_str = getenv("MALLOC_FAIL_AT"); | |
| if (malloc_fail_at_str != NULL) { | |
| countdown_to_fail = atoi(malloc_fail_at_str); | |
| } | |
| } | |
| { | |
| const char* const malloc_limit_str = getenv("MALLOC_LIMIT"); | |
| mem_limit = (size_t)MALLOC_LIMIT; | |
| if (malloc_limit_str != NULL) { | |
| mem_limit = atoi(malloc_limit_str); | |
| } | |
| } | |
| (void)countdown_to_fail; | |
| (void)mem_limit; | |
| atexit(PrintMemInfo); | |
| exit_registered = 1; | |
| } | |
| ++*v; | |
| } | |
| static void AddMem(void* ptr, size_t size) { | |
| if (ptr != NULL) { | |
| MemBlock* const b = (MemBlock*)malloc(sizeof(*b)); | |
| if (b == NULL) abort(); | |
| b->next_ = all_blocks; | |
| all_blocks = b; | |
| b->ptr_ = ptr; | |
| b->size_ = size; | |
| total_mem += size; | |
| total_mem_allocated += size; | |
| fprintf(stderr, "fail-count: %5d [mem=%u]\n", | |
| num_malloc_calls + num_calloc_calls, (uint32_t)total_mem); | |
| fprintf(stderr, "Mem: %u (+%u)\n", (uint32_t)total_mem, (uint32_t)size); | |
| if (total_mem > high_water_mark) high_water_mark = total_mem; | |
| } | |
| } | |
| static void SubMem(void* ptr) { | |
| if (ptr != NULL) { | |
| MemBlock** b = &all_blocks; | |
| // Inefficient search, but that's just for debugging. | |
| while (*b != NULL && (*b)->ptr_ != ptr) b = &(*b)->next_; | |
| if (*b == NULL) { | |
| fprintf(stderr, "Invalid pointer free! (%p)\n", ptr); | |
| abort(); | |
| } | |
| { | |
| MemBlock* const block = *b; | |
| *b = block->next_; | |
| total_mem -= block->size_; | |
| fprintf(stderr, "Mem: %u (-%u)\n", | |
| (uint32_t)total_mem, (uint32_t)block->size_); | |
| free(block); | |
| } | |
| } | |
| } | |
| // Returns 0 in case of overflow of nmemb * size. | |
| static int CheckSizeArgumentsOverflow(uint64_t nmemb, size_t size) { | |
| const uint64_t total_size = nmemb * size; | |
| if (nmemb == 0) return 1; | |
| if ((uint64_t)size > WEBP_MAX_ALLOCABLE_MEMORY / nmemb) return 0; | |
| if (!CheckSizeOverflow(total_size)) return 0; | |
| if (countdown_to_fail > 0 && --countdown_to_fail == 0) { | |
| return 0; // fake fail! | |
| } | |
| if (mem_limit > 0) { | |
| const uint64_t new_total_mem = (uint64_t)total_mem + total_size; | |
| if (!CheckSizeOverflow(new_total_mem) || | |
| new_total_mem > mem_limit) { | |
| return 0; // fake fail! | |
| } | |
| } | |
| return 1; | |
| } | |
| void* WebPSafeMalloc(uint64_t nmemb, size_t size) { | |
| void* ptr; | |
| Increment(&num_malloc_calls); | |
| if (!CheckSizeArgumentsOverflow(nmemb, size)) return NULL; | |
| assert(nmemb * size > 0); | |
| ptr = malloc((size_t)(nmemb * size)); | |
| AddMem(ptr, (size_t)(nmemb * size)); | |
| return ptr; | |
| } | |
| void* WebPSafeCalloc(uint64_t nmemb, size_t size) { | |
| void* ptr; | |
| Increment(&num_calloc_calls); | |
| if (!CheckSizeArgumentsOverflow(nmemb, size)) return NULL; | |
| assert(nmemb * size > 0); | |
| ptr = calloc((size_t)nmemb, size); | |
| AddMem(ptr, (size_t)(nmemb * size)); | |
| return ptr; | |
| } | |
| void WebPSafeFree(void* const ptr) { | |
| if (ptr != NULL) { | |
| Increment(&num_free_calls); | |
| SubMem(ptr); | |
| } | |
| free(ptr); | |
| } | |
| // Public API functions. | |
| void* WebPMalloc(size_t size) { | |
| return WebPSafeMalloc(1, size); | |
| } | |
| void WebPFree(void* ptr) { | |
| WebPSafeFree(ptr); | |
| } | |
| //------------------------------------------------------------------------------ | |
| void WebPCopyPlane(const uint8_t* src, int src_stride, | |
| uint8_t* dst, int dst_stride, int width, int height) { | |
| assert(src != NULL && dst != NULL); | |
| assert(abs(src_stride) >= width && abs(dst_stride) >= width); | |
| while (height-- > 0) { | |
| memcpy(dst, src, width); | |
| src += src_stride; | |
| dst += dst_stride; | |
| } | |
| } | |
| void WebPCopyPixels(const WebPPicture* const src, WebPPicture* const dst) { | |
| assert(src != NULL && dst != NULL); | |
| assert(src->width == dst->width && src->height == dst->height); | |
| assert(src->use_argb && dst->use_argb); | |
| WebPCopyPlane((uint8_t*)src->argb, 4 * src->argb_stride, (uint8_t*)dst->argb, | |
| 4 * dst->argb_stride, 4 * src->width, src->height); | |
| } | |
| //------------------------------------------------------------------------------ | |
| int WebPGetColorPalette(const WebPPicture* const pic, uint32_t* const palette) { | |
| return GetColorPalette(pic, palette); | |
| } | |
| //------------------------------------------------------------------------------ | |
| const uint8_t WebPLogTable8bit[256] = { // 31 ^ clz(i) | |
| 0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, | |
| 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, | |
| 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, | |
| 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, | |
| 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, | |
| 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, | |
| 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, | |
| 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, | |
| 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, | |
| 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, | |
| 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, | |
| 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, | |
| 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, | |
| 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, | |
| 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, | |
| 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7 | |
| }; | |
| //------------------------------------------------------------------------------ | |