[gst-devel] electric fence

vishnu at pobox.com vishnu at pobox.com
Fri Sep 14 20:33:02 CEST 2001


Here is a thread-safe patch for electric fence, if anyone is interested.

-- 
Victory to the Divine Mother!!
  http://sahajayoga.org
-------------- next part --------------
# This is a patch for electric-fence-2.1.9 to update it to electric-fence-2.1.9
# 
# To apply this patch:
# STEP 1: Chdir to the source directory.
# STEP 2: Run the 'applypatch' program with this patch file as input.
#
# If you do not have 'applypatch', it is part of the 'makepatch' package
# that you can fetch from the Comprehensive Perl Archive Network:
# http://www.perl.com/CPAN/authors/Johan_Vromans/makepatch-x.y.tar.gz
# In the above URL, 'x' should be 2 or higher.
#
# To apply this patch without the use of 'applypatch':
# STEP 1: Chdir to the source directory.
# STEP 2: Run the 'patch' program with this file as input.
#
#### End of Preamble ####

#### Patch data follows ####
diff -up '/tmp/mp30581.d/old/electric-fence-2.1.9/Makefile' 'electric-fence-2.1.9/Makefile'
Index: ./Makefile
--- ./Makefile	Thu Mar 12 11:52:19 1998
+++ ./Makefile	Fri Sep 14 19:59:59 2001
@@ -46,7 +46,7 @@ install: libefence.a libefence.3
 
 clean:
 	- rm -f $(OBJECTS) tstheap.o eftest.o tstheap eftest libefence.a \
-	 libefence.cat ElectricFence.shar
+	 libefence.cat ElectricFence.shar libefence.so
 
 roff:
 	nroff -man < libefence.3 > libefence.cat
diff -up '/tmp/mp30581.d/old/electric-fence-2.1.9/debian/rules' 'electric-fence-2.1.9/debian/rules'
Index: ./debian/rules
--- ./debian/rules	Mon Apr  9 06:57:29 2001
+++ ./debian/rules	Fri Sep 14 20:03:45 2001
@@ -22,7 +22,7 @@ build-stamp:
 	make CFLAGS="-O2 -g -Wall"
 	rm *.o
 	make CFLAGS="-O2 -g -Wall -fPIC"
-	gcc -g -shared -Wl,-soname,libefence.so.0 -o libefence.so.0.0 efence.o page.o print.o -lc
+	gcc -g -shared -Wl,-soname,libefence.so.0 -o libefence.so.0.0 efence.o page.o print.o -lpthread -lc
 	touch build-stamp
 
 clean:
diff -up '/tmp/mp30581.d/old/electric-fence-2.1.9/efence.c' 'electric-fence-2.1.9/efence.c'
Index: ./efence.c
--- ./efence.c	Mon Apr  9 06:54:26 2001
+++ ./efence.c	Fri Sep 14 20:28:37 2001
@@ -34,6 +34,7 @@
 #include <unistd.h>
 #include <memory.h>
 #include <string.h>
+#include <pthread.h>
 
 #ifdef	malloc
 #undef	malloc
@@ -46,6 +47,8 @@
 static const char	version[] = "\n  Electric Fence 2.1"
  " Copyright (C) 1987-1998 Bruce Perens.\n";
 
+static pthread_mutex_t mutex;
+
 /*
  * MEMORY_CREATION_SIZE is the amount of memory to get from the operating
  * system at one time. We'll break that memory down into smaller pieces for
@@ -187,6 +190,9 @@ internalError(void)
 	EF_Abort("Internal error in allocator.");
 }
 
+static void internal_free(void * address);
+static void *internal_memalign(size_t alignment, size_t userSize);
+
 /*
  * initialize sets up the memory allocation arena and the run-time
  * configuration information.
@@ -199,6 +205,8 @@ initialize(void)
 	char *	string;
 	Slot *	slot;
 
+	pthread_mutex_init (&mutex, NULL);
+
        if ( EF_DISABLE_BANNER == -1 ) {
                if ( (string = getenv("EF_DISABLE_BANNER")) != 0 )
                        EF_DISABLE_BANNER = atoi(string);
@@ -330,7 +338,7 @@ allocateMoreSlots(void)
 	noAllocationListProtection = 1;
 	internalUse = 1;
 
-	newAllocation = malloc(newSize);
+	newAllocation = internal_memalign(EF_ALIGNMENT, newSize);
 	memcpy(newAllocation, allocationList, allocationListSize);
 	memset(&(((char *)newAllocation)[allocationListSize]), 0, bytesPerPage);
 
@@ -339,7 +347,7 @@ allocateMoreSlots(void)
 	slotCount += slotsPerPage;
 	unUsedSlots += slotsPerPage;
 
-	free(oldAllocation);
+	internal_free(oldAllocation);
 
 	/*
 	 * Keep access to the allocation list open at this point, because
@@ -371,8 +379,8 @@ allocateMoreSlots(void)
  * so that it won't waste even more. It's slow, but thrashing because your
  * working set is too big for a system's RAM is even slower. 
  */
-extern C_LINKAGE void *
-memalign(size_t alignment, size_t userSize)
+static void *
+internal_memalign(size_t alignment, size_t userSize)
 {
 	register Slot *	slot;
 	register size_t	count;
@@ -382,9 +390,6 @@ memalign(size_t alignment, size_t userSi
 	size_t		slack;
 	char *		address;
 
-	if ( allocationList == 0 )
-		initialize();
-
 	if ( userSize == 0 && !EF_ALLOW_MALLOC_0 )
 		EF_Abort("Allocating 0 bytes, probably a bug.");
 
@@ -571,6 +576,25 @@ memalign(size_t alignment, size_t userSi
 	return address;
 }
 
+extern C_LINKAGE void *
+memalign(size_t alignment, size_t userSize)
+{
+  void *ptr;
+
+  if ( allocationList == 0 )
+    initialize();
+
+  pthread_mutex_lock (&mutex);
+
+  ptr = internal_memalign (alignment, userSize);
+
+  pthread_mutex_unlock (&mutex);
+
+  memset(ptr, ~0, userSize);
+
+  return ptr;
+}
+
 /*
  * Find the slot structure for a user address.
  */
@@ -626,8 +650,8 @@ slotForInternalAddressPreviousTo(void * 
 	return 0;
 }
 
-extern C_LINKAGE void
-free(void * address)
+static void
+internal_free(void * address)
 {
 	Slot *	slot;
 	Slot *	previousSlot = 0;
@@ -705,10 +729,24 @@ free(void * address)
 		Page_DenyAccess(allocationList, allocationListSize);
 }
 
+extern C_LINKAGE void
+free(void * address)
+{
+  pthread_mutex_lock (&mutex);
+
+  internal_free (address);
+  
+  pthread_mutex_unlock (&mutex);
+}
+
 extern C_LINKAGE void *
 realloc(void * oldBuffer, size_t newSize)
 {
-	void *	newBuffer = malloc(newSize);
+	void *	newBuffer;
+
+	pthread_mutex_lock (&mutex);
+
+	newBuffer = internal_memalign(EF_ALIGNMENT, newSize);
 
 	if ( oldBuffer ) {
 		size_t	size;
@@ -731,7 +769,7 @@ realloc(void * oldBuffer, size_t newSize
 		if ( size > 0 )
 			memcpy(newBuffer, oldBuffer, size);
 
-		free(oldBuffer);
+		internal_free(oldBuffer);
 		noAllocationListProtection = 0;
 		Page_DenyAccess(allocationList, allocationListSize);
 
@@ -741,16 +779,30 @@ realloc(void * oldBuffer, size_t newSize
 		/* Internal memory was re-protected in free() */
 	}
 
+	pthread_mutex_unlock (&mutex);
+
+	memset(newBuffer, ~0, newSize);
+
 	return newBuffer;
 }
 
 extern C_LINKAGE void *
 malloc(size_t size)
 {
+  void *ptr;
+
 	if ( allocationList == 0 )
 		initialize();	/* This sets EF_ALIGNMENT */
 
-	return memalign(EF_ALIGNMENT, size);
+	pthread_mutex_lock (&mutex);
+
+	ptr = internal_memalign(EF_ALIGNMENT, size);
+
+	pthread_mutex_unlock (&mutex);
+
+	memset(ptr, ~0, size);
+
+	return ptr;
 }
 
 extern C_LINKAGE void *
@@ -760,6 +812,7 @@ calloc(size_t nelem, size_t elsize)
 	void *	allocation = malloc(size);
 
 	memset(allocation, 0, size);
+
 	return allocation;
 }
 
@@ -770,5 +823,18 @@ calloc(size_t nelem, size_t elsize)
 extern C_LINKAGE void *
 valloc (size_t size)
 {
-	return memalign(bytesPerPage, size);
+  void *ptr;
+
+	if ( allocationList == 0 )
+		initialize();	/* This sets EF_ALIGNMENT */
+
+  pthread_mutex_lock (&mutex);
+
+  ptr = internal_memalign(bytesPerPage, size);
+
+  pthread_mutex_unlock (&mutex);
+
+  memset(ptr, ~0, size);
+
+  return ptr;
 }
#### End of Patch data ####

#### ApplyPatch data follows ####
# Data version        : 1.0
# Date generated      : Fri Sep 14 20:29:59 2001
# Generated by        : makepatch 2.00_03
# Recurse directories : Yes
# p 'Makefile' 2100 1000522799 0100644
# p 'debian/rules' 2022 1000523025 0100755
# p 'efence.c' 22535 1000524517 0100644
#### End of ApplyPatch data ####

#### End of Patch kit [created: Fri Sep 14 20:29:59 2001] ####
#### Patch checksum: 251 6456 49295 ####
#### Checksum: 269 7158 41955 ####


More information about the gstreamer-devel mailing list