d5d797c786f2c2364d36e7433fde02aac3ba896d
[openafs.git] / src / WINNT / afssvrmgr / general.cpp
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  *
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #include <winsock2.h>
11 #include <ws2tcpip.h>
12
13 extern "C" {
14 #include <afs/param.h>
15 #include <afs/stds.h>
16 }
17
18 #include "svrmgr.h"
19 #include "general.h"
20
21
22 /*
23  * ROUTINES ___________________________________________________________________
24  *
25  */
26
27 /*
28  *** InterlockedIncrementByWindow
29  *** InterlockedDecrementByWindow
30  *
31  * Associates a zero-initialized LONG with an HWND, and calls Interlocked*()
32  * on that LONG.
33  *
34  */
35
36 #define cREALLOC_WINDOWLIST 16
37
38 static struct
39    {
40    HWND hWnd;
41    LONG dw;
42    } *aWindowList = NULL;
43
44 static size_t cWindowList = 0;
45 static LPCRITICAL_SECTION pcsWindowList = NULL;
46
47 LONG *FindLongByWindow (HWND hWnd)
48 {
49    LONG *lpdw = NULL;
50
51    if (pcsWindowList == NULL)
52       {
53       pcsWindowList = New (CRITICAL_SECTION);
54       InitializeCriticalSection (pcsWindowList);
55       }
56
57    EnterCriticalSection (pcsWindowList);
58
59    size_t ii;
60    for (ii = 0; !lpdw && ii < cWindowList; ++ii)
61       {
62       if (aWindowList[ ii ].hWnd == hWnd)
63          lpdw = &aWindowList[ ii ].dw;
64       }
65    if (ii == cWindowList)
66       {
67       if (REALLOC( aWindowList, cWindowList, 1+ii, cREALLOC_WINDOWLIST ))
68          {
69          aWindowList[ ii ].hWnd = hWnd;
70          aWindowList[ ii ].dw = 0;
71          lpdw = &aWindowList[ ii ].dw;
72          }
73       }
74
75    LeaveCriticalSection (pcsWindowList);
76    return lpdw;
77 }
78
79 LONG InterlockedIncrementByWindow (HWND hWnd)
80 {
81    LONG *lpdw;
82    if ((lpdw = FindLongByWindow (hWnd)) == NULL)
83       return 0;
84    return InterlockedIncrement (lpdw);
85 }
86
87 LONG InterlockedDecrementByWindow (HWND hWnd)
88 {
89    LONG *lpdw;
90    if ((lpdw = FindLongByWindow (hWnd)) == NULL)
91       return 0;
92    return InterlockedDecrement (lpdw);
93 }
94