No Description
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.

main.c 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <stdint.h>
  4. #include <stddef.h>
  5. #include <unistd.h>
  6. #include <sys/mman.h>
  7. #include <string.h>
  8. #include "alloc_api.h"
  9. void *main_get_new_region(uintptr_t size) {
  10. void *m = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  11. if (m == MAP_FAILED) {
  12. return NULL;
  13. }
  14. return m;
  15. }
  16. void error(char *msg) {
  17. printf("=== ALLOCATOR ERROR ===\n%s\n====== END ERROR ======\n", msg);
  18. }
  19. struct AllocationRecord {
  20. uintptr_t size;
  21. void *allocation;
  22. void *reference;
  23. int id;
  24. struct AllocationRecord *next;
  25. struct AllocationRecord *prev;
  26. };
  27. void insert_record(struct AllocationRecord *dummy, void *allocation, uintptr_t size) {
  28. struct AllocationRecord *record = malloc(sizeof(struct AllocationRecord));
  29. record->size = size;
  30. record->allocation = allocation;
  31. record->reference = malloc(size);
  32. record->next = dummy;
  33. record->prev = dummy->prev;
  34. record->id = dummy->id;
  35. dummy->prev->next = record;
  36. dummy->prev = record;
  37. dummy->size += 1;
  38. dummy->id += 1;
  39. memcpy(record->reference, allocation, size);
  40. }
  41. void delete_record(struct AllocationRecord *dummy, struct AllocationRecord *delete) {
  42. free(delete->reference);
  43. delete->prev->next = delete->next;
  44. delete->next->prev = delete->prev;
  45. free(delete);
  46. dummy->size -= 1;
  47. }
  48. struct AllocationRecord *get_record(struct AllocationRecord *dummy, int which) {
  49. struct AllocationRecord *head = dummy->next;
  50. while (which--) {
  51. head = head->next;
  52. }
  53. return head;
  54. }
  55. int validate_record(struct AllocationRecord *record) {
  56. if (memcmp(record->allocation, record->reference, record->size) != 0) {
  57. return 0;
  58. }
  59. return 1;
  60. }
  61. void new_region(struct AllocationRecord *dummy, struct Arena *arena) {
  62. uintptr_t size = 1 + (rand() % 8192);
  63. uintptr_t align = 1 << (rand() % 4);
  64. printf("alloc'ing a region of size %lu with alignment %lu, id %i\n", size, align, dummy->id);
  65. void *region = alloc(arena, size, align);
  66. if (region == NULL) {
  67. printf("memory allocator broke, we have 1L though\n");
  68. return;
  69. }
  70. for (int ii = 0; ii < size; ii++) {
  71. ((char*) region)[ii] = (char) rand();
  72. }
  73. insert_record(dummy, region, size);
  74. }
  75. void delete_region(struct AllocationRecord *dummy, struct Arena *arena) {
  76. if (dummy->size == 0) {
  77. return;
  78. }
  79. uintptr_t which = rand() % dummy->size;
  80. struct AllocationRecord *zap = get_record(dummy, which);
  81. printf("dealloc'ing a region of size %lu, id %i\n", zap->size, zap->id);
  82. unalloc(arena, zap->allocation);
  83. delete_record(dummy, zap);
  84. }
  85. int act(struct AllocationRecord *dummy, struct Arena *arena) {
  86. if (rand() & 1) {
  87. new_region(dummy, arena);
  88. } else {
  89. delete_region(dummy, arena);
  90. }
  91. int die = 0;
  92. struct AllocationRecord *head = dummy->next;
  93. while (head != dummy) {
  94. if (!validate_record(head)) {
  95. //printf("validation failed at id %i\n", head->id);
  96. die = 1;
  97. }
  98. head = head->next;
  99. }
  100. return die;
  101. }
  102. int main() {
  103. srand(1);
  104. struct Arena arena = {
  105. NULL,
  106. NULL,
  107. main_get_new_region,
  108. error,
  109. };
  110. void *reg = alloc(&arena, 20, 4);
  111. unalloc(&arena, reg);
  112. struct AllocationRecord dummy = {
  113. .size = 0,
  114. .allocation = NULL,
  115. .reference = NULL,
  116. .id = 0,
  117. };
  118. dummy.next = &dummy;
  119. dummy.prev = &dummy;
  120. for (int ii = 0; ii < 100; ii++) {
  121. act(&dummy, &arena);
  122. }
  123. return 0;
  124. }