Browse Source

try to make the memory management thread save by using a mutex upon tree accesses

1.0.2
Georg Hopp 11 years ago
parent
commit
16e092af3e
  1. 2
      src/Makefile.am
  2. 7
      src/memory.c

2
src/Makefile.am

@ -24,6 +24,6 @@ TR_CLASS = memory.c \
lib_LTLIBRARIES = libtrbase.la
libtrbase_la_SOURCES = $(TR_CLASS)
libtrbase_la_CFLAGS = $(AM_CFLAGS)
libtrbase_la_CFLAGS = $(AM_CFLAGS) -lpthread
libtrbase_la_LIBADD =
libtrbase_la_LDFLAGS = -version-info 1:0:1

7
src/memory.c

@ -48,6 +48,7 @@
#include <stdio.h>
#include <pthread.h>
#include <stdlib.h>
#include <string.h>
#include <search.h>
@ -377,6 +378,8 @@ TR_reference(void * mem)
return mem;
}
pthread_mutex_t TR_memop_lock = PTHREAD_MUTEX_INITIALIZER;
/*
* This tries to reflect the memory management behaviour of the
* GNU version of malloc. For other versions this might need
@ -435,7 +438,9 @@ TR_malloc(size_t size)
}
#ifdef MEM_OPT
pthread_mutex_lock(&TR_memop_lock);
seg = deleteElement(&segments, size);
pthread_mutex_unlock(&TR_memop_lock);
#endif
if (NULL == seg) {
@ -474,7 +479,9 @@ TR_free(void ** mem)
seg->ref_count--;
} else {
#ifdef MEM_OPT
pthread_mutex_lock(&TR_memop_lock);
insertElement(&segments, seg);
pthread_mutex_unlock(&TR_memop_lock);
#else
free(seg);
#endif

Loading…
Cancel
Save