summaryrefslogtreecommitdiff
path: root/lib/misc.c
blob: 7534f4f506262a02e275676dca06c7bc4435747d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/********************************************************************
 *                                                                  *
 * THIS FILE IS PART OF THE Ogg Vorbis SOFTWARE CODEC SOURCE CODE.  *
 * USE, DISTRIBUTION AND REPRODUCTION OF THIS SOURCE IS GOVERNED BY *
 * THE GNU PUBLIC LICENSE 2, WHICH IS INCLUDED WITH THIS SOURCE.    *
 * PLEASE READ THESE TERMS DISTRIBUTING.                            *
 *                                                                  *
 * THE OggSQUISH SOURCE CODE IS (C) COPYRIGHT 1994-2000             *
 * by Monty <monty@xiph.org> and The XIPHOPHORUS Company            *
 * http://www.xiph.org/                                             *
 *                                                                  *
 ********************************************************************/

#define HEAD_ALIGN 32
#include <pthread.h>
#include <stdlib.h>
#include <stdio.h>
#include "vorbis/codec.h"
#define MISC_C
#include "misc.h"

static pthread_mutex_t memlock=PTHREAD_MUTEX_INITIALIZER;
void **pointers=NULL;
long *insertlist=NULL; /* We can't embed this in the pointer list;
			  a pointer can have any value... */
int ptop=0;
int palloced=0;
int pinsert=0;

typedef struct {
  char *file;
  long line;
  long ptr;
} head;

static void *_insert(void *ptr,char *file,long line){
  ((head *)ptr)->file=file;
  ((head *)ptr)->line=line;
  ((head *)ptr)->ptr=pinsert;

  pthread_mutex_lock(&memlock);
  if(pinsert>=palloced){
    palloced+=64;
    if(pointers){
      pointers=(void **)realloc(pointers,sizeof(void **)*palloced);
      insertlist=(long *)realloc(insertlist,sizeof(long *)*palloced);
    }else{
      pointers=(void **)malloc(sizeof(void **)*palloced);
      insertlist=(long *)malloc(sizeof(long *)*palloced);
    }
  }

  pointers[pinsert]=ptr;

  if(pinsert==ptop)
    pinsert=++ptop;
  else
    pinsert=insertlist[pinsert];
  
  pthread_mutex_unlock(&memlock);
  return(ptr+HEAD_ALIGN);
}

static void _ripremove(void *ptr){
  int insert;
  pthread_mutex_lock(&memlock);
  insert=((head *)ptr)->ptr;
  insertlist[insert]=pinsert;
  pinsert=insert;
  pointers[insert]=NULL;
  pthread_mutex_unlock(&memlock);
}

void _VDBG_dump(void){
  int i;
  pthread_mutex_lock(&memlock);
  for(i=0;i<ptop;i++){
    head *ptr=pointers[i];
    if(ptr)
      fprintf(stderr,"unfreed bytes from %s:%ld\n",
	      ptr->file,ptr->line);
  }

  pthread_mutex_unlock(&memlock);
}

extern void *_VDBG_malloc(void *ptr,long bytes,char *file,long line){
  bytes+=HEAD_ALIGN;
  if(ptr){
    ptr-=HEAD_ALIGN;
    _ripremove(ptr);
    ptr=realloc(ptr,bytes);
  }else{
    ptr=malloc(bytes);
    memset(ptr,0,bytes);
  }
  return _insert(ptr,file,line);
}

extern void _VDBG_free(void *ptr,char *file,long line){
  if(ptr){
    ptr-=HEAD_ALIGN;
    _ripremove(ptr);
    free(ptr);
  }
}