main.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * lws-api-test-lwsac
  3. *
  4. * Written in 2010-2019 by Andy Green <[email protected]>
  5. *
  6. * This file is made available under the Creative Commons CC0 1.0
  7. * Universal Public Domain Dedication.
  8. */
  9. #include <libwebsockets.h>
  10. struct mytest {
  11. int payload;
  12. /* notice doesn't have to be at start of struct */
  13. lws_list_ptr list_next;
  14. /* a struct can appear on multiple lists too... */
  15. };
  16. /* converts a ptr to struct mytest .list_next to a ptr to struct mytest */
  17. #define list_to_mytest(p) lws_list_ptr_container(p, struct mytest, list_next)
  18. int main(int argc, const char **argv)
  19. {
  20. int n, logs = LLL_USER | LLL_ERR | LLL_WARN | LLL_NOTICE, acc;
  21. lws_list_ptr list_head = NULL, iter;
  22. struct lwsac *lwsac = NULL;
  23. struct mytest *m;
  24. const char *p;
  25. if ((p = lws_cmdline_option(argc, argv, "-d")))
  26. logs = atoi(p);
  27. lws_set_log_level(logs, NULL);
  28. lwsl_user("LWS API selftest: lwsac\n");
  29. /*
  30. * 1) allocate and create 1000 struct mytest in a linked-list
  31. */
  32. for (n = 0; n < 1000; n++) {
  33. m = lwsac_use(&lwsac, sizeof(*m), 0);
  34. if (!m)
  35. return -1;
  36. m->payload = n;
  37. lws_list_ptr_insert(&list_head, &m->list_next, NULL);
  38. }
  39. /*
  40. * 2) report some debug info about the lwsac state... those 1000
  41. * allocations actually only required 4 mallocs
  42. */
  43. lwsac_info(lwsac);
  44. /* 3) iterate the list, accumulating the payloads */
  45. acc = 0;
  46. iter = list_head;
  47. while (iter) {
  48. m = list_to_mytest(iter);
  49. acc += m->payload;
  50. lws_list_ptr_advance(iter);
  51. }
  52. if (acc != 499500) {
  53. lwsl_err("%s: FAIL acc %d\n", __func__, acc);
  54. return 1;
  55. }
  56. /*
  57. * 4) deallocate everything (lwsac is also set to NULL). It just
  58. * deallocates the 4 mallocs, everything in there is gone accordingly
  59. */
  60. lwsac_free(&lwsac);
  61. lwsl_user("Completed: PASS\n");
  62. return 0;
  63. }