1 /*****************************************************************************\ 2 * xmalloc.h - enhanced malloc routines for slurm 3 * - default: never return if errors are encountered. 4 * - attempt to report file, line, and calling function on assertion failure 5 * - use configurable slurm log facility for reporting errors 6 ***************************************************************************** 7 * Copyright (C) 2002 The Regents of the University of California. 8 * Produced at Lawrence Livermore National Laboratory (cf, DISCLAIMER). 9 * Written by Jim Garlick <garlick1@llnl.gov> and 10 * Mark Grondona <mgrondona@llnl.gov> 11 * CODE-OCEC-09-009. All rights reserved. 12 * 13 * This file is part of Slurm, a resource management program. 14 * For details, see <https://slurm.schedmd.com/>. 15 * Please also read the included file: DISCLAIMER. 16 * 17 * Slurm is free software; you can redistribute it and/or modify it under 18 * the terms of the GNU General Public License as published by the Free 19 * Software Foundation; either version 2 of the License, or (at your option) 20 * any later version. 21 * 22 * In addition, as a special exception, the copyright holders give permission 23 * to link the code of portions of this program with the OpenSSL library under 24 * certain conditions as described in each individual source file, and 25 * distribute linked combinations including the two. You must obey the GNU 26 * General Public License in all respects for all of the code used other than 27 * OpenSSL. If you modify file(s) with this exception, you may extend this 28 * exception to your version of the file(s), but you are not obligated to do 29 * so. If you do not wish to do so, delete this exception statement from your 30 * version. If you delete this exception statement from all source files in 31 * the program, then also delete it here. 32 * 33 * Slurm is distributed in the hope that it will be useful, but WITHOUT ANY 34 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 35 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 36 * details. 37 * 38 * You should have received a copy of the GNU General Public License along 39 * with Slurm; if not, write to the Free Software Foundation, Inc., 40 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 41 ***************************************************************************** 42 * Description: 43 * 44 * void *xmalloc(size_t size); 45 * void xrealloc(void *p, size_t newsize); 46 * void xfree(void *p); 47 * int xsize(void *p); 48 * 49 * xmalloc(size) allocates size bytes and returns a pointer to the allocated 50 * memory. The memory is set to zero. xmalloc() will not return unless 51 * there are no errors. The memory must be freed using xfree(). 52 * 53 * xrealloc(p, newsize) changes the size of the block pointed to by p to the 54 * value of newsize. Newly allocated memory is zeroed. If p is NULL, 55 * xrealloc() performs the same function as `p = xmalloc(newsize)'. If p 56 * is not NULL, it is required to have been initialized with a call to 57 * [try_]xmalloc() or [try_]xrealloc(). 58 * 59 * xfree(p) frees the memory block pointed to by p. The memory must have been 60 * initialized with a call to [try_]xmalloc() or [try_]xrealloc(). 61 * 62 * xsize(p) returns the current size of the memory allocation pointed to by 63 * p. The memory must have been allocated with [try_]xmalloc() or 64 * [try_]xrealloc(). 65 * 66 \*****************************************************************************/ 67 68 #ifndef _XMALLOC_H 69 #define _XMALLOC_H 70 71 #include <stdbool.h> 72 #include <sys/types.h> 73 74 #define xcalloc(__cnt, __sz) \ 75 slurm_xcalloc(__cnt, __sz, true, false, __FILE__, __LINE__, __func__) 76 77 #define try_xcalloc(__cnt, __sz) \ 78 slurm_xcalloc(__cnt, __sz, true, true, __FILE__, __LINE__, __func__) 79 80 #define xcalloc_nz(__cnt, __sz) \ 81 slurm_xcalloc(__cnt, __sz, false, false, __FILE__, __LINE__, __func__) 82 83 #define xmalloc(__sz) \ 84 slurm_xcalloc(1, __sz, true, false, __FILE__, __LINE__, __func__) 85 86 #define try_xmalloc(__sz) \ 87 slurm_xcalloc(1, __sz, true, true, __FILE__, __LINE__, __func__) 88 89 #define xmalloc_nz(__sz) \ 90 slurm_xcalloc(1, __sz, false, false, __FILE__, __LINE__, __func__) 91 92 #define xfree(__p) \ 93 slurm_xfree((void **)&(__p), __FILE__, __LINE__, __func__) 94 95 #define xrecalloc(__p, __cnt, __sz) \ 96 slurm_xrecalloc((void **)&(__p), __cnt, __sz, true, false, __FILE__, __LINE__, __func__) 97 98 #define xrealloc(__p, __sz) \ 99 slurm_xrecalloc((void **)&(__p), 1, __sz, true, false, __FILE__, __LINE__, __func__) 100 101 #define try_xrealloc(__p, __sz) \ 102 slurm_xrecalloc((void **)&(__p), 1, __sz, true, true, __FILE__, __LINE__, __func__) 103 104 #define xrealloc_nz(__p, __sz) \ 105 slurm_xrecalloc((void **)&(__p), 1, __sz, false, false, __FILE__, __LINE__, __func__) 106 107 #define xsize(__p) \ 108 slurm_xsize((void *)__p, __FILE__, __LINE__, __func__) 109 110 void *slurm_xcalloc(size_t, size_t, bool, bool, const char *, int, const char *); 111 void slurm_xfree(void **, const char *, int, const char *); 112 void *slurm_xrecalloc(void **, size_t, size_t, bool, bool, const char *, int, const char *); 113 size_t slurm_xsize(void *, const char *, int, const char *); 114 115 void xfree_ptr(void *); 116 117 #endif /* !_XMALLOC_H */ 118