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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
|
// A simple C program for traversal of a linked list
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
struct Node {
int data;
char* buf;
struct Node* next;
};
// This function prints contents of linked list starting from
// the given node
void printList(struct Node* n)
{
while (n != NULL) {
printf(" %d ", n->data);
n = n->next;
}
}
long long int lo = 1L << 30;
long long int hi = 16L << 30;
struct Node* newNode(int sz) {
struct Node* n = (struct Node*)calloc(1, sizeof(struct Node));
n->buf = calloc(sz, 1);
for (int i = 0; i < sz; i++) {
n->buf[i] = 0xff;
}
n->data = sz;
n->next = NULL;
return n;
}
void allocate(struct Node* n, int sz) {
struct Node* nn = newNode(sz);
struct Node* tmp = n->next;
n->next = nn;
nn->next = tmp;
}
int dealloc(struct Node* n) {
if (n->next == NULL) {
printf("n->next is NULL\n");
exit(1);
}
struct Node* tmp = n->next;
n->next = tmp->next;
int sz = tmp->data;
free(tmp->buf);
free(tmp);
return sz;
}
int main()
{
struct Node* root = newNode(100);
long long int total = 0;
int increase = 1;
while(1) {
if (increase == 1) {
int sz = (1 + rand() % 256) << 20;
allocate(root, sz);
if (root->next == NULL) {
printf("root->next is NULL\n");
exit(1);
}
total += sz;
if (total > hi) {
increase = 0;
}
} else {
int sz = dealloc(root);
total -= sz;
if (total < lo) {
increase = 1;
sleep(5);
} else {
usleep(10);
}
}
long double gb = total;
gb /= (1 << 30);
printf("Total size: %.2LF\n", gb);
};
return 0;
}
|