2 * Copyright (C) 2004-2008 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file string.c Memory allocation and string handling functions. */
15 * Paraslash's version of realloc().
17 * \param p Pointer to the memory block, may be \p NULL.
18 * \param size The desired new size.
20 * A wrapper for realloc(3). It calls \p exit(\p EXIT_FAILURE) on errors,
21 * i.e. there is no need to check the return value in the caller.
23 * \return A pointer to the newly allocated memory, which is suitably aligned
24 * for any kind of variable and may be different from \a p.
28 __must_check __malloc void *adu_realloc(void *p, size_t size)
31 * No need to check for NULL pointers: If p is NULL, the call
32 * to realloc is equivalent to malloc(size)
35 if (!(p = realloc(p, size))) {
36 EMERG_LOG("realloc failed (size = %zu), aborting\n",
44 * Paraslash's version of malloc().
46 * \param size The desired new size.
48 * A wrapper for malloc(3) which exits on errors.
50 * \return A pointer to the allocated memory, which is suitably aligned for any
55 __must_check __malloc void *adu_malloc(size_t size)
58 void *p = malloc(size);
61 EMERG_LOG("malloc failed (size = %zu), aborting\n",
69 * Paraslash's version of calloc().
71 * \param size The desired new size.
73 * A wrapper for calloc(3) which exits on errors.
75 * \return A pointer to the allocated and zeroed-out memory, which is suitably
76 * aligned for any kind of variable.
80 __must_check __malloc void *adu_calloc(size_t size)
82 void *ret = adu_malloc(size);
89 * Paraslash's version of strdup().
91 * \param s The string to be duplicated.
93 * A wrapper for strdup(3). It calls \p exit(EXIT_FAILURE) on errors, i.e.
94 * there is no need to check the return value in the caller.
96 * \return A pointer to the duplicated string. If \p s was the NULL pointer,
97 * an pointer to an empty string is returned.
101 __must_check __malloc char *adu_strdup(const char *s)
105 if ((ret = strdup(s? s: "")))
107 EMERG_LOG("strdup failed, aborting\n");
112 * Allocate a sufficiently large string and print into it.
114 * \param fmt A usual format string.
116 * Produce output according to \p fmt. No artificial bound on the length of the
117 * resulting string is imposed.
119 * \return This function either returns a pointer to a string that must be
120 * freed by the caller or aborts without returning.
124 __must_check __printf_1_2 __malloc char *make_message(const char *fmt, ...)
132 /** \cond LLONG_MAX and LLONG_LIN might not be defined. */
134 #define LLONG_MAX (1 << (sizeof(long) - 1))
137 #define LLONG_MIN (-LLONG_MAX - 1LL)
142 * Convert a string to a 64-bit signed integer value.
144 * \param str The string to be converted.
145 * \param value Result pointer.
149 * \sa strtol(3), atoi(3).
151 __must_check int atoi64(const char *str, int64_t *result)
156 errno = 0; /* To distinguish success/failure after call */
157 tmp = strtoll(str, &endptr, 10);
158 if (errno == ERANGE && (tmp == LLONG_MAX || tmp == LLONG_MIN))
159 return -E_ATOI_OVERFLOW;
160 if (errno != 0 && tmp == 0) /* other error */
163 return -E_ATOI_NO_DIGITS;
164 if (*endptr != '\0') /* Further characters after number */
165 return -E_ATOI_JUNK_AT_END;