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