2014-02-10 02:10:30 +01:00
|
|
|
// Copyright 2012 Google Inc. All Rights Reserved.
|
|
|
|
//
|
2014-02-11 02:37:07 +01:00
|
|
|
// This code is licensed under the same terms as WebM:
|
|
|
|
// Software License Agreement: http://www.webmproject.org/license/software/
|
|
|
|
// Additional IP Rights Grant: http://www.webmproject.org/license/additional/
|
2014-02-10 02:10:30 +01:00
|
|
|
// -----------------------------------------------------------------------------
|
|
|
|
//
|
|
|
|
// Misc. common utility functions
|
|
|
|
//
|
|
|
|
// Author: Skal (pascal.massimino@gmail.com)
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "./utils.h"
|
|
|
|
|
2014-02-11 02:37:07 +01:00
|
|
|
#if defined(__cplusplus) || defined(c_plusplus)
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
2014-02-10 02:10:30 +01:00
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
// Checked memory allocation
|
|
|
|
|
2014-02-11 02:37:07 +01:00
|
|
|
static int CheckSizeArguments(uint64_t nmemb, size_t size) {
|
2014-02-10 02:10:30 +01:00
|
|
|
const uint64_t total_size = nmemb * size;
|
|
|
|
if (nmemb == 0) return 1;
|
|
|
|
if ((uint64_t)size > WEBP_MAX_ALLOCABLE_MEMORY / nmemb) return 0;
|
|
|
|
if (total_size != (size_t)total_size) return 0;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void* WebPSafeMalloc(uint64_t nmemb, size_t size) {
|
2014-02-11 02:37:07 +01:00
|
|
|
if (!CheckSizeArguments(nmemb, size)) return NULL;
|
2014-02-10 02:10:30 +01:00
|
|
|
return malloc((size_t)(nmemb * size));
|
|
|
|
}
|
|
|
|
|
|
|
|
void* WebPSafeCalloc(uint64_t nmemb, size_t size) {
|
2014-02-11 02:37:07 +01:00
|
|
|
if (!CheckSizeArguments(nmemb, size)) return NULL;
|
2014-02-10 02:10:30 +01:00
|
|
|
return calloc((size_t)nmemb, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
//------------------------------------------------------------------------------
|
|
|
|
|
2014-02-11 02:37:07 +01:00
|
|
|
#if defined(__cplusplus) || defined(c_plusplus)
|
|
|
|
} // extern "C"
|
|
|
|
#endif
|