summaryrefslogtreecommitdiff
path: root/plugins/SMS/src/AdditionalFunctions/ListMT.h
blob: fd9ab3baafa8bf6507239f9070a4d7ce43121b66 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#if !defined(AFX_LIST_MT__H__INCLUDED_)
#define AFX_LIST_MT__H__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000


#include "InterlockedFunctions.h"

#if defined(_MSC_VER)
#if _MSC_VER >= 800
#if _MSC_VER >= 1200
#pragma warning(push)
#endif
#pragma warning(disable:4312) // warning C4312: 'type cast' : conversion from 'LONG' to 'PVOID' of greater size
#endif
#endif


// ñòðóêòóðà äëÿ ðàáîòû ñî ñïèñêîì, ýëåìåíò ñïèñêà
typedef struct _LIST_MT_ITEM
{
#ifdef __cplusplus
	_LIST_MT_ITEM	*plmtiPrev;	// *ïðåäûäóùèé ýëåìåíò â ñïèñêå
	_LIST_MT_ITEM	*plmtiNext;	// *ñëåäóþùèé ýëåìåíò â ñïèñêå
#else
	LPVOID			*plmtiPrev;	// *ïðåäûäóùèé ýëåìåíò â ñïèñêå
	LPVOID			*plmtiNext;	// *ñëåäóþùèé ýëåìåíò â ñïèñêå
#endif
	LPVOID			lpData;		// óêàçàòåëü íà äàííûå, ñâÿçàííûå ñ ýëåìåíòîì ñïèñêà
	LPVOID			lpListMT;	// óêàçàòåëü íà çàãîëîâîê ñïèñêà, ñì ñòðóêòóðó íèæå
}LIST_MT_ITEM, *PLIST_MT_ITEM, *LPLIST_MT_ITEM;
typedef CONST PLIST_MT_ITEM	PCLIST_MT_ITEM, LPCLIST_MT_ITEM;


// ñòðóêòóðà äëÿ ðàáîòû ñî ñïèñêîì, çàãîëîâîê ñïèñêà
typedef struct _LIST_MT
{
	SIZE_T				nCount;			// *êîëëè÷åñòâî ýëåìåíòîâ â ñïèñêå
	PLIST_MT_ITEM		plmtiFirst;		// *óêàçûâàåò íà ïåðâûé ýëåìåíò â ñïèñêå
	PLIST_MT_ITEM		plmtiLast;		// *óêàçûâàåò íà ïîñëåäíèé ýëåìåíò â ñïèñêå
	CRITICAL_SECTION	cs;				// *section for exclysive access to List
}LIST_MT, *PLIST_MT, *LPLIST_MT;
typedef CONST PLIST_MT	PCLIST_MT, LPCLIST_MT;


// ñòðóêòóðà äëÿ ðàáîòû ñ èòåðàòîðîì
typedef struct _LIST_MT_ITERATOR
{
	PLIST_MT_ITEM plmtListMTItem;
}LIST_MT_ITERATOR, *PLIST_MT_ITERATOR, *LPLIST_MT_ITERATOR;
//typedef LIST_MT_ITEM			LIST_MT_ITERATOR, *PLIST_MT_ITERATOR, *LPLIST_MT_ITERATOR;
typedef CONST PLIST_MT_ITERATOR	PCLIST_MT_ITERATOR, LPCLIST_MT_ITERATOR;




__inline DWORD ListMTInitialize(PCLIST_MT pclmtListMT,DWORD dwSpinCount)
{
	DWORD dwRetErrorCode;

#if (_WIN32_WINNT >= 0x0403)
	if (InitializeCriticalSectionAndSpinCount(&pclmtListMT->cs,((dwSpinCount)? (dwSpinCount | 0x80000000):0L)))
#else
	InitializeCriticalSection(&pclmtListMT->cs);
	if (TRUE)
#endif
	{
		InterlockedExchangePointer(&pclmtListMT->nCount,NULL);
		pclmtListMT->plmtiFirst=NULL;
		pclmtListMT->plmtiLast=NULL;
		dwRetErrorCode=NO_ERROR;
	}else{
		dwRetErrorCode=GetLastError();
	}
return(dwRetErrorCode);
}


