Thomas Johnson пре 5 месеци
родитељ
комит
a3f3a9e837
2 измењених фајлова са 66 додато и 3 уклоњено
  1. 65
    2
      main.c
  2. 1
    1
      util.c

+ 65
- 2
main.c Прегледај датотеку

@@ -4,6 +4,7 @@
4 4
 #include <stddef.h>
5 5
 #include <unistd.h>
6 6
 #include <sys/mman.h>
7
+#include <string.h>
7 8
 
8 9
 #include "alloc_api.h"
9 10
 
@@ -23,9 +24,10 @@ struct AllocationRecord {
23 24
   uintptr_t size;
24 25
   void *allocation;
25 26
   void *reference;
27
+  int id;
26 28
   struct AllocationRecord *next;
27 29
   struct AllocationRecord *prev;
28
-}
30
+};
29 31
 
30 32
 void insert_record(struct AllocationRecord *dummy, void *allocation, uintptr_t size) {
31 33
   struct AllocationRecord *record = malloc(sizeof(struct AllocationRecord));
@@ -34,9 +36,11 @@ void insert_record(struct AllocationRecord *dummy, void *allocation, uintptr_t s
34 36
   record->reference = malloc(size);
35 37
   record->next = dummy;
36 38
   record->prev = dummy->prev;
39
+  record->id = dummy->id;
37 40
   dummy->prev->next = record;
38 41
   dummy->prev = record;
39 42
   dummy->size += 1;
43
+  dummy->id += 1;
40 44
   memcpy(record->reference, allocation, size);
41 45
 }
42 46
 
@@ -48,6 +52,14 @@ void delete_record(struct AllocationRecord *dummy, struct AllocationRecord *dele
48 52
   dummy->size -= 1;
49 53
 }
50 54
 
55
+struct AllocationRecord *get_record(struct AllocationRecord *dummy, int which) {
56
+  struct AllocationRecord *head = dummy->next;
57
+  while (--which) {
58
+    head = head->next;
59
+  }
60
+  return head;
61
+}
62
+
51 63
 int validate_record(struct AllocationRecord *record) {
52 64
   if (memcmp(record->allocation, record->reference, record->size) != 0) {
53 65
     return 0;
@@ -55,8 +67,48 @@ int validate_record(struct AllocationRecord *record) {
55 67
   return 1;
56 68
 }
57 69
 
70
+void new_region(struct AllocationRecord *dummy, struct Arena *arena) {
71
+  int size = 1 + (rand() % 8192);
72
+  int align = 1 << (rand() % 4);
73
+  void *region = alloc(arena, size, align);
74
+  if (region == NULL) {
75
+    printf("memory allocator broke, we have 1L though\n");
76
+    return;
77
+  }
78
+  for (int ii = 0; ii < size; ii++) {
79
+    ((char*) region)[ii] = (char) rand();
80
+  }
81
+  insert_record(dummy, region, size);
82
+}
83
+
84
+void delete_region(struct AllocationRecord *dummy, struct Arena *arena) {
85
+  if (dummy->size == 0) {
86
+    return;
87
+  }
88
+  uintptr_t which = rand() % dummy->size;
89
+  struct AllocationRecord *zap = get_record(dummy, which);
90
+  unalloc(arena, zap->allocation);
91
+  delete_record(dummy, zap);
92
+}
93
+
94
+int act(struct AllocationRecord *dummy, struct Arena *arena) {
95
+  if (rand() & 1) {
96
+    new_region(dummy, arena);
97
+  } else {
98
+    delete_region(dummy, arena);
99
+  }
100
+  int die = 0;
101
+  struct AllocationRecord *head = dummy->next;
102
+  while (head != dummy) {
103
+    if (!validate_record(head)) {
104
+      printf("validation failed at id %i\n", head->id);
105
+      die = 1;
106
+    }
107
+  }
108
+  return die;
109
+}
110
+
58 111
 int main() {
59
-	printf("Hello, World!\n");
60 112
   struct Arena arena = {
61 113
     NULL,
62 114
     NULL,
@@ -65,5 +117,16 @@ int main() {
65 117
   };
66 118
   void *reg = alloc(&arena, 20, 4);
67 119
   unalloc(&arena, reg);
120
+  struct AllocationRecord dummy = {
121
+    .size = 0,
122
+    .allocation = NULL,
123
+    .reference = NULL,
124
+    .id = 0,
125
+  };
126
+  dummy.next = &dummy;
127
+  dummy.prev = &dummy;
128
+  for (int ii = 0; ii < 100; ii++) {
129
+    act(&dummy, &arena);
130
+  }
68 131
 	return 0;
69 132
 }

+ 1
- 1
util.c Прегледај датотеку

@@ -15,7 +15,7 @@ void* align_after(void* address, int align) {
15 15
     rv = (void*) (addr + align - align % offset);
16 16
   }
17 17
 #ifdef DEBUG
18
-  printf("aligning %p at alignment %lx to %p\n", address, align, rv);
18
+  printf("aligning %p at alignment %x to %p\n", address, align, rv);
19 19
 #endif
20 20
   return rv;
21 21
 }

Loading…
Откажи
Сачувај