1// Copyright 2012 Google Inc. All Rights Reserved. 2// 3// This code is licensed under the same terms as WebM: 4// Software License Agreement: http://www.webmproject.org/license/software/ 5// Additional IP Rights Grant: http://www.webmproject.org/license/additional/ 6// ----------------------------------------------------------------------------- 7// 8// Misc. common utility functions 9// 10// Author: Skal (pascal.massimino@gmail.com) 11 12#include <stdlib.h> 13#include "./utils.h" 14 15#if defined(__cplusplus) || defined(c_plusplus) 16extern "C" { 17#endif 18 19//------------------------------------------------------------------------------ 20// Checked memory allocation 21 22static int CheckSizeArguments(uint64_t nmemb, size_t size) { 23 const uint64_t total_size = nmemb * size; 24 if (nmemb == 0) return 1; 25 if ((uint64_t)size > WEBP_MAX_ALLOCABLE_MEMORY / nmemb) return 0; 26 if (total_size != (size_t)total_size) return 0; 27 return 1; 28} 29 30void* WebPSafeMalloc(uint64_t nmemb, size_t size) { 31 if (!CheckSizeArguments(nmemb, size)) return NULL; 32 return malloc((size_t)(nmemb * size)); 33} 34 35void* WebPSafeCalloc(uint64_t nmemb, size_t size) { 36 if (!CheckSizeArguments(nmemb, size)) return NULL; 37 return calloc((size_t)nmemb, size); 38} 39 40//------------------------------------------------------------------------------ 41 42#if defined(__cplusplus) || defined(c_plusplus) 43} // extern "C" 44#endif 45