api-20-alloc-attr.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. #include "config.h"
  2. #include "../ntdb.h"
  3. #include "../private.h"
  4. #include "tap-interface.h"
  5. #include <ccan/hash/hash.h>
  6. #include <assert.h>
  7. #include "logging.h"
  8. #include "helpapi-external-agent.h"
  9. static const struct ntdb_context *curr_ntdb;
  10. static const struct ntdb_file *curr_file;
  11. static int owner_null_count,
  12. owner_weird_count, alloc_count, free_count, expand_count;
  13. static void *test_alloc(const void *owner, size_t len, void *priv_data)
  14. {
  15. void *ret;
  16. if (!owner) {
  17. owner_null_count++;
  18. } else if (owner != curr_ntdb && owner != curr_file) {
  19. owner_weird_count++;
  20. }
  21. alloc_count++;
  22. ret = malloc(len);
  23. /* The first time, this is the current ntdb, next is
  24. * for the file struct. */
  25. if (!owner) {
  26. if (!curr_ntdb) {
  27. curr_ntdb = ret;
  28. } else if (!curr_file) {
  29. curr_file = ret;
  30. }
  31. }
  32. assert(priv_data == &owner_weird_count);
  33. return ret;
  34. }
  35. static void *test_expand(void *old, size_t newlen, void *priv_data)
  36. {
  37. expand_count++;
  38. assert(priv_data == &owner_weird_count);
  39. return realloc(old, newlen);
  40. }
  41. static void test_free(void *old, void *priv_data)
  42. {
  43. assert(priv_data == &owner_weird_count);
  44. if (old) {
  45. free_count++;
  46. }
  47. free(old);
  48. }
  49. int main(int argc, char *argv[])
  50. {
  51. unsigned int i, j;
  52. union ntdb_attribute alloc_attr;
  53. struct ntdb_context *ntdb;
  54. int flags[] = { NTDB_INTERNAL, NTDB_DEFAULT, NTDB_NOMMAP,
  55. NTDB_INTERNAL|NTDB_CONVERT, NTDB_CONVERT,
  56. NTDB_NOMMAP|NTDB_CONVERT };
  57. NTDB_DATA key = { (unsigned char *)&j, sizeof(j) };
  58. NTDB_DATA data = { (unsigned char *)&j, sizeof(j) };
  59. alloc_attr.base.next = &tap_log_attr;
  60. alloc_attr.base.attr = NTDB_ATTRIBUTE_ALLOCATOR;
  61. alloc_attr.alloc.alloc = test_alloc;
  62. alloc_attr.alloc.expand = test_expand;
  63. alloc_attr.alloc.free = test_free;
  64. alloc_attr.alloc.priv_data = &owner_weird_count;
  65. plan_tests(sizeof(flags) / sizeof(flags[0]) * (1 + 700 * 3 + 4) + 1);
  66. for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) {
  67. curr_ntdb = NULL;
  68. curr_file = NULL;
  69. ntdb = ntdb_open("run-20-alloc-attr.ntdb", flags[i]|MAYBE_NOSYNC,
  70. O_RDWR|O_CREAT|O_TRUNC, 0600, &alloc_attr);
  71. ok1(ntdb);
  72. if (!ntdb)
  73. continue;
  74. for (j = 0; j < 700; j++) {
  75. NTDB_DATA d = { NULL, 0 }; /* Bogus GCC warning */
  76. ok1(ntdb_store(ntdb, key, data, NTDB_REPLACE) == 0);
  77. ok1(ntdb_fetch(ntdb, key, &d) == NTDB_SUCCESS);
  78. ok1(ntdb_deq(d, data));
  79. test_free(d.dptr, &owner_weird_count);
  80. }
  81. ntdb_close(ntdb);
  82. ok1(owner_null_count == 2+i*2);
  83. ok1(owner_weird_count == 0);
  84. ok1(alloc_count == free_count);
  85. ok1(expand_count != 0);
  86. }
  87. ok1(tap_log_messages == 0);
  88. return exit_status();
  89. }