Enhance ListDictonnary

This patch enhances ListDictionnary so that you can set callbacks for
keys and for values.
This commit is contained in:
Hardening 2014-04-18 18:26:04 +02:00
parent 6febe88026
commit ef11358668
4 changed files with 61 additions and 58 deletions

View File

@ -659,7 +659,7 @@ void drive_register_drive_path(PDEVICE_SERVICE_ENTRY_POINTS pEntryPoints, char*
drive->path = path;
drive->files = ListDictionary_New(TRUE);
ListDictionary_Object(drive->files)->fnObjectFree = (OBJECT_FREE_FN) drive_file_free;
ListDictionary_ValueObject(drive->files)->fnObjectFree = (OBJECT_FREE_FN) drive_file_free;
drive->IrpQueue = MessageQueue_New(NULL);
drive->thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) drive_thread_func, drive, CREATE_SUSPENDED, NULL);

View File

@ -55,7 +55,7 @@ DEVMAN* devman_new(rdpdrPlugin* rdpdr)
devman->devices = ListDictionary_New(TRUE);
ListDictionary_Object(devman->devices)->fnObjectFree =
ListDictionary_ValueObject(devman->devices)->fnObjectFree =
(OBJECT_FREE_FN) devman_device_free;
return devman;

View File

@ -192,18 +192,19 @@ struct _wListDictionary
CRITICAL_SECTION lock;
wListDictionaryItem* head;
wObject object;
wObject objectKey;
wObject objectValue;
};
typedef struct _wListDictionary wListDictionary;
#define ListDictionary_Object(_dictionary) (&_dictionary->object)
#define ListDictionary_ValueObject(_dictionary) (&_dictionary->objectValue)
WINPR_API int ListDictionary_Count(wListDictionary* listDictionary);
WINPR_API void ListDictionary_Lock(wListDictionary* listDictionary);
WINPR_API void ListDictionary_Unlock(wListDictionary* listDictionary);
WINPR_API void ListDictionary_Add(wListDictionary* listDictionary, void* key, void* value);
WINPR_API BOOL ListDictionary_Add(wListDictionary* listDictionary, void* key, void* value);
WINPR_API void* ListDictionary_Remove(wListDictionary* listDictionary, void* key);
WINPR_API void* ListDictionary_Remove_Head(wListDictionary* listDictionary);
WINPR_API void ListDictionary_Clear(wListDictionary* listDictionary);

View File

