2
0

testpools.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. /*
  2. * Copyright (c) 2018-2023 SignalWire, Inc
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in all
  12. * copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. #include "libks/ks.h"
  23. #include <stdlib.h>
  24. #include <stdio.h>
  25. #include <string.h>
  26. #include "tap.h"
  27. #define STR "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  28. #ifdef KS_DEBUG_POOL
  29. /* Include the private pool header so we can test some pool debug features */
  30. #include "libks/internal/ks_pool.h"
  31. #endif
  32. static void fill(char *str, int bytes, char c)
  33. {
  34. memset(str, c, bytes -1);
  35. *(str+(bytes-1)) = '\0';
  36. }
  37. struct foo {
  38. int x;
  39. char *str;
  40. };
  41. void cleanup(void *ptr, void *arg, ks_pool_cleanup_action_t action, ks_pool_cleanup_type_t type)
  42. {
  43. struct foo *foo = (struct foo *) ptr;
  44. printf("Cleanup %p action: %d\n", ptr, action);
  45. switch(action) {
  46. case KS_MPCL_ANNOUNCE:
  47. break;
  48. case KS_MPCL_TEARDOWN:
  49. break;
  50. case KS_MPCL_DESTROY:
  51. printf("DESTROY STR [%s]\n", foo->str);
  52. free(foo->str);
  53. foo->str = NULL;
  54. }
  55. }
  56. int main(int argc, char **argv)
  57. {
  58. ks_pool_t *pool;
  59. int err = 0;
  60. char *str = NULL;
  61. int bytes = 1024;
  62. ks_status_t status;
  63. struct foo *foo;
  64. ks_init();
  65. plan(14);
  66. if (argc > 1) {
  67. int tmp = atoi(argv[1]);
  68. if (tmp > 0) {
  69. bytes = tmp;
  70. } else {
  71. fprintf(stderr, "INVALID\n");
  72. exit(255);
  73. }
  74. }
  75. ks_pool_open(&pool);
  76. void *blah = ks_pool_alloc(pool, 64 * 1024);
  77. ks_pool_free(&blah);
  78. blah = ks_pool_alloc(pool, 2 * 1024);
  79. ks_pool_close(&pool);
  80. status = ks_pool_open(&pool);
  81. printf("OPEN: %p\n", (void *)pool);
  82. ok(status == KS_STATUS_SUCCESS);
  83. if (status != KS_STATUS_SUCCESS) {
  84. fprintf(stderr, "OPEN ERR: %d [%s]\n", err, ks_pool_strerror(status));
  85. exit(255);
  86. }
  87. printf("ALLOC:\n");
  88. str = ks_pool_alloc(pool, bytes);
  89. ok(str != NULL);
  90. if (!str) {
  91. fprintf(stderr, "ALLOC ERR\n");
  92. exit(255);
  93. }
  94. fill(str, bytes, '.');
  95. printf("%s\n", str);
  96. printf("FREE:\n");
  97. status = ks_pool_free(&str);
  98. if (status != KS_STATUS_SUCCESS) {
  99. fprintf(stderr, "FREE ERR: [%s]\n", ks_pool_strerror(err));
  100. exit(255);
  101. }
  102. printf("ALLOC2:\n");
  103. str = ks_pool_alloc(pool, bytes);
  104. ok(str != NULL);
  105. if (!str) {
  106. fprintf(stderr, "ALLOC2 ERR: [FAILED]\n");
  107. exit(255);
  108. }
  109. ks_snprintf(str, bytes, "%s", STR);
  110. printf("%s\n", str);
  111. printf("ALLOC3 (refs):\n");
  112. str = ks_pool_ref(str);
  113. printf("STR [%s]\n", str);
  114. ks_pool_free(&str);
  115. ok(str != NULL && !strcmp(str, STR));
  116. printf("STR [%s]\n", str);
  117. ks_pool_free(&str);
  118. ok(str == NULL);
  119. str = ks_pool_alloc(pool, bytes);
  120. ok(str != NULL);
  121. if (!str) {
  122. fprintf(stderr, "ALLOC2 ERR: [FAILED]\n");
  123. exit(255);
  124. }
  125. fill(str, bytes, '-');
  126. printf("%s\n", str);
  127. printf("ALLOC OBJ:\n");
  128. foo = ks_pool_alloc(pool, sizeof(struct foo));
  129. ok(foo != NULL);
  130. if (!foo) {
  131. fprintf(stderr, "ALLOC OBJ: [FAILED]\n");
  132. exit(255);
  133. } else {
  134. printf("ALLOC OBJ [%p]:\n", (void *) foo);
  135. }
  136. foo->x = 12;
  137. foo->str = strdup("This is a test 1234 abcd; This will be called on explicit free\n");
  138. ks_pool_set_cleanup(foo, NULL, cleanup);
  139. printf("FREE OBJ:\n");
  140. status = ks_pool_free(&foo);
  141. ok(status == KS_STATUS_SUCCESS);
  142. if (status != KS_STATUS_SUCCESS) {
  143. fprintf(stderr, "FREE OBJ ERR: [%s]\n", ks_pool_strerror(status));
  144. exit(255);
  145. }
  146. printf("ALLOC OBJ2:\n");
  147. foo = ks_pool_alloc(pool, sizeof(struct foo));
  148. ok(foo != NULL);
  149. if (!foo) {
  150. fprintf(stderr, "ALLOC OBJ2: [FAILED]\n");
  151. exit(255);
  152. } else {
  153. printf("ALLOC OBJ2 [%p]:\n", (void *) foo);
  154. }
  155. foo->x = 12;
  156. foo->str = strdup("This is a second test 1234 abcd; This will be called on pool clear/destroy\n");
  157. ks_pool_set_cleanup(foo, NULL, cleanup);
  158. printf("ALLOC OBJ3: %p\n", (void *)pool);
  159. foo = ks_pool_alloc(pool, sizeof(struct foo));
  160. ok(foo != NULL);
  161. if (!foo) {
  162. fprintf(stderr, "ALLOC OBJ3: [FAILED]\n");
  163. exit(255);
  164. } else {
  165. printf("ALLOC OBJ3 [%p]:\n", (void *) foo);
  166. }
  167. printf("CLEANUP: %p\n", (void *)pool);
  168. foo->x = 12;
  169. foo->str = strdup("This is a third test 1234 abcd; This will be called on pool clear/destroy\n");
  170. ks_pool_set_cleanup(foo, NULL, cleanup);
  171. printf("RESIZE: %p\n", (void *)pool);
  172. ks_snprintf(str, bytes, "%s", STR);
  173. printf("1 STR [%s]\n", str);
  174. bytes *= 2;
  175. str = ks_pool_resize(str, bytes);
  176. printf("2 STR [%s]\n", str);
  177. ok(!strcmp(str, STR));
  178. if (!str) {
  179. fprintf(stderr, "RESIZE ERR: [FAILED]\n");
  180. exit(255);
  181. }
  182. fill(str, bytes, '*');
  183. printf("%s\n", str);
  184. printf("FREE 2:\n");
  185. status = ks_pool_free(&str);
  186. ok(status == KS_STATUS_SUCCESS);
  187. if (status != KS_STATUS_SUCCESS) {
  188. fprintf(stderr, "FREE2 ERR: [%s]\n", ks_pool_strerror(status));
  189. exit(255);
  190. }
  191. printf("CLEAR:\n");
  192. status = ks_pool_clear(pool);
  193. ok(status == KS_STATUS_SUCCESS);
  194. if (status != KS_STATUS_SUCCESS) {
  195. fprintf(stderr, "CLEAR ERR: [%s]\n", ks_pool_strerror(status));
  196. exit(255);
  197. }
  198. printf("CLOSE:\n");
  199. status = ks_pool_close(&pool);
  200. ok(status == KS_STATUS_SUCCESS);
  201. if (status != KS_STATUS_SUCCESS) {
  202. fprintf(stderr, "CLOSE ERR: [%s]\n", ks_pool_strerror(err));
  203. exit(255);
  204. }
  205. ks_shutdown();
  206. done_testing();
  207. }