Blame gnulib/lib/safe-alloc.h

Packit Service a2ae7a
/* safe-alloc.h: safer memory allocation
Packit Service a2ae7a
Packit Service a2ae7a
   Copyright (C) 2009-2019 Free Software Foundation, Inc.
Packit Service a2ae7a
Packit Service a2ae7a
   This program is free software: you can redistribute it and/or modify it
Packit Service a2ae7a
   under the terms of the GNU Lesser General Public License as published by the
Packit Service a2ae7a
   Free Software Foundation; either version 2.1 of the License, or any
Packit Service a2ae7a
   later version.
Packit Service a2ae7a
Packit Service a2ae7a
   This program is distributed in the hope that it will be useful,
Packit Service a2ae7a
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service a2ae7a
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit Service a2ae7a
   GNU Lesser General Public License for more details.
Packit Service a2ae7a
Packit Service a2ae7a
   You should have received a copy of the GNU Lesser General Public License
Packit Service a2ae7a
   along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
Packit Service a2ae7a
Packit Service a2ae7a
/* Written by Daniel Berrange <berrange@redhat.com>, 2008 */
Packit Service a2ae7a
Packit Service a2ae7a
#ifndef SAFE_ALLOC_H_
Packit Service a2ae7a
# define SAFE_ALLOC_H_
Packit Service a2ae7a
Packit Service a2ae7a
# include <stdlib.h>
Packit Service a2ae7a
Packit Service a2ae7a
#ifndef __GNUC_PREREQ
Packit Service a2ae7a
# if defined __GNUC__ && defined __GNUC_MINOR__
Packit Service a2ae7a
#  define __GNUC_PREREQ(maj, min)                                       \
Packit Service a2ae7a
  ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
Packit Service a2ae7a
# else
Packit Service a2ae7a
#  define __GNUC_PREREQ(maj, min) 0
Packit Service a2ae7a
# endif
Packit Service a2ae7a
#endif
Packit Service a2ae7a
Packit Service a2ae7a
# ifndef _GL_ATTRIBUTE_RETURN_CHECK
Packit Service a2ae7a
#  if __GNUC_PREREQ (3, 4)
Packit Service a2ae7a
#   define _GL_ATTRIBUTE_RETURN_CHECK __attribute__((__warn_unused_result__))
Packit Service a2ae7a
#  else
Packit Service a2ae7a
#   define _GL_ATTRIBUTE_RETURN_CHECK
Packit Service a2ae7a
#  endif
Packit Service a2ae7a
# endif
Packit Service a2ae7a
Packit Service a2ae7a
/* Don't call these directly - use the macros below */
Packit Service a2ae7a
int
Packit Service a2ae7a
safe_alloc_alloc_n (void *ptrptr, size_t size, size_t count, int zeroed)
Packit Service a2ae7a
  _GL_ATTRIBUTE_RETURN_CHECK;
Packit Service a2ae7a
Packit Service a2ae7a
int
Packit Service a2ae7a
safe_alloc_realloc_n (void *ptrptr, size_t size, size_t count)
Packit Service a2ae7a
  _GL_ATTRIBUTE_RETURN_CHECK;
Packit Service a2ae7a
Packit Service a2ae7a
/**
Packit Service a2ae7a
 * ALLOC:
Packit Service a2ae7a
 * @ptr: pointer to hold address of allocated memory
Packit Service a2ae7a
 *
Packit Service a2ae7a
 * Allocate sizeof(*ptr) bytes of memory and store
Packit Service a2ae7a
 * the address of allocated memory in 'ptr'. Fill the
Packit Service a2ae7a
 * newly allocated memory with zeros.
Packit Service a2ae7a
 *
Packit Service a2ae7a
 * Return -1 on failure to allocate, zero on success
Packit Service a2ae7a
 */
