Revert "Array-index based devPrivates implementation."
This reverts commit 8ef37c194f
.
This commit is contained in:
parent
ca659813a8
commit
b595b65e54
205
dix/privates.c
205
dix/privates.c
|
@ -40,8 +40,9 @@ from The Open Group.
|
|||
#include "inputstr.h"
|
||||
|
||||
struct _Private {
|
||||
int state;
|
||||
pointer value;
|
||||
DevPrivateKey key;
|
||||
pointer value;
|
||||
struct _Private *next;
|
||||
};
|
||||
|
||||
typedef struct _PrivateDesc {
|
||||
|
@ -49,36 +50,22 @@ typedef struct _PrivateDesc {
|
|||
unsigned size;
|
||||
CallbackListPtr initfuncs;
|
||||
CallbackListPtr deletefuncs;
|
||||
struct _PrivateDesc *next;
|
||||
} PrivateDescRec;
|
||||
|
||||
#define PRIV_MAX 256
|
||||
#define PRIV_STEP 16
|
||||
|
||||
/* list of all allocated privates */
|
||||
static PrivateDescRec items[PRIV_MAX];
|
||||
static int nextPriv;
|
||||
static PrivateDescRec *items = NULL;
|
||||
|
||||
static PrivateDescRec *
|
||||
static _X_INLINE PrivateDescRec *
|
||||
findItem(const DevPrivateKey key)
|
||||
{
|
||||
if (!*key) {
|
||||
if (nextPriv >= PRIV_MAX)
|
||||
return NULL;
|
||||
|
||||
items[nextPriv].key = key;
|
||||
*key = nextPriv;
|
||||
nextPriv++;
|
||||
PrivateDescRec *item = items;
|
||||
while (item) {
|
||||
if (item->key == key)
|
||||
return item;
|
||||
item = item->next;
|
||||
}
|
||||
|
||||
return items + *key;
|
||||
}
|
||||
|
||||
static _X_INLINE int
|
||||
privateExists(PrivateRec **privates, const DevPrivateKey key)
|
||||
{
|
||||
return *key && *privates &&
|
||||
(*privates)[0].state > *key &&
|
||||
(*privates)[*key].state;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -88,10 +75,21 @@ _X_EXPORT int
|
|||
dixRequestPrivate(const DevPrivateKey key, unsigned size)
|
||||
{
|
||||
PrivateDescRec *item = findItem(key);
|
||||
if (!item)
|
||||
return FALSE;
|
||||
if (size > item->size)
|
||||
if (item) {
|
||||
if (size > item->size)
|
||||
item->size = size;
|
||||
} else {
|
||||
item = (PrivateDescRec *)xalloc(sizeof(PrivateDescRec));
|
||||
if (!item)
|
||||
return FALSE;
|
||||
memset(item, 0, sizeof(PrivateDescRec));
|
||||
|
||||
/* add privates descriptor */
|
||||
item->key = key;
|
||||
item->size = size;
|
||||
item->next = items;
|
||||
items = item;
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
@ -102,52 +100,25 @@ _X_EXPORT pointer *
|
|||
dixAllocatePrivate(PrivateRec **privates, const DevPrivateKey key)
|
||||
{
|
||||
PrivateDescRec *item = findItem(key);
|
||||
PrivateCallbackRec calldata;
|
||||
PrivateRec *ptr;
|
||||
pointer value;
|
||||
int oldsize, newsize;
|
||||
unsigned size = sizeof(PrivateRec);
|
||||
|
||||
if (item)
|
||||
size += item->size;
|
||||
|
||||
newsize = (*key / PRIV_STEP + 1) * PRIV_STEP;
|
||||
|
||||
/* resize or init privates array */
|
||||
if (!item)
|
||||
ptr = (PrivateRec *)xcalloc(size, 1);
|
||||
if (!ptr)
|
||||
return NULL;
|
||||
ptr->key = key;
|
||||
ptr->value = (size > sizeof(PrivateRec)) ? (ptr + 1) : NULL;
|
||||
ptr->next = *privates;
|
||||
*privates = ptr;
|
||||
|
||||
/* initialize privates array if necessary */
|
||||
if (!*privates) {
|
||||
ptr = xcalloc(newsize, sizeof(*ptr));
|
||||
if (!ptr)
|
||||
return NULL;
|
||||
*privates = ptr;
|
||||
(*privates)[0].state = newsize;
|
||||
/* call any init funcs and return */
|
||||
if (item) {
|
||||
PrivateCallbackRec calldata = { key, &ptr->value };
|
||||
CallCallbacks(&item->initfuncs, &calldata);
|
||||
}
|
||||
|
||||
oldsize = (*privates)[0].state;
|
||||
|
||||
/* resize privates array if necessary */
|
||||
if (*key >= oldsize) {
|
||||
ptr = xrealloc(*privates, newsize * sizeof(*ptr));
|
||||
if (!ptr)
|
||||
return NULL;
|
||||
memset(ptr + oldsize, 0, (newsize - oldsize) * sizeof(*ptr));
|
||||
*privates = ptr;
|
||||
(*privates)[0].state = newsize;
|
||||
}
|
||||
|
||||
/* initialize slot */
|
||||
ptr = *privates + *key;
|
||||
ptr->state = 1;
|
||||
if (item->size) {
|
||||
value = xcalloc(item->size, 1);
|
||||
if (!value)
|
||||
return NULL;
|
||||
ptr->value = value;
|
||||
}
|
||||
|
||||
calldata.key = key;
|
||||
calldata.value = &ptr->value;
|
||||
CallCallbacks(&item->initfuncs, &calldata);
|
||||
|
||||
return &ptr->value;
|
||||
}
|
||||
|
||||
|
@ -157,10 +128,14 @@ dixAllocatePrivate(PrivateRec **privates, const DevPrivateKey key)
|
|||
_X_EXPORT pointer
|
||||
dixLookupPrivate(PrivateRec **privates, const DevPrivateKey key)
|
||||
{
|
||||
PrivateRec *rec = *privates;
|
||||
pointer *ptr;
|
||||
|
||||
if (privateExists(privates, key))
|
||||
return (*privates)[*key].value;
|
||||
while (rec) {
|
||||
if (rec->key == key)
|
||||
return rec->value;
|
||||
rec = rec->next;
|
||||
}
|
||||
|
||||
ptr = dixAllocatePrivate(privates, key);
|
||||
return ptr ? *ptr : NULL;
|
||||
|
@ -172,8 +147,13 @@ dixLookupPrivate(PrivateRec **privates, const DevPrivateKey key)
|
|||
_X_EXPORT pointer *
|
||||
dixLookupPrivateAddr(PrivateRec **privates, const DevPrivateKey key)
|
||||
{
|
||||
if (privateExists(privates, key))
|
||||
return &(*privates)[*key].value;
|
||||
PrivateRec *rec = *privates;
|
||||
|
||||
while (rec) {
|
||||
if (rec->key == key)
|
||||
return &rec->value;
|
||||
rec = rec->next;
|
||||
}
|
||||
|
||||
return dixAllocatePrivate(privates, key);
|
||||
}
|
||||
|
@ -184,10 +164,16 @@ dixLookupPrivateAddr(PrivateRec **privates, const DevPrivateKey key)
|
|||
_X_EXPORT int
|
||||
dixSetPrivate(PrivateRec **privates, const DevPrivateKey key, pointer val)
|
||||
{
|
||||
PrivateRec *rec;
|
||||
|
||||
top:
|
||||
if (privateExists(privates, key)) {
|
||||
(*privates)[*key].value = val;
|
||||
return TRUE;
|
||||
rec = *privates;
|
||||
while (rec) {
|
||||
if (rec->key == key) {
|
||||
rec->value = val;
|
||||
return TRUE;
|
||||
}
|
||||
rec = rec->next;
|
||||
}
|
||||
|
||||
if (!dixAllocatePrivate(privates, key))
|
||||
|
@ -201,23 +187,27 @@ dixSetPrivate(PrivateRec **privates, const DevPrivateKey key, pointer val)
|
|||
_X_EXPORT void
|
||||
dixFreePrivates(PrivateRec *privates)
|
||||
{
|
||||
int i;
|
||||
PrivateRec *ptr, *next;
|
||||
PrivateDescRec *item;
|
||||
PrivateCallbackRec calldata;
|
||||
|
||||
if (privates)
|
||||
for (i = 1; i < privates->state; i++)
|
||||
if (privates[i].state) {
|
||||
/* call the delete callbacks */
|
||||
calldata.key = items[i].key;
|
||||
calldata.value = &privates[i].value;
|
||||
CallCallbacks(&items[i].deletefuncs, &calldata);
|
||||
|
||||
/* free pre-allocated memory */
|
||||
if (items[i].size)
|
||||
xfree(privates[i].value);
|
||||
}
|
||||
|
||||
xfree(privates);
|
||||
/* first pass calls the delete callbacks */
|
||||
for (ptr = privates; ptr; ptr = ptr->next) {
|
||||
item = findItem(ptr->key);
|
||||
if (item) {
|
||||
calldata.key = ptr->key;
|
||||
calldata.value = &ptr->value;
|
||||
CallCallbacks(&item->deletefuncs, &calldata);
|
||||
}
|
||||
}
|
||||
|
||||
/* second pass frees the memory */
|
||||
ptr = privates;
|
||||
while (ptr) {
|
||||
next = ptr->next;
|
||||
xfree(ptr);
|
||||
ptr = next;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -228,9 +218,11 @@ dixRegisterPrivateInitFunc(const DevPrivateKey key,
|
|||
CallbackProcPtr callback, pointer data)
|
||||
{
|
||||
PrivateDescRec *item = findItem(key);
|
||||
if (!item)
|
||||
return FALSE;
|
||||
|
||||
if (!item) {
|
||||
if (!dixRequestPrivate(key, 0))
|
||||
return FALSE;
|
||||
item = findItem(key);
|
||||
}
|
||||
return AddCallback(&item->initfuncs, callback, data);
|
||||
}
|
||||
|
||||
|
@ -239,9 +231,11 @@ dixRegisterPrivateDeleteFunc(const DevPrivateKey key,
|
|||
CallbackProcPtr callback, pointer data)
|
||||
{
|
||||
PrivateDescRec *item = findItem(key);
|
||||
if (!item)
|
||||
return FALSE;
|
||||
|
||||
if (!item) {
|
||||
if (!dixRequestPrivate(key, 0))
|
||||
return FALSE;
|
||||
item = findItem(key);
|
||||
}
|
||||
return AddCallback(&item->deletefuncs, callback, data);
|
||||
}
|
||||
|
||||
|
@ -298,17 +292,16 @@ dixLookupPrivateOffset(RESTYPE type)
|
|||
int
|
||||
dixResetPrivates(void)
|
||||
{
|
||||
int i;
|
||||
PrivateDescRec *next;
|
||||
|
||||
/* reset private descriptors */
|
||||
for (i = 1; i < nextPriv; i++) {
|
||||
*items[i].key = 0;
|
||||
DeleteCallbackList(&items[i].initfuncs);
|
||||
DeleteCallbackList(&items[i].deletefuncs);
|
||||
/* reset internal structures */
|
||||
while (items) {
|
||||
next = items->next;
|
||||
DeleteCallbackList(&items->initfuncs);
|
||||
DeleteCallbackList(&items->deletefuncs);
|
||||
xfree(items);
|
||||
items = next;
|
||||
}
|
||||
nextPriv = 1;
|
||||
|
||||
/* reset offsets */
|
||||
if (offsets)
|
||||
xfree(offsets);
|
||||
offsetsSize = sizeof(offsetDefaults);
|
||||
|
|
|
@ -19,7 +19,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|||
* STUFF FOR PRIVATES
|
||||
*****************************************************************/
|
||||
|
||||
typedef int *DevPrivateKey;
|
||||
typedef void *DevPrivateKey;
|
||||
struct _Private;
|
||||
typedef struct _Private PrivateRec;
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user