Concurrent I/O without threads
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.

118 lines
2.5KB

  1. /* threadless.io
  2. * Copyright (c) 2016 Justin R. Cutler
  3. * Licensed under the MIT License. See LICENSE file in the project root for
  4. * full license information.
  5. */
  6. /** @file
  7. * heap interface test
  8. * @author Justin R. Cutler <justin.r.cutler@gmail.com>
  9. */
  10. /* HAVE_* */
  11. #include "config.h"
  12. /* errno, EINVAL */
  13. #include <errno.h>
  14. /* printf, perror */
  15. #include <stdio.h>
  16. /* EXIT_SUCCESS, EXIT_FAILURE */
  17. #include <stdlib.h>
  18. /* container_of */
  19. #include <threadless/container_of.h>
  20. /* default_allocator_get */
  21. #include <threadless/default_allocator.h>
  22. #ifdef HAVE_MMAP
  23. /* mmap_allocator_get */
  24. # include <threadless/mmap_allocator.h>
  25. #endif
  26. /* ... */
  27. #include <threadless/heap.h>
  28. const int data[] = { 4 /* duplicate */, 1, 9, 2, 8, 4, 0, 5, 3, 6, 7, 99 };
  29. const size_t data_count = sizeof(data) / sizeof(data[0]);
  30. typedef struct {
  31. heap_node_t node;
  32. int value;
  33. } value_t;
  34. static int min_compare(const heap_node_t *a, const heap_node_t *b)
  35. {
  36. int va = container_of(a, const value_t, node)->value;
  37. int vb = container_of(b, const value_t, node)->value;
  38. return va - vb;
  39. }
  40. static int run(allocator_t *allocator)
  41. {
  42. int error = 0;
  43. allocation_t alloc;
  44. value_t *values = NULL;
  45. heap_t heap;
  46. size_t i;
  47. heap_node_t *node;
  48. allocation_init(&alloc, allocator);
  49. error = allocation_realloc_array(&alloc, data_count, sizeof(*values));
  50. if (error) {
  51. return error;
  52. }
  53. values = alloc.memory;
  54. heap_init(&heap, allocator, min_compare);
  55. /* push values into heap */
  56. for (i = 0; !error && i < data_count; ++i) {
  57. values[i].value = data[i];
  58. error = heap_push(&heap, &(values[i].node));
  59. }
  60. /* remove from middle */
  61. heap_remove(&(values[0].node));
  62. /* remove from end */
  63. heap_remove(&(values[data_count - 1].node));
  64. /* pull values out of heap (minimum first) */
  65. while (NULL != (node = heap_pop(&heap))) {
  66. value_t *value = container_of(node, value_t, node);
  67. printf("%i\n", value->value);
  68. }
  69. heap_fini(&heap);
  70. allocation_free(&alloc);
  71. return error;
  72. }
  73. int main(int argc, char *argv[])
  74. {
  75. int error;
  76. allocator_t *allocator;
  77. (void) argc;
  78. (void) argv;
  79. printf("default allocator:\n");
  80. allocator = default_allocator_get();
  81. error = run(allocator);
  82. allocator_destroy(allocator);
  83. #ifdef HAVE_MMAP
  84. if (!error) {
  85. printf("mmap allocator:\n");
  86. allocator = mmap_allocator_get();
  87. error = run(allocator);
  88. allocator_destroy(allocator);
  89. }
  90. #endif
  91. return !error ? EXIT_SUCCESS : EXIT_FAILURE;
  92. }