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.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 allocated;
  9. struct xlat xlat;
  10. struct xlat_data *data;
  11. };
  12. struct dyxlat *
  13. dyxlat_alloc(const size_t nmemb)
  14. {
  15. struct dyxlat *const dyxlat = xmalloc(sizeof(*dyxlat));
  16. dyxlat->xlat.type = XT_NORMAL;
  17. dyxlat->xlat.size = 0;
  18. dyxlat->allocated = nmemb;
  19. dyxlat->xlat.data = dyxlat->data = xgrowarray(NULL, &dyxlat->allocated,
  20. sizeof(struct xlat_data));
  21. return dyxlat;
  22. }
  23. void
  24. dyxlat_free(struct dyxlat *const dyxlat)
  25. {
  26. size_t i;
  27. for (i = 0; i < dyxlat->xlat.size; ++i) {
  28. free((void *) dyxlat->data[i].str);
  29. dyxlat->data[i].str = NULL;
  30. }
  31. free(dyxlat->data);
  32. dyxlat->xlat.data = NULL;
  33. free(dyxlat);
  34. }
  35. const struct xlat *
  36. dyxlat_get(const struct dyxlat *const dyxlat)
  37. {
  38. return &dyxlat->xlat;
  39. }
  40. void
  41. dyxlat_add_pair(struct dyxlat *const dyxlat, const uint64_t val,
  42. const char *const str, const size_t len)
  43. {
  44. size_t i;
  45. for (i = 0; i < dyxlat->xlat.size; ++i) {
  46. if (dyxlat->data[i].val == val) {
  47. if (strncmp(dyxlat->data[i].str, str, len) == 0
  48. && dyxlat->data[i].str[len] == '\0')
  49. return;
  50. free((void *) dyxlat->data[i].str);
  51. dyxlat->data[i].str = xstrndup(str, len);
  52. return;
  53. }
  54. }
  55. if (dyxlat->xlat.size >= dyxlat->allocated)
  56. dyxlat->xlat.data = dyxlat->data =
  57. xgrowarray(dyxlat->data, &dyxlat->allocated,
  58. sizeof(struct xlat_data));
  59. dyxlat->data[dyxlat->xlat.size].val = val;
  60. dyxlat->data[dyxlat->xlat.size].str = xstrndup(str, len);
  61. dyxlat->xlat.size++;
  62. }