home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_07_05 / v7n5024a.txt < prev    next >
Text File  |  1989-04-04  |  5KB  |  146 lines

  1. Listing 1:
  2.  
  3. /******************************************************************
  4. MEM.C
  5.  
  6. By: Leonard Zerman
  7. Date: 02/05/1989
  8. Version: 1.00
  9. Revision: None
  10. Notice: Placed into the public domain by Leonard Zerman.
  11.  
  12. ******************************************************************/
  13.  
  14. #include <stdlib.h>            /* malloc and free prototype*/
  15.  
  16. #define LARGEST_ALLOC  Oxffef  /* 64k - 1 paragraph for alignment */
  17. #define SMALLEST_ALLOC Oxffef  /* single paragraph - 1*/
  18.  
  19. /********************************************************************
  20.  
  21. Syntax: memleft ();
  22. Parameters: None
  23. Prototype: long memleft (void);
  24. Returns: Number of bytes free for allocation with malloc.
  25.          Zero if none.
  26. Operation: The memleft function keeps allocating the 
  27.            largest chuncks of memory it can and 
  28.            stores the pointer in vptrarray.
  29.            If an allocation request is too large, 
  30.            it is cut in half and tried again.
  31.            This continues until request are smaller
  32.            than 1 memory paragraph.
  33.  
  34. Source notes:
  35. *********************************************************************/
  36. long memleft (void)
  37.      {
  38.      int i:                        /* counter */
  39.      unsigned allocsize;           /* allocation block size */
  40.      long totalmem;                /* accumulates total */
  41.      void * vptr;                  /* temp pointer */
  42.      void * vptrarray [30];        /* holds all pointers */
  43.                                    /* make static if shy on stack */
  44.  
  45.      i = 0;                        /* initialize counter */
  46.      totalmem = OL;                /* initialize accumulator */
  47.      allocsize = LARGEST_ALLOC;    /* large allocation number */
  48.      
  49.      while  (allocsize  > SMALLEST_ALLOC) /* while bytes left  to 
  50.                                              allocate */
  51.         {
  52.         if ((vptr = (void *) malloc(allocsize)) ! = (void *) 0)
  53.            {                       /* if not NULL pointer */
  54.            vptrarray[i} = vptr;    /* store pointer */
  55.            totalmem += allocsize;  /* add to total */
  56.            i++;
  57.            }
  58.         else                       /* if malloc failed */
  59.            allocsize /= 2;         /* try 1/2 the allocation */
  60.         }
  61.      for ( ; i ; i--)              /* for all valid pointers */
  62.          {
  63.          free (vptrarray[i -1]);   /* free in reverse order */
  64.          }
  65.      return(totalmem);             /* return total */
  66.      }
  67.  
  68.  
  69. /*****************************************************************/
  70. Syntax: memlargest ();
  71. Parameters: None
  72. Prototype: unsigned int memlargest (void);
  73. Returns: Size in bytes of the largest block malloc can return.
  74. Operation: The memlargest function tries to allocate a large 
  75.            block.  When successful it returns the size of that 
  76.            block.  If it can't allocate that size it reduces it`s 
  77.            request until it can.
  78. Source notes: Memory fragmentation during runtime can make this 
  79.               function return smaller and smaller sizes.           
  80. Anomaly: In small memory model memlargest may return a value 
  81.          slightly larger than memleft.  In that case, memlargest 
  82.          is more accurate.
  83. ******************************************************************/
  84.  
  85. unsigned int memlargest (void)
  86.      {
  87.      unsigned allocsize;
  88.      void * vptr;
  89.      
  90.      allocsize = LARGEST_ALLOC;    /* start with largest block */
  91.  
  92.      while ((vptr = (void *) malloc(allocsize)) == (void *) 0 )
  93.        {                             /* while request is too large */
  94.        allocsize -= SMALLEST_ALLOC;  /* make request smaller */
  95.        if (allocsize < SMALLEST_ALLOC)
  96.           return (0);                /* not enough memory */
  97.        }
  98.      free (vptr);                   /* free pointer */
  99.      return (allocsize);            /* return size of largest block */ 
  100.      }
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.    enum e_print_mode {
  112.        PRINT_START_ENLARGED ,
  113.        PRINT_STOP_ENLARGED,
  114.        PRINT_START_EMPHASIZED, 
  115.        PRINT_STOP_EMPHASIZED};         /* Defines printer modes */
  116.  
  117.    void set_print_mode(printer, mode)
  118.    /* Set the printer mode */
  119.    FILE *printer;              /* Which file to output to */       
  120.    enum e_print_mode mode;     /* Which mode to output */
  121.        {
  122.        switch(mode)
  123.            {
  124.        case PRINT_START_ENLARGED:
  125.            fputc(14, printer);
  126.            break;
  127.        case PRINT_STOP_ENLARGED:
  128.            fputc(4, printer);
  129.            break;
  130.        case PRINT_START_EMPHASIZED:
  131.            fputc(27, printer);
  132.            fputc('E', printer);
  133.            break;
  134.        case PRINT_STOP_EMPHASIZED:
  135.            fputc(27, printer);
  136.            fputc('F', printer);
  137.            break;
  138.        default:
  139.            printf("\n BAD PRINT MODE ENCOUNTERED");
  140.            break;
  141.            }
  142.        return;
  143.        }   
  144.        
  145.        
  146.