@ -172,7 +172,7 @@ int ListDictionary_GetKeys(wListDictionary* listDictionary, ULONG_PTR** ppKeys)
* Adds an entry with the specified key and value into the ListDictionary.
*/
void ListDictionary_Add(wListDictionary* listDictionary, void* key, void* value)
BOOL ListDictionary_Add(wListDictionary* listDictionary, void* key, void* value)
{
wListDictionaryItem* item;
wListDictionaryItem* lastItem;
@ -181,6 +181,8 @@ void ListDictionary_Add(wListDictionary* listDictionary, void* key, void* value)
EnterCriticalSection(&listDictionary->lock);
item = (wListDictionaryItem*) malloc(sizeof(wListDictionaryItem));
if (!item)
return FALSE;
item->key = key;
item->value = value;
@ -203,6 +205,7 @@ void ListDictionary_Add(wListDictionary* listDictionary, void* key, void* value)
if (listDictionary->synchronized)
LeaveCriticalSection(&listDictionary->lock);
return TRUE;
}
/**
@ -224,8 +227,8 @@ void ListDictionary_Clear(wListDictionary* listDictionary)
while (item)
{
nextItem = item->next;
if (listDictionary->object.fnObjectFree)
listDictionary->object.fnObjectFree(item->value);
if (listDictionary->objectValue.fnObjectFree)
listDictionary->objectValue.fnObjectFree(item->value);
free(item);
item = nextItem;
}
@ -243,31 +246,27 @@ void ListDictionary_Clear(wListDictionary* listDictionary)
BOOL ListDictionary_Contains(wListDictionary* listDictionary, void* key)
{
BOOL status = FALSE;
wListDictionaryItem* item;
OBJECT_EQUALS_FN keyEquals;
if (listDictionary->synchronized)
EnterCriticalSection(&listDictionary->lock);
if (listDictionary->head)
keyEquals = listDictionary->objectKey.fnObjectEquals;
item = listDictionary->head;
while (item)
{
item = listDictionary->head;
if (keyEquals(item->key, key))
break;
while (item)
{
if (item->key == key)
break;
item = item->next;
}
status = (item) ? TRUE : FALSE;
item = item->next;
}
if (listDictionary->synchronized)
LeaveCriticalSection(&listDictionary->lock);
return status;
return (item) ? TRUE : FALSE;
}
/**
@ -279,42 +278,31 @@ void* ListDictionary_Remove(wListDictionary* listDictionary, void* key)
void* value = NULL;
wListDictionaryItem* item;
wListDictionaryItem* prevItem;
OBJECT_EQUALS_FN keyEquals;
if (listDictionary->synchronized)
EnterCriticalSection(&listDictionary->lock);
if (listDictionary->head)
{
item = listDictionary->head;
keyEquals = listDictionary->objectKey.fnObjectEquals;
if (listDictionary->head->key == key)
item = listDictionary->head;
prevItem = NULL;
while (item)
{
if (keyEquals(item->key, key))
{
listDictionary->head = listDictionary->head->next;
if (!prevItem)
listDictionary->head = item->next;
else
prevItem->next = item->next;
value = item->value;
free(item);
break;
}
else
{
if (item->next)
{
prevItem = item;
item = item->next;
while (item)
{
if (item->key == key)
{
prevItem->next = item->next;
value = item->value;
free(item);
break;
}
prevItem = item;
item = item->next;
}
}
}
prevItem = item;
item = item->next;
}
if (listDictionary->synchronized)
@ -356,17 +344,20 @@ void* ListDictionary_GetItemValue(wListDictionary* listDictionary, void* key)
{
void* value = NULL;
wListDictionaryItem* item = NULL;
OBJECT_EQUALS_FN keyEquals;
if (listDictionary->synchronized)
EnterCriticalSection(&listDictionary->lock);
keyEquals = listDictionary->objectKey.fnObjectEquals;
if (listDictionary->head)
{
item = listDictionary->head;
while (item)
{
if (item->key == key)
if (keyEquals(item->key, key))
break;
item = item->next;
@ -389,24 +380,30 @@ BOOL ListDictionary_SetItemValue(wListDictionary* listDictionary, void* key, voi
{
BOOL status = FALSE;
wListDictionaryItem* item;
OBJECT_EQUALS_FN keyEquals;
if (listDictionary->synchronized)
EnterCriticalSection(&listDictionary->lock);
keyEquals = listDictionary->objectKey.fnObjectEquals;
if (listDictionary->head)
{
item = listDictionary->head;
while (item)
{
if (item->key == key)
if (keyEquals(item->key, key))
break;
item = item->next;
}
if (item)
{
if (listDictionary->objectValue.fnObjectFree)
listDictionary->objectValue.fnObjectFree(item->value);
item->value = value;
}
status = (item) ? TRUE : FALSE;
}
@ -417,6 +414,10 @@ BOOL ListDictionary_SetItemValue(wListDictionary* listDictionary, void* key, voi
return status;
}
static BOOL default_equal_function(void *obj1, void *obj2)
{
return (obj1 == obj2);
}
/**
* Construction, Destruction
*/
@ -425,19 +426,20 @@ wListDictionary* ListDictionary_New(BOOL synchronized)
{
wListDictionary* listDictionary = NULL;
listDictionary = (wListDictionary*) malloc(sizeof(wListDictionary));
listDictionary = (wListDictionary*) calloc(1, sizeof(wListDictionary));
if (!listDictionary)
return NULL;
if (listDictionary)
listDictionary->synchronized = synchronized;
if (!InitializeCriticalSectionAndSpinCount(&listDictionary->lock, 4000))
{
listDictionary->synchronized = synchronized;
listDictionary->head = NULL;
InitializeCriticalSectionAndSpinCount(&listDictionary->lock, 4000);
ZeroMemory(&(listDictionary->object), sizeof(wObject));
free(listDictionary);
return NULL;
}
listDictionary->objectKey.fnObjectEquals = default_equal_function;
listDictionary->objectValue.fnObjectEquals = default_equal_function;
return listDictionary;
}