634819bbc4ce88b913232bf3675308db62fc140f
[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 extern "C" {
11 #include <afs/param.h>
12 #include <afs/stds.h>
13 }
14
15 #include "svrmgr.h"
16 #include "general.h"
17
18
19 /*
20  * ROUTINES ___________________________________________________________________
21  *
22  */
23
24 /*
25  *** InterlockedIncrementByWindow
26  *** InterlockedDecrementByWindow
27  *
28  * Associates a zero-initialized LONG with an HWND, and calls Interlocked*()
29  * on that LONG.
30  *
31  */
32
33 #define cREALLOC_WINDOWLIST 16
34
35 static struct
36    {
37    HWND hWnd;
38    LONG dw;
39    } *aWindowList = NULL;
40
41 static size_t cWindowList = 0;
42 static LPCRITICAL_SECTION pcsWindowList = NULL;
43
44 LONG *FindLongByWindow (HWND hWnd)
45 {
46    LONG *lpdw = NULL;
47
48    if (pcsWindowList == NULL)
49       {
50       pcsWindowList = New (CRITICAL_SECTION);
51       InitializeCriticalSection (pcsWindowList);
52       }
53
54    EnterCriticalSection (pcsWindowList);
55
56    size_t ii;
57    for (ii = 0; !lpdw && ii < cWindowList; ++ii)
58       {
59       if (aWindowList[ ii ].hWnd == hWnd)
60          lpdw = &aWindowList[ ii ].dw;
61       }
62    if (ii == cWindowList)
63       {
64       if (REALLOC( aWindowList, cWindowList, 1+ii, cREALLOC_WINDOWLIST ))
65          {
66          aWindowList[ ii ].hWnd = hWnd;
67          aWindowList[ ii ].dw = 0;
68          lpdw = &aWindowList[ ii ].dw;
69          }
70       }
71
72    LeaveCriticalSection (pcsWindowList);
73    return lpdw;
74 }
75
76 LONG InterlockedIncrementByWindow (HWND hWnd)
77 {
78    LONG *lpdw;
79    if ((lpdw = FindLongByWindow (hWnd)) == NULL)
80       return 0;
81    return InterlockedIncrement (lpdw);
82 }
83
84 LONG InterlockedDecrementByWindow (HWND hWnd)
85 {
86    LONG *lpdw;
87    if ((lpdw = FindLongByWindow (hWnd)) == NULL)
88       return 0;
89    return InterlockedDecrement (lpdw);
90 }
91