misc.c 6.4 KB

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