misc.c 5.3 KB

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