summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMonty <xiphmont@xiph.org>2000-03-30 01:18:50 +0000
committerMonty <xiphmont@xiph.org>2000-03-30 01:18:50 +0000
commit13dd90c70ca724153c45e951b8ecac9179bb62b0 (patch)
tree1f49029eaa19f8a5e1689d8b9ad1da73ec671982
parent03f1ea17e5834567832d7b6530072a7b89748a6f (diff)
downloadlibvorbis-git-unlabeled-1.1.2.tar.gz
Missed earlier.unlabeled-1.1.2
Monty svn path=/branches/unlabeled-1.1.2/vorbis/; revision=288
-rw-r--r--lib/misc.c107
1 files changed, 107 insertions, 0 deletions
diff --git a/lib/misc.c b/lib/misc.c
new file mode 100644
index 00000000..7534f4f5
--- /dev/null
+++ b/lib/misc.c
@@ -0,0 +1,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);
+ }
+}
+