@@ -124,6 +124,17 @@ static struct mallinfo g_alloc_info;
124
124
* Private Functions
125
125
****************************************************************************/
126
126
127
+ static bool is_oversize (int size )
128
+ {
129
+ if (size < 0 )
130
+ {
131
+ return false;
132
+ }
133
+
134
+ unsigned long threshold = g_alloc_info .mxordblk * 3 / 4 ;
135
+ return size > threshold ;
136
+ }
137
+
127
138
static void mm_showmallinfo (void )
128
139
{
129
140
g_alloc_info = mallinfo ();
@@ -149,17 +160,22 @@ static void do_mallocs(FAR void **mem, FAR const int *size,
149
160
for (i = 0 ; i < n ; i ++ )
150
161
{
151
162
j = seq [i ];
163
+ int allocsize = MM_ALIGN_UP (size [j ] + MM_SIZEOF_ALLOCNODE );
152
164
if (!mem [j ])
153
165
{
154
- printf ("(%d)Allocating %d bytes\n" , i , size [j ]);
166
+ printf ("(%d)Allocating %d bytes\n" , i , allocsize );
167
+ if (is_oversize (allocsize ))
168
+ {
169
+ printf ("(%d)The allocated memory exceeds the threshold, "
170
+ "skipping\n" , i );
171
+ continue ;
172
+ }
155
173
156
174
mem [j ] = malloc (size [j ]);
157
175
printf ("(%d)Memory allocated at %p\n" , i , mem [j ]);
158
176
159
177
if (mem [j ] == NULL )
160
178
{
161
- int allocsize = MM_ALIGN_UP (size [j ] + MM_SIZEOF_ALLOCNODE );
162
-
163
179
fprintf (stderr , "(%d)malloc failed for allocsize=%d\n" ,
164
180
i , allocsize );
165
181
@@ -196,6 +212,15 @@ static void do_reallocs(FAR void **mem, FAR const int *oldsize,
196
212
for (i = 0 ; i < n ; i ++ )
197
213
{
198
214
j = seq [i ];
215
+ int allocsize = MM_ALIGN_UP (newsize [j ] + MM_SIZEOF_ALLOCNODE ) -
216
+ MM_ALIGN_UP (oldsize [j ] + MM_SIZEOF_ALLOCNODE );
217
+ if (is_oversize (allocsize ))
218
+ {
219
+ printf ("(%d)The reallocs memory exceeds the threshold, "
220
+ "skipping\n" , i );
221
+ continue ;
222
+ }
223
+
199
224
printf ("(%d)Re-allocating at %p from %d to %d bytes\n" ,
200
225
i , mem [j ], oldsize [j ], newsize [j ]);
201
226
@@ -213,8 +238,6 @@ static void do_reallocs(FAR void **mem, FAR const int *oldsize,
213
238
214
239
if (ptr == NULL )
215
240
{
216
- int allocsize = MM_ALIGN_UP (newsize [j ] + MM_SIZEOF_ALLOCNODE );
217
-
218
241
fprintf (stderr ,
219
242
"(%d)realloc failed for allocsize=%d\n" , i , allocsize );
220
243
if (allocsize > g_alloc_info .mxordblk )
@@ -249,17 +272,22 @@ static void do_memaligns(FAR void **mem,
249
272
for (i = 0 ; i < n ; i ++ )
250
273
{
251
274
j = seq [i ];
275
+ int allocsize = MM_ALIGN_UP (size [j ] + MM_SIZEOF_ALLOCNODE ) +
276
+ 2 * align [i ];
252
277
printf ("(%d)Allocating %d bytes aligned to 0x%08x\n" ,
253
278
i , size [j ], align [i ]);
279
+ if (is_oversize (allocsize ))
280
+ {
281
+ printf ("(%d)The reallocs memory exceeds the threshold, "
282
+ "skipping\n" , i );
283
+ continue ;
284
+ }
254
285
255
286
mem [j ] = memalign (align [i ], size [j ]);
256
287
printf ("(%d)Memory allocated at %p\n" , i , mem [j ]);
257
288
258
289
if (mem [j ] == NULL )
259
290
{
260
- int allocsize = MM_ALIGN_UP (size [j ] + MM_SIZEOF_ALLOCNODE ) +
261
- 2 * align [i ];
262
-
263
291
fprintf (stderr ,
264
292
"(%d)memalign failed for allocsize=%d\n" , i , allocsize );
265
293
if (allocsize > g_alloc_info .mxordblk )
@@ -311,36 +339,11 @@ static void do_frees(FAR void **mem, FAR const int *size,
311
339
}
312
340
}
313
341
314
- static int mm_stress_test (int argc , FAR char * argv [] )
342
+ static int mm_stress_test (int delay , int prio , int maxsize )
315
343
{
316
344
FAR unsigned char * tmp ;
317
- int delay = 1 ;
318
- int prio = 0 ;
319
345
int size ;
320
346
int i ;
321
- int maxsize = 1024 ;
322
-
323
- while ((i = getopt (argc , argv , "d:p:s:" )) != ERROR )
324
- {
325
- if (i == 'd' )
326
- {
327
- delay = atoi (optarg );
328
- }
329
- else if (i == 'p' )
330
- {
331
- prio = atoi (optarg );
332
- }
333
- else if (i == 's' )
334
- {
335
- maxsize = atoi (optarg );
336
- }
337
- else
338
- {
339
- printf ("Unrecognized option: '%c'\n" , i );
340
- return - EINVAL ;
341
- }
342
- }
343
-
344
347
if (prio != 0 )
345
348
{
346
349
struct sched_param param ;
@@ -380,9 +383,51 @@ static int mm_stress_test(int argc, FAR char *argv[])
380
383
381
384
int main (int argc , FAR char * argv [])
382
385
{
383
- if (argc > 1 )
386
+ int stress_test_mode = 0 ;
387
+ int delay = 1 ;
388
+ int prio = 0 ;
389
+ int i ;
390
+ int maxsize = 1024 ;
391
+
392
+ while ((i = getopt (argc , argv , "mfd:p:s:" )) != ERROR )
393
+ {
394
+ switch (i )
395
+ {
396
+ case 'm' :
397
+ {
398
+ stress_test_mode = 1 ;
399
+ break ;
400
+ }
401
+
402
+ case 'd' :
403
+ {
404
+ delay = atoi (optarg );
405
+ break ;
406
+ }
407
+
408
+ case 'p' :
409
+ {
410
+ prio = atoi (optarg );
411
+ break ;
412
+ }
413
+
414
+ case 's' :
415
+ {
416
+ maxsize = atoi (optarg );
417
+ break ;
418
+ }
419
+
420
+ default :
421
+ {
422
+ printf ("Unrecognized option: '%c'\n" , i );
423
+ return - EINVAL ;
424
+ }
425
+ }
426
+ }
427
+
428
+ if (stress_test_mode )
384
429
{
385
- return mm_stress_test (argc , argv );
430
+ return mm_stress_test (delay , prio , maxsize );
386
431
}
387
432
388
433
mm_showmallinfo ();
0 commit comments