10760f5d2Smrg/*
20760f5d2Smrg * Copyright (c) 2015, Oracle and/or its affiliates.
30760f5d2Smrg *
40760f5d2Smrg * Permission is hereby granted, free of charge, to any person obtaining a
50760f5d2Smrg * copy of this software and associated documentation files (the "Software"),
60760f5d2Smrg * to deal in the Software without restriction, including without limitation
70760f5d2Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
80760f5d2Smrg * and/or sell copies of the Software, and to permit persons to whom the
90760f5d2Smrg * Software is furnished to do so, subject to the following conditions:
100760f5d2Smrg *
110760f5d2Smrg * The above copyright notice and this permission notice (including the next
120760f5d2Smrg * paragraph) shall be included in all copies or substantial portions of the
130760f5d2Smrg * Software.
140760f5d2Smrg *
150760f5d2Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
160760f5d2Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
170760f5d2Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
180760f5d2Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
190760f5d2Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
200760f5d2Smrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
210760f5d2Smrg * DEALINGS IN THE SOFTWARE.
220760f5d2Smrg */
230760f5d2Smrg
240760f5d2Smrg#ifdef HAVE_CONFIG_H
250760f5d2Smrg#include <config.h>
260760f5d2Smrg#endif
270760f5d2Smrg
280760f5d2Smrg#include <sys/types.h>
290760f5d2Smrg#include <stdlib.h>
300760f5d2Smrg#include <X11/Xfuncproto.h>
310760f5d2Smrg
320760f5d2Smrg#ifndef HAVE_REALLOCARRAY
330760f5d2Smrgextern _X_HIDDEN void *xreallocarray(void *optr, size_t nmemb, size_t size);
340760f5d2Smrg# define reallocarray(ptr, n, size)	xreallocarray((ptr), (size_t)(n), (size_t)(size))
350760f5d2Smrg#endif
360760f5d2Smrg
370760f5d2Smrg#if defined(MALLOC_0_RETURNS_NULL) || defined(__clang_analyzer__)
380760f5d2Smrg# define Xreallocarray(ptr, n, size) \
390760f5d2Smrg    reallocarray((ptr), ((n) == 0 ? 1 : (n)), size)
400760f5d2Smrg#else
410760f5d2Smrg# define Xreallocarray(ptr, n, size)	reallocarray((ptr), (n), (size))
420760f5d2Smrg#endif
430760f5d2Smrg
440760f5d2Smrg#define Xmallocarray(n, size)		Xreallocarray(NULL, (n), (size))
45