lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame] | 1 | #include <malloc.h> |
| 2 | #include <stdio.h> |
| 3 | #include <stdlib.h> |
| 4 | #include <string.h> |
| 5 | |
| 6 | #define N 10000 |
| 7 | |
| 8 | static void *arr[N]; |
| 9 | |
| 10 | static int |
| 11 | do_test (void) |
| 12 | { |
| 13 | for (int i = 0; i < N; ++i) |
| 14 | { |
| 15 | size_t size = random () % 16384; |
| 16 | |
| 17 | if ((arr[i] = malloc (size)) == NULL) |
| 18 | { |
| 19 | nomem: |
| 20 | puts ("not enough memory"); |
| 21 | return 0; |
| 22 | } |
| 23 | |
| 24 | memset (arr[i], size, size); |
| 25 | } |
| 26 | |
| 27 | void *p = malloc (256); |
| 28 | if (p == NULL) |
| 29 | goto nomem; |
| 30 | memset (p, 1, 256); |
| 31 | |
| 32 | puts ("=================================================================="); |
| 33 | |
| 34 | for (int i = 0; i < N; ++i) |
| 35 | if (i % 13 != 0) |
| 36 | free (arr[i]); |
| 37 | |
| 38 | puts ("=================================================================="); |
| 39 | |
| 40 | malloc_trim (0); |
| 41 | |
| 42 | puts ("=================================================================="); |
| 43 | |
| 44 | p = malloc (30000); |
| 45 | if (p == NULL) |
| 46 | goto nomem; |
| 47 | |
| 48 | memset (p, 2, 30000); |
| 49 | |
| 50 | malloc_trim (0); |
| 51 | |
| 52 | return 0; |
| 53 | } |
| 54 | |
| 55 | #define TEST_FUNCTION do_test () |
| 56 | #include "../test-skeleton.c" |