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.

rspamd_mem_pool_test.c 1.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <glib.h>
  5. #include "../src/mem_pool.h"
  6. #include "tests.h"
  7. #define TEST_BUF "test bufffer"
  8. #define TEST2_BUF "test bufffertest bufffer"
  9. void
  10. rspamd_mem_pool_test_func ()
  11. {
  12. memory_pool_t *pool;
  13. memory_pool_stat_t st;
  14. char *tmp, *tmp2, *tmp3;
  15. pid_t pid;
  16. int ret;
  17. pool = memory_pool_new (sizeof (TEST_BUF));
  18. tmp = memory_pool_alloc (pool, sizeof (TEST_BUF));
  19. tmp2 = memory_pool_alloc (pool, sizeof (TEST_BUF) * 2);
  20. tmp3 = memory_pool_alloc_shared (pool, sizeof (TEST_BUF));
  21. snprintf (tmp, sizeof (TEST_BUF), "%s", TEST_BUF);
  22. snprintf (tmp2, sizeof (TEST_BUF) * 2, "%s", TEST2_BUF);
  23. snprintf (tmp3, sizeof (TEST_BUF), "%s", TEST_BUF);
  24. g_assert (strncmp (tmp, TEST_BUF, sizeof (TEST_BUF)) == 0);
  25. g_assert (strncmp (tmp2, TEST2_BUF, sizeof (TEST2_BUF)) == 0);
  26. g_assert (strncmp (tmp3, TEST_BUF, sizeof (TEST_BUF)) == 0);
  27. memory_pool_lock_shared (pool, tmp3);
  28. if ((pid = fork ()) == 0) {
  29. memory_pool_lock_shared (pool, tmp3);
  30. g_assert (*tmp3 == 's');
  31. *tmp3 = 't';
  32. memory_pool_unlock_shared (pool, tmp3);
  33. exit (EXIT_SUCCESS);
  34. }
  35. else {
  36. *tmp3 = 's';
  37. memory_pool_unlock_shared (pool, tmp3);
  38. }
  39. wait (&ret);
  40. g_assert (*tmp3 == 't');
  41. memory_pool_delete (pool);
  42. memory_pool_stat (&st);
  43. /* Check allocator stat */
  44. g_assert (st.bytes_allocated == sizeof (TEST_BUF) * 4);
  45. g_assert (st.chunks_allocated == 2);
  46. g_assert (st.shared_chunks_allocated == 1);
  47. g_assert (st.chunks_freed == 3);
  48. }