misc.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /********************************************************************
  2. * *
  3. * THIS FILE IS PART OF THE Ogg Vorbis SOFTWARE CODEC SOURCE CODE. *
  4. * USE, DISTRIBUTION AND REPRODUCTION OF THIS SOURCE IS GOVERNED BY *
  5. * THE GNU PUBLIC LICENSE 2, WHICH IS INCLUDED WITH THIS SOURCE. *
  6. * PLEASE READ THESE TERMS DISTRIBUTING. *
  7. * *
  8. * THE OggSQUISH SOURCE CODE IS (C) COPYRIGHT 1994-2000 *
  9. * by Monty <monty@xiph.org> and The XIPHOPHORUS Company *
  10. * http://www.xiph.org/ *
  11. * *
  12. ********************************************************************/
  13. #define HEAD_ALIGN 32
  14. #include <pthread.h>
  15. #include <stdlib.h>
  16. #include <stdio.h>
  17. #include "vorbis/codec.h"
  18. #define MISC_C
  19. #include "misc.h"
  20. static pthread_mutex_t memlock=PTHREAD_MUTEX_INITIALIZER;
  21. void **pointers=NULL;
  22. long *insertlist=NULL; /* We can't embed this in the pointer list;
  23. a pointer can have any value... */
  24. int ptop=0;
  25. int palloced=0;
  26. int pinsert=0;
  27. typedef struct {
  28. char *file;
  29. long line;
  30. long ptr;
  31. } head;
  32. static void *_insert(void *ptr,char *file,long line){
  33. ((head *)ptr)->file=file;
  34. ((head *)ptr)->line=line;
  35. ((head *)ptr)->ptr=pinsert;
  36. pthread_mutex_lock(&memlock);
  37. if(pinsert>=palloced){
  38. palloced+=64;
  39. if(pointers){
  40. pointers=(void **)realloc(pointers,sizeof(void **)*palloced);
  41. insertlist=(long *)realloc(insertlist,sizeof(long *)*palloced);
  42. }else{
  43. pointers=(void **)malloc(sizeof(void **)*palloced);
  44. insertlist=(long *)malloc(sizeof(long *)*palloced);
  45. }
  46. }
  47. pointers[pinsert]=ptr;
  48. if(pinsert==ptop)
  49. pinsert=++ptop;
  50. else
  51. pinsert=insertlist[pinsert];
  52. pthread_mutex_unlock(&memlock);
  53. return(ptr+HEAD_ALIGN);
  54. }
  55. static void _ripremove(void *ptr){
  56. int insert;
  57. pthread_mutex_lock(&memlock);
  58. insert=((head *)ptr)->ptr;
  59. insertlist[insert]=pinsert;
  60. pinsert=insert;
  61. pointers[insert]=NULL;
  62. pthread_mutex_unlock(&memlock);
  63. }
  64. void _VDBG_dump(void){
  65. int i;
  66. pthread_mutex_lock(&memlock);
  67. for(i=0;i<ptop;i++){
  68. head *ptr=pointers[i];
  69. if(ptr)
  70. fprintf(stderr,"unfreed bytes from %s:%ld\n",
  71. ptr->file,ptr->line);
  72. }
  73. pthread_mutex_unlock(&memlock);
  74. }
  75. extern void *_VDBG_malloc(void *ptr,long bytes,char *file,long line){
  76. bytes+=HEAD_ALIGN;
  77. if(ptr){
  78. ptr-=HEAD_ALIGN;
  79. _ripremove(ptr);
  80. ptr=realloc(ptr,bytes);
  81. }else{
  82. ptr=malloc(bytes);
  83. memset(ptr,0,bytes);
  84. }
  85. return _insert(ptr,file,line);
  86. }
  87. extern void _VDBG_free(void *ptr,char *file,long line){
  88. if(ptr){
  89. ptr-=HEAD_ALIGN;
  90. _ripremove(ptr);
  91. free(ptr);
  92. }
  93. }