__inline void ListMTDestroy(PCLIST_MT pclmtListMT)
{
	InterlockedExchangePointer(&pclmtListMT->nCount,NULL);
	pclmtListMT->plmtiFirst=NULL;
	pclmtListMT->plmtiLast=NULL;
	DeleteCriticalSection(&pclmtListMT->cs);
}


__inline BOOL ListMTTryLock(PCLIST_MT pclmtListMT)
{
#if (_WIN32_WINNT >= 0x0400)
	return(TryEnterCriticalSection(&pclmtListMT->cs));
#else
	return(FALSE);
#endif
}


__inline void ListMTLock(PCLIST_MT pclmtListMT)
{
	EnterCriticalSection(&pclmtListMT->cs);
}


__inline void ListMTUnLock(PCLIST_MT pclmtListMT)
{
	LeaveCriticalSection(&pclmtListMT->cs);
}


__inline SIZE_T ListMTGetCount(PCLIST_MT pclmtListMT)
{
	return((SIZE_T)InterlockedCompareExchangePointer((LPVOID*)&pclmtListMT->nCount,NULL,NULL));
}


__inline SIZE_T ListMTItemAdd(PCLIST_MT pclmtListMT,PCLIST_MT_ITEM pclmtListMTItem,LPVOID lpData)
{
	SIZE_T dwRet=(SIZE_T)InterlockedIncrementPointer(&pclmtListMT->nCount);//pclmtListMT->nCount++;
	pclmtListMTItem->lpData=lpData;
	pclmtListMTItem->lpListMT=pclmtListMT;

    if (pclmtListMT->plmtiLast) 
	{// add node to end of list        
        pclmtListMTItem->plmtiPrev=pclmtListMT->plmtiLast;
        pclmtListMTItem->plmtiNext=NULL;
        pclmtListMT->plmtiLast->plmtiNext=pclmtListMTItem;
		pclmtListMT->plmtiLast=pclmtListMTItem;
	}else{// add the first node to the linked list
        pclmtListMTItem->plmtiPrev=NULL;
        pclmtListMTItem->plmtiNext=NULL;
		pclmtListMT->plmtiFirst=pclmtListMTItem;
        pclmtListMT->plmtiLast=pclmtListMTItem;
 	}

return(dwRet);
}


__inline DWORD ListMTItemDelete(PCLIST_MT pclmtListMT,PCLIST_MT_ITEM pclmtListMTItem)
{
	DWORD dwRetErrorCode;

	if (pclmtListMTItem->lpListMT==pclmtListMT && pclmtListMT)
	{// Äàííûé ýëåìåíò ïðèíàäëåæèò ê ýòîìó ñïèñêó, ìîæíî óäàëèòü.
		PLIST_MT_ITEM plmtiPrevNode=pclmtListMTItem->plmtiPrev,plmtiNextNode=pclmtListMTItem->plmtiNext;
		
		if (plmtiPrevNode || plmtiNextNode) 
		{
			if (plmtiPrevNode && plmtiNextNode==NULL) 
			{// This is the start node in the list to delete
			// îòêëþ÷èëñÿ ïîñëåäíèé ïîäêëþ÷èâøèéñÿ
				plmtiPrevNode->plmtiNext=NULL;
				pclmtListMT->plmtiLast=plmtiPrevNode;
			}else{
				if (plmtiPrevNode==NULL && plmtiNextNode) 
				{// This is the end node in the list to delete
				// îòêëþ÷èëñÿ ïåðâûé ïîäêëþ÷èâøèéñÿ
					plmtiNextNode->plmtiPrev=NULL;
					pclmtListMT->plmtiFirst=plmtiNextNode;
				}else{// îêëþ÷èëñÿ êëèåíò íå ïåðâûé è íå ïîñëåäíèé
					//if (plmtiPrev && plmtiNext) 
					{// Neither start node nor end node in the list
						plmtiPrevNode->plmtiNext=plmtiNextNode;
						plmtiNextNode->plmtiPrev=plmtiPrevNode;
					}
				}
			}
		}else{// This is the only node in the list to delete
			pclmtListMT->plmtiFirst=NULL;
			pclmtListMT->plmtiLast=NULL;
		}

		pclmtListMTItem->lpListMT=NULL;
		InterlockedDecrementPointer(&pclmtListMT->nCount);// pclmtListMT->nCount--;	
		dwRetErrorCode=NO_ERROR;
	}else{// ïîïûòàëèñü óäàëèòü ýëåìåíò íå îòíîñÿùèéñÿ ê äàííîìó ñïèñêó
		dwRetErrorCode=ERROR_INVALID_HANDLE;
	}
return(dwRetErrorCode);
}


__inline LPVOID ListMTItemDataGet(PCLIST_MT_ITEM pclmtListMTItem)
{
	return(pclmtListMTItem->lpData);
}


__inline void ListMTItemDataSet(PCLIST_MT_ITEM pclmtListMTItem, LPVOID lpData)
{
	pclmtListMTItem->lpData=lpData;
}


__inline DWORD ListMTItemGetFirst(PCLIST_MT pclmtListMT,PLIST_MT_ITEM *pplmtListMTItem,LPVOID *plpData)
{// åñëè íóæíî ãàðàíòèðîâàòü ýêñêëþçèâíûé äîñòóï, òî åñòü ListMTLock è ListMTUnLock
	DWORD dwRetErrorCode;

	if (pclmtListMT->plmtiFirst)
	{
		if (pplmtListMTItem)	(*pplmtListMTItem)=pclmtListMT->plmtiFirst;
		if (plpData)			(*plpData)=pclmtListMT->plmtiFirst->lpData;
		dwRetErrorCode=NO_ERROR;
	}else{
		dwRetErrorCode=ERROR_NO_MORE_ITEMS;
	}
return(dwRetErrorCode);
}


__inline DWORD ListMTItemGetLast(PCLIST_MT pclmtListMT,PLIST_MT_ITEM *pplmtListMTItem,LPVOID *plpData)
{// åñëè íóæíî ãàðàíòèðîâàòü ýêñêëþçèâíûé äîñòóï, òî åñòü ListMTLock è ListMTUnLock
	DWORD dwRetErrorCode;

	if (pclmtListMT->plmtiLast)
	{
		if (pplmtListMTItem)	(*pplmtListMTItem)=pclmtListMT->plmtiLast;
		if (plpData)			(*plpData)=pclmtListMT->plmtiLast->lpData;
		dwRetErrorCode=NO_ERROR;
	}else{
		dwRetErrorCode=ERROR_NO_MORE_ITEMS;
	}
return(dwRetErrorCode);
}



__inline void ListMTItemSwap(PCLIST_MT pclmtListMT,PCLIST_MT_ITEM pclmtListMTItem1,PCLIST_MT_ITEM pclmtListMTItem2)
{// ïîìåíÿòü äâà ýëåìåíòà ñïèñêà ìåñòàìè, äàæå åñëè îíè èç ðàçíûõ ñïèñêîâ

	if (pclmtListMTItem1!=pclmtListMTItem2)
	{// ýòî ðàçíûå ýëåìåíòû ñïèñêà
		PLIST_MT_ITEM lpTemp;

		lpTemp=pclmtListMTItem1->plmtiPrev;
		if ((pclmtListMTItem1->plmtiPrev=pclmtListMTItem2->plmtiPrev)==NULL)
		{// pclmtListMTItem2 áûë ïåðâûì, îáíîâëÿåì çàãîëâîê ëèñòà, òåïåðü ïåðâûé pclmtListMTItem1
			pclmtListMT->plmtiFirst=pclmtListMTItem1;
		}

		if ((pclmtListMTItem2->plmtiPrev=lpTemp)==NULL)
		{// pclmtListMTItem1 áûë ïåðâûì, îáíîâëÿåì çàãîëâîê ëèñòà, òåïåðü ïåðâûé pclmtListMTItem2
			pclmtListMT->plmtiFirst=pclmtListMTItem2;
		}


		lpTemp=pclmtListMTItem1->plmtiNext;
		if ((pclmtListMTItem1->plmtiNext=pclmtListMTItem2->plmtiNext)==NULL)
		{// pclmtListMTItem2 áûë ïîñëåäíèì, îáíîâëÿåì çàãîëâîê ëèñòà, òåïåðü ïîñëåäíèé pclmtListMTItem1
			pclmtListMT->plmtiLast=pclmtListMTItem1;
		}

		if ((pclmtListMTItem2->plmtiNext=lpTemp)==NULL)
		{// pclmtListMTItem1 áûë ïîñëåäíèì, îáíîâëÿåì çàãîëâîê ëèñòà, òåïåðü ïîñëåäíèé pclmtListMTItem2
			pclmtListMT->plmtiLast=pclmtListMTItem2;
		}
	}
}


__inline BOOL ListMTIteratorMoveFirst(PCLIST_MT pclmtListMT,PCLIST_MT_ITERATOR pclmtiIterator)
{// åñëè íóæíî ãàðàíòèðîâàòü ýêñêëþçèâíûé äîñòóï, òî åñòü ListMTLock è ListMTUnLock
	return((pclmtiIterator->plmtListMTItem=pclmtListMT->plmtiFirst)? TRUE:FALSE);
}


__inline BOOL ListMTIteratorMoveLast(PCLIST_MT pclmtListMT,PCLIST_MT_ITERATOR pclmtiIterator)
{// åñëè íóæíî ãàðàíòèðîâàòü ýêñêëþçèâíûé äîñòóï, òî åñòü ListMTLock è ListMTUnLock
	return((pclmtiIterator->plmtListMTItem=pclmtListMT->plmtiLast)? TRUE:FALSE);
}


__inline BOOL ListMTIteratorMovePrev(PCLIST_MT_ITERATOR pclmtiIterator)
{// åñëè íóæíî ãàðàíòèðîâàòü ýêñêëþçèâíûé äîñòóï, òî åñòü ListMTLock è ListMTUnLock
	BOOL bRet=FALSE;

	if (pclmtiIterator->plmtListMTItem)
	{
		if (pclmtiIterator->plmtListMTItem=pclmtiIterator->plmtListMTItem->plmtiPrev) bRet=TRUE;
	}
return(bRet);
}


__inline BOOL ListMTIteratorMoveNext(PCLIST_MT_ITERATOR pclmtiIterator)
{// åñëè íóæíî ãàðàíòèðîâàòü ýêñêëþçèâíûé äîñòóï, òî åñòü ListMTLock è ListMTUnLock
	BOOL bRet=FALSE;

	if (pclmtiIterator->plmtListMTItem) 
	{
		if (pclmtiIterator->plmtListMTItem=pclmtiIterator->plmtListMTItem->plmtiNext) bRet=TRUE;
	}
return(bRet);
}


__inline DWORD ListMTIteratorGet(PCLIST_MT_ITERATOR pclmtiIterator,PLIST_MT_ITEM *pplmtListMTItem,LPVOID *plpData)
{// åñëè íóæíî ãàðàíòèðîâàòü ýêñêëþçèâíûé äîñòóï, òî åñòü ListMTLock è ListMTUnLock
	DWORD dwRetErrorCode;

	if (pclmtiIterator->plmtListMTItem)
	{
		if (pplmtListMTItem)	(*pplmtListMTItem)=pclmtiIterator->plmtListMTItem;
		if (plpData)			(*plpData)=pclmtiIterator->plmtListMTItem->lpData;
		dwRetErrorCode=NO_ERROR;
	}else{
		dwRetErrorCode=ERROR_NO_MORE_ITEMS;
	}
return(dwRetErrorCode);
}



#if defined(_MSC_VER)
#if _MSC_VER >= 800
#if _MSC_VER >= 1200
#pragma warning(pop)
#else
#pragma warning(default:4312) // warning C4312: 'type cast' : conversion from 'LONG' to 'PVOID' of greater size
#endif
#endif
#endif



#endif // !defined(AFX_LIST_MT__H__INCLUDED_)