github.com/etecs-ru/ristretto@v0.9.1/contrib/memtestc/list.c (about) 1 /* 2 * Copyright 2020 Dgraph Labs, Inc. and Contributors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 // A simple C program for traversal of a linked list 18 #include <stdio.h> 19 #include <stdlib.h> 20 #include <unistd.h> 21 22 struct Node { 23 int data; 24 char* buf; 25 struct Node* next; 26 }; 27 28 // This function prints contents of linked list starting from 29 // the given node 30 void printList(struct Node* n) 31 { 32 while (n != NULL) { 33 printf(" %d ", n->data); 34 n = n->next; 35 } 36 } 37 38 long long int lo = 1L << 30; 39 long long int hi = 16L << 30; 40 41 struct Node* newNode(int sz) { 42 struct Node* n = (struct Node*)calloc(1, sizeof(struct Node)); 43 n->buf = calloc(sz, 1); 44 for (int i = 0; i < sz; i++) { 45 n->buf[i] = 0xff; 46 } 47 n->data = sz; 48 n->next = NULL; 49 return n; 50 } 51 52 void allocate(struct Node* n, int sz) { 53 struct Node* nn = newNode(sz); 54 struct Node* tmp = n->next; 55 n->next = nn; 56 nn->next = tmp; 57 } 58 59 int dealloc(struct Node* n) { 60 if (n->next == NULL) { 61 printf("n->next is NULL\n"); 62 exit(1); 63 } 64 struct Node* tmp = n->next; 65 n->next = tmp->next; 66 int sz = tmp->data; 67 free(tmp->buf); 68 free(tmp); 69 return sz; 70 } 71 72 int main() 73 { 74 struct Node* root = newNode(100); 75 76 long long int total = 0; 77 int increase = 1; 78 while(1) { 79 if (increase == 1) { 80 int sz = (1 + rand() % 256) << 20; 81 allocate(root, sz); 82 if (root->next == NULL) { 83 printf("root->next is NULL\n"); 84 exit(1); 85 } 86 total += sz; 87 if (total > hi) { 88 increase = 0; 89 } 90 } else { 91 int sz = dealloc(root); 92 total -= sz; 93 if (total < lo) { 94 increase = 1; 95 sleep(5); 96 } else { 97 usleep(10); 98 } 99 } 100 101 long double gb = total; 102 gb /= (1 << 30); 103 printf("Total size: %.2LF\n", gb); 104 }; 105 106 return 0; 107 } 108