From dc4414aeda179bc8ebe3fc46ac53e4a24d265d16 Mon Sep 17 00:00:00 2001 From: Adam Dickmeiss Date: Tue, 17 Apr 2007 20:26:18 +0000 Subject: [PATCH] Split YAZ library into two libs : libyaz.la and libyaz_server.la. libyaz.la is the core of YAZ except the generic frontend server and does not depend on POSIX threads anymore. libyaz_server.la is the generic frontend server facilities and uses POSIX thread functionality if available. The libyaz.la no longer depends on POSIX threads because the number of global structures is limited. NMEM no longer re-uses blocks between threads ; it simply free's memory immediately but allocates in "large" chunks as usual. We don't expect any performance penalties because of this. The yaz_log system is still using a global log_level so caution must be taken when modifying it with yaz_log_init_.. + yaz_log_mask_str. This, however, should not cause any trouble because these functions are called during initialization of application code anyway. `yaz-config --libs server` returns libs for server applications; `yaz-config --libs` returns libs for non-server applications. --- NEWS | 15 ++ configure.ac | 8 +- include/yaz/Makefile.am | 4 +- include/yaz/mutex.h | 62 ++++++++ include/yaz/nmem.h | 60 +------- src/Makefile.am | 15 +- src/log.c | 60 +------- src/mutex.c | 115 ++++++++++++++ src/nmem.c | 386 ++--------------------------------------------- test/tstnmem.c | 4 +- util/marcdump.c | 4 +- util/srwtst.c | 4 +- yaz-config.in | 39 +++-- zoom/zoomsh.c | 4 +- zoom/zoomtst7.c | 3 +- ztest/Makefile.am | 4 +- 16 files changed, 270 insertions(+), 517 deletions(-) create mode 100644 include/yaz/mutex.h create mode 100644 src/mutex.c diff --git a/NEWS b/NEWS index a3163d5..1c5d0a6 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,18 @@ +Split YAZ library into two libs : libyaz.la and libyaz_server.la. +libyaz.la is the core of YAZ except the generic frontend server and +does not depend on POSIX threads anymore. libyaz_server.la is the +generic frontend server facilities and uses POSIX thread functionality +if available. The libyaz.la no longer depends on POSIX threads because +the number of global structures is limited. NMEM no longer re-uses blocks +between threads ; it simply free's memory immediately but allocates in +"large" chunks as usual. We don't expect any performance penalties +because of this. The yaz_log system is still using a global log_level so +caution must be taken when modifying it with yaz_log_init_.. + +yaz_log_mask_str. This, however, should not cause any trouble because +these functions are called during initialization of application code +anyway. `yaz-config --libs server` returns libs for server applications; +`yaz-config --libs` returns libs for non-server applications. + New OID database - with public definitions in oid_db.h. Removed old OID database including the head oid.h and definitions such as enum oid_value and struct oident. The new OID database uses the same string names as diff --git a/configure.ac b/configure.ac index e889524..2b7d3c5 100644 --- a/configure.ac +++ b/configure.ac @@ -1,6 +1,6 @@ dnl YAZ Toolkit, Index Data 1995-2007 dnl See the file LICENSE for details. -dnl $Id: configure.ac,v 1.74 2007-04-16 21:53:08 adam Exp $ +dnl $Id: configure.ac,v 1.75 2007-04-17 20:26:18 adam Exp $ AC_PREREQ(2.59) AC_INIT([yaz],[2.1.99.3],[yaz-help@indexdata.dk]) AC_CONFIG_SRCDIR(configure.ac) @@ -340,8 +340,8 @@ dnl ------ POSIX Threads AC_ARG_ENABLE(threads, [ --disable-threads disable POSIX threads],[enable_threads=$enableval],[enable_threads=yes]) if test "$enable_threads" = "yes" -a "$HAVETHREADS" = "0"; then ACX_PTHREAD([ + OCFLAGS=$CFLAGS CFLAGS="$CFLAGS $PTHREAD_CFLAGS" - AC_DEFINE(YAZ_POSIX_THREADS) dnl unfortunately empty thread lib spec is problematic because dnl 'yaz-config --cflags' is not always passed to linker in dnl applications using YAZ (such as Zebra). @@ -356,8 +356,8 @@ if test "$enable_threads" = "yes" -a "$HAVETHREADS" = "0"; then done LIBS=$OLIBS fi - LIBS="$PTHREAD_LIBS $LIBS" - YAZ_CONFIG_CFLAGS="$YAZ_CONFIG_CFLAGS $PTHREAD_CFLAGS -DYAZ_POSIX_THREADS=1" + CFLAGS=$OCFLAGS + PTHREAD_CFLAGS="$PTHREAD_CFLAGS -DYAZ_POSIX_THREADS=1" ]) fi diff --git a/include/yaz/Makefile.am b/include/yaz/Makefile.am index acf1c52..2e0bf02 100644 --- a/include/yaz/Makefile.am +++ b/include/yaz/Makefile.am @@ -1,4 +1,4 @@ -## $Id: Makefile.am,v 1.42 2007-04-16 21:53:08 adam Exp $ +## $Id: Makefile.am,v 1.43 2007-04-17 20:26:18 adam Exp $ pkginclude_HEADERS= backend.h ccl.h ccl_xml.h cql.h comstack.h \ diagbib1.h diagsrw.h diagsru_update.h sortspec.h log.h logrpn.h marcdisp.h \ @@ -8,7 +8,7 @@ pkginclude_HEADERS= backend.h ccl.h ccl_xml.h cql.h comstack.h \ tcpip.h test.h timing.h unix.h tpath.h wrbuf.h xmalloc.h \ yaz-ccl.h yaz-iconv.h yaz-util.h yaz-version.h yconfig.h proto.h \ xmlquery.h libxml2_error.h xmltypes.h snprintf.h query-charset.h \ - oid_db.h oid_util.h oid_std.h \ + mutex.h oid_db.h oid_util.h oid_std.h \ \ ill.h ill-core.h item-req.h z-accdes1.h z-accform1.h \ z-acckrb1.h z-core.h z-date.h z-diag1.h z-espec1.h z-estask.h z-exp.h \ diff --git a/include/yaz/mutex.h b/include/yaz/mutex.h new file mode 100644 index 0000000..009e091 --- /dev/null +++ b/include/yaz/mutex.h @@ -0,0 +1,62 @@ +/* + * Copyright (c) 1995-2007, Index Data + * All rights reserved. + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of Index Data nor the names of its contributors + * may be used to endorse or promote products derived from this + * software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +/* $Id: mutex.h,v 1.1 2007-04-17 20:26:18 adam Exp $ */ + +/** + * \file mutex.h + * \brief Header for Mutex functions + */ +#ifndef YAZ_MUTEX_H +#define YAZ_MUTEX_H + +#include +#include + +YAZ_BEGIN_CDECL + +/** \brief NMEM/YAZ MUTEX opaque pointer */ +typedef struct yaz_mutex *YAZ_MUTEX; +/** \brief create Mutex */ +YAZ_EXPORT void yaz_mutex_create(YAZ_MUTEX *); +/** \brief enter critical section / AKA lock */ +YAZ_EXPORT void yaz_mutex_enter(YAZ_MUTEX); +/** \brief leave critical section / AKA unlock */ +YAZ_EXPORT void yaz_mutex_leave(YAZ_MUTEX); +/** \brief destroy MUTEX */ +YAZ_EXPORT void yaz_mutex_destroy(YAZ_MUTEX *); + +YAZ_END_CDECL + +#endif +/* + * Local variables: + * c-basic-offset: 4 + * indent-tabs-mode: nil + * End: + * vim: shiftwidth=4 tabstop=8 expandtab + */ + diff --git a/include/yaz/nmem.h b/include/yaz/nmem.h index e786d0b..6bffb6a 100644 --- a/include/yaz/nmem.h +++ b/include/yaz/nmem.h @@ -24,7 +24,7 @@ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -/* $Id: nmem.h,v 1.24 2007-01-03 08:42:14 adam Exp $ */ +/* $Id: nmem.h,v 1.25 2007-04-17 20:26:18 adam Exp $ */ /** * \file nmem.h @@ -39,25 +39,8 @@ #include #include -#define NMEM_DEBUG 0 - -#ifndef NMEM_DEBUG -#define NMEM_DEBUG 0 -#endif - YAZ_BEGIN_CDECL -/** \brief NMEM/YAZ MUTEX opaque pointer */ -typedef struct nmem_mutex *NMEM_MUTEX; -/** \brief create Mutex */ -YAZ_EXPORT void nmem_mutex_create(NMEM_MUTEX *); -/** \brief enter critical section / AKA lock */ -YAZ_EXPORT void nmem_mutex_enter(NMEM_MUTEX); -/** \brief leave critical section / AKA unlock */ -YAZ_EXPORT void nmem_mutex_leave(NMEM_MUTEX); -/** \brief destroy MUTEX */ -YAZ_EXPORT void nmem_mutex_destroy(NMEM_MUTEX *); - /** \brief NMEM handle (an opaque pointer to memory) */ typedef struct nmem_control *NMEM; @@ -99,25 +82,6 @@ YAZ_EXPORT int *nmem_intdup (NMEM mem, int v); /** \brief transfers memory from one NMEM handle to another */ YAZ_EXPORT void nmem_transfer (NMEM dst, NMEM src); -/** \brief internal (do not use) */ -YAZ_EXPORT void nmem_critical_enter (void); -/** \brief internal (do not use) */ -YAZ_EXPORT void nmem_critical_leave (void); - -#if NMEM_DEBUG - -YAZ_EXPORT NMEM nmem_create_f(const char *file, int line); -YAZ_EXPORT void nmem_destroy_f(const char *file, int line, NMEM n); -YAZ_EXPORT void *nmem_malloc_f(const char *file, int line, NMEM n, int size); -#define nmem_create() nmem_create_f(__FILE__, __LINE__) -#define nmem_destroy(x) nmem_destroy_f(__FILE__, __LINE__, (x)) -#define nmem_malloc(x, y) nmem_malloc_f(__FILE__, __LINE__, (x), (y)) - -YAZ_EXPORT void nmem_print_list (void); -YAZ_EXPORT void nmem_print_list_l (int level); - -#else - /** \brief returns new NMEM handle */ YAZ_EXPORT NMEM nmem_create(void); @@ -127,32 +91,10 @@ YAZ_EXPORT void nmem_destroy(NMEM n); /** \brief allocate memory block on NMEM handle */ YAZ_EXPORT void *nmem_malloc(NMEM n, int size); -#define nmem_print_list() - -#endif - -/** \brief initializes NMEM system - This function increments a usage counter for NMEM.. Only - on first usage the system is initialized.. The \fn nmem_exit - decrements the counter. So these must be called in pairs -*/ -YAZ_EXPORT void nmem_init (void); - -/** \brief destroys NMEM system */ -YAZ_EXPORT void nmem_exit (void); - YAZ_EXPORT int yaz_errno (void); YAZ_EXPORT void yaz_set_errno (int v); YAZ_EXPORT void yaz_strerror(char *buf, int max); -/** \brief returns memory in use (by application) - \param p pointer to size (in bytes) - */ -YAZ_EXPORT void nmem_get_memory_in_use(size_t *p); -/** \brief returns memory in free (for later reuse) - */ -YAZ_EXPORT void nmem_get_memory_free(size_t *p); - YAZ_END_CDECL #endif diff --git a/src/Makefile.am b/src/Makefile.am index ea5f2cd..8ee4ef9 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -1,10 +1,10 @@ ## This file is part of the YAZ toolkit. ## Copyright (C) 1995-2007, Index Data, All rights reserved. -## $Id: Makefile.am,v 1.60 2007-04-17 08:15:15 adam Exp $ +## $Id: Makefile.am,v 1.61 2007-04-17 20:26:18 adam Exp $ YAZ_VERSION_INFO=3:0:0 -lib_LTLIBRARIES = libyaz.la +lib_LTLIBRARIES = libyaz.la libyaz_server.la # noinst_PROGRAMS = cqlparse dist-hook: @@ -28,6 +28,8 @@ YAZCOMP_Z = $(YAZCOMP) -d $(srcdir)/z.tcl -i yaz -I$(top_srcdir)/include YAZCOMP_I = $(YAZCOMP) -d $(srcdir)/ill.tcl -i yaz -I$(top_srcdir)/include AM_CPPFLAGS=-I$(top_srcdir)/include $(XML2_CFLAGS) $(SSL_CFLAGS) +libyaz_server_la_CFLAGS = $(PTHREAD_CFLAGS) + AM_YFLAGS=-p cql_ @@ -88,14 +90,19 @@ libyaz_la_SOURCES=version.c options.c log.c \ cclqfile.c cclstr.c cclxmlconfig.c \ cql.y cqlstdio.c cqltransform.c cqlutil.c xcqlutil.c cqlstring.c \ cqlstrer.c querytowrbuf.c \ - eventl.c seshigh.c statserv.c requestq.c tcpdchk.c \ - eventl.h service.c service.h session.h test.c timing.c \ + tcpdchk.c \ + test.c timing.c \ xmlquery.c http.c \ mime.c mime.h oid_util.c \ record_conv.c retrieval.c elementset.c snprintf.c query-charset.c libyaz_la_LDFLAGS=-version-info $(YAZ_VERSION_INFO) +libyaz_server_la_SOURCES = statserv.c seshigh.c eventl.c \ + requestq.c eventl.h session.h service.c service.h mutex.c + +libyaz_server_la_LDFLAGS=-version-info $(YAZ_VERSION_INFO) + # Rules for Z39.50 V3 z-accdes1.c \ z-accform1.c \ diff --git a/src/log.c b/src/log.c index ced99c5..ab74836 100644 --- a/src/log.c +++ b/src/log.c @@ -2,7 +2,7 @@ * Copyright (C) 1995-2007, Index Data ApS * See the file LICENSE for details. * - * $Id: log.c,v 1.49 2007-03-22 09:13:13 adam Exp $ + * $Id: log.c,v 1.50 2007-04-17 20:26:18 adam Exp $ */ /** @@ -18,14 +18,6 @@ #include #endif -#if YAZ_POSIX_THREADS -#include -#endif - -#if YAZ_GNU_THREADS -#include -#endif - #include #include #include @@ -39,8 +31,6 @@ #include #include -static NMEM_MUTEX log_mutex = 0; - #define HAS_STRERROR 1 @@ -123,8 +113,6 @@ static void internal_log_init(void) return; mutex_init_flag = 1; /* here, 'cause nmem_mutex_create may call yaz_log */ - nmem_mutex_create(&log_mutex); - env = getenv("YAZ_LOG"); if (env) l_level = yaz_log_mask_str_x(env, l_level); @@ -228,10 +216,8 @@ void yaz_log_init_level(int level) yaz_log(YLOG_LOGLVL, "Setting log level to %d = 0x%08x", l_level, l_level); /* determine size of mask_names (locked) */ - nmem_mutex_enter(log_mutex); for (sz = 0; mask_names[sz].name; sz++) ; - nmem_mutex_leave(log_mutex); /* second pass without lock */ for (i = 0; i < sz; i++) if (mask_names[i].mask && *mask_names[i].name) @@ -355,14 +341,12 @@ static void yaz_log_do_reopen(const char *filemode) struct tm *tm; #endif - nmem_mutex_enter(log_mutex); #if HAVE_LOCALTIME_R localtime_r(&cur_time, tm); #else tm = localtime(&cur_time); #endif yaz_log_open_check(tm, 1, filemode); - nmem_mutex_leave(log_mutex); } @@ -376,34 +360,10 @@ void yaz_log_trunc() yaz_log_do_reopen("w"); } - - static void yaz_strftime(char *dst, size_t sz, const char *fmt, const struct tm *tm) { - const char *cp = strstr(fmt, "%!"); - if (cp && strlen(fmt) < 60) - { - char fmt2[80]; - char tpidstr[20]; -#ifdef WIN32 - DWORD tid = GetCurrentThreadId(); -#else -#if YAZ_POSIX_THREADS - pthread_t tid = pthread_self(); -#else - long tid = 0; -#endif -#endif - memcpy(fmt2, fmt, cp-fmt); - fmt2[cp-fmt] = '\0'; - sprintf(tpidstr, "%08lx", (long) tid); - strcat(fmt2, tpidstr); - strcat(fmt2, cp+2); - strftime(dst, sz, fmt2, tm); - } - else - strftime(dst, sz, fmt, tm); + strftime(dst, sz, fmt, tm); } static void yaz_log_to_file(int level, const char *log_message) @@ -418,8 +378,6 @@ static void yaz_log_to_file(int level, const char *log_message) internal_log_init(); - nmem_mutex_enter(log_mutex); - #if HAVE_LOCALTIME_R localtime_r(&ti, tm); #else @@ -460,7 +418,6 @@ static void yaz_log_to_file(int level, const char *log_message) if (l_level & YLOG_FLUSH) fflush(file); } - nmem_mutex_leave(log_mutex); } void yaz_log(int level, const char *fmt, ...) @@ -475,7 +432,11 @@ void yaz_log(int level, const char *fmt, ...) return; va_start(ap, fmt); - yaz_vsnprintf(buf, sizeof(buf)-1, fmt, ap); + /* 30 is enough for our 'rest of output' message */ + yaz_vsnprintf(buf, sizeof(buf)-30, fmt, ap); + if (strlen(buf) >= sizeof(buf)-31) + strcat(buf, " [rest of output omitted]"); + if (o_level & YLOG_ERRNO) { strcat(buf, " ["); @@ -532,16 +493,13 @@ static int define_module_bit(const char *name) { int i; - nmem_mutex_enter(log_mutex); for (i = 0; mask_names[i].name; i++) if (0 == strcmp(mask_names[i].name, name)) { - nmem_mutex_leave(log_mutex); return mask_names[i].mask; } if ( (i>=MAX_MASK_NAMES) || (next_log_bit & (1<<31) )) { - nmem_mutex_leave(log_mutex); yaz_log(YLOG_WARN, "No more log bits left, not logging '%s'", name); return 0; } @@ -551,7 +509,6 @@ static int define_module_bit(const char *name) strcpy(mask_names[i].name, name); mask_names[i+1].name = NULL; mask_names[i+1].mask = 0; - nmem_mutex_leave(log_mutex); return mask_names[i].mask; } @@ -562,17 +519,14 @@ int yaz_log_module_level(const char *name) char *n = clean_name(name, strlen(name), clean, sizeof(clean)); internal_log_init(); - nmem_mutex_enter(log_mutex); for (i = 0; mask_names[i].name; i++) if (0==strcmp(n, mask_names[i].name)) { - nmem_mutex_leave(log_mutex); yaz_log(YLOG_LOGLVL, "returning log bit 0x%x for '%s' %s", mask_names[i].mask, n, strcmp(n,name) ? name : ""); return mask_names[i].mask; } - nmem_mutex_leave(log_mutex); yaz_log(YLOG_LOGLVL, "returning NO log bit for '%s' %s", n, strcmp(n, name) ? name : "" ); return 0; diff --git a/src/mutex.c b/src/mutex.c new file mode 100644 index 0000000..b2c8c49 --- /dev/null +++ b/src/mutex.c @@ -0,0 +1,115 @@ +/* + * Copyright (C) 1995-2007, Index Data ApS + * See the file LICENSE for details. + * + * $Id: mutex.c,v 1.1 2007-04-17 20:26:18 adam Exp $ + */ + +/** + * \file mutex.c + * \brief Implements MUTEX functions + * + */ +#if HAVE_CONFIG_H +#include +#endif + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifdef WIN32 +#include +#endif + +#if YAZ_POSIX_THREADS +#include +#endif + +#if YAZ_GNU_THREADS +#include +#endif + +#ifdef WIN32 +struct yaz_mutex { + CRITICAL_SECTION m_handle; +}; +#elif YAZ_POSIX_THREADS +struct yaz_mutex { + pthread_mutex_t m_handle; +}; +#elif YAZ_GNU_THREADS +struct yaz_mutex { + pth_mutex_t m_handle; +}; +#else +struct yaz_mutex { + int dummy; +}; +#endif + +YAZ_EXPORT void yaz_mutex_create(YAZ_MUTEX *p) +{ + if (!*p) + { + *p = (YAZ_MUTEX) malloc(sizeof(**p)); +#ifdef WIN32 + InitializeCriticalSection(&(*p)->m_handle); +#elif YAZ_POSIX_THREADS + pthread_mutex_init(&(*p)->m_handle, 0); +#elif YAZ_GNU_THREADS + pth_mutex_init(&(*p)->m_handle); +#endif + } +} + +YAZ_EXPORT void yaz_mutex_enter(YAZ_MUTEX p) +{ + if (p) + { +#ifdef WIN32 + EnterCriticalSection(&p->m_handle); +#elif YAZ_POSIX_THREADS + pthread_mutex_lock(&p->m_handle); +#endif + } +} + +YAZ_EXPORT void yaz_mutex_leave(YAZ_MUTEX p) +{ + if (p) + { +#ifdef WIN32 + LeaveCriticalSection(&p->m_handle); +#elif YAZ_POSIX_THREADS + pthread_mutex_unlock(&p->m_handle); +#endif + } +} + +YAZ_EXPORT void yaz_mutex_destroy(YAZ_MUTEX *p) +{ + if (*p) + { +#ifdef WIN32 + DeleteCriticalSection(&(*p)->m_handle); +#endif + free(*p); + *p = 0; + } +} + +/* + * Local variables: + * c-basic-offset: 4 + * indent-tabs-mode: nil + * End: + * vim: shiftwidth=4 tabstop=8 expandtab + */ + diff --git a/src/nmem.c b/src/nmem.c index ce08d99..9ad8c28 100644 --- a/src/nmem.c +++ b/src/nmem.c @@ -2,7 +2,7 @@ * Copyright (C) 1995-2007, Index Data ApS * See the file LICENSE for details. * - * $Id: nmem.c,v 1.29 2007-04-12 13:52:57 adam Exp $ + * $Id: nmem.c,v 1.30 2007-04-17 20:26:18 adam Exp $ */ /** @@ -32,14 +32,6 @@ #include #endif -#if YAZ_POSIX_THREADS -#include -#endif - -#if YAZ_GNU_THREADS -#include -#endif - #define NMEM_CHUNK (4*1024) struct nmem_block @@ -77,198 +69,34 @@ struct align { static int log_level = 0; static int log_level_initialized = 0; -#ifdef WIN32 -static CRITICAL_SECTION critical_section; -#define NMEM_ENTER EnterCriticalSection(&critical_section) -#define NMEM_LEAVE LeaveCriticalSection(&critical_section) -struct nmem_mutex { - CRITICAL_SECTION m_handle; -}; -#elif YAZ_POSIX_THREADS -static pthread_mutex_t nmem_mutex = PTHREAD_MUTEX_INITIALIZER; -#define NMEM_ENTER pthread_mutex_lock(&nmem_mutex); -#define NMEM_LEAVE pthread_mutex_unlock(&nmem_mutex); -struct nmem_mutex { - pthread_mutex_t m_handle; -}; -#elif YAZ_GNU_THREADS -static pth_mutex_t nmem_mutex = PTH_MUTEX_INIT; -#define NMEM_ENTER pth_mutex_acquire(&nmem_mutex, 0, 0) -#define NMEM_LEAVE pth_mutex_release(&nmem_mutex) -struct nmem_mutex { - pth_mutex_t m_handle; -}; -#else -#define NMEM_ENTER -#define NMEM_LEAVE -struct nmem_mutex { - int dummy; -}; -#endif - -size_t nmem_memory_in_use = 0; -size_t nmem_memory_free = 0; - -YAZ_EXPORT void nmem_mutex_create(NMEM_MUTEX *p) -{ - NMEM_ENTER; - if (!*p) - { - *p = (NMEM_MUTEX) malloc(sizeof(**p)); -#ifdef WIN32 - InitializeCriticalSection(&(*p)->m_handle); -#elif YAZ_POSIX_THREADS - pthread_mutex_init(&(*p)->m_handle, 0); -#elif YAZ_GNU_THREADS - pth_mutex_init(&(*p)->m_handle); -#endif - } - NMEM_LEAVE; - if (!log_level_initialized) - { - log_level_initialized = 1; - log_level = yaz_log_module_level("nmem"); - } - -} - -YAZ_EXPORT void nmem_mutex_enter(NMEM_MUTEX p) -{ - if (p) - { -#ifdef WIN32 - EnterCriticalSection(&p->m_handle); -#elif YAZ_POSIX_THREADS - pthread_mutex_lock(&p->m_handle); -#endif - } -} - -YAZ_EXPORT void nmem_mutex_leave(NMEM_MUTEX p) -{ - if (p) - { -#ifdef WIN32 - LeaveCriticalSection(&p->m_handle); -#elif YAZ_POSIX_THREADS - pthread_mutex_unlock(&p->m_handle); -#endif - } -} - -YAZ_EXPORT void nmem_mutex_destroy(NMEM_MUTEX *p) -{ - if (*p) - { -#ifdef WIN32 - DeleteCriticalSection(&(*p)->m_handle); -#endif - free(*p); - *p = 0; - } -} - -/** \brief free NMEM memory blocks . Reused in get_block */ -static struct nmem_block *freelist = NULL; - -/** \brief free NMEM control blocks. Reused in nmem_create */ -static struct nmem_control *cfreelist = NULL; - -/** \brief number NMEM's in use (number of nmem_controls not in free list) */ -static int nmem_active_no = 0; - -/** \brief NMEM usage counter */ -static int nmem_init_flag = 0; - -/** \brief whether nmem blocks should be reassigned to heap */ -static int nmem_release_in_heap = 0; - -#if NMEM_DEBUG -struct nmem_debug_info { - void *p; - char file[40]; - int line; - struct nmem_debug_info *next; -}; - -struct nmem_debug_info *nmem_debug_list = 0; -#endif - static void free_block(struct nmem_block *p) { - nmem_memory_in_use -= p->size; - if (nmem_release_in_heap) - { - xfree(p->buf); - xfree(p); - } - else - { - memset(p->buf, 'Y', p->size); - p->next = freelist; - freelist = p; - nmem_memory_free += p->size; - } + xfree(p->buf); + xfree(p); if (log_level) yaz_log (log_level, "nmem free_block p=%p", p); } -#if NMEM_DEBUG -void nmem_print_list (void) -{ - if (log_level) - nmem_print_list_l(log_level); -} - -void nmem_print_list_l (int level) -{ - struct nmem_debug_info *p; - - yaz_log (level, "nmem print list"); - NMEM_ENTER; - for (p = nmem_debug_list; p; p = p->next) - yaz_log (level, " %s:%d p=%p size=%d", p->file, p->line, p->p, - nmem_total(p->p)); - NMEM_LEAVE; -} -#endif /* * acquire a block with a minimum of size free bytes. */ static struct nmem_block *get_block(size_t size) { - struct nmem_block *r, *l; + struct nmem_block *r; + size_t get = NMEM_CHUNK; if (log_level) yaz_log (log_level, "nmem get_block size=%ld", (long) size); - for (r = freelist, l = 0; r; l = r, r = r->next) - if (r->size >= size) - break; - if (r) - { - if (log_level) - yaz_log (log_level, "nmem get_block found free block p=%p", r); - if (l) - l->next = r->next; - else - freelist = r->next; - nmem_memory_free -= r->size; - } - else - { - size_t get = NMEM_CHUNK; - - if (get < size) - get = size; - if(log_level) - yaz_log (log_level, "nmem get_block alloc new block size=%ld", - (long) get); - - r = (struct nmem_block *) xmalloc(sizeof(*r)); - r->buf = (char *)xmalloc(r->size = get); - } - nmem_memory_in_use += r->size; + + if (get < size) + get = size; + if(log_level) + yaz_log (log_level, "nmem get_block alloc new block size=%ld", + (long) get); + + r = (struct nmem_block *) xmalloc(sizeof(*r)); + r->buf = (char *)xmalloc(r->size = get); r->top = 0; return r; } @@ -280,7 +108,6 @@ void nmem_reset(NMEM n) yaz_log (log_level, "nmem_reset p=%p", n); if (!n) return; - NMEM_ENTER; while (n->blocks) { t = n->blocks; @@ -288,32 +115,18 @@ void nmem_reset(NMEM n) free_block(t); } n->total = 0; - NMEM_LEAVE; } -#if NMEM_DEBUG -void *nmem_malloc_f (const char *file, int line, NMEM n, int size) -#else void *nmem_malloc(NMEM n, int size) -#endif { struct nmem_block *p; char *r; -#if NMEM_DEBUG - if (log_level) - yaz_log (log_level, "%s:%d: nmem_malloc p=%p size=%d", - file, line, n, size); -#endif if (!n) { yaz_log (YLOG_FATAL, "calling nmem_malloc with an null pointer"); abort (); } -#ifdef WIN32 - assert (nmem_init_flag); -#endif - NMEM_ENTER; p = n->blocks; if (!p || p->size < size + p->top) { @@ -325,7 +138,6 @@ void *nmem_malloc(NMEM n, int size) /* align size */ p->top += (size + (NMEM_ALIGN - 1)) & ~(NMEM_ALIGN - 1); n->total += size; - NMEM_LEAVE; return r; } @@ -334,107 +146,31 @@ int nmem_total(NMEM n) return n->total; } -#if NMEM_DEBUG -NMEM nmem_create_f(const char *file, int line) -#else NMEM nmem_create(void) -#endif { NMEM r; -#if NMEM_DEBUG - struct nmem_debug_info *debug_p; -#endif if (!log_level_initialized) { log_level = yaz_log_module_level("nmem"); log_level_initialized = 1; } - NMEM_ENTER; - nmem_active_no++; - r = cfreelist; - if (r) - cfreelist = cfreelist->next; - else - r = (struct nmem_control *)xmalloc(sizeof(*r)); - NMEM_LEAVE; + r = (struct nmem_control *)xmalloc(sizeof(*r)); -#if NMEM_DEBUG - yaz_log (YLOG_DEBUG, "%s:%d: nmem_create %d p=%p", file, line, - nmem_active_no, r); -#endif r->blocks = 0; r->total = 0; r->next = 0; -#if NMEM_DEBUG - for (debug_p = nmem_debug_list; debug_p; debug_p = debug_p->next) - if (debug_p->p == r) - { - yaz_log (YLOG_FATAL, "multi used block in nmem"); - abort (); - } - debug_p = xmalloc (sizeof(*debug_p)); - strncpy (debug_p->file, file, sizeof(debug_p->file)-1); - debug_p->file[sizeof(debug_p->file)-1] = '\0'; - debug_p->line = line; - debug_p->p = r; - debug_p->next = nmem_debug_list; - nmem_debug_list = debug_p; - - nmem_print_list(); -#endif return r; } -#if NMEM_DEBUG -void nmem_destroy_f(const char *file, int line, NMEM n) -#else void nmem_destroy(NMEM n) -#endif { -#if NMEM_DEBUG - struct nmem_debug_info **debug_p; - int ok = 0; -#endif if (!n) return; -#if NMEM_DEBUG - yaz_log (log_level, "%s:%d: nmem_destroy %d p=%p", file, line, - nmem_active_no-1, n); - NMEM_ENTER; - for (debug_p = &nmem_debug_list; *debug_p; debug_p = &(*debug_p)->next) - if ((*debug_p)->p == n) - { - struct nmem_debug_info *debug_save = *debug_p; - *debug_p = (*debug_p)->next; - xfree (debug_save); - ok = 1; - break; - } - NMEM_LEAVE; - nmem_print_list(); - if (!ok) - { - yaz_log (YLOG_WARN, "%s:%d destroying unallocated nmem block p=%p", - file, line, n); - return; - } -#endif nmem_reset(n); - NMEM_ENTER; - nmem_active_no--; - if (nmem_release_in_heap) - { - xfree(n); - } - else - { - n->next = cfreelist; - cfreelist = n; - } - NMEM_LEAVE; + xfree(n); } void nmem_transfer (NMEM dst, NMEM src) @@ -450,92 +186,6 @@ void nmem_transfer (NMEM dst, NMEM src) src->total = 0; } -void nmem_get_memory_in_use(size_t *p) -{ - NMEM_ENTER; - *p = nmem_memory_in_use; - NMEM_LEAVE; -} - -void nmem_get_memory_free(size_t *p) -{ - NMEM_ENTER; - *p = nmem_memory_free; - NMEM_LEAVE; -} - -void nmem_critical_enter (void) -{ - NMEM_ENTER; -} - -void nmem_critical_leave (void) -{ - NMEM_LEAVE; -} - -void nmem_init (void) -{ - if (++nmem_init_flag == 1) - { -#ifdef WIN32 - InitializeCriticalSection(&critical_section); -#elif YAZ_GNU_THREADS - pth_init (); -#endif - nmem_active_no = 0; - freelist = NULL; - cfreelist = NULL; - } - if (!log_level_initialized) - { - log_level = yaz_log_module_level("nmem"); - log_level_initialized = 1; - } -} - -void nmem_exit (void) -{ - if (--nmem_init_flag == 0) - { - while (freelist) - { - struct nmem_block *fl = freelist; - nmem_memory_free -= fl->size; - freelist = freelist->next; - xfree (fl->buf); - xfree (fl); - } - while (cfreelist) - { - struct nmem_control *cfl = cfreelist; - cfreelist = cfreelist->next; - xfree (cfl); - } -#ifdef WIN32 - DeleteCriticalSection(&critical_section); -#endif - } -} - - -#ifdef WIN32 -BOOL WINAPI DllMain (HINSTANCE hinstDLL, - DWORD reason, - LPVOID reserved) -{ - switch (reason) - { - case DLL_PROCESS_ATTACH: - nmem_init (); - break; - case DLL_PROCESS_DETACH: - nmem_exit (); - } - return TRUE; -} -#endif - int yaz_errno(void) { return errno; @@ -576,7 +226,6 @@ void yaz_strerror(char *buf, int max) #else /* UNIX */ #if HAVE_STRERROR_R -#if YAZ_POSIX_THREADS *buf = '\0'; strerror_r(errno, buf, max); /* if buffer is unset - use strerror anyway (GLIBC bug) */ @@ -585,9 +234,6 @@ void yaz_strerror(char *buf, int max) #else strcpy(buf, strerror(yaz_errno())); #endif -#else - strcpy(buf, strerror(yaz_errno())); -#endif /* UNIX */ #endif if ((cp = strrchr(buf, '\n'))) diff --git a/test/tstnmem.c b/test/tstnmem.c index 930d18c..4d51ed7 100644 --- a/test/tstnmem.c +++ b/test/tstnmem.c @@ -2,7 +2,7 @@ * Copyright (C) 1995-2007, Index Data ApS * See the file LICENSE for details. * - * $Id: tstnmem.c,v 1.7 2007-01-03 08:42:16 adam Exp $ + * $Id: tstnmem.c,v 1.8 2007-04-17 20:26:18 adam Exp $ */ #if HAVE_CONFIG_H @@ -23,7 +23,6 @@ void tst(void) int j; char *cp; - nmem_init(); n = nmem_create(); YAZ_CHECK(n); @@ -47,7 +46,6 @@ void tst(void) YAZ_CHECK(cp); } nmem_destroy(n); - nmem_exit(); } int main (int argc, char **argv) diff --git a/util/marcdump.c b/util/marcdump.c index 967fdd7..4a5ac25 100644 --- a/util/marcdump.c +++ b/util/marcdump.c @@ -2,7 +2,7 @@ * Copyright (C) 1995-2007, Index Data ApS * See the file LICENSE for details. * - * $Id: marcdump.c,v 1.51 2007-03-19 14:40:08 adam Exp $ + * $Id: marcdump.c,v 1.52 2007-04-17 20:26:18 adam Exp $ */ #define _FILE_OFFSET_BITS 64 @@ -328,7 +328,6 @@ int main (int argc, char **argv) const char *leader_spec = 0; int write_using_libxml2 = 0; - nmem_init(); #if HAVE_LOCALE_H setlocale(LC_CTYPE, ""); #endif @@ -443,7 +442,6 @@ int main (int argc, char **argv) } if (cfile) fclose (cfile); - nmem_exit(); if (!no) { usage(prog); diff --git a/util/srwtst.c b/util/srwtst.c index 232efda..f293620 100644 --- a/util/srwtst.c +++ b/util/srwtst.c @@ -2,7 +2,7 @@ * Copyright (C) 1995-2007, Index Data ApS * See the file LICENSE for details. * - * $Id: srwtst.c,v 1.6 2007-01-03 08:42:16 adam Exp $ + * $Id: srwtst.c,v 1.7 2007-04-17 20:26:18 adam Exp $ */ #include @@ -25,7 +25,6 @@ int main(int argc, char **argv) ODR decode, encode; int debug = 0; - nmem_init(); if (argc == 2 && !strcmp(argv[1], "debug")) debug = 1; no = fread(buf, 1, sizeof(buf), stdin); @@ -96,7 +95,6 @@ int main(int argc, char **argv) } odr_destroy(decode); odr_destroy(encode); - nmem_exit(); exit(0); } #else diff --git a/yaz-config.in b/yaz-config.in index d14e507..e57da01 100644 --- a/yaz-config.in +++ b/yaz-config.in @@ -1,5 +1,5 @@ #!/bin/sh -# $Id: yaz-config.in,v 1.27 2007-04-13 10:24:32 adam Exp $ +# $Id: yaz-config.in,v 1.28 2007-04-17 20:26:18 adam Exp $ yazprefix=@prefix@ yaz_echo_cflags=no yaz_echo_libs=no @@ -9,6 +9,8 @@ yaz_echo_lalibs=no yaz_echo_comp=no yaz_src_root=@YAZ_SRC_ROOT@ yaz_build_root=@YAZ_BUILD_ROOT@ +PTHREAD_LIBS="@PTHREAD_LIBS@" +PTHREAD_CFLAGS="@PTHREAD_CFLAGS@" yazextralibs="@LIBS@" YAZVERSION=@VERSION@ @@ -63,8 +65,9 @@ while test $# -gt 0; do threads) lib_thread=yes ;; - malloc) - lib_malloc=yes + server) + lib_thread=yes + lib_server=yes ;; -*) yaz_echo_help=yes @@ -74,30 +77,48 @@ while test $# -gt 0; do done lyaz="-lyaz" +if test "$lib_server" = "yes"; then + lyaz="-lyaz_server $lyaz" +fi YAZINC="@YAZ_CONFIG_CFLAGS@" if test "$yaz_echo_source" = "yes"; then YAZBIN=${yaz_build_root}/bin - YAZLIB="-L${yaz_build_root}/src/.libs $lyaz $yazextralibs" - YAZLALIB="${yaz_build_root}/src/libyaz.la $yazextralibs" + + YAZLIB="-L${yaz_build_root}/src/.libs $lyaz" + YAZLIB="$YAZLIB $yazextralibs" + + YAZLALIB="${yaz_build_root}/src/libyaz.la" + if test "$lib_server" = "yes"; then + YAZLALIB="${yaz_build_root}/src/libyaz_server.la $YAZLALIB" + fi + YAZLALIB="$YAZLALIB $yazextralibs" + YAZINC="-I${yaz_src_root}/include $YAZINC" YAZCOMP=${yaz_src_root}/util/yaz-asncomp else YAZBIN=${yazprefix}/bin YAZCOMP=${yazprefix}/bin/yaz-asncomp - if test "$yazprefix" = "/usr"; then - YAZLIB="$lyaz $yazextralibs" - else - YAZLIB="-L$yazprefix/lib $lyaz $yazextralibs" + if test "$yazprefix" != "/usr"; then + YAZLIB="-L$yazprefix/lib" fi + YAZLIB="$YAZLIB $lyaz $yazextralibs" YAZLALIB=$YAZLIB if test "$yazprefix" != "/usr"; then YAZINC="-I$yazprefix/include $YAZINC" fi fi +if test "$lib_thread" = "yes"; then + YAZINC="$PTHREAD_CFLAGS $YAZINC" +fi + +if test "$lib_thread" = "yes"; then + YAZLIB="$YAZLIB $PTHREAD_LIBS" +fi + if test "$yaz_echo_help" = "yes"; then usage 1 1>&2 fi diff --git a/zoom/zoomsh.c b/zoom/zoomsh.c index 0bbd70b..8b790f1 100644 --- a/zoom/zoomsh.c +++ b/zoom/zoomsh.c @@ -2,7 +2,7 @@ * Copyright (C) 1995-2007, Index Data ApS * See the file LICENSE for details. * - * $Id: zoomsh.c,v 1.46 2007-04-12 13:52:58 adam Exp $ + * $Id: zoomsh.c,v 1.47 2007-04-17 20:26:19 adam Exp $ */ /** \file zoomsh.c @@ -644,9 +644,7 @@ int main(int argc, char **argv) int mask = yaz_log_mask_str(maskstr); yaz_log_init_level(mask); } - nmem_init(); zoomsh(argc, argv); - nmem_exit(); exit (0); } /* diff --git a/zoom/zoomtst7.c b/zoom/zoomtst7.c index 7816bd0..235b470 100644 --- a/zoom/zoomtst7.c +++ b/zoom/zoomtst7.c @@ -1,4 +1,4 @@ -/* $Id: zoomtst7.c,v 1.18 2007-03-21 11:27:47 adam Exp $ */ +/* $Id: zoomtst7.c,v 1.19 2007-04-17 20:26:19 adam Exp $ */ /** \file zoomtst7.c \brief Mix of operations @@ -21,7 +21,6 @@ int main(int argc, char **argv) ZOOM_resultset r[10]; /* and result sets .. */ ZOOM_options o; - nmem_init (); o = ZOOM_options_create (); z = ZOOM_connection_new ("localhost", 9999); diff --git a/ztest/Makefile.am b/ztest/Makefile.am index c47aa8b..35e90d7 100644 --- a/ztest/Makefile.am +++ b/ztest/Makefile.am @@ -1,4 +1,4 @@ -## $Id: Makefile.am,v 1.21 2007-02-25 14:46:51 adam Exp $ +## $Id: Makefile.am,v 1.22 2007-04-17 20:26:19 adam Exp $ bin_PROGRAMS=yaz-ztest @@ -6,6 +6,6 @@ yaz_ztest_SOURCES=ztest.c read-grs.c read-marc.c EXTRA_DIST=dummy-records dummy-words dummy-grs ztest.pem config1.xml -yaz_ztest_LDADD=../src/libyaz.la +yaz_ztest_LDADD=../src/libyaz_server.la ../src/libyaz.la $(PTHREAD_LIBS) AM_CPPFLAGS=-I$(top_srcdir)/include $(XML2_CFLAGS) -- 1.7.10.4