Commit 28da89eb authored by Thomas Jahns's avatar Thomas Jahns 🤸
Browse files

Fix resource deallocation.

parent bb31da8a
#if defined (HAVE_CONFIG_H)
# include "config.h"
#endif
#ifndef _XOPEN_SOURCE
#define _XOPEN_SOURCE 600 /* PTHREAD_MUTEX_RECURSIVE */
#endif
#include <limits.h> #include <limits.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include "cdi.h" #include "cdi.h"
#include "dmemory.h" #include "dmemory.h"
#include "namespace.h" #include "namespace.h"
...@@ -63,13 +72,28 @@ static int namespacesSize = 1; ...@@ -63,13 +72,28 @@ static int namespacesSize = 1;
#if defined (HAVE_LIBPTHREAD) #if defined (HAVE_LIBPTHREAD)
# include <pthread.h> # include <pthread.h>
static pthread_mutex_t namespaceMutex = PTHREAD_MUTEX_INITIALIZER; static pthread_once_t namespaceOnce = PTHREAD_ONCE_INIT;
static pthread_mutex_t namespaceMutex;
static void
namespaceInitialize(void)
{
pthread_mutexattr_t ma;
pthread_mutexattr_init(&ma);
pthread_mutexattr_settype(&ma, PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(&namespaceMutex, &ma);
pthread_mutexattr_destroy(&ma);
}
# define NAMESPACE_LOCK() pthread_mutex_lock(&namespaceMutex) # define NAMESPACE_LOCK() pthread_mutex_lock(&namespaceMutex)
# define NAMESPACE_UNLOCK() pthread_mutex_unlock(&namespaceMutex) # define NAMESPACE_UNLOCK() pthread_mutex_unlock(&namespaceMutex)
# define NAMESPACE_INIT() pthread_once(&namespaceOnce, \
namespaceInitialize)
#else #else
# define NAMESPACE_INIT() do { } while (0)
# define NAMESPACE_LOCK() # define NAMESPACE_LOCK()
# define NAMESPACE_UNLOCK() # define NAMESPACE_UNLOCK()
...@@ -117,6 +141,7 @@ int ...@@ -117,6 +141,7 @@ int
namespaceNew() namespaceNew()
{ {
int newNamespaceID = -1; int newNamespaceID = -1;
NAMESPACE_INIT();
NAMESPACE_LOCK(); NAMESPACE_LOCK();
if (namespacesSize > nNamespaces) if (namespacesSize > nNamespaces)
{ {
...@@ -165,6 +190,7 @@ namespaceNew() ...@@ -165,6 +190,7 @@ namespaceNew()
void void
namespaceDelete(int namespaceID) namespaceDelete(int namespaceID)
{ {
NAMESPACE_INIT();
NAMESPACE_LOCK(); NAMESPACE_LOCK();
xassert(namespaceID < namespacesSize && nNamespaces); xassert(namespaceID < namespacesSize && nNamespaces);
reshListDestruct(namespaceID); reshListDestruct(namespaceID);
...@@ -173,18 +199,6 @@ namespaceDelete(int namespaceID) ...@@ -173,18 +199,6 @@ namespaceDelete(int namespaceID)
NAMESPACE_UNLOCK(); NAMESPACE_UNLOCK();
} }
void namespaceCleanup ( void )
{
if ( nNamespaces > 1 )
{
initialNamespace = namespaces[0];
free(namespaces);
namespaces = &initialNamespace;
nNamespaces = 1;
}
}
int namespaceGetNumber () int namespaceGetNumber ()
{ {
return nNamespaces; return nNamespaces;
...@@ -267,10 +281,16 @@ union namespaceSwitchValue namespaceSwitchGet(enum namespaceSwitch sw) ...@@ -267,10 +281,16 @@ union namespaceSwitchValue namespaceSwitchGet(enum namespaceSwitch sw)
void cdiReset(void) void cdiReset(void)
{ {
NAMESPACE_INIT();
NAMESPACE_LOCK(); NAMESPACE_LOCK();
for (int namespaceID = 0; namespaceID < namespacesSize; ++namespaceID) for (int namespaceID = 0; namespaceID < namespacesSize; ++namespaceID)
if (namespaces[namespaceID].resStage != STAGE_UNUSED)
namespaceDelete(namespaceID); namespaceDelete(namespaceID);
if (namespaces != &initialNamespace)
{
free(namespaces);
namespaces = &initialNamespace; namespaces = &initialNamespace;
}
namespacesSize = 1; namespacesSize = 1;
nNamespaces = 1; nNamespaces = 1;
activeNamespace = 0; activeNamespace = 0;
......
...@@ -150,6 +150,7 @@ static void listDestroy ( void ) ...@@ -150,6 +150,7 @@ static void listDestroy ( void )
namespaceDelete(i); namespaceDelete(i);
free(resHList); free(resHList);
resHList = NULL; resHList = NULL;
cdiReset();
LIST_UNLOCK(); LIST_UNLOCK();
} }
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment