1*c85f09ccSJohn Levon #ifndef UTILS_H 2*c85f09ccSJohn Levon #define UTILS_H 3*c85f09ccSJohn Levon 4*c85f09ccSJohn Levon /// 5*c85f09ccSJohn Levon // Miscellaneous utilities 6*c85f09ccSJohn Levon // ----------------------- 7*c85f09ccSJohn Levon 8*c85f09ccSJohn Levon #include <stddef.h> 9*c85f09ccSJohn Levon #include <stdarg.h> 10*c85f09ccSJohn Levon 11*c85f09ccSJohn Levon /// 12*c85f09ccSJohn Levon // duplicate a memory buffer in a newly allocated buffer. 13*c85f09ccSJohn Levon // @src: a pointer to the memory buffer to be duplicated 14*c85f09ccSJohn Levon // @len: the size of the memory buffer to be duplicated 15*c85f09ccSJohn Levon // @return: a pointer to a copy of @src allocated via 16*c85f09ccSJohn Levon // :func:`__alloc_bytes()`. 17*c85f09ccSJohn Levon void *xmemdup(const void *src, size_t len); 18*c85f09ccSJohn Levon 19*c85f09ccSJohn Levon /// 20*c85f09ccSJohn Levon // duplicate a null-terminated string in a newly allocated buffer. 21*c85f09ccSJohn Levon // @src: a pointer to string to be duplicated 22*c85f09ccSJohn Levon // @return: a pointer to a copy of @str allocated via 23*c85f09ccSJohn Levon // :func:`__alloc_bytes()`. 24*c85f09ccSJohn Levon char *xstrdup(const char *src); 25*c85f09ccSJohn Levon 26*c85f09ccSJohn Levon /// 27*c85f09ccSJohn Levon // printf to allocated string 28*c85f09ccSJohn Levon // @fmt: the format followed by its arguments. 29*c85f09ccSJohn Levon // @return: the allocated & formatted string. 30*c85f09ccSJohn Levon // This function is similar to asprintf() but the resulting string 31*c85f09ccSJohn Levon // is allocated with __alloc_bytes(). 32*c85f09ccSJohn Levon char *xasprintf(const char *fmt, ...); 33*c85f09ccSJohn Levon 34*c85f09ccSJohn Levon /// 35*c85f09ccSJohn Levon // vprintf to allocated string 36*c85f09ccSJohn Levon // @fmt: the format 37*c85f09ccSJohn Levon // @ap: the variadic arguments 38*c85f09ccSJohn Levon // @return: the allocated & formatted string. 39*c85f09ccSJohn Levon // This function is similar to asprintf() but the resulting string 40*c85f09ccSJohn Levon // is allocated with __alloc_bytes(). 41*c85f09ccSJohn Levon char *xvasprintf(const char *fmt, va_list ap); 42*c85f09ccSJohn Levon 43*c85f09ccSJohn Levon #endif 44