home *** CD-ROM | disk | FTP | other *** search
- /* XALLOC.C */
- static char sccsid[] = "@(#)xalloc.c 1.12 93/06/07 Copyright (c)1993 thalerd";
- #ifndef NeXT
- #ifndef bsdi
- #include <malloc.h>
- #endif
- #endif
- /* to get flags var for debugging only... */
- #include "config.h"
- #include "struct.h"
- #include "xalloc.h"
- #include "globals.h"
- #define NUMXST 3
- static long xst[NUMXST][2]={ 0 };
- #define XS_ACALLS 0
- #define XS_RCALLS 1
- #define XS_FCALLS 2
-
- /* Information about each array */
- typedef struct mem_tt {
- char **ptr;
- struct mem_tt *next;
- short num; /* # of elements */
- short eltsize; /* size of an element */
- } mem_t;
- static mem_t *first_arr=0,*first_mem=0;
-
- /******************************************************************************/
- /* DUMP ALL ARRAYS CURRENTLY ALLOCATED */
- /******************************************************************************/
- void /* RETURNS: (nothing) */
- xdump() /* ARGUMENTS: (none) */
- {
- mem_t *this;
- short i;
-
- printf("ARRAYS:\n");
- for (this=first_arr; this; this = this->next) {
- printf("xdump: ptr %x size %hd*%hd\n",this->ptr,this->num,this->eltsize);
- for (i=0; i<this->num; i++)
- if (this->ptr[i]) printf("%4hd: %s\n",i,this->ptr[i]);
- }
- printf("\nOTHER:\n");
- for (this=first_mem; this; this = this->next)
- printf("xdump: ptr %x size %hd*%hd str !%s!\n",this->ptr,this->num,
- this->eltsize,this->ptr);
- }
-
- /******************************************************************************/
- /* GET # OF ELEMENTS IN AN ARRAY */
- /******************************************************************************/
- short /* RETURNS: size of array */
- xsizeof(arr) /* ARGUMENTS: */
- char **arr; /* Array */
- {
- mem_t *this;
-
- if (!arr) return 0;
- for (this=first_arr; this && this->ptr != arr; this = this->next);
- if (!this)
- for (this=first_mem; this && this->ptr != arr; this = this->next);
- if (!this) {
- (void)wputs("Tried to get sizeof unxalloced ptr\n");
- return 0; /* not found */
- }
- return this->num;
- }
-
- /******************************************************************************/
- /* ALLOCATE A NEW ARRAY */
- /******************************************************************************/
- char ** /* RETURNS: New array */
- xalloc(num,eltsize) /* ARGUMENTS: */
- short num; /* Size of array to get */
- short eltsize; /* Size of each element */
- {
- mem_t *this;
- char **mem;
-
- mem=(char **)calloc((unsigned)num?num:1,(unsigned)eltsize);
- /*printf("xalloc: %d %d > %x\n",num,eltsize,mem);*/
- this = (mem_t *)malloc(sizeof(mem_t));
- this->ptr = mem;
- this->num = num;
- this->eltsize = eltsize;
- if (num) {
- this->next = first_arr;
- first_arr = this;
- } else {
- this->next = first_mem;
- first_mem = this;
- }
-
- if (flags & O_DEBUG) printf("xalloc: got %x size %hd*%hd\n",
- mem,num,eltsize);
- xst[XS_ACALLS][!num]++;
- return mem;
- }
-
- /******************************************************************************/
- /* CHANGE SIZE OF AN ARRAY */
- /******************************************************************************/
- char ** /* RETURNS: new array */
- xrealloc(arr,num) /* ARGUMENTS: */
- char **arr; /* Old array */
- short num; /* New size */
- {
- mem_t *this;
- char **mem;
- short i;
-
- for (this=first_arr; this->ptr != arr; this = this->next);
- if (!this) {
- (void)wputs("Tried to xrealloc unxalloced ptr\n");
- return 0; /* not found */
- }
- for (i=num; i<this->num; i++)
- if (arr[i])
- xfree((char *)arr[i]);
- if (flags & O_DEBUG) printf("xrealloc: from %x size %hd*%hd\n",
- arr,this->num,this->eltsize);
- mem=(char **)realloc((char *)arr,(unsigned)num * this->eltsize);
- this->num = num;
- this->ptr = mem;
- if (flags & O_DEBUG) printf("xrealloc: to %x size %hd*%hd\n",
- mem,num,this->eltsize);
- xst[XS_RCALLS][!num]++;
- return mem;
- }
-
- /******************************************************************************/
- /* VERIFY THAT ALL ARRAYS HAVE BEEN FREED */
- /******************************************************************************/
- void
- xcheck()
- {
- if (!first_arr && !first_mem) {
- if (flags & O_DEBUG)
- puts("xcheck: Everything freed.\n");
- } else {
- printf("xcheck: Error, failed to xfree the following:\n");
- xdump();
- }
- /*
- if (first_arr)
- printf("xcheck: Error, failed to xfree array %x!\n",first_arr->ptr);
- else if (first_mem)
- printf("xcheck: Error, failed to xfree other %x!\n",first_mem->ptr);
- else if (flags & O_DEBUG)
- puts("xcheck: Everything freed.\n");
- */
- }
-
- /******************************************************************************/
- /* FREE AN ARRAY */
- /******************************************************************************/
- void /* RETURNS: (nothing) */
- xfree(arr) /* ARGUMENTS: */
- void *arr; /* Array to free */
- {
- mem_t *this,*prev=0;
- short i,typ=0;
-
- if (!arr) return;
- for (this=first_arr; this && this->ptr != arr; prev=this, this = this->next);
- if (!this) {
- prev=0;
- for (this=first_mem; this && this->ptr != arr; prev=this, this = this->next);
- typ++;
- }
- if (!this) {
- (void)wputs("Tried to free unxalloced ptr\n");
- return; /* not found */
- }
- /*printf("xfree: %d %d > %x\n",this->num,this->eltsize,arr);*/
- if (flags & O_DEBUG) printf("xfree: free %x size %hd*%hd\n",
- arr,this->num,this->eltsize);
- if ((flags & O_DEBUG) && typ)
- printf("Str:!%s!\n",(char*)arr);
-
- for (i=0; i<this->num; i++) {
- if (((char**)arr)[i]) xfree(((char**)arr)[i]);
- }
- free((char *)arr);
- if (!prev) {
- if (!typ) {
- first_arr = this->next;
- } else {
- first_mem = this->next;
- }
- } else prev->next = this->next;
- xst[XS_FCALLS][!this->num]++;
- free((char *)this);
- }
-
- /******************************************************************************/
- /* STANDARD strdup() CODE FOR THOSE OS'S WHICH DON'T HAVE IT */
- /******************************************************************************/
- char * /* RETURNS: New string */
- xstrdup(str) /* ARGUMENTS: */
- char *str; /* String to duplicate */
- {
- char *ptr;
- ptr = (char*)xalloc(0,strlen(str)+1);
- strcpy(ptr,str);
- return ptr;
- }
-
- /******************************************************************************/
- /* DUMP ALL ARRAYS CURRENTLY ALLOCATED */
- /******************************************************************************/
- void /* RETURNS: (nothing) */
- xstat() /* ARGUMENTS: (none) */
- {
- mem_t *this;
- short i;
- long n,s,b,nt=0,st=0,bt=0;
-
- printf("Category: Number Size Bytes Acalls Rcalls Fcalls\n");
- n=s=b=0;
- for (this=first_arr; this; this = this->next) {
- n++;
- s += this->num;
- b += (this->num * this->eltsize);
- }
- printf("Arrays : %6d %6d %6d",n,s,b);
- for (i=0; i<NUMXST; i++)
- printf(" %6d",xst[i][0]);
- nt+=n; st+=s; bt+=b;
-
- n=s=b=0;
- for (this=first_mem; this; this = this->next) {
- n++;
- s++;
- b+= this->eltsize;
- }
- printf("\nStrings : %6d %6d %6d",n,s,b);
- for (i=0; i<NUMXST; i++)
- printf(" %6d",xst[i][1]);
- nt+=n; st+=s; bt+=b;
-
- printf("\nTotals : %6d %6d %6d",nt,st,bt);
- for (i=0; i<NUMXST; i++)
- printf(" %6d",xst[i][0]+xst[i][1]);
- printf("\n");
- }
-