Mirror of strace – the linux syscall tracer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

dyxlat.c 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*
  2. * Copyright (c) 2017-2018 The strace developers.
  3. *
  4. * SPDX-License-Identifier: LGPL-2.1-or-later
  5. */
  6. #include "defs.h"
  7. struct dyxlat {
  8. size_t used;
  9. size_t allocated;
  10. struct xlat *xlat;
  11. };
  12. #define MARK_END(xlat) \
  13. do { \
  14. (xlat).val = 0; \
  15. (xlat).str = 0; \
  16. } while (0)
  17. struct dyxlat *
  18. dyxlat_alloc(const size_t nmemb)
  19. {
  20. struct dyxlat *const dyxlat = xmalloc(sizeof(*dyxlat));
  21. dyxlat->used = 1;
  22. dyxlat->allocated = nmemb;
  23. dyxlat->xlat = xgrowarray(NULL, &dyxlat->allocated, sizeof(struct xlat));
  24. MARK_END(dyxlat->xlat[0]);
  25. return dyxlat;
  26. }
  27. void
  28. dyxlat_free(struct dyxlat *const dyxlat)
  29. {
  30. size_t i;
  31. for (i = 0; i < dyxlat->used - 1; ++i) {
  32. free((void *) dyxlat->xlat[i].str);
  33. dyxlat->xlat[i].str = NULL;
  34. }
  35. free(dyxlat->xlat);
  36. dyxlat->xlat = NULL;
  37. free(dyxlat);
  38. }
  39. const struct xlat *
  40. dyxlat_get(const struct dyxlat *const dyxlat)
  41. {
  42. return dyxlat->xlat;
  43. }
  44. void
  45. dyxlat_add_pair(struct dyxlat *const dyxlat, const uint64_t val,
  46. const char *const str, const size_t len)
  47. {
  48. size_t i;
  49. for (i = 0; i < dyxlat->used - 1; ++i) {
  50. if (dyxlat->xlat[i].val == val) {
  51. if (strncmp(dyxlat->xlat[i].str, str, len) == 0
  52. && dyxlat->xlat[i].str[len] == '\0')
  53. return;
  54. free((void *) dyxlat->xlat[i].str);
  55. dyxlat->xlat[i].str = xstrndup(str, len);
  56. return;
  57. }
  58. }
  59. if (dyxlat->used >= dyxlat->allocated)
  60. dyxlat->xlat = xgrowarray(dyxlat->xlat, &dyxlat->allocated,
  61. sizeof(struct xlat));
  62. dyxlat->xlat[dyxlat->used - 1].val = val;
  63. dyxlat->xlat[dyxlat->used - 1].str = xstrndup(str, len);
  64. MARK_END(dyxlat->xlat[dyxlat->used]);
  65. dyxlat->used++;
  66. }