Packit Service a2ae7a
# define ALLOC(ptr)                                     \
Packit Service a2ae7a
  safe_alloc_alloc_n (&(ptr), sizeof (*(ptr)), 1, 1)
Packit Service a2ae7a
Packit Service a2ae7a
/**
Packit Service a2ae7a
 * ALLOC_N:
Packit Service a2ae7a
 * @ptr: pointer to hold address of allocated memory
Packit Service a2ae7a
 * @count: number of elements to allocate
Packit Service a2ae7a
 *
Packit Service a2ae7a
 * Allocate an array of 'count' elements, each sizeof(*ptr)
Packit Service a2ae7a
 * bytes long and store the address of allocated memory in
Packit Service a2ae7a
 * 'ptr'. Fill the newly allocated memory with zeros.
Packit Service a2ae7a
 *
Packit Service a2ae7a
 * Return -1 on failure, 0 on success
Packit Service a2ae7a
 */
Packit Service a2ae7a
# define ALLOC_N(ptr, count)                                    \
Packit Service a2ae7a
  safe_alloc_alloc_n (&(ptr), sizeof (*(ptr)), (count), 1)
Packit Service a2ae7a
Packit Service a2ae7a
/**
Packit Service a2ae7a
 * ALLOC_N_UNINITIALIZED:
Packit Service a2ae7a
 * @ptr: pointer to hold address of allocated memory
Packit Service a2ae7a
 * @count: number of elements to allocate
Packit Service a2ae7a
 *
Packit Service a2ae7a
 * Allocate an array of 'count' elements, each sizeof(*ptr)
Packit Service a2ae7a
 * bytes long and store the address of allocated memory in
Packit Service a2ae7a
 * 'ptr'. Do not initialize the new memory at all.
Packit Service a2ae7a
 *
Packit Service a2ae7a
 * Return -1 on failure to allocate, zero on success
Packit Service a2ae7a
 */
Packit Service a2ae7a
# define ALLOC_N_UNINITIALIZED(ptr, count)                      \
Packit Service a2ae7a
  safe_alloc_alloc_n (&(ptr), sizeof (*(ptr)), (count), 0)
Packit Service a2ae7a
Packit Service a2ae7a
/**
Packit Service a2ae7a
 * REALLOC_N:
Packit Service a2ae7a
 * @ptr: pointer to hold address of allocated memory
Packit Service a2ae7a
 * @count: number of elements to allocate
Packit Service a2ae7a
 *
Packit Service a2ae7a
 * Re-allocate an array of 'count' elements, each sizeof(*ptr)
Packit Service a2ae7a
 * bytes long and store the address of allocated memory in
Packit Service a2ae7a
 * 'ptr'. Fill the newly allocated memory with zeros
Packit Service a2ae7a
 *
Packit Service a2ae7a
 * Return -1 on failure to reallocate, zero on success
Packit Service a2ae7a
 */
Packit Service a2ae7a
# define REALLOC_N(ptr, count)                                  \
Packit Service a2ae7a
  safe_alloc_realloc_n (&(ptr), sizeof (*(ptr)), (count))
Packit Service a2ae7a
Packit Service a2ae7a
/**
Packit Service a2ae7a
 * FREE:
Packit Service a2ae7a
 * @ptr: pointer holding address to be freed
Packit Service a2ae7a
 *
Packit Service a2ae7a
 * Free the memory stored in 'ptr' and update to point
Packit Service a2ae7a
 * to NULL.
Packit Service a2ae7a
 */
Packit Service a2ae7a
# define FREE(ptr)                              \
Packit Service a2ae7a
  do                                            \
Packit Service a2ae7a
    {                                           \
Packit Service a2ae7a
      free (ptr);                               \
Packit Service a2ae7a
      (ptr) = NULL;                             \
Packit Service a2ae7a
    }                                           \
Packit Service a2ae7a
  while (0)
Packit Service a2ae7a
Packit Service a2ae7a
#endif /* SAFE_ALLOC_H_ */