util.h 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * util.h - Common utility functions
  3. *
  4. * Written 2006, 2009, 2010 by Werner Almesberger
  5. * Copyright 2006, 2009, 2010 Werner Almesberger
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. */
  12. #ifndef UTIL_H
  13. #define UTIL_H
  14. #include <stdarg.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #define alloc_size(s) \
  18. ({ void *alloc_size_tmp = malloc(s); \
  19. if (!alloc_size_tmp) \
  20. abort(); \
  21. alloc_size_tmp; })
  22. #define alloc_type(t) ((t *) alloc_size(sizeof(t)))
  23. #define zalloc_size(s) \
  24. ({ void *zalloc_size_tmp = alloc_size(s); \
  25. memset(zalloc_size_tmp, 0, (s)); \
  26. zalloc_size_tmp; })
  27. #define zalloc_type(t) \
  28. ({ t *zalloc_type_tmp = alloc_type(t); \
  29. memset(zalloc_type_tmp, 0, sizeof(t)); \
  30. zalloc_type_tmp; })
  31. #define stralloc(s) \
  32. ({ char *stralloc_tmp = strdup(s); \
  33. if (!stralloc_tmp) \
  34. abort(); \
  35. stralloc_tmp; })
  36. #define strnalloc(s, n) \
  37. ({ char *strnalloc_tmp = alloc_size((n)+1); \
  38. if (!strnalloc_tmp) \
  39. abort(); \
  40. strncpy(strnalloc_tmp, (s), (n)); \
  41. strnalloc_tmp[n] = 0; \
  42. strnalloc_tmp; })
  43. #define SWAP(a, b) \
  44. ({ typeof(a) SWAP_tmp = (a); \
  45. (a) = (b); \
  46. (b) = SWAP_tmp; })
  47. char *stralloc_vprintf(const char *fmt, va_list ap);
  48. char *stralloc_printf(const char *fmt, ...)
  49. __attribute__((format(printf, 1, 2)));
  50. int is_id_char(char c, int first);
  51. int is_id(const char *s);
  52. const char *unique(const char *s);
  53. void unique_cleanup(void);
  54. #endif /* !UTIL_H */