Skip to content
Snippets Groups Projects
Commit fa384961 authored by Thomas Jahns's avatar Thomas Jahns :cartwheel: Committed by Sergey Kosukhin
Browse files

Make table of namespace switches dynamic.

parent e6d18b05
No related branches found
No related tags found
2 merge requests!34Version 2.2.0,!13Consolidation with CDI-PIO (develop)
......@@ -69,11 +69,14 @@ enum namespaceStatus
NAMESPACE_STATUS_UNUSED,
};
static union namespaceSwitchValue initialSwitches[NUM_NAMESPACE_SWITCH] = defaultSwitches;
static struct Namespace
{
enum namespaceStatus resStage;
union namespaceSwitchValue switches[NUM_NAMESPACE_SWITCH];
} initialNamespace = { .resStage = NAMESPACE_STATUS_INUSE, .switches = defaultSwitches };
unsigned numSwitches;
union namespaceSwitchValue *switches;
} initialNamespace = { .resStage = NAMESPACE_STATUS_INUSE, .numSwitches = NUM_NAMESPACE_SWITCH, .switches = initialSwitches };
static struct Namespace *namespaces = &initialNamespace;
......@@ -191,11 +194,16 @@ namespaceNew()
xassert(newNamespaceID >= 0 && newNamespaceID < NUM_NAMESPACES);
++nNamespaces;
namespaces[newNamespaceID].resStage = NAMESPACE_STATUS_INUSE;
namespaces[newNamespaceID].numSwitches = NUM_NAMESPACE_SWITCH;
enum
{
initialNSSWSize = sizeof(union namespaceSwitchValue) * NUM_NAMESPACE_SWITCH
};
namespaces[newNamespaceID].switches = (union namespaceSwitchValue *) Malloc(initialNSSWSize);
#if defined(SX) || defined(__cplusplus)
memcpy(namespaces[newNamespaceID].switches, defaultSwitches_, sizeof(namespaces[newNamespaceID].switches));
memcpy(namespaces[newNamespaceID].switches, defaultSwitches_, initialNSSWSize);
#else
memcpy(namespaces[newNamespaceID].switches, (union namespaceSwitchValue[NUM_NAMESPACE_SWITCH]) defaultSwitches,
sizeof(namespaces[newNamespaceID].switches));
memcpy(namespaces[newNamespaceID].switches, (union namespaceSwitchValue[NUM_NAMESPACE_SWITCH]) defaultSwitches, initialNSSWSize);
#endif
reshListCreate(newNamespaceID);
NAMESPACE_UNLOCK();
......@@ -209,6 +217,7 @@ namespaceDelete(int namespaceID)
NAMESPACE_LOCK();
xassert(namespaceID >= 0 && (unsigned) namespaceID < namespacesSize && nNamespaces);
reshListDestruct(namespaceID);
if (namespaces[namespaceID].switches != initialSwitches) Free(namespaces[namespaceID].switches);
namespaces[namespaceID].resStage = NAMESPACE_STATUS_UNUSED;
--nNamespaces;
NAMESPACE_UNLOCK();
......@@ -268,19 +277,53 @@ namespaceAdaptKey2(int originResH)
}
void
namespaceSwitchSet(enum namespaceSwitch sw, union namespaceSwitchValue value)
namespaceSwitchSet(int sw, union namespaceSwitchValue value)
{
xassert(sw > NSSWITCH_NO_SUCH_SWITCH && sw < NUM_NAMESPACE_SWITCH);
xassert(sw > NSSWITCH_NO_SUCH_SWITCH);
int nsp = namespaceGetActive();
NAMESPACE_LOCK();
if (namespaces[nsp].numSwitches <= (unsigned) sw)
{
if (namespaces[nsp].switches != initialSwitches)
namespaces[nsp].switches
= (union namespaceSwitchValue *) Realloc(namespaces[nsp].switches, ((unsigned) sw + 1) * sizeof value);
else
{
void *temp = Malloc(((unsigned) sw + 1) * sizeof value);
memcpy(temp, (void *) namespaces[nsp].switches, namespaces[nsp].numSwitches * sizeof value);
namespaces[nsp].switches = (union namespaceSwitchValue *) temp;
}
namespaces[nsp].numSwitches = (unsigned) sw + 1;
}
namespaces[nsp].switches[sw] = value;
NAMESPACE_UNLOCK();
}
union namespaceSwitchValue
namespaceSwitchGet(enum namespaceSwitch sw)
namespaceSwitchGet(int sw)
{
xassert(sw > NSSWITCH_NO_SUCH_SWITCH && sw < NUM_NAMESPACE_SWITCH);
int nsp = namespaceGetActive();
return namespaces[nsp].switches[sw];
xassert(sw > NSSWITCH_NO_SUCH_SWITCH && (unsigned) sw < namespaces[nsp].numSwitches);
NAMESPACE_LOCK();
union namespaceSwitchValue sw_val = namespaces[nsp].switches[sw];
NAMESPACE_UNLOCK();
return sw_val;
}
int
cdiNamespaceSwitchNewKey(void)
{
static unsigned reservedKeys = 0;
#if defined(HAVE_LIBPTHREAD)
static pthread_mutex_t keyMutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_lock(&keyMutex);
#endif
if (reservedKeys >= INT_MAX - NUM_NAMESPACE_SWITCH - 1) Error("pool of available namespace switch keys exhausted!");
int newKey = (int) (reservedKeys++) + NUM_NAMESPACE_SWITCH;
#if defined(HAVE_LIBPTHREAD)
pthread_mutex_unlock(&keyMutex);
#endif
return newKey;
}
void
......
......@@ -61,8 +61,10 @@ int namespaceIdxEncode2(int, int);
namespaceTuple_t namespaceResHDecode(int);
int namespaceAdaptKey(int originResH, int originNamespace);
int namespaceAdaptKey2(int);
void namespaceSwitchSet(enum namespaceSwitch sw, union namespaceSwitchValue value);
union namespaceSwitchValue namespaceSwitchGet(enum namespaceSwitch sw);
void namespaceSwitchSet(int sw, union namespaceSwitchValue value);
union namespaceSwitchValue namespaceSwitchGet(int sw);
/* reserve new dynamic key */
int cdiNamespaceSwitchNewKey(void);
#endif
/*
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment