1 /* **************************** */
2 /* Memory management functions. */
3 /* **************************** */
5 /* ***************************************************************** */
6 /* Created by Kevin Locke (from numerous canonical examples). */
8 /* Adapted for use by smenu. */
10 /* I hereby place this file in the public domain. It may be freely */
11 /* reproduced, distributed, used, modified, built upon, or otherwise */
12 /* employed by anyone for any purpose without restriction. */
13 /* ***************************************************************** */
23 /* The following rpl_* function are necessary for AIX which doesn't */
24 /* provide 'GNU compatible' allocation functions. */
25 /* Every call to malloc()/realloc() is then replaced by a call to */
26 /* rpl_malloc()/rpl_realloc() as defined in the GNU generated config.h. */
27 /* """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" */
32 extern void *malloc(size_t);
35 rpl_malloc(size_t size
)
44 realloc(void *, size_t);
47 rpl_realloc(void *ptr
, size_t size
)
51 return (ptr
? realloc(ptr
, size
) : malloc(size
));
56 /* ================================================================== */
57 /* Customized malloc. */
58 /* Displays an error message and exits gracefully if an error occurs. */
59 /* ================================================================== */
66 real_size
= (size
> 0) ? size
: 1;
67 allocated
= malloc(real_size
);
68 if (allocated
== NULL
)
71 "Error: Insufficient memory (attempt to malloc %zu bytes)\n",
80 /* ================================================================== */
81 /* Customized calloc. */
82 /* Displays an error message and exits gracefully if an error occurs. */
83 /* ================================================================== */
85 xcalloc(size_t n
, size_t size
)
90 size
= (size
> 0) ? size
: 1;
91 allocated
= calloc(n
, size
);
92 if (allocated
== NULL
)
95 "Error: Insufficient memory (attempt to calloc %zu bytes)\n",
104 /* ================================================================== */
105 /* Customized realloc. */
106 /* Displays an error message and exits gracefully if an error occurs. */
107 /* ================================================================== */
109 xrealloc(void *p
, size_t size
)
113 allocated
= realloc(p
, size
);
114 if (allocated
== NULL
&& size
> 0)
117 "Error: Insufficient memory (attempt to realloc %zu bytes)\n",
126 /* ================================================================== */
127 /* strdup implementation using xmalloc. */
128 /* Displays an error message and exits gracefully if an error occurs. */
129 /* ================================================================== */
131 xstrdup(const char *str
)
135 p
= malloc(strlen(str
) + 1);
139 fprintf(stderr
, "Error: Insufficient memory for xstrdup.\n");
148 /* ================================================================== */
149 /* strndup implementation using xmalloc. */
150 /* This version guarantees that there is a final '\0'. */
151 /* Displays an error message and exits gracefully if an error occurs. */
152 /* ================================================================== */
154 xstrndup(const char *str
, size_t len
)
158 p
= memchr(str
, '\0', len
);
167 fprintf(stderr
, "Error: Insufficient memory for xstrndup.\n");