2023-02-05 01:43:59 +05:30
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: 2023, Alejandro Colomar <alx@kernel.org>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2023-02-20 01:10:16 +05:30
|
|
|
#ifndef SHADOW_INCLUDE_LIB_MALLOC_H_
|
|
|
|
#define SHADOW_INCLUDE_LIB_MALLOC_H_
|
|
|
|
|
|
|
|
|
2023-02-05 01:43:59 +05:30
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
#include <errno.h>
|
2023-02-20 01:10:16 +05:30
|
|
|
#include <stddef.h>
|
2023-02-05 01:43:59 +05:30
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
#include "defines.h"
|
|
|
|
|
|
|
|
|
|
|
|
ATTR_MALLOC(free)
|
|
|
|
inline void *xmalloc(size_t size);
|
|
|
|
ATTR_MALLOC(free)
|
|
|
|
inline void *xmallocarray(size_t nmemb, size_t size);
|
|
|
|
ATTR_MALLOC(free)
|
|
|
|
inline void *mallocarray(size_t nmemb, size_t size);
|
|
|
|
ATTR_MALLOC(free)
|
|
|
|
inline void *reallocarrayf(void *p, size_t nmemb, size_t size);
|
|
|
|
ATTR_MALLOC(free)
|
|
|
|
inline char *xstrdup(const char *str);
|
|
|
|
|
|
|
|
ATTR_MALLOC(free)
|
|
|
|
void *xcalloc(size_t nmemb, size_t size);
|
|
|
|
ATTR_MALLOC(free)
|
|
|
|
void *xreallocarray(void *p, size_t nmemb, size_t size);
|
|
|
|
|
|
|
|
|
|
|
|
inline void *
|
|
|
|
xmalloc(size_t size)
|
|
|
|
{
|
|
|
|
return xmallocarray(1, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline void *
|
|
|
|
xmallocarray(size_t nmemb, size_t size)
|
|
|
|
{
|
|
|
|
return xreallocarray(NULL, nmemb, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline void *
|
|
|
|
mallocarray(size_t nmemb, size_t size)
|
|
|
|
{
|
|
|
|
return reallocarray(NULL, nmemb, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
inline void *
|
|
|
|
reallocarrayf(void *p, size_t nmemb, size_t size)
|
|
|
|
{
|
|
|
|
void *q;
|
|
|
|
|
|
|
|
q = reallocarray(p, nmemb, size);
|
|
|
|
|
|
|
|
/* realloc(p, 0) is equivalent to free(p); avoid double free. */
|
|
|
|
if (q == NULL && nmemb != 0 && size != 0)
|
|
|
|
free(p);
|
|
|
|
return q;
|
|
|
|
}
|
|
|
|
|
2023-02-20 01:10:16 +05:30
|
|
|
|
2023-02-05 01:43:59 +05:30
|
|
|
inline char *
|
|
|
|
xstrdup(const char *str)
|
|
|
|
{
|
|
|
|
return strcpy(XMALLOCARRAY(strlen(str) + 1, char), str);
|
|
|
|
}
|
2023-02-20 01:10:16 +05:30
|
|
|
|
|
|
|
|
|
|
|
#endif // include guard
|