1 /********************************************************************
3 * THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. *
4 * USE, DISTRIBUTION AND REPRODUCTION OF THIS LIBRARY SOURCE IS *
5 * GOVERNED BY A BSD-STYLE SOURCE LICENSE INCLUDED WITH THIS SOURCE *
6 * IN 'COPYING'. PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
8 * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2002 *
9 * by the XIPHOPHORUS Company http://www.xiph.org/ *
11 ********************************************************************/
18 #include "vorbis/codec.h"
23 static pthread_mutex_t memlock=PTHREAD_MUTEX_INITIALIZER;
24 static void **pointers=NULL;
25 static long *insertlist=NULL; /* We can't embed this in the pointer list;
26 a pointer can have any value... */
28 static char **files=NULL;
29 static long *file_bytes=NULL;
30 static int filecount=0;
33 static int palloced=0;
46 static void *_insert(void *ptr,long bytes,char *file,long line){
47 ((head *)ptr)->file=file;
48 ((head *)ptr)->line=line;
49 ((head *)ptr)->ptr=pinsert;
50 ((head *)ptr)->bytes=bytes-HEAD_ALIGN;
52 pthread_mutex_lock(&memlock);
53 if(pinsert>=palloced){
56 pointers=(void **)realloc(pointers,sizeof(void **)*palloced);
57 insertlist=(long *)realloc(insertlist,sizeof(long *)*palloced);
59 pointers=(void **)malloc(sizeof(void **)*palloced);
60 insertlist=(long *)malloc(sizeof(long *)*palloced);
64 pointers[pinsert]=ptr;
69 pinsert=insertlist[pinsert];
71 #ifdef _VDBG_GRAPHFILE
75 static struct timezone tz;
78 gettimeofday(&tv,&tz);
80 for(i=0;i<filecount;i++)
81 if(!strcmp(file,files[i]))break;
86 files=malloc(filecount*sizeof(*files));
87 file_bytes=malloc(filecount*sizeof(*file_bytes));
89 files=realloc(files,filecount*sizeof(*files));
90 file_bytes=realloc(file_bytes,filecount*sizeof(*file_bytes));
92 files[i]=strdup(file);
96 file_bytes[i]+=bytes-HEAD_ALIGN;
98 if(start_time==-1)start_time=(tv.tv_sec*1000)+(tv.tv_usec/1000);
100 snprintf(buffer,80,"%s%s",file,_VDBG_GRAPHFILE);
101 out=fopen(buffer,"a");
102 fprintf(out,"%ld, %ld\n",-start_time+(tv.tv_sec*1000)+(tv.tv_usec/1000),
103 file_bytes[i]-(bytes-HEAD_ALIGN));
104 fprintf(out,"%ld, %ld # FILE %s LINE %ld\n",
105 -start_time+(tv.tv_sec*1000)+(tv.tv_usec/1000),
106 file_bytes[i],file,line);
109 out=fopen(_VDBG_GRAPHFILE,"a");
110 fprintf(out,"%ld, %ld\n",-start_time+(tv.tv_sec*1000)+(tv.tv_usec/1000),
112 fprintf(out,"%ld, %ld\n",-start_time+(tv.tv_sec*1000)+(tv.tv_usec/1000),
113 global_bytes+(bytes-HEAD_ALIGN));
118 global_bytes+=(bytes-HEAD_ALIGN);
120 pthread_mutex_unlock(&memlock);
121 return(ptr+HEAD_ALIGN);
124 static void _ripremove(void *ptr){
126 pthread_mutex_lock(&memlock);
128 #ifdef _VDBG_GRAPHFILE
130 FILE *out=fopen(_VDBG_GRAPHFILE,"a");
132 static struct timezone tz;
134 char *file =((head *)ptr)->file;
135 long bytes =((head *)ptr)->bytes;
138 gettimeofday(&tv,&tz);
139 fprintf(out,"%ld, %ld\n",-start_time+(tv.tv_sec*1000)+(tv.tv_usec/1000),
141 fprintf(out,"%ld, %ld\n",-start_time+(tv.tv_sec*1000)+(tv.tv_usec/1000),
142 global_bytes-((head *)ptr)->bytes);
145 for(i=0;i<filecount;i++)
146 if(!strcmp(file,files[i]))break;
148 snprintf(buffer,80,"%s%s",file,_VDBG_GRAPHFILE);
149 out=fopen(buffer,"a");
150 fprintf(out,"%ld, %ld\n",-start_time+(tv.tv_sec*1000)+(tv.tv_usec/1000),
152 fprintf(out,"%ld, %ld\n",-start_time+(tv.tv_sec*1000)+(tv.tv_usec/1000),
153 file_bytes[i]-bytes);
156 file_bytes[i]-=bytes;
161 global_bytes-=((head *)ptr)->bytes;
163 insert=((head *)ptr)->ptr;
164 insertlist[insert]=pinsert;
167 if(pointers[insert]==NULL){
168 fprintf(stderr,"DEBUGGING MALLOC ERROR: freeing previously freed memory\n");
169 fprintf(stderr,"\t%s %ld\n",((head *)ptr)->file,((head *)ptr)->line);
173 fprintf(stderr,"DEBUGGING MALLOC ERROR: freeing unmalloced memory\n");
176 pointers[insert]=NULL;
177 pthread_mutex_unlock(&memlock);
180 void _VDBG_dump(void){
182 pthread_mutex_lock(&memlock);
184 head *ptr=pointers[i];
186 fprintf(stderr,"unfreed bytes from %s:%ld\n",
187 ptr->file,ptr->line);
190 pthread_mutex_unlock(&memlock);
193 extern void *_VDBG_malloc(void *ptr,long bytes,char *file,long line){
198 ptr=realloc(ptr,bytes);
203 return _insert(ptr,bytes,file,line);
206 extern void _VDBG_free(void *ptr,char *file,long line){