home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / unix / volume28 / yapp / part01 / xalloc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-05-29  |  7.6 KB  |  247 lines

  1. /* XALLOC.C */
  2. static    char sccsid[] = "@(#)xalloc.c 1.12 93/06/07 Copyright (c)1993 thalerd";
  3. #ifndef NeXT
  4. #ifndef bsdi
  5. #include <malloc.h>
  6. #endif
  7. #endif
  8. /* to get flags var for debugging only... */
  9. #include "config.h"
  10. #include "struct.h"
  11. #include "xalloc.h"
  12. #include "globals.h"
  13. #define NUMXST 3
  14. static long xst[NUMXST][2]={ 0 };
  15. #define XS_ACALLS 0
  16. #define XS_RCALLS 1
  17. #define XS_FCALLS 2
  18.  
  19. /* Information about each array */
  20. typedef struct mem_tt {
  21.    char          **ptr;
  22.    struct mem_tt *next;
  23.    short          num; /* # of elements */
  24.    short          eltsize; /* size of an element */
  25. } mem_t;
  26. static mem_t *first_arr=0,*first_mem=0;
  27.  
  28. /******************************************************************************/
  29. /* DUMP ALL ARRAYS CURRENTLY ALLOCATED                                        */
  30. /******************************************************************************/
  31. void    /* RETURNS: (nothing) */
  32. xdump() /* ARGUMENTS: (none)  */
  33. {
  34.    mem_t *this;
  35.    short i;
  36.  
  37.    printf("ARRAYS:\n");
  38.    for (this=first_arr; this; this = this->next) {
  39.       printf("xdump: ptr %x size %hd*%hd\n",this->ptr,this->num,this->eltsize); 
  40.       for (i=0; i<this->num; i++) 
  41.          if (this->ptr[i]) printf("%4hd: %s\n",i,this->ptr[i]);
  42.    }
  43.    printf("\nOTHER:\n");
  44.    for (this=first_mem; this; this = this->next) 
  45.       printf("xdump: ptr %x size %hd*%hd str !%s!\n",this->ptr,this->num,
  46.        this->eltsize,this->ptr); 
  47. }
  48.  
  49. /******************************************************************************/
  50. /* GET # OF ELEMENTS IN AN ARRAY                                              */
  51. /******************************************************************************/
  52. short        /* RETURNS: size of array */
  53. xsizeof(arr) /* ARGUMENTS: */
  54. char **arr;  /*    Array */
  55. {
  56.    mem_t *this;
  57.  
  58.    if (!arr) return 0;
  59.    for (this=first_arr; this && this->ptr != arr; this = this->next);
  60.    if (!this)
  61.       for (this=first_mem; this && this->ptr != arr; this = this->next);
  62.    if (!this) {
  63.       (void)wputs("Tried to get sizeof unxalloced ptr\n");
  64.       return 0; /* not found */
  65.    }
  66.    return this->num;
  67. }
  68.  
  69. /******************************************************************************/
  70. /* ALLOCATE A NEW    ARRAY                                                    */
  71. /******************************************************************************/
  72. char **             /* RETURNS: New array */
  73. xalloc(num,eltsize) /* ARGUMENTS: */
  74. short num;          /*    Size of array to get */
  75. short eltsize;      /*    Size of each element */
  76. {
  77.    mem_t *this;
  78.    char **mem;
  79.  
  80.    mem=(char **)calloc((unsigned)num?num:1,(unsigned)eltsize);
  81. /*printf("xalloc: %d %d > %x\n",num,eltsize,mem);*/
  82.    this          = (mem_t *)malloc(sizeof(mem_t));
  83.    this->ptr     = mem;
  84.    this->num     = num;
  85.    this->eltsize = eltsize;
  86.    if (num) {
  87.       this->next    = first_arr;
  88.       first_arr     = this;
  89.    } else {
  90.       this->next    = first_mem;
  91.       first_mem     = this;
  92.    }
  93.  
  94.    if (flags & O_DEBUG) printf("xalloc: got %x size %hd*%hd\n",
  95.       mem,num,eltsize);
  96.    xst[XS_ACALLS][!num]++;
  97.    return mem;
  98. }
  99.  
  100. /******************************************************************************/
  101. /* CHANGE SIZE OF AN ARRAY                                                    */
  102. /******************************************************************************/
  103. char **           /* RETURNS: new array */
  104. xrealloc(arr,num) /* ARGUMENTS: */
  105. char **arr;       /*    Old array */
  106. short num;        /*    New size */
  107. {
  108.    mem_t *this;
  109.    char **mem;
  110.    short i;
  111.  
  112.    for (this=first_arr; this->ptr != arr; this = this->next);
  113.    if (!this) {
  114.       (void)wputs("Tried to xrealloc unxalloced ptr\n");
  115.       return 0; /* not found */
  116.    }
  117.    for (i=num; i<this->num; i++)
  118.       if (arr[i]) 
  119.          xfree((char *)arr[i]);
  120.    if (flags & O_DEBUG) printf("xrealloc: from %x size %hd*%hd\n",
  121.       arr,this->num,this->eltsize);
  122.    mem=(char **)realloc((char *)arr,(unsigned)num * this->eltsize);
  123.    this->num = num;
  124.    this->ptr = mem;
  125.    if (flags & O_DEBUG) printf("xrealloc:   to %x size %hd*%hd\n",
  126.       mem,num,this->eltsize);
  127.    xst[XS_RCALLS][!num]++;
  128.    return mem;
  129. }
  130.  
  131. /******************************************************************************/
  132. /* VERIFY THAT ALL ARRAYS HAVE BEEN FREED                                     */
  133. /******************************************************************************/
  134. void
  135. xcheck()
  136. {
  137.    if (!first_arr && !first_mem) {
  138.       if (flags & O_DEBUG)
  139.          puts("xcheck: Everything freed.\n");
  140.    } else {
  141.       printf("xcheck: Error, failed to xfree the following:\n");
  142.       xdump();
  143.    }
  144. /*
  145.    if (first_arr)
  146.       printf("xcheck: Error, failed to xfree array %x!\n",first_arr->ptr);
  147.    else if (first_mem)
  148.       printf("xcheck: Error, failed to xfree other %x!\n",first_mem->ptr);
  149.    else if (flags & O_DEBUG)
  150.       puts("xcheck: Everything freed.\n");
  151. */
  152. }
  153.  
  154. /******************************************************************************/
  155. /* FREE AN ARRAY                                                              */
  156. /******************************************************************************/
  157. void        /* RETURNS: (nothing) */
  158. xfree(arr)  /* ARGUMENTS:         */
  159. void *arr; /*    Array to free   */
  160. {
  161.    mem_t *this,*prev=0;
  162.    short i,typ=0;
  163.  
  164.    if (!arr) return;
  165.    for (this=first_arr; this && this->ptr != arr; prev=this, this = this->next);
  166.    if (!this) {
  167.       prev=0;
  168.       for (this=first_mem; this && this->ptr != arr; prev=this, this = this->next);
  169.       typ++;
  170.    }
  171.    if (!this) {
  172.       (void)wputs("Tried to free unxalloced ptr\n");
  173.       return; /* not found */
  174.    }
  175. /*printf("xfree: %d %d > %x\n",this->num,this->eltsize,arr);*/
  176.    if (flags & O_DEBUG) printf("xfree: free %x size %hd*%hd\n",
  177.       arr,this->num,this->eltsize); 
  178.    if ((flags & O_DEBUG) && typ)
  179.       printf("Str:!%s!\n",(char*)arr);
  180.  
  181.    for (i=0; i<this->num; i++) {
  182.       if (((char**)arr)[i]) xfree(((char**)arr)[i]);
  183.    }
  184.    free((char *)arr);
  185.    if (!prev) {
  186.       if (!typ) {
  187.          first_arr  = this->next;
  188.       } else {
  189.          first_mem  = this->next;
  190.       }
  191.    } else       prev->next = this->next;
  192.    xst[XS_FCALLS][!this->num]++;
  193.    free((char *)this);
  194. }
  195.  
  196. /******************************************************************************/
  197. /* STANDARD strdup() CODE FOR THOSE OS'S WHICH DON'T HAVE IT                  */
  198. /******************************************************************************/
  199. char *      /* RETURNS: New string    */
  200. xstrdup(str) /* ARGUMENTS:             */
  201. char *str;  /*    String to duplicate */
  202. {
  203.    char *ptr;
  204.    ptr = (char*)xalloc(0,strlen(str)+1);
  205.    strcpy(ptr,str);
  206.    return ptr;
  207. }
  208.  
  209. /******************************************************************************/
  210. /* DUMP ALL ARRAYS CURRENTLY ALLOCATED                                        */
  211. /******************************************************************************/
  212. void    /* RETURNS: (nothing) */
  213. xstat() /* ARGUMENTS: (none)  */
  214. {
  215.    mem_t *this;
  216.    short i;
  217.    long n,s,b,nt=0,st=0,bt=0;
  218.  
  219.    printf("Category: Number   Size  Bytes Acalls Rcalls Fcalls\n");
  220.    n=s=b=0;
  221.    for (this=first_arr; this; this = this->next) {
  222.       n++;
  223.       s += this->num;
  224.       b += (this->num * this->eltsize);
  225.    }
  226.    printf("Arrays  : %6d %6d %6d",n,s,b);
  227.    for (i=0; i<NUMXST; i++)
  228.       printf(" %6d",xst[i][0]);
  229.    nt+=n; st+=s; bt+=b;
  230.  
  231.    n=s=b=0;
  232.    for (this=first_mem; this; this = this->next) {
  233.       n++;
  234.       s++;
  235.       b+= this->eltsize;
  236.    }
  237.    printf("\nStrings : %6d %6d %6d",n,s,b);
  238.    for (i=0; i<NUMXST; i++)
  239.       printf(" %6d",xst[i][1]);
  240.    nt+=n; st+=s; bt+=b;
  241.  
  242.    printf("\nTotals  : %6d %6d %6d",nt,st,bt);
  243.    for (i=0; i<NUMXST; i++)
  244.       printf(" %6d",xst[i][0]+xst[i][1]);
  245.    printf("\n");
  246. }
  247.