* published by the Free Software Foundation. Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * A machinery for safe calculation of sizes used when allocating memory. * All size checks are performed against the SIZE_MAX (the maximum value for * size_t). All numerical arguments as well as the result of calculation must * be non-negative integers less than or equal to SIZE_MAX, otherwise the * calculated size is considered unsafe. * If the SIZECALC_ALLOC_THROWING_BAD_ALLOC macro is defined, then _ALLOC_ * helper macros throw the std::bad_alloc instead of returning NULL. #
include <
stdint.h>
/* SIZE_MAX for C99+ */ * A helper macro to safely allocate an array of size m*n. * int* p = (int*)SAFE_SIZE_ARRAY_ALLOC(malloc, sizeof(int), n); * if (!p) throw OutOfMemory; * // Use the allocated array... * A helper macro to safely allocate an array of type 'type' with 'n' items * using the C++ new[] operator. * MyClass* p = SAFE_SIZE_NEW_ARRAY(MyClass, n); * This macro throws the std::bad_alloc C++ exception to indicate * NOTE: if 'n' is calculated, the calling code is responsible for using the * IS_SAFE_... macros to check if the calculations are safe. * Checks if a data structure of size (a + m*n) can be safely allocated * w/o producing an integer overflow when calculating its size. * A helper macro for implementing safe memory allocation for a data structure * void * p = SAFE_SIZE_ALLOC(malloc, header, num, itemSize); * if (!p) throw OutOfMemory; * // Use the allocated memory...