misc.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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-2001 *
  9. * by the XIPHOPHORUS Company http://www.xiph.org/ *
  10. ********************************************************************/
  11. #define HEAD_ALIGN 32
  12. #include <pthread.h>
  13. #include <stdlib.h>
  14. #include <stdio.h>
  15. #include "vorbis/codec.h"
  16. #define MISC_C
  17. #include "misc.h"
  18. static pthread_mutex_t memlock=PTHREAD_MUTEX_INITIALIZER;
  19. void **pointers=NULL;
  20. long *insertlist=NULL; /* We can't embed this in the pointer list;
  21. a pointer can have any value... */
  22. int ptop=0;
  23. int palloced=0;
  24. int pinsert=0;
  25. typedef struct {
  26. char *file;
  27. long line;
  28. long ptr;
  29. } head;
  30. static void *_insert(void *ptr,char *file,long line){
  31. ((head *)ptr)->file=file;
  32. ((head *)ptr)->line=line;
  33. ((head *)ptr)->ptr=pinsert;
  34. pthread_mutex_lock(&memlock);
  35. if(pinsert>=palloced){
  36. palloced+=64;
  37. if(pointers){
  38. pointers=(void **)realloc(pointers,sizeof(void **)*palloced);
  39. insertlist=(long *)realloc(insertlist,sizeof(long *)*palloced);
  40. }else{
  41. pointers=(void **)malloc(sizeof(void **)*palloced);
  42. insertlist=(long *)malloc(sizeof(long *)*palloced);
  43. }
  44. }
  45. pointers[pinsert]=ptr;
  46. if(pinsert==ptop)
  47. pinsert=++ptop;
  48. else
  49. pinsert=insertlist[pinsert];
  50. pthread_mutex_unlock(&memlock);
  51. return(ptr+HEAD_ALIGN);
  52. }
  53. static void _ripremove(void *ptr){
  54. int insert;
  55. pthread_mutex_lock(&memlock);
  56. insert=((head *)ptr)->ptr;
  57. insertlist[insert]=pinsert;
  58. pinsert=insert;
  59. pointers[insert]=NULL;
  60. pthread_mutex_unlock(&memlock);
  61. }
  62. void _VDBG_dump(void){
  63. int i;
  64. pthread_mutex_lock(&memlock);
  65. for(i=0;i<ptop;i++){
  66. head *ptr=pointers[i];
  67. if(ptr)
  68. fprintf(stderr,"unfreed bytes from %s:%ld\n",
  69. ptr->file,ptr->line);
  70. }
  71. pthread_mutex_unlock(&memlock);
  72. }
  73. extern void *_VDBG_malloc(void *ptr,long bytes,char *file,long line){
  74. bytes+=HEAD_ALIGN;
  75. if(ptr){
  76. ptr-=HEAD_ALIGN;
  77. _ripremove(ptr);
  78. ptr=realloc(ptr,bytes);
  79. }else{
  80. ptr=malloc(bytes);
  81. memset(ptr,0,bytes);
  82. }
  83. return _insert(ptr,file,line);
  84. }
  85. extern void _VDBG_free(void *ptr,char *file,long line){
  86. if(ptr){
  87. ptr-=HEAD_ALIGN;
  88. _ripremove(ptr);
  89. free(ptr);
  90. }
  91. }