-
Notifications
You must be signed in to change notification settings - Fork 1
/
free_every_other.c
180 lines (148 loc) · 5.34 KB
/
free_every_other.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
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include "mem_usage.h"
#include <stdlib.h> /* for calloc, free, rand, rand */
#include <stdio.h> /* for printf */
#include <string.h> /* for memset */
#include <unistd.h> /* for sleep */
/* Linked list header for array data packets. */
typedef struct BUF_HEADER_
{
struct BUF_HEADER_* next_buf;
uint8_t ii_filled;
uint8_t RESERVED[7];
} BUF_HEADER;
int main(int argc, char* argv[])
{
BUF_HEADER* buf_list_start = NULL;
BUF_HEADER* buf_hdr_cur = NULL;
uint32_t i, buf_count, prev_count;
const uint32_t init_buffer_count = 100;
const uint32_t data_buf_size = 1024 * 1024;
BUF_HEADER* buf_next;
BUF_HEADER* buf_prev;
uint8_t ii_del = 1;
int action;
// INITIALIZATION
printf("Start.\n");
fflush(stdout);
print_memory_usage("mem_test", 8);
// Allocate an array of buffers, which we will form into a circular linked
// list.
buf_list_start = (BUF_HEADER*)calloc(1, data_buf_size);
if (buf_list_start == NULL)
{
printf("ERROR: Failed to allocate memory for buf_list_start. "
"Exiting.\n");
return 1;
}
memset(buf_list_start, 0, data_buf_size);
buf_hdr_cur = buf_list_start;
for (i = 1; i < init_buffer_count; i++)
{
buf_hdr_cur->next_buf = (BUF_HEADER*)calloc(1, data_buf_size);
if (buf_hdr_cur->next_buf == NULL)
{
printf("ERROR: Failed to allocate memory for packet buffer. "
"Exiting.\n");
return 1;
}
buf_hdr_cur = buf_hdr_cur->next_buf;
memset(buf_hdr_cur, 0, data_buf_size);
}
buf_hdr_cur->next_buf = buf_list_start;
// Set up initial buffer. Use data_buf_cur as a shortcut to the data portion
// of the buffer, which follows in memory directly after the buffer header.
buf_hdr_cur = buf_list_start;
buf_count = init_buffer_count;
printf("Total allocated buffers: %u\n", buf_count);
fflush(stdout);
print_memory_usage("mem_test", 8);
while (1)
{
sleep(1);
// "Randomly" decide whether to allocate 100 buffers or deallocate half
// of all buffers. Enforce a lower limit to prevent segfaults and keep
// the test interesting.
action = 1 + rand() % 3;
if (buf_count < 50 || action > 1)
{
// ADDITIONAL ALLOCATION
buf_hdr_cur = buf_hdr_cur->next_buf->next_buf->next_buf->next_buf->next_buf;
buf_next = buf_hdr_cur->next_buf;
buf_prev = buf_hdr_cur;
printf("Allocating additional buffers.\n");
fflush(stdout);
// Allocate a batch of new buffers and fit them into
// the existing linked list.
for (i = 0; i < init_buffer_count; i++)
{
buf_hdr_cur->next_buf = (BUF_HEADER*)calloc(1, data_buf_size);
if (buf_hdr_cur->next_buf == NULL)
{
// If we run out of memory and can't
// allocate additional buffers, we are in
// trouble. We may drop data.
printf("ERROR: Failed to allocate memory for packet buffer. "
"Attempting to continue anyway.\n");
fflush(stdout);
break;
}
buf_count++;
buf_hdr_cur = buf_hdr_cur->next_buf;
memset(buf_hdr_cur, 0, data_buf_size);
}
buf_hdr_cur->next_buf = buf_next;
buf_hdr_cur = buf_prev;
printf("Total allocated buffers: %u\n", buf_count);
fflush(stdout);
print_memory_usage("mem_test", 8);
}
else
{
// DEALLOCATE ADDITIONAL BUFFERS
printf("Deallocating some buffers.\n");
fflush(stdout);
prev_count = buf_count;
buf_next = buf_hdr_cur->next_buf;
buf_prev = buf_hdr_cur;
// One by one, free the excess buffers.
while (buf_count > prev_count / 2)
{
buf_next = buf_next->next_buf;
// Only free every other buffer.
if (ii_del)
{
free(buf_prev->next_buf);
buf_prev->next_buf = buf_next;
buf_count--;
ii_del = 0;
}
else
{
// If we don't free, make sure to move the prev pointer.
ii_del = 1;
buf_prev = buf_prev->next_buf;
}
}
buf_list_start = buf_hdr_cur = buf_prev;
printf("Total allocated buffers: %u\n", buf_count);
fflush(stdout);
print_memory_usage("mem_test", 8);
}
}
// DEALLOCATE REMAINING BUFFERS
printf("Deallocating initial buffers.\n");
fflush(stdout);
// Skip the first buffer, but set it's next_buf to NULL so that we'll know
// when we've gotten back to the start.
buf_hdr_cur = buf_list_start->next_buf;
buf_list_start->next_buf = NULL;
while (buf_hdr_cur != NULL)
{
BUF_HEADER* prev_buf = (BUF_HEADER*)buf_hdr_cur;
buf_hdr_cur = buf_hdr_cur->next_buf;
free(prev_buf);
}
printf("Total allocated buffers: %u\n", buf_count);
fflush(stdout);
print_memory_usage("mem_test", 8);
}