rfmonk

free_and_sbrk.c

Jan 3rd, 2014
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.38 KB | None | 0 0
  1. #include "tlpi_hdr.h"
  2. #define MAX_ALLOCS 1000000
  3.  
  4. int
  5. main(int argc, char *argv[])
  6. {
  7.     char *ptr[MAX_ALLOCS];
  8.     int freeStep, freeMin, freeMax, blockSize, numAllocs, j;
  9.  
  10.     printf("\n");
  11.  
  12.     if (argc < 3 || strcmp(argv[1], "--help") ==0)
  13.         usageErr("%s num-alcholic block-size [step [min [max]]]\n", argv[0]);
  14.  
  15.     numAllocs = getInt(argv[1], GN_GT_0, "num-allocs");
  16.     if (numAllocs > MAX_ALLOCS)
  17.         cmdLineErr("num-allocs > %d\n", MAX_ALLOCS);
  18.  
  19.     blockSize = getInt(argv[2], GN_GT_0 | GN_ANY_BASE, "block-size");
  20.  
  21.     freeStep = (argc > 3) ? getInt(argv[3], GN_GT_0, "step") : 1;
  22.     freeMin  = (argc > 4) ? getInt(argv[4], GN_GT_0, "min") : 1;
  23.     freeMax  = (argc > 5) ? getInt(argv[5], GN_GT_0, "max") : numAllocs;
  24.  
  25.     if (freeMax > numAllocs)
  26.         cmdLineErr("free-max > num-allocs\n");
  27.  
  28.     printf("Initial program break:      %10p\n", sbrk(0));
  29.  
  30.     printf("Allocating %d*%d bytes\n", numAllocs, blockSize);
  31.     for (j = 0; j < numAllocs; j++) {
  32.         ptr[j] = malloc(blockSize);
  33.  
  34.         if (ptr[j] == NULL)
  35.             errExit("malloc");
  36.  
  37.     }
  38.  
  39.     printf("Program break is now:       %10p\n", sbrk(0));
  40.  
  41.     printf("Freeing blocks from %d to %d in steps of %d\n",
  42.             freeMin, freeMax, freeStep);
  43.     for (j = freeMin - 1; j < freeMax; j += freeStep)
  44.         free(ptr[j]);
  45.  
  46.     printf("After free(), program break is: %10p\n", sbrk(0));
  47.  
  48.     exit(EXIT_SUCCESS);
  49. }
Add Comment
Please, Sign In to add comment