-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache-test.c
52 lines (42 loc) · 1.23 KB
/
cache-test.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <stdio.h>
#include <stdlib.h>
#define ACCESSES 1000000000
// Define a linked list node type with no data
typedef struct node {
struct node* next;
} node_t;
int main(int argc, char** argv) {
// Check to make sure we received a command line option
if(argc < 2) {
fprintf(stderr, "Usage: %s <list size>\n", argv[0]);
return 1;
}
// How many items should we have in the list?
int items = atoi(argv[1]);
// Keep a pointer to the beginning and end of the list
node_t* head = NULL;
node_t* last = NULL;
// Repeatedly add items to the end of the list
for(int i=0; i<items; i++) {
// Allocate memory for the linked list node
node_t* n = (node_t*)malloc(sizeof(node_t));
// We're adding to the end, so the next pointer is NULL
n->next = NULL;
// Is the list empty? If so, the new node is the head and tail
if(last == NULL) {
head = n;
last = n;
} else {
last->next = n;
last = n;
}
}
// Now that we have a list, traverse the list over and over again until we've
// visited `ACCESSES` nodes in our linked list.
node_t* current = head;
for(int i=0; i<ACCESSES; i++) {
if(current == NULL) current = head;
else current = current->next;
}
return 0;
}