xref: /aosp_15_r20/external/tremolo/Tremolo/misc.c (revision bda690e46497e1f65c5077173b9c548e6e0cd5a1)
1*bda690e4SXin Li /************************************************************************
2*bda690e4SXin Li  * Copyright (C) 2002-2009, Xiph.org Foundation
3*bda690e4SXin Li  * Copyright (C) 2010, Robin Watts for Pinknoise Productions Ltd
4*bda690e4SXin Li  * All rights reserved.
5*bda690e4SXin Li  *
6*bda690e4SXin Li  * Redistribution and use in source and binary forms, with or without
7*bda690e4SXin Li  * modification, are permitted provided that the following conditions
8*bda690e4SXin Li  * are met:
9*bda690e4SXin Li  *
10*bda690e4SXin Li  *     * Redistributions of source code must retain the above copyright
11*bda690e4SXin Li  * notice, this list of conditions and the following disclaimer.
12*bda690e4SXin Li  *     * Redistributions in binary form must reproduce the above
13*bda690e4SXin Li  * copyright notice, this list of conditions and the following disclaimer
14*bda690e4SXin Li  * in the documentation and/or other materials provided with the
15*bda690e4SXin Li  * distribution.
16*bda690e4SXin Li  *     * Neither the names of the Xiph.org Foundation nor Pinknoise
17*bda690e4SXin Li  * Productions Ltd nor the names of its contributors may be used to
18*bda690e4SXin Li  * endorse or promote products derived from this software without
19*bda690e4SXin Li  * specific prior written permission.
20*bda690e4SXin Li  *
21*bda690e4SXin Li  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22*bda690e4SXin Li  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23*bda690e4SXin Li  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24*bda690e4SXin Li  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25*bda690e4SXin Li  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26*bda690e4SXin Li  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27*bda690e4SXin Li  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28*bda690e4SXin Li  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29*bda690e4SXin Li  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30*bda690e4SXin Li  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31*bda690e4SXin Li  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*bda690e4SXin Li  ************************************************************************/
33*bda690e4SXin Li 
34*bda690e4SXin Li #define HEAD_ALIGN 64
35*bda690e4SXin Li #include <stdlib.h>
36*bda690e4SXin Li #include <string.h>
37*bda690e4SXin Li #include <stdio.h>
38*bda690e4SXin Li #define MISC_C
39*bda690e4SXin Li #include "misc.h"
40*bda690e4SXin Li //#include <sys/time.h>
41*bda690e4SXin Li 
42*bda690e4SXin Li static void **pointers=NULL;
43*bda690e4SXin Li static long *insertlist=NULL; /* We can't embed this in the pointer list;
44*bda690e4SXin Li 			  a pointer can have any value... */
45*bda690e4SXin Li 
46*bda690e4SXin Li static char **files=NULL;
47*bda690e4SXin Li static long *file_bytes=NULL;
48*bda690e4SXin Li static int  filecount=0;
49*bda690e4SXin Li 
50*bda690e4SXin Li static int ptop=0;
51*bda690e4SXin Li static int palloced=0;
52*bda690e4SXin Li static int pinsert=0;
53*bda690e4SXin Li 
54*bda690e4SXin Li typedef struct {
55*bda690e4SXin Li   char *file;
56*bda690e4SXin Li   long line;
57*bda690e4SXin Li   long ptr;
58*bda690e4SXin Li   long bytes;
59*bda690e4SXin Li } head;
60*bda690e4SXin Li 
61*bda690e4SXin Li long global_bytes=0;
62*bda690e4SXin Li long start_time=-1;
63*bda690e4SXin Li 
_insert(void * ptr,long bytes,char * file,long line)64*bda690e4SXin Li static void *_insert(void *ptr,long bytes,char *file,long line){
65*bda690e4SXin Li   ((head *)ptr)->file=file;
66*bda690e4SXin Li   ((head *)ptr)->line=line;
67*bda690e4SXin Li   ((head *)ptr)->ptr=pinsert;
68*bda690e4SXin Li   ((head *)ptr)->bytes=bytes-HEAD_ALIGN;
69*bda690e4SXin Li 
70*bda690e4SXin Li   if(pinsert>=palloced){
71*bda690e4SXin Li     palloced+=64;
72*bda690e4SXin Li     if(pointers){
73*bda690e4SXin Li       pointers=(void **)realloc(pointers,sizeof(void **)*palloced);
74*bda690e4SXin Li       insertlist=(long *)realloc(insertlist,sizeof(long *)*palloced);
75*bda690e4SXin Li     }else{
76*bda690e4SXin Li       pointers=(void **)malloc(sizeof(void **)*palloced);
77*bda690e4SXin Li       insertlist=(long *)malloc(sizeof(long *)*palloced);
78*bda690e4SXin Li     }
79*bda690e4SXin Li   }
80*bda690e4SXin Li 
81*bda690e4SXin Li   pointers[pinsert]=ptr;
82*bda690e4SXin Li 
83*bda690e4SXin Li   if(pinsert==ptop)
84*bda690e4SXin Li     pinsert=++ptop;
85*bda690e4SXin Li   else
86*bda690e4SXin Li     pinsert=insertlist[pinsert];
87*bda690e4SXin Li 
88*bda690e4SXin Li #ifdef _VDBG_GRAPHFILE
89*bda690e4SXin Li   {
90*bda690e4SXin Li     FILE *out;
91*bda690e4SXin Li     struct timeval tv;
92*bda690e4SXin Li     static struct timezone tz;
93*bda690e4SXin Li     int i;
94*bda690e4SXin Li     char buffer[80];
95*bda690e4SXin Li     gettimeofday(&tv,&tz);
96*bda690e4SXin Li 
97*bda690e4SXin Li     for(i=0;i<filecount;i++)
98*bda690e4SXin Li       if(!strcmp(file,files[i]))break;
99*bda690e4SXin Li 
100*bda690e4SXin Li     if(i==filecount){
101*bda690e4SXin Li       filecount++;
102*bda690e4SXin Li       if(!files){
103*bda690e4SXin Li 	files=malloc(filecount*sizeof(*files));
104*bda690e4SXin Li 	file_bytes=malloc(filecount*sizeof(*file_bytes));
105*bda690e4SXin Li       }else{
106*bda690e4SXin Li 	files=realloc(files,filecount*sizeof(*files));
107*bda690e4SXin Li 	file_bytes=realloc(file_bytes,filecount*sizeof(*file_bytes));
108*bda690e4SXin Li       }
109*bda690e4SXin Li       files[i]=strdup(file);
110*bda690e4SXin Li       file_bytes[i]=0;
111*bda690e4SXin Li     }
112*bda690e4SXin Li 
113*bda690e4SXin Li     file_bytes[i]+=bytes-HEAD_ALIGN;
114*bda690e4SXin Li 
115*bda690e4SXin Li     if(start_time==-1)start_time=(tv.tv_sec*1000)+(tv.tv_usec/1000);
116*bda690e4SXin Li 
117*bda690e4SXin Li     snprintf(buffer,80,"%s",file);
118*bda690e4SXin Li     if(strchr(buffer,'.'))strchr(buffer,'.')[0]=0;
119*bda690e4SXin Li     strcat(buffer,_VDBG_GRAPHFILE);
120*bda690e4SXin Li     out=fopen(buffer,"a");
121*bda690e4SXin Li     fprintf(out,"%ld, %ld\n",-start_time+(tv.tv_sec*1000)+(tv.tv_usec/1000),
122*bda690e4SXin Li 	    file_bytes[i]-(bytes-HEAD_ALIGN));
123*bda690e4SXin Li     fprintf(out,"%ld, %ld # FILE %s LINE %ld\n",
124*bda690e4SXin Li 	    -start_time+(tv.tv_sec*1000)+(tv.tv_usec/1000),
125*bda690e4SXin Li 	    file_bytes[i],file,line);
126*bda690e4SXin Li     fclose(out);
127*bda690e4SXin Li 
128*bda690e4SXin Li     out=fopen("total"_VDBG_GRAPHFILE,"a");
129*bda690e4SXin Li     fprintf(out,"%ld, %ld\n",-start_time+(tv.tv_sec*1000)+(tv.tv_usec/1000),
130*bda690e4SXin Li 	    global_bytes);
131*bda690e4SXin Li     fprintf(out,"%ld, %ld\n",-start_time+(tv.tv_sec*1000)+(tv.tv_usec/1000),
132*bda690e4SXin Li 	    global_bytes+(bytes-HEAD_ALIGN));
133*bda690e4SXin Li     fclose(out);
134*bda690e4SXin Li   }
135*bda690e4SXin Li #endif
136*bda690e4SXin Li 
137*bda690e4SXin Li   global_bytes+=(bytes-HEAD_ALIGN);
138*bda690e4SXin Li 
139*bda690e4SXin Li   return(void *)(((char *)ptr)+HEAD_ALIGN);
140*bda690e4SXin Li }
141*bda690e4SXin Li 
_ripremove(void * ptr)142*bda690e4SXin Li static void _ripremove(void *ptr){
143*bda690e4SXin Li   int insert;
144*bda690e4SXin Li 
145*bda690e4SXin Li #ifdef _VDBG_GRAPHFILE
146*bda690e4SXin Li   {
147*bda690e4SXin Li     FILE *out=fopen("total"_VDBG_GRAPHFILE,"a");
148*bda690e4SXin Li     struct timeval tv;
149*bda690e4SXin Li     static struct timezone tz;
150*bda690e4SXin Li     char buffer[80];
151*bda690e4SXin Li     char *file =((head *)ptr)->file;
152*bda690e4SXin Li     long bytes =((head *)ptr)->bytes;
153*bda690e4SXin Li     int i;
154*bda690e4SXin Li 
155*bda690e4SXin Li     gettimeofday(&tv,&tz);
156*bda690e4SXin Li     fprintf(out,"%ld, %ld\n",-start_time+(tv.tv_sec*1000)+(tv.tv_usec/1000),
157*bda690e4SXin Li 	    global_bytes);
158*bda690e4SXin Li     fprintf(out,"%ld, %ld\n",-start_time+(tv.tv_sec*1000)+(tv.tv_usec/1000),
159*bda690e4SXin Li 	    global_bytes-((head *)ptr)->bytes);
160*bda690e4SXin Li     fclose(out);
161*bda690e4SXin Li 
162*bda690e4SXin Li     for(i=0;i<filecount;i++)
163*bda690e4SXin Li       if(!strcmp(file,files[i]))break;
164*bda690e4SXin Li 
165*bda690e4SXin Li     snprintf(buffer,80,"%s",file);
166*bda690e4SXin Li     if(strchr(buffer,'.'))strchr(buffer,'.')[0]=0;
167*bda690e4SXin Li     strcat(buffer,_VDBG_GRAPHFILE);
168*bda690e4SXin Li     out=fopen(buffer,"a");
169*bda690e4SXin Li     fprintf(out,"%ld, %ld\n",-start_time+(tv.tv_sec*1000)+(tv.tv_usec/1000),
170*bda690e4SXin Li 	    file_bytes[i]);
171*bda690e4SXin Li     fprintf(out,"%ld, %ld\n",-start_time+(tv.tv_sec*1000)+(tv.tv_usec/1000),
172*bda690e4SXin Li 	    file_bytes[i]-bytes);
173*bda690e4SXin Li     fclose(out);
174*bda690e4SXin Li 
175*bda690e4SXin Li     file_bytes[i]-=bytes;
176*bda690e4SXin Li 
177*bda690e4SXin Li   }
178*bda690e4SXin Li #endif
179*bda690e4SXin Li 
180*bda690e4SXin Li   global_bytes-=((head *)ptr)->bytes;
181*bda690e4SXin Li 
182*bda690e4SXin Li   insert=((head *)ptr)->ptr;
183*bda690e4SXin Li   insertlist[insert]=pinsert;
184*bda690e4SXin Li   pinsert=insert;
185*bda690e4SXin Li 
186*bda690e4SXin Li   if(pointers[insert]==NULL){
187*bda690e4SXin Li     fprintf(stderr,"DEBUGGING MALLOC ERROR: freeing previously freed memory\n");
188*bda690e4SXin Li     fprintf(stderr,"\t%s %ld\n",((head *)ptr)->file,((head *)ptr)->line);
189*bda690e4SXin Li   }
190*bda690e4SXin Li 
191*bda690e4SXin Li   if(global_bytes<0){
192*bda690e4SXin Li     fprintf(stderr,"DEBUGGING MALLOC ERROR: freeing unmalloced memory\n");
193*bda690e4SXin Li   }
194*bda690e4SXin Li 
195*bda690e4SXin Li   pointers[insert]=NULL;
196*bda690e4SXin Li }
197*bda690e4SXin Li 
_VDBG_dump(void)198*bda690e4SXin Li void _VDBG_dump(void){
199*bda690e4SXin Li   int i;
200*bda690e4SXin Li   for(i=0;i<ptop;i++){
201*bda690e4SXin Li     head *ptr=pointers[i];
202*bda690e4SXin Li     if(ptr)
203*bda690e4SXin Li       fprintf(stderr,"unfreed bytes from %s:%ld\n",
204*bda690e4SXin Li 	      ptr->file,ptr->line);
205*bda690e4SXin Li   }
206*bda690e4SXin Li 
207*bda690e4SXin Li }
208*bda690e4SXin Li 
_VDBG_malloc(void * ptr,long bytes,char * file,long line)209*bda690e4SXin Li extern void *_VDBG_malloc(void *ptr,long bytes,char *file,long line){
210*bda690e4SXin Li   bytes+=HEAD_ALIGN;
211*bda690e4SXin Li   if(ptr){
212*bda690e4SXin Li     ptr=(void *)(((char *)ptr)-HEAD_ALIGN);
213*bda690e4SXin Li     _ripremove(ptr);
214*bda690e4SXin Li     ptr=realloc(ptr,bytes);
215*bda690e4SXin Li   }else{
216*bda690e4SXin Li     ptr=malloc(bytes);
217*bda690e4SXin Li     memset(ptr,0,bytes);
218*bda690e4SXin Li   }
219*bda690e4SXin Li   return _insert(ptr,bytes,file,line);
220*bda690e4SXin Li }
221*bda690e4SXin Li 
_VDBG_free(void * ptr)222*bda690e4SXin Li extern void _VDBG_free(void *ptr){
223*bda690e4SXin Li   if(ptr){
224*bda690e4SXin Li     ptr=(void *)(((char *)ptr)-HEAD_ALIGN);
225*bda690e4SXin Li     _ripremove(ptr);
226*bda690e4SXin Li     free(ptr);
227*bda690e4SXin Li   }
228*bda690e4SXin Li }
229*bda690e4SXin Li 
230