Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/native/common/sizecalc.h
38825 views
/*1* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425#ifndef SIZECALC_H26#define SIZECALC_H2728/*29* A machinery for safe calculation of sizes used when allocating memory.30*31* All size checks are performed against the SIZE_MAX (the maximum value for32* size_t). All numerical arguments as well as the result of calculation must33* be non-negative integers less than or equal to SIZE_MAX, otherwise the34* calculated size is considered unsafe.35*36* If the SIZECALC_ALLOC_THROWING_BAD_ALLOC macro is defined, then _ALLOC_37* helper macros throw the std::bad_alloc instead of returning NULL.38*/3940#include <stdint.h> /* SIZE_MAX for C99+ */41/* http://stackoverflow.com/questions/3472311/what-is-a-portable-method-to-find-the-maximum-value-of-size-t */42#ifndef SIZE_MAX43#define SIZE_MAX ((size_t)-1)44#endif4546#define IS_SAFE_SIZE_T(x) ((x) >= 0 && (unsigned long long)(x) <= SIZE_MAX)4748#define IS_SAFE_SIZE_MUL(m, n) \49(IS_SAFE_SIZE_T(m) && IS_SAFE_SIZE_T(n) && ((m) == 0 || (n) == 0 || (size_t)(n) <= (SIZE_MAX / (size_t)(m))))5051#define IS_SAFE_SIZE_ADD(a, b) \52(IS_SAFE_SIZE_T(a) && IS_SAFE_SIZE_T(b) && (size_t)(b) <= (SIZE_MAX - (size_t)(a)))53545556/* Helper macros */5758#ifdef SIZECALC_ALLOC_THROWING_BAD_ALLOC59#define FAILURE_RESULT throw std::bad_alloc()60#else61#define FAILURE_RESULT NULL62#endif6364/*65* A helper macro to safely allocate an array of size m*n.66* Example usage:67* int* p = (int*)SAFE_SIZE_ARRAY_ALLOC(malloc, sizeof(int), n);68* if (!p) throw OutOfMemory;69* // Use the allocated array...70*/71#define SAFE_SIZE_ARRAY_ALLOC(func, m, n) \72(IS_SAFE_SIZE_MUL((m), (n)) ? ((func)((m) * (n))) : FAILURE_RESULT)7374#define SAFE_SIZE_ARRAY_REALLOC(func, p, m, n) \75(IS_SAFE_SIZE_MUL((m), (n)) ? ((func)((p), (m) * (n))) : FAILURE_RESULT)7677/*78* A helper macro to safely allocate an array of type 'type' with 'n' items79* using the C++ new[] operator.80* Example usage:81* MyClass* p = SAFE_SIZE_NEW_ARRAY(MyClass, n);82* // Use the pointer.83* This macro throws the std::bad_alloc C++ exception to indicate84* a failure.85* NOTE: if 'n' is calculated, the calling code is responsible for using the86* IS_SAFE_... macros to check if the calculations are safe.87*/88#define SAFE_SIZE_NEW_ARRAY(type, n) \89(IS_SAFE_SIZE_MUL(sizeof(type), (n)) ? (new type[(n)]) : throw std::bad_alloc())9091#define SAFE_SIZE_NEW_ARRAY2(type, n, m) \92(IS_SAFE_SIZE_MUL((m), (n)) && IS_SAFE_SIZE_MUL(sizeof(type), (n) * (m)) ? \93(new type[(n) * (m)]) : throw std::bad_alloc())9495/*96* Checks if a data structure of size (a + m*n) can be safely allocated97* w/o producing an integer overflow when calculating its size.98*/99#define IS_SAFE_STRUCT_SIZE(a, m, n) \100( \101IS_SAFE_SIZE_MUL((m), (n)) && IS_SAFE_SIZE_ADD((m) * (n), (a)) \102)103104/*105* A helper macro for implementing safe memory allocation for a data structure106* of size (a + m * n).107* Example usage:108* void * p = SAFE_SIZE_ALLOC(malloc, header, num, itemSize);109* if (!p) throw OutOfMemory;110* // Use the allocated memory...111*/112#define SAFE_SIZE_STRUCT_ALLOC(func, a, m, n) \113(IS_SAFE_STRUCT_SIZE((a), (m), (n)) ? ((func)((a) + (m) * (n))) : FAILURE_RESULT)114115116#endif /* SIZECALC_H */117118119120