2013-01-18 22:50:35 +04:00
|
|
|
/**
|
|
|
|
* WinPR: Windows Portable Runtime
|
|
|
|
* Reference Count Table
|
|
|
|
*
|
|
|
|
* Copyright 2012 Marc-Andre Moreau <marcandre.moreau@gmail.com>
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
#include <unistd.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <winpr/crt.h>
|
|
|
|
|
|
|
|
#include <winpr/collections.h>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* C reference counting
|
|
|
|
* http://msdn.microsoft.com/en-us/library/windows/desktop/ms693431/
|
|
|
|
*/
|
|
|
|
|
|
|
|
wReference* ReferenceTable_FindEntry(wReferenceTable* referenceTable, void* ptr)
|
|
|
|
{
|
2014-02-11 07:12:13 +04:00
|
|
|
UINT32 index = 0;
|
2013-01-18 22:50:35 +04:00
|
|
|
BOOL found = FALSE;
|
|
|
|
wReference* reference = NULL;
|
|
|
|
|
|
|
|
for (index = 0; index < referenceTable->size; index++)
|
|
|
|
{
|
|
|
|
reference = &referenceTable->array[index];
|
|
|
|
|
|
|
|
if (reference->Pointer == ptr)
|
|
|
|
found = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (found) ? reference : NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
wReference* ReferenceTable_GetFreeEntry(wReferenceTable* referenceTable)
|
|
|
|
{
|
2014-02-11 07:12:13 +04:00
|
|
|
UINT32 index = 0;
|
2013-01-18 22:50:35 +04:00
|
|
|
BOOL found = FALSE;
|
|
|
|
wReference* reference = NULL;
|
|
|
|
|
|
|
|
for (index = 0; index < referenceTable->size; index++)
|
|
|
|
{
|
|
|
|
reference = &referenceTable->array[index];
|
|
|
|
|
|
|
|
if (reference->Pointer == NULL)
|
|
|
|
{
|
|
|
|
reference->Count = 0;
|
|
|
|
found = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!found)
|
|
|
|
{
|
2015-03-11 09:59:56 +03:00
|
|
|
UINT32 new_size;
|
|
|
|
wReference *new_ref;
|
|
|
|
|
2013-08-29 17:30:22 +04:00
|
|
|
if (!referenceTable->size)
|
2013-09-02 11:12:33 +04:00
|
|
|
{
|
|
|
|
if (referenceTable->array)
|
|
|
|
free(referenceTable->array);
|
|
|
|
referenceTable->array = NULL;
|
2013-08-29 17:30:22 +04:00
|
|
|
return NULL;
|
2013-09-02 11:12:33 +04:00
|
|
|
}
|
2013-08-29 17:30:22 +04:00
|
|
|
|
2015-03-11 09:59:56 +03:00
|
|
|
new_size = referenceTable->size * 2;
|
|
|
|
new_ref = (wReference*) realloc(referenceTable->array,
|
|
|
|
sizeof(wReference) * new_size);
|
2015-03-11 11:34:38 +03:00
|
|
|
if (!new_ref)
|
|
|
|
return NULL;
|
2013-01-18 22:50:35 +04:00
|
|
|
|
2015-03-11 09:59:56 +03:00
|
|
|
referenceTable->size = new_size;
|
|
|
|
referenceTable->array = new_ref;
|
2013-01-18 22:50:35 +04:00
|
|
|
ZeroMemory(&referenceTable->array[(referenceTable->size / 2)],
|
|
|
|
sizeof(wReference) * (referenceTable->size / 2));
|
|
|
|
|
|
|
|
return ReferenceTable_GetFreeEntry(referenceTable);
|
|
|
|
}
|
|
|
|
|
|
|
|
return reference;
|
|
|
|
}
|
|
|
|
|
|
|
|
UINT32 ReferenceTable_Add(wReferenceTable* referenceTable, void* ptr)
|
|
|
|
{
|
|
|
|
UINT32 count = 0;
|
|
|
|
wReference* reference = NULL;
|
|
|
|
|
|
|
|
if (referenceTable->synchronized)
|
2013-08-02 14:07:05 +04:00
|
|
|
EnterCriticalSection(&referenceTable->lock);
|
2013-01-18 22:50:35 +04:00
|
|
|
|
|
|
|
reference = ReferenceTable_FindEntry(referenceTable, ptr);
|
|
|
|
|
|
|
|
if (!reference)
|
|
|
|
{
|
|
|
|
reference = ReferenceTable_GetFreeEntry(referenceTable);
|
|
|
|
reference->Pointer = ptr;
|
|
|
|
reference->Count = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
count = ++(reference->Count);
|
|
|
|
|
|
|
|
if (referenceTable->synchronized)
|
2013-08-02 14:07:05 +04:00
|
|
|
LeaveCriticalSection(&referenceTable->lock);
|
2013-01-18 22:50:35 +04:00
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
UINT32 ReferenceTable_Release(wReferenceTable* referenceTable, void* ptr)
|
|
|
|
{
|
|
|
|
UINT32 count = 0;
|
|
|
|
wReference* reference = NULL;
|
|
|
|
|
|
|
|
if (referenceTable->synchronized)
|
2013-08-02 14:07:05 +04:00
|
|
|
EnterCriticalSection(&referenceTable->lock);
|
2013-01-18 22:50:35 +04:00
|
|
|
|
|
|
|
reference = ReferenceTable_FindEntry(referenceTable, ptr);
|
|
|
|
|
|
|
|
if (reference)
|
|
|
|
{
|
|
|
|
count = --(reference->Count);
|
|
|
|
|
|
|
|
if (count < 1)
|
|
|
|
{
|
|
|
|
if (referenceTable->ReferenceFree)
|
|
|
|
{
|
|
|
|
referenceTable->ReferenceFree(referenceTable->context, ptr);
|
|
|
|
reference->Pointer = NULL;
|
|
|
|
reference->Count = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (referenceTable->synchronized)
|
2013-08-02 14:07:05 +04:00
|
|
|
LeaveCriticalSection(&referenceTable->lock);
|
2013-01-18 22:50:35 +04:00
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
wReferenceTable* ReferenceTable_New(BOOL synchronized, void* context, REFERENCE_FREE ReferenceFree)
|
|
|
|
{
|
|
|
|
wReferenceTable* referenceTable;
|
|
|
|
|
|
|
|
referenceTable = (wReferenceTable*) malloc(sizeof(wReferenceTable));
|
|
|
|
|
|
|
|
if (referenceTable)
|
|
|
|
{
|
2015-04-03 17:21:01 +03:00
|
|
|
referenceTable->array = (wReference*) calloc(1, sizeof(wReference) * referenceTable->size);
|
|
|
|
if (!referenceTable->array)
|
|
|
|
{
|
|
|
|
free(referenceTable);
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-01-18 22:50:35 +04:00
|
|
|
referenceTable->context = context;
|
|
|
|
referenceTable->ReferenceFree = ReferenceFree;
|
|
|
|
|
|
|
|
referenceTable->size = 32;
|
|
|
|
|
|
|
|
referenceTable->synchronized = synchronized;
|
2013-08-07 19:10:43 +04:00
|
|
|
InitializeCriticalSectionAndSpinCount(&referenceTable->lock, 4000);
|
2013-01-18 22:50:35 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return referenceTable;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ReferenceTable_Free(wReferenceTable* referenceTable)
|
|
|
|
{
|
|
|
|
if (referenceTable)
|
|
|
|
{
|
2013-08-02 14:07:05 +04:00
|
|
|
DeleteCriticalSection(&referenceTable->lock);
|
2013-01-18 22:50:35 +04:00
|
|
|
free(referenceTable->array);
|
|
|
|
free(referenceTable);
|
|
|
|
}
|
|
|
|
}
|