2
0

misc.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /********************************************************************
  2. * *
  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. *
  7. * *
  8. * THE OggVorbis SOURCE CODE IS (C) COPYRIGHT 1994-2002 *
  9. * by the XIPHOPHORUS Company http://www.xiph.org/ *
  10. * *
  11. ********************************************************************/
  12. #define HEAD_ALIGN 64
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include <stdio.h>
  16. #define MISC_C
  17. #include "misc.h"
  18. #include <sys/time.h>
  19. static void **pointers=NULL;
  20. static long *insertlist=NULL; /* We can't embed this in the pointer list;
  21. a pointer can have any value... */
  22. static char **files=NULL;
  23. static long *file_bytes=NULL;
  24. static int filecount=0;
  25. static int ptop=0;
  26. static int palloced=0;
  27. static int pinsert=0;
  28. typedef struct {
  29. char *file;
  30. long line;
  31. long ptr;
  32. long bytes;
  33. } head;
  34. long global_bytes=0;
  35. long start_time=-1;
  36. static void *_insert(void *ptr,long bytes,char *file,long line){
  37. ((head *)ptr)->file=file;
  38. ((head *)ptr)->line=line;
  39. ((head *)ptr)->ptr=pinsert;
  40. ((head *)ptr)->bytes=bytes-HEAD_ALIGN;
  41. if(pinsert>=palloced){
  42. palloced+=64;
  43. if(pointers){
  44. pointers=(void **)realloc(pointers,sizeof(void **)*palloced);
  45. insertlist=(long *)realloc(insertlist,sizeof(long *)*palloced);
  46. }else{
  47. pointers=(void **)malloc(sizeof(void **)*palloced);
  48. insertlist=(long *)malloc(sizeof(long *)*palloced);
  49. }
  50. }
  51. pointers[pinsert]=ptr;
  52. if(pinsert==ptop)
  53. pinsert=++ptop;
  54. else
  55. pinsert=insertlist[pinsert];
  56. #ifdef _VDBG_GRAPHFILE
  57. {
  58. FILE *out;
  59. struct timeval tv;
  60. static struct timezone tz;
  61. int i;
  62. char buffer[80];
  63. gettimeofday(&tv,&tz);
  64. for(i=0;i<filecount;i++)
  65. if(!strcmp(file,files[i]))break;
  66. if(i==filecount){
  67. filecount++;
  68. if(!files){
  69. files=malloc(filecount*sizeof(*files));
  70. file_bytes=malloc(filecount*sizeof(*file_bytes));
  71. }else{
  72. files=realloc(files,filecount*sizeof(*files));
  73. file_bytes=realloc(file_bytes,filecount*sizeof(*file_bytes));
  74. }
  75. files[i]=strdup(file);
  76. file_bytes[i]=0;
  77. }
  78. file_bytes[i]+=bytes-HEAD_ALIGN;
  79. if(start_time==-1)start_time=(tv.tv_sec*1000)+(tv.tv_usec/1000);
  80. snprintf(buffer,80,"%s",file);
  81. if(strchr(buffer,'.'))strchr(buffer,'.')[0]=0;
  82. strcat(buffer,_VDBG_GRAPHFILE);
  83. out=fopen(buffer,"a");
  84. fprintf(out,"%ld, %ld\n",-start_time+(tv.tv_sec*1000)+(tv.tv_usec/1000),
  85. file_bytes[i]-(bytes-HEAD_ALIGN));
  86. fprintf(out,"%ld, %ld # FILE %s LINE %ld\n",
  87. -start_time+(tv.tv_sec*1000)+(tv.tv_usec/1000),
  88. file_bytes[i],file,line);
  89. fclose(out);
  90. out=fopen("total"_VDBG_GRAPHFILE,"a");
  91. fprintf(out,"%ld, %ld\n",-start_time+(tv.tv_sec*1000)+(tv.tv_usec/1000),
  92. global_bytes);
  93. fprintf(out,"%ld, %ld\n",-start_time+(tv.tv_sec*1000)+(tv.tv_usec/1000),
  94. global_bytes+(bytes-HEAD_ALIGN));
  95. fclose(out);
  96. }
  97. #endif
  98. global_bytes+=(bytes-HEAD_ALIGN);
  99. return(ptr+HEAD_ALIGN);
  100. }
  101. static void _ripremove(void *ptr){
  102. int insert;
  103. #ifdef _VDBG_GRAPHFILE
  104. {
  105. FILE *out=fopen("total"_VDBG_GRAPHFILE,"a");
  106. struct timeval tv;
  107. static struct timezone tz;
  108. char buffer[80];
  109. char *file =((head *)ptr)->file;
  110. long bytes =((head *)ptr)->bytes;
  111. int i;
  112. gettimeofday(&tv,&tz);
  113. fprintf(out,"%ld, %ld\n",-start_time+(tv.tv_sec*1000)+(tv.tv_usec/1000),
  114. global_bytes);
  115. fprintf(out,"%ld, %ld\n",-start_time+(tv.tv_sec*1000)+(tv.tv_usec/1000),
  116. global_bytes-((head *)ptr)->bytes);
  117. fclose(out);
  118. for(i=0;i<filecount;i++)
  119. if(!strcmp(file,files[i]))break;
  120. snprintf(buffer,80,"%s",file);
  121. if(strchr(buffer,'.'))strchr(buffer,'.')[0]=0;
  122. strcat(buffer,_VDBG_GRAPHFILE);
  123. out=fopen(buffer,"a");
  124. fprintf(out,"%ld, %ld\n",-start_time+(tv.tv_sec*1000)+(tv.tv_usec/1000),
  125. file_bytes[i]);
  126. fprintf(out,"%ld, %ld\n",-start_time+(tv.tv_sec*1000)+(tv.tv_usec/1000),
  127. file_bytes[i]-bytes);
  128. fclose(out);
  129. file_bytes[i]-=bytes;
  130. }
  131. #endif
  132. global_bytes-=((head *)ptr)->bytes;
  133. insert=((head *)ptr)->ptr;
  134. insertlist[insert]=pinsert;
  135. pinsert=insert;
  136. if(pointers[insert]==NULL){
  137. fprintf(stderr,"DEBUGGING MALLOC ERROR: freeing previously freed memory\n");
  138. fprintf(stderr,"\t%s %ld\n",((head *)ptr)->file,((head *)ptr)->line);
  139. }
  140. if(global_bytes<0){
  141. fprintf(stderr,"DEBUGGING MALLOC ERROR: freeing unmalloced memory\n");
  142. }
  143. pointers[insert]=NULL;
  144. }
  145. void _VDBG_dump(void){
  146. int i;
  147. for(i=0;i<ptop;i++){
  148. head *ptr=pointers[i];
  149. if(ptr)
  150. fprintf(stderr,"unfreed bytes from %s:%ld\n",
  151. ptr->file,ptr->line);
  152. }
  153. }
  154. extern void *_VDBG_malloc(void *ptr,long bytes,char *file,long line){
  155. bytes+=HEAD_ALIGN;
  156. if(ptr){
  157. ptr-=HEAD_ALIGN;
  158. _ripremove(ptr);
  159. ptr=realloc(ptr,bytes);
  160. }else{
  161. ptr=malloc(bytes);
  162. memset(ptr,0,bytes);
  163. }
  164. return _insert(ptr,bytes,file,line);
  165. }
  166. extern void _VDBG_free(void *ptr,char *file,long line){
  167. if(ptr){
  168. ptr-=HEAD_ALIGN;
  169. _ripremove(ptr);
  170. free(ptr);
  171. }
  172. }