Annotation of 41BSD/cmd/cifplot/alloc.c, revision 1.1.1.1

1.1       root        1: /*******************************************************************
                      2: *                                                                  *
                      3: *    File: CIFPLOT/alloc.c                                         *
                      4: *    Written by Dan Fitzpatrick                                    *
                      5: *    copyright 1980 -- Regents of the University of California     *
                      6: *                                                                  *
                      7: ********************************************************************/
                      8: 
                      9: #include <stdio.h>
                     10: #include "defs.h"
                     11: 
                     12: IMPORT int *malloc();
                     13: IMPORT int *realloc();
                     14: 
                     15: #define BLOCKSIZE 4            /* Memory is allocated in multiples of BLOCKSIZE */
                     16: 
                     17: /* ALLOC_ID, PALLOC_ID, & FREED_ID are used to mark memory as
                     18:  * allocated, permanently allocated, & freed, repectively */
                     19: #define ALLOC_ID  0xAA00FFFF
                     20: #define PALLOC_ID 0x55AAA0F0
                     21: #define FREED_ID  0xAFFA8118
                     22: 
                     23: /* size returns how much memory 'x' points to */
                     24: #define size(x)          ( *(x-1) - ((int) x) + 1 )
                     25: 
                     26: /* These are variables that are used to keep track of how much
                     27:  * memory has been allocated.          */
                     28: int AllocCount,FreeCount,AllocBytes;
                     29: int FreeBytes,PallocCount,PallocBytes,MaxBytes;
                     30: 
                     31: int *
                     32: alloc(nbytes)
                     33: int nbytes;
                     34: /* Returns a pointer to a block of memory of size 'nbytes' */
                     35: {
                     36:     int nunits;
                     37:     int *ptr;
                     38: 
                     39: #ifdef ADEBUG
                     40:     nbytes += sizeof(int);
                     41: #endif
                     42: 
                     43:     nunits = ((nbytes-1)/BLOCKSIZE) + 1;
                     44:     if (NULL == (ptr = malloc(nunits*BLOCKSIZE)))
                     45:        Error("Out of space!\n",INTERNAL);
                     46:     
                     47: #ifdef ADEBUG
                     48:     /* Set up an identifier to show this has been allocated */
                     49:     *ptr = ALLOC_ID;
                     50:     AllocCount++;
                     51:     AllocBytes += size(ptr);
                     52:     if( (AllocBytes-FreeBytes) > MaxBytes )
                     53:        MaxBytes = AllocBytes - FreeBytes;
                     54:     return(++ptr);
                     55: #else
                     56:     return(ptr);
                     57: #endif
                     58:     }
                     59: 
                     60: int *
                     61: palloc(n)
                     62: int n;
                     63: /* Allocate n bytes of permanent memory (this memory will not be freed) */
                     64: {
                     65:     int *p;
                     66:     p = alloc(n);
                     67: #ifdef ADEBUG
                     68:     PallocCount++;
                     69:     PallocBytes += n;
                     70:     *(p-1) = PALLOC_ID;
                     71: #endif
                     72:     return(p);
                     73:     }
                     74: 
                     75: Free(ptr)
                     76: int *ptr;
                     77: /* Free the block of memory pointed to by 'ptr'. This memory must
                     78:  * have been allocated by 'alloc'. */
                     79: {
                     80: 
                     81: #ifdef ADEBUG
                     82:     /* Check to see that the memory was allocated by 'alloc' */
                     83:     --ptr;
                     84:     if( *ptr != ALLOC_ID)
                     85:        if( *ptr == PALLOC_ID )
                     86:                Error("Tried to Free Permanent Storage",INTERNAL);
                     87:            else
                     88:                if( *ptr == FREED_ID )
                     89:                        Error("Tried to Free an already free block",INTERNAL);
                     90:                    else
                     91:                        Error("Tried to Free an Unallocated Block",INTERNAL);
                     92:     /* Mark this memory as free, if memory is freed again flag an error */
                     93:     *ptr = FREED_ID;
                     94:     FreeCount++;
                     95:     FreeBytes += size(ptr);
                     96: #endif
                     97:     free(ptr);
                     98:     return;
                     99:    }
                    100: 
                    101: int *
                    102: expand(ptr, n)
                    103: int *ptr;
                    104: int n;
                    105: /* 'expand' changes the size of an array pointed to by 'ptr' to be an
                    106:  * array of n bytes. This is useful to avoid table overflow. Instead
                    107:  * of aborting on overflow we can just make the table bigger.  */
                    108: {
                    109:     Free(ptr);
                    110: #ifdef ADEBUG
                    111:     n += sizeof(int);
                    112:     ptr--;
                    113:     if(*ptr != FREED_ID)
                    114:        Error("Block has not been freed",INTERNAL);
                    115: #endif
                    116:     ptr = realloc(ptr,n);
                    117:     if(ptr == NULL)
                    118:        Error("Out of memory",INTERNAL);
                    119: #ifdef ADEBUG
                    120:     if(*ptr != FREED_ID)
                    121:        Error("Block has not been copied correctly",INTERNAL);
                    122:     *ptr = ALLOC_ID;
                    123:     AllocCount++;
                    124:     AllocBytes += size(ptr);
                    125:     if( (AllocBytes-FreeBytes) > MaxBytes )
                    126:        MaxBytes = AllocBytes - FreeBytes;
                    127:     return(++ptr);
                    128: #else
                    129:     return(ptr);
                    130: #endif
                    131:     }
                    132: 
                    133: int acnt0,acnt1,acnt2,acnt3,acnt4,acnt5,acnt6,acnt7,acnt8,acnt9;
                    134: int bcnt0,bcnt1,bcnt2,bcnt3,bcnt4,bcnt5,bcnt6,bcnt7,bcnt8,bcnt9;
                    135: 
                    136: AllocSummary()
                    137: /* Prints out statistics of memory allocated */
                    138: {
                    139:    fprintf(stderr,"%6d blocks, %8d bytes allocated\n",AllocCount,AllocBytes);
                    140:    fprintf(stderr,"%6d blocks, %8d bytes permanent requests\n",PallocCount,PallocBytes);
                    141:    fprintf(stderr,"%6d blocks, %8d bytes freed\n",FreeCount,FreeBytes);
                    142:    fprintf(stderr,"%6d blocks, %8d bytes unretrieved\n",
                    143:                        AllocCount-FreeCount,AllocBytes-FreeBytes);
                    144:    fprintf(stderr,"%8d bytes were required\n",MaxBytes);
                    145:    fprintf(stderr,"\nPlot Time allocation\n");
                    146:    fprintf(stderr,"%d,%d,%d,%d,%d,%d,%d,%d,%d,%d\n",
                    147:        acnt0,acnt1,acnt2,acnt3,acnt4,acnt5,acnt6,acnt7,acnt8,acnt9);
                    148:    fprintf(stderr,"%d,%d,%d,%d,%d,%d,%d,%d,%d,%d\n",
                    149:        bcnt0,bcnt1,bcnt2,bcnt3,bcnt4,bcnt5,bcnt6,bcnt7,bcnt8,bcnt9);
                    150:    }

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.