cfengine  3.15.4
About: CFEngine is a configuration management system for configuring and maintaining Unix-like computers (using an own high level policy language). Community version.
  Fossies Dox: cfengine-3.15.4.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

alloc.c
Go to the documentation of this file.
1 /*
2  Copyright 2020 Northern.tech AS
3 
4  This file is part of CFEngine 3 - written and maintained by Northern.tech AS.
5 
6  This program is free software; you can redistribute it and/or modify it
7  under the terms of the GNU General Public License as published by the
8  Free Software Foundation; version 3.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; if not, write to the Free Software
17  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
18 
19  To the extent this program is licensed as part of the Enterprise
20  versions of CFEngine, the applicable Commercial Open Source License
21  (COSL) may apply to this file if you as a licensee so wish it. See
22  included file COSL.txt.
23 */
24 
25 #define ALLOC_IMPL
26 #include <platform.h>
27 #include <alloc.h>
28 #include <cleanup.h>
29 
30 static void *CheckResult(void *ptr, const char *fn, bool check_result)
31 {
32  if ((ptr == NULL) && (check_result))
33  {
34  fputs(fn, stderr);
35  fputs("CRITICAL: Unable to allocate memory\n", stderr);
36  DoCleanupAndExit(255);
37  }
38  return ptr;
39 }
40 
41 void *xmalloc(size_t size)
42 {
43  return CheckResult(malloc(size), "xmalloc", size != 0);
44 }
45 
46 void *xcalloc(size_t nmemb, size_t size)
47 {
48  return CheckResult(calloc(nmemb, size), "xcalloc", (nmemb != 0) && (size != 0));
49 }
50 
51 void *xrealloc(void *ptr, size_t size)
52 {
53  return CheckResult(realloc(ptr, size), "xrealloc", size != 0);
54 }
55 
56 char *xstrdup(const char *str)
57 {
58  return CheckResult(strdup(str), "xstrdup", true);
59 }
60 
61 char *xstrndup(const char *str, size_t n)
62 {
63  return CheckResult(strndup(str, n), "xstrndup", true);
64 }
65 
66 void *xmemdup(const void *data, size_t size)
67 {
68  return CheckResult(memdup(data, size), "xmemdup", size != 0);
69 }
70 
71 int xasprintf(char **strp, const char *fmt, ...)
72 {
73  va_list ap;
74 
75  va_start(ap, fmt);
76  int res = xvasprintf(strp, fmt, ap);
77 
78  va_end(ap);
79  return res;
80 }
81 
82 int xvasprintf(char **strp, const char *fmt, va_list ap)
83 {
84  int res = vasprintf(strp, fmt, ap);
85 
86  CheckResult(res == -1 ? NULL : *strp, "xvasprintf", true);
87  return res;
88 }
void * xmemdup(const void *data, size_t size)
Definition: alloc.c:66
int xasprintf(char **strp, const char *fmt,...)
Definition: alloc.c:71
void * xmalloc(size_t size)
Definition: alloc.c:41
static void * CheckResult(void *ptr, const char *fn, bool check_result)
Definition: alloc.c:30
int xvasprintf(char **strp, const char *fmt, va_list ap)
Definition: alloc.c:82
void * xrealloc(void *ptr, size_t size)
Definition: alloc.c:51
char * xstrndup(const char *str, size_t n)
Definition: alloc.c:61
void * xcalloc(size_t nmemb, size_t size)
Definition: alloc.c:46
char * xstrdup(const char *str)
Definition: alloc.c:56
#define realloc
Definition: cf3lex.c:805
#define malloc
Definition: cf3lex.c:804
void DoCleanupAndExit(int ret)
Definition: cleanup.c:57
#define NULL
Definition: getopt1.c:56
void * memdup(const void *data, size_t size)
Definition: memdup.c:36
char * strdup(const char *str)
Definition: strdup.c:36
char * strndup(const char *str, size_t n)
Definition: strndup.c:38