mirror of
https://github.com/X11Libre/xserver.git
synced 2026-03-24 10:14:52 +00:00
Using calloc() instead of malloc() as preventive measure, so there never can be any hidden bugs or leaks due uninitialized memory. The extra cost of using this compiler intrinsic should be practically impossible to measure - in many cases a good compiler can even deduce if certain areas really don't need to be zero'd (because they're written to right after allocation) and create more efficient machine code. The code pathes in question are pretty cold anyways, so it's probably not worth even thinking about potential extra runtime costs. Signed-off-by: Enrico Weigelt, metux IT consult <info@metux.net>
61 lines
1.2 KiB
C
61 lines
1.2 KiB
C
/* SPDX-License-Identifier: MIT OR X11
|
|
*
|
|
* Copyright © 1987, 1998 The Open Group
|
|
* Copyright © 2024 Enrico Weigelt, metux IT consult <info@metux.net>
|
|
*/
|
|
#include <dix-config.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "os.h"
|
|
|
|
void *
|
|
XNFalloc(unsigned long amount)
|
|
{
|
|
void *ptr = calloc(1, amount);
|
|
|
|
if (!ptr)
|
|
FatalError("Out of memory");
|
|
return ptr;
|
|
}
|
|
|
|
/* The original XNFcalloc was used with the xnfcalloc macro which multiplied
|
|
* the arguments at the call site without allowing calloc to check for overflow.
|
|
* XNFcallocarray was added to fix that without breaking ABI.
|
|
*/
|
|
void *
|
|
XNFcalloc(unsigned long amount)
|
|
{
|
|
return XNFcallocarray(1, amount);
|
|
}
|
|
|
|
void *
|
|
XNFcallocarray(size_t nmemb, size_t size)
|
|
{
|
|
void *ret = calloc(nmemb, size);
|
|
|
|
if (!ret)
|
|
FatalError("XNFcalloc: Out of memory");
|
|
return ret;
|
|
}
|
|
|
|
void *
|
|
XNFrealloc(void *ptr, unsigned long amount)
|
|
{
|
|
void *ret = realloc(ptr, amount);
|
|
|
|
if (!ret)
|
|
FatalError("XNFrealloc: Out of memory");
|
|
return ret;
|
|
}
|
|
|
|
void *
|
|
XNFreallocarray(void *ptr, size_t nmemb, size_t size)
|
|
{
|
|
void *ret = reallocarray(ptr, nmemb, size);
|
|
|
|
if (!ret)
|
|
FatalError("XNFreallocarray: Out of memory");
|
|
return ret;
|
|
}
|