Nav apraksta
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

main.c 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 *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 0;
  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. int size = 1 + (rand() % 8192);
  63. int align = 1 << (rand() % 4);
  64. void *region = alloc(arena, size, align);
  65. if (region == NULL) {
  66. printf("memory allocator broke, we have 1L though\n");
  67. return;
  68. }
  69. for (int ii = 0; ii < size; ii++) {
  70. ((char*) region)[ii] = (char) rand();
  71. }
  72. insert_record(dummy, region, size);
  73. }
  74. void delete_region(struct AllocationRecord *dummy, struct Arena *arena) {
  75. if (dummy->size == 0) {
  76. return;
  77. }
  78. uintptr_t which = rand() % dummy->size;
  79. struct AllocationRecord *zap = get_record(dummy, which);
  80. unalloc(arena, zap->allocation);
  81. delete_record(dummy, zap);
  82. }
  83. int act(struct AllocationRecord *dummy, struct Arena *arena) {
  84. if (rand() & 1) {
  85. new_region(dummy, arena);
  86. } else {
  87. delete_region(dummy, arena);
  88. }
  89. int die = 0;
  90. struct AllocationRecord *head = dummy->next;
  91. while (head != dummy) {
  92. if (!validate_record(head)) {
  93. printf("validation failed at id %i\n", head->id);
  94. die = 1;
  95. }
  96. }
  97. return die;
  98. }
  99. int main() {
  100. struct Arena arena = {
  101. NULL,
  102. NULL,
  103. get_new_region,
  104. error,
  105. };
  106. void *reg = alloc(&arena, 20, 4);
  107. unalloc(&arena, reg);
  108. struct AllocationRecord dummy = {
  109. .size = 0,
  110. .allocation = NULL,
  111. .reference = NULL,
  112. .id = 0,
  113. };
  114. dummy.next = &dummy;
  115. dummy.prev = &dummy;
  116. for (int ii = 0; ii < 100; ii++) {
  117. act(&dummy, &arena);
  118. }
  119. return 0;
  120. }