Repository navigation
Expand file tree
/
Copy pathhalf_fit_test.c
More file actions
543 lines (399 loc) · 11.1 KB
/
Copy pathhalf_fit_test.c
File metadata and controls
543 lines (399 loc) · 11.1 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
#include "half_fit.h"
#include "lpc17xx.h"
#include <stdio.h>
#include <errno.h>
#include <stdbool.h>
#include <stdlib.h>
#include "uart.h"
#define smlst_blk 5
#define smlst_blk_sz (1 << smlst_blk)
#define lrgst_blk 15
#define lrgst_blk_sz (1 << lrgst_blk)
// How many random blocks are allocated and removed. This random blocks are at most 2*RNDM_TESTS
#define RNDM_TESTS 100
//DO_PRINT allows or prevents the printf statements from being called. The elapsed time printf is the only statement that is run.
//#define DO_PRINT
/**-----------------------------------------------------------------Timer part--------------------------------------------------------*/
// How many ticks are passed
volatile uint32_t msTicks;
volatile uint8_t run_timer;
// SysTick interrupt Handler.
void SysTick_Handler( void ) {
if ( run_timer ) {
++msTicks;
}
}
__inline TimerInit(){
msTicks = 0;
run_timer = 0;
/* Configure SysTick to generate an interrupt every millisecond */
while ( SysTick_Config(SystemCoreClock / 1000) ) {
/* Check return code for errors */
}
}
__inline TimerStart(){
run_timer = 1;
}
__inline TimerStop(){
run_timer = 0;
}
__inline current_elapsed_time(){
return msTicks;
}
// The timer can be reset if it is already stopped
__inline TimerReset(){
if ( run_timer == 0 ) {
msTicks = 0;
}
}
/**--------------------------------------------------------------End of Timer part--------------------------------------------------------*/
/*
take all allocated blocks and find any violations.
The violations are not revelaed on demand. all the allocated blocks are
stored in an array, then sorted. The sorted array is traversed
to find any block stated before the previous one finished.
Such a simple algorithm does not need any complicated data structure and
is implemented using quick sort.
*/
typedef struct block {
void *ptr; // The actual pointer
size_t len; // The requested size
} block_t;
typedef struct pair {
block_t first;
block_t second;
} pair_t;
/* Find the maximum possible allocating block
*/
size_t find_max_block( void ) {
size_t i;
void *p = NULL;
for ( i = lrgst_blk_sz; i > 0; --i ) {
p = half_alloc( i );
if ( p != NULL ) {
half_free( p );
return i;
}
}
return 0;
}
int cmpr_blks( const void * a, const void * b ) {
return (char *)( ((block_t *) a)->ptr ) - (char *)( ((block_t *) b)->ptr );
}
bool is_violated( pair_t pair ) {
bool rslt = !( pair.first.ptr == NULL
&& pair.first.len == 0
&& pair.second.ptr == NULL
&& pair.second.len == 0 );
if ( rslt ) {
#ifdef DO_PRINT
printf( "The %d Byte block starts at %d overlaps with the %d Byte block starts at %d\n",
pair.first.len, pair.first.ptr, pair.second.len, pair.second.ptr );
#endif
}
return rslt;
}
pair_t find_violation( block_t * blk_arr, size_t len ) {
pair_t result;
uint32_t i;
block_t empty_blk;
qsort( blk_arr, len, sizeof(block_t), cmpr_blks );
empty_blk.ptr = NULL;
empty_blk.len = 0;
result.first = empty_blk;
result.second = empty_blk;
for ( i = 0; i < len - 1; ++i) {
if ( ((char *)blk_arr[i + 1].ptr - (char *)blk_arr[i].ptr) < blk_arr[i].len ) {
// Violation happened
result.first = blk_arr[i];
result.second = blk_arr[i + 1];
break;
}
}
return result;
}
uint32_t log_2( uint32_t n ) {
uint32_t b, m;
if ( n <= 0 ) {
#ifdef DO_PRINT
printf( "log_2(0) is wrong\n" );
#endif
}
b = sizeof( unsigned int ) * 8 - 1;
m = 1 << b;
while ( !(n & m) && (m != 0) ) {
m >>= 1;
b--;
}
return b;
}
bool test_max_alc( void ) {
bool rslt = true;
uint32_t blk_sz, max_blk_sz;
half_init();
blk_sz = find_max_block();
#ifdef DO_PRINT
printf( "blk_sz = %d", blk_sz );
#endif
max_blk_sz = 0x01 << lrgst_blk;
if ( ((int)max_blk_sz - (int)blk_sz) / max_blk_sz > 1 ) {
// The algorithm wasted more than 1% of memory.
rslt = false;
}
return rslt;
}
bool test_alc_free_max( void ) {
bool rslt = true;
uint32_t blk_sz;
void* ptr;
half_init();
blk_sz = find_max_block();
ptr = half_alloc( blk_sz );
if ( ptr == NULL ) {
rslt = false;
}
return rslt;
}
bool test_static_alc_free( void ) {
bool rslt = true;
uint32_t max_sz;
void *ptr_1, *ptr_2, *ptr_3, *ptr_4, *ptr_5, *ptr_6;
half_init();
max_sz = find_max_block();
ptr_1 = half_alloc( (1 << 5) + 1 );
if ( ptr_1 == NULL ) {
return false;
}
ptr_2 = half_alloc( (1 << 9) - 1 );
if ( ptr_2 == NULL ) {
return false;
}
ptr_3 = half_alloc( (1 << 5) + 1 );
if ( ptr_3 == NULL ) {
return false;
}
ptr_4 = half_alloc( 1 << 10 );
if ( ptr_4 == NULL ) {
return false;
}
ptr_5 = half_alloc( 12345 );
if ( ptr_5 == NULL ) {
return false;
}
half_free(ptr_1);
ptr_6 = half_alloc( 1 );
if ( ptr_6 == NULL ) {
return false;
}
half_free( ptr_3 );
half_free( ptr_4 );
ptr_1 = half_alloc( 1 << 9 );
if ( ptr_1 == NULL ) {
return false;
}
half_free( ptr_6 );
half_free( ptr_1 );
half_free( ptr_2 );
half_free( ptr_5 );
// Check wether all allocated memory blocks are freed.
ptr_1 = half_alloc( max_sz );
if ( ptr_1 == NULL ) {
rslt = false;
#ifdef DO_PRINT
printf( "Memory is defraged.\n" );
#endif
} else {
half_free( ptr_1 );
}
return rslt;
}
void alloc_blk_in_arr( block_t* blks, size_t *blks_sz, size_t len ) {
blks[*blks_sz].ptr = half_alloc( len );
blks[*blks_sz].len = len;
if ( blks[*blks_sz].ptr != NULL ) {
(*blks_sz)++;
}
}
bool test_static_alc_free_violation( void ) {
bool rslt = true;
size_t max_sz, blks_sz;
block_t blks[5];
void* ptr_1;
half_init();
max_sz = find_max_block();
blks_sz = 0;
alloc_blk_in_arr( blks, &blks_sz, (1 << 5) + 1 );
alloc_blk_in_arr( blks, &blks_sz, (1 << 9) - 1 );
alloc_blk_in_arr( blks, &blks_sz, (1 << 5) + 1 );
alloc_blk_in_arr( blks, &blks_sz, (1 << 10) );
alloc_blk_in_arr( blks, &blks_sz, 12345 );
if ( blks_sz == 0 ) {
#ifdef DO_PRINT
printf( "Failure on allocating any memory block. The memory access violation is irrelevant.\n" );
#endif
return false;
}
// Checking any violation
if ( is_violated( find_violation( blks, blks_sz ) ) ) {
return false;
}
--blks_sz;
half_free(blks[blks_sz].ptr);
--blks_sz;
half_free(blks[blks_sz].ptr);
--blks_sz;
half_free(blks[blks_sz].ptr);
--blks_sz;
half_free(blks[blks_sz].ptr);
alloc_blk_in_arr(blks, &blks_sz, (1 << 9));
// Checking any violation
if ( is_violated(find_violation(blks, blks_sz)) ) {
return false;
}
--blks_sz;
half_free(blks[blks_sz].ptr);
--blks_sz;
half_free(blks[blks_sz].ptr);
// Check wether all allocated memory blocks are freed.
ptr_1 = half_alloc(max_sz);
if ( ptr_1 == NULL ) {
rslt = false;
#ifdef DO_PRINT
printf( "Memory is defraged.\n" );
#endif
} else {
half_free( ptr_1 );
}
return rslt;
}
size_t get_random_block_size( void ) {
// Generate the random numbe in the range of [2^0, 2^(lrgst_blk-smlst_blk) )
size_t r_num = rand() % ((1 << (lrgst_blk - smlst_blk)) - 1) + 1;
r_num = (lrgst_blk - 1) - log_2(r_num);
r_num = (rand() % (1 << r_num)) + (1 << r_num) + 1;
return r_num;
}
bool test_rndm_alc_free( void ) {
bool rslt = true;
size_t line = 0, max_sz, blks_sz, alc_rec, tbf, blk_sz;
int i;
block_t blks[RNDM_TESTS << 1];
block_t blk;
void *ptr_1;
blks_sz = 0;
half_init();
max_sz = find_max_block();
// 'alc_rec' stores how many times 'half_alloc' successfully returns a requested block.
alc_rec = 0;
// Allocating random memory blocks
for ( i = 0; i < RNDM_TESTS; ++i ) {
// Making a new memory block and storing its pointer in the array
size_t blk_sz = get_random_block_size();
block_t blk;
blk.ptr = half_alloc(blk_sz);
blk.len = blk_sz;
if ( blk.ptr != 0 ) {
blks[blks_sz] = blk;
++blks_sz;
alc_rec++;
#ifdef DO_PRINT
printf( "%i)The allocated %d Byte block starts from %d \n", ++line, blk.len, blk.ptr );
#endif
}
}
// Checking any violation
if ( is_violated(find_violation(blks, blks_sz)) ) {
return false;
}
// Free almost half of the allocation blocks
for ( i = 0; i < RNDM_TESTS >> 1 ; ++i ) {
if ( (rand() % 2) && (blks_sz > 0) ) {
// Free a random block
tbf = rand() % blks_sz; // To be freed idex
half_free(blks[tbf].ptr);
#ifdef DO_PRINT
printf( "%i)The %d Byte block starting from %d is free1\n", ++line, blks[tbf].len, blks[tbf].ptr );
#endif
--blks_sz;
blks[tbf] = blks[blks_sz];
} else {
blk_sz = get_random_block_size();
blk.ptr = half_alloc(blk_sz);
blk.len = blk_sz;
if ( blk.ptr != 0 ) {
blks[blks_sz] = blk;
++blks_sz;
alc_rec++;
#ifdef DO_PRINT
printf( "%i)The allocated %d Byte block starts from %d \n", ++line, blk.len, blk.ptr );
#endif
}
}
}
// Checking any violation
if ( is_violated( find_violation( blks, blks_sz ) ) ) {
return false;
}
for ( i = blks_sz - 1; i >= 0; --i ) {
half_free(blks[i].ptr);
--blks_sz;
#ifdef DO_PRINT
printf( "%i)The %d Byte block starting from %d is free2\n", ++line, blks[i].len, blks[i].ptr );
#endif
}
// All allocated memories have to be freed now.
#ifdef DO_PRINT
printf("%d random blocks are allocated and freed without any violation.\n", alc_rec);
#endif
ptr_1 = half_alloc(max_sz);
if ( ptr_1 == NULL ) {
rslt = false;
#ifdef DO_PRINT
printf( "Memory is defraged.\n" );
#endif
} else {
half_free( ptr_1 );
}
return rslt;
}
bool test_max_alc_1_byte( void ) {
bool rslt = true;
uint32_t c = 0;
size_t max_sz;
half_init();
max_sz = find_max_block();
// Allocate 1 bytes until no half_aloc returns NULL
while ( half_alloc( 1 ) != NULL ) {
c++;
}
#ifdef DO_PRINT
printf( "Only %d 1-Byte block can be allocated within %d addressable Bytes.\n", c, max_sz );
#endif
if ( c == 0 || !(max_sz >= smlst_blk_sz * c || smlst_blk_sz * c >= lrgst_blk_sz) ) {
#ifdef DO_PRINT
printf( "32 * %d = %d is not equal to the maximum allocable block which is %d\n", c, c*32 , max_sz );
#endif
rslt = false;
}
return rslt;
}
bool test_max_alc_rand_byte( void ) {
return false;
}
int main( void ) {
SystemInit();
SystemCoreClockUpdate();
TimerInit();
TimerStart(); {
printf( "max_alc: %i\n", test_max_alc() );
printf( "alc_free_max: %i\n", test_alc_free_max() );
printf( "static_alc_free: %i\n", test_static_alc_free() );
printf( "static_alc_free_violation: %i\n", test_static_alc_free_violation() );
printf( "rndm_alc_free: %i\n", test_rndm_alc_free() );
printf( "max_alc_1_byte: %i\n", test_max_alc_1_byte() );
} TimerStop();
printf( "The elappsed time: %d ms\n", current_elapsed_time());
while( 1 ) {
// Infinite loop
}
}