jdk/src/java.base/share/native/libjava/sizecalc.h
author sherman
Mon, 23 May 2016 12:53:56 -0700
changeset 38466 4bcf5f2bb351
parent 25859 3317bb8137f4
permissions -rw-r--r--
8147588: Jar file and Zip file not removed in spite of the OPEN_DELETE flag Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
18232
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
     1
/*
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
     2
 * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
     4
 *
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    10
 *
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    15
 * accompanied this code).
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    16
 *
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    20
 *
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    23
 * questions.
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    24
 */
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    25
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    26
#ifndef SIZECALC_H
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    27
#define SIZECALC_H
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    28
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    29
/*
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    30
 * A machinery for safe calculation of sizes used when allocating memory.
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    31
 *
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    32
 * All size checks are performed against the SIZE_MAX (the maximum value for
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    33
 * size_t). All numerical arguments as well as the result of calculation must
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    34
 * be non-negative integers less than or equal to SIZE_MAX, otherwise the
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    35
 * calculated size is considered unsafe.
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    36
 *
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    37
 * If the SIZECALC_ALLOC_THROWING_BAD_ALLOC macro is defined, then _ALLOC_
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    38
 * helper macros throw the std::bad_alloc instead of returning NULL.
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    39
 */
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    40
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    41
#include <stdint.h> /* SIZE_MAX for C99+ */
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    42
/* http://stackoverflow.com/questions/3472311/what-is-a-portable-method-to-find-the-maximum-value-of-size-t */
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    43
#ifndef SIZE_MAX
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    44
#define SIZE_MAX ((size_t)-1)
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    45
#endif
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    46
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    47
#define IS_SAFE_SIZE_T(x) ((x) >= 0 && (unsigned long long)(x) <= SIZE_MAX)
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    48
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    49
#define IS_SAFE_SIZE_MUL(m, n) \
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    50
    (IS_SAFE_SIZE_T(m) && IS_SAFE_SIZE_T(n) && ((m) == 0 || (n) == 0 || (size_t)(n) <= (SIZE_MAX / (size_t)(m))))
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    51
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    52
#define IS_SAFE_SIZE_ADD(a, b) \
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    53
    (IS_SAFE_SIZE_T(a) && IS_SAFE_SIZE_T(b) && (size_t)(b) <= (SIZE_MAX - (size_t)(a)))
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    54
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    55
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    56
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    57
/* Helper macros */
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    58
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    59
#ifdef SIZECALC_ALLOC_THROWING_BAD_ALLOC
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    60
#define FAILURE_RESULT throw std::bad_alloc()
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    61
#else
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    62
#define FAILURE_RESULT NULL
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    63
#endif
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    64
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    65
/*
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    66
 * A helper macro to safely allocate an array of size m*n.
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    67
 * Example usage:
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    68
 *    int* p = (int*)SAFE_SIZE_ARRAY_ALLOC(malloc, sizeof(int), n);
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    69
 *    if (!p) throw OutOfMemory;
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    70
 *    // Use the allocated array...
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    71
 */
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    72
#define SAFE_SIZE_ARRAY_ALLOC(func, m, n) \
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    73
    (IS_SAFE_SIZE_MUL((m), (n)) ? ((func)((m) * (n))) : FAILURE_RESULT)
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    74
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    75
#define SAFE_SIZE_ARRAY_REALLOC(func, p, m, n) \
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    76
    (IS_SAFE_SIZE_MUL((m), (n)) ? ((func)((p), (m) * (n))) : FAILURE_RESULT)
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    77
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    78
/*
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    79
 * A helper macro to safely allocate an array of type 'type' with 'n' items
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    80
 * using the C++ new[] operator.
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    81
 * Example usage:
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    82
 *    MyClass* p = SAFE_SIZE_NEW_ARRAY(MyClass, n);
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    83
 *    // Use the pointer.
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    84
 * This macro throws the std::bad_alloc C++ exception to indicate
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    85
 * a failure.
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    86
 * NOTE: if 'n' is calculated, the calling code is responsible for using the
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    87
 * IS_SAFE_... macros to check if the calculations are safe.
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    88
 */
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    89
#define SAFE_SIZE_NEW_ARRAY(type, n) \
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    90
    (IS_SAFE_SIZE_MUL(sizeof(type), (n)) ? (new type[(n)]) : throw std::bad_alloc())
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    91
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    92
#define SAFE_SIZE_NEW_ARRAY2(type, n, m) \
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    93
    (IS_SAFE_SIZE_MUL((m), (n)) && IS_SAFE_SIZE_MUL(sizeof(type), (n) * (m)) ? \
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    94
     (new type[(n) * (m)]) : throw std::bad_alloc())
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    95
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    96
/*
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    97
 * Checks if a data structure of size (a + m*n) can be safely allocated
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    98
 * w/o producing an integer overflow when calculating its size.
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
    99
 */
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
   100
#define IS_SAFE_STRUCT_SIZE(a, m, n) \
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
   101
    ( \
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
   102
      IS_SAFE_SIZE_MUL((m), (n)) && IS_SAFE_SIZE_ADD((m) * (n), (a)) \
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
   103
    )
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
   104
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
   105
/*
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
   106
 * A helper macro for implementing safe memory allocation for a data structure
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
   107
 * of size (a + m * n).
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
   108
 * Example usage:
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
   109
 *    void * p = SAFE_SIZE_ALLOC(malloc, header, num, itemSize);
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
   110
 *    if (!p) throw OutOfMemory;
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
   111
 *    // Use the allocated memory...
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
   112
 */
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
   113
#define SAFE_SIZE_STRUCT_ALLOC(func, a, m, n) \
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
   114
    (IS_SAFE_STRUCT_SIZE((a), (m), (n)) ? ((func)((a) + (m) * (n))) : FAILURE_RESULT)
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
   115
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
   116
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
   117
#endif /* SIZECALC_H */
b538b71fb429 8009071: Improve shape handling
anthony
parents:
diff changeset
   118