main.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #include <assert.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <stdbool.h>
  5. #include <stdint.h>
  6. #include "./heap.h"
  7. #define JIM_IMPLEMENTATION
  8. #include "jim.h"
  9. typedef struct Node Node;
  10. struct Node {
  11. char x;
  12. Node *left;
  13. Node *right;
  14. };
  15. Node *generate_tree(size_t level_cur, size_t level_max)
  16. {
  17. if (level_cur < level_max) {
  18. Node *root = heap_alloc(sizeof(*root));
  19. assert((char) level_cur - 'a' <= 'z');
  20. root->x = level_cur + 'a';
  21. root->left = generate_tree(level_cur + 1, level_max);
  22. root->right = generate_tree(level_cur + 1, level_max);
  23. return root;
  24. } else {
  25. return NULL;
  26. }
  27. }
  28. void print_tree(Node *root, Jim *jim)
  29. {
  30. if (root != NULL) {
  31. jim_object_begin(jim);
  32. jim_member_key(jim, "value");
  33. jim_string_sized(jim, &root->x, 1);
  34. jim_member_key(jim, "left");
  35. print_tree(root->left, jim);
  36. jim_member_key(jim, "right");
  37. print_tree(root->right, jim);
  38. jim_object_end(jim);
  39. } else {
  40. jim_null(jim);
  41. }
  42. }
  43. #define N 10
  44. void *ptrs[N] = {0};
  45. int main()
  46. {
  47. stack_base = (const uintptr_t*)__builtin_frame_address(0);
  48. for (size_t i = 0; i < 10; ++i) {
  49. heap_alloc(i);
  50. }
  51. Node *root = generate_tree(0, 3);
  52. printf("root: %p\n", (void*)root);
  53. Jim jim = {
  54. .sink = stdout,
  55. .write = (Jim_Write) fwrite,
  56. };
  57. print_tree(root, &jim);
  58. printf("\n------------------------------\n");
  59. heap_collect();
  60. chunk_list_dump(&alloced_chunks, "Alloced");
  61. chunk_list_dump(&freed_chunks, "Freed");
  62. printf("------------------------------\n");
  63. root = NULL;
  64. heap_collect();
  65. chunk_list_dump(&alloced_chunks, "Alloced");
  66. chunk_list_dump(&freed_chunks, "Freed");
  67. return 0;
  68. }