Windows: fix checked UNICODE build of talocale
[openafs.git] / src / WINNT / talocale / tal_alloc.cpp
index 532d879..a8d1493 100644 (file)
@@ -70,8 +70,8 @@ typedef struct
    {
    PVOID pData;
    size_t cbData;
-   LPSTR pszExpr;
-   LPSTR pszFile;
+   LPTSTR pszExpr;
+   LPTSTR pszFile;
    DWORD dwLine;
    DWORD dwTick;
    DWORD dwEndSig;
@@ -90,25 +90,25 @@ typedef struct
 
 typedef struct
    {
-   LONG cAllocCpp;
-   LONG cAllocDyna;
-   LONG cAllocTotal;
-   LONG cAllocCppTared;
-   LONG cAllocDynaTared;
-   LONG cAllocTotalTared;
-   LONG cbAllocCpp;
-   LONG cbAllocDyna;
-   LONG cbAllocTotal;
-   LONG cbAllocCppTared;
-   LONG cbAllocDynaTared;
-   LONG cbAllocTotalTared;
+   size_t cAllocCpp;
+   size_t cAllocDyna;
+   size_t cAllocTotal;
+   size_t cAllocCppTared;
+   size_t cAllocDynaTared;
+   size_t cAllocTotalTared;
+   size_t cbAllocCpp;
+   size_t cbAllocDyna;
+   size_t cbAllocTotal;
+   size_t cbAllocCppTared;
+   size_t cbAllocDynaTared;
+   size_t cbAllocTotalTared;
    } STATISTICS;
 
 static struct l
    {
    CRITICAL_SECTION *pcs;
    HWND hManager;
-   int idTimer;
+   UINT_PTR idTimer;
 
    ALLOCEXPANDARRAY *pHeap;
    size_t cChunks;
@@ -248,12 +248,15 @@ typedef struct
 #define cyLABELS    15
 
 
-HWND MakeWindow (LPCTSTR pszClass, LPCTSTR pszTitle, DWORD dwStyle, RECT *prSource, HWND hParent, UINT idc, DWORD dwStyleEx = 0)
+HWND MakeWindow (LPCTSTR pszClass, LPCTSTR pszTitle, DWORD dwStyle, RECT *prSource, 
+                 HWND hParent, UINT idc, DWORD dwStyleEx = 0)
 {
    RECT rr = { 0, 0, 16, 16 };
    if (prSource)
       rr = *prSource;
-   HWND hWnd = CreateWindowEx (dwStyleEx, pszClass, pszTitle, dwStyle, rr.left, rr.top, rr.right - rr.left, rr.bottom - rr.top, hParent, (HMENU)idc, GetModuleHandle(0), 0);
+   HWND hWnd = CreateWindowEx (dwStyleEx, pszClass, pszTitle, dwStyle, 
+                               rr.left, rr.top, rr.right - rr.left, rr.bottom - rr.top, 
+                               hParent, (HMENU)UIntToPtr(idc), GetModuleHandle(0), 0);
    if (IsWindow (hWnd))
       SendMessage (hWnd, WM_SETFONT, (WPARAM)GetStockObject(DEFAULT_GUI_FONT), 1);
    return hWnd;
@@ -261,7 +264,8 @@ HWND MakeWindow (LPCTSTR pszClass, LPCTSTR pszTitle, DWORD dwStyle, RECT *prSour
 
 void SetWindowRect (HWND hWnd, RECT *pr)
 {
-   SetWindowPos (hWnd, 0, pr->left, pr->top, pr->right - pr->left, pr->bottom - pr->top, SWP_NOZORDER | SWP_NOACTIVATE);
+   SetWindowPos (hWnd, 0, pr->left, pr->top, pr->right - pr->left, 
+                 pr->bottom - pr->top, SWP_NOZORDER | SWP_NOACTIVATE);
 }
 
 void FormatBytes (LPTSTR pszText, double lfValue)
@@ -351,10 +355,10 @@ void SetDlgItemBytes (HWND hDlg, int idc, double lfValue)
 }
 
 
-void MemMgr_ShowWarning (PMEMCHUNK pChunk, LPSTR pszFile, DWORD dwLine, LPTSTR pszDesc)
+void MemMgr_ShowWarning (PMEMCHUNK pChunk, LPTSTR pszFile, DWORD dwLine, LPTSTR pszDesc)
 {
    TCHAR szMessage[ 1024 ];
-   wsprintf (szMessage, TEXT("%s\n\n   Address: 0x%08lX (%s)\n   Allocated: %s line %ld\n   Freed: %s line %ld\n\nClick OK for memory details."), pszDesc, (long)pChunk->pData, pChunk->pszExpr, pChunk->pszFile, pChunk->dwLine, pszFile, dwLine);
+   wsprintf (szMessage, TEXT("%s\n\n   Address: 0x%08p (%s)\n   Allocated: %s line %ld\n   Freed: %s line %ld\n\nClick OK for memory details."), pszDesc, pChunk->pData, pChunk->pszExpr, pChunk->pszFile, pChunk->dwLine, pszFile, dwLine);
    if (MessageBox (NULL, szMessage, cszTITLE, MB_ICONHAND | MB_OKCANCEL | MB_DEFBUTTON2) == IDOK)
       {
       // TODO: Show Details
@@ -567,7 +571,7 @@ LPTSTR MemMgr_GetItemKey (HWND hList, int iItem)
          if (pChunk)
             {
             if (lr.iColSort == 0)
-               pszReturn = (LPTSTR)pChunk->dwTick;
+               pszReturn = (LPTSTR)UlongToPtr(pChunk->dwTick);
             else if (lr.iColSort == 4)
                pszReturn = (LPTSTR)pChunk->cbData;
             else // (lr.iColSort == 5)
@@ -596,7 +600,7 @@ int MemMgr_CompareItems (LPTSTR pszKey1, LPTSTR pszKey2)
       case 0:
       case 4:
       case 5:
-         dw = (int)( (DWORD)pszKey1 - (DWORD)pszKey2 );
+         dw = (int)( pszKey1 - pszKey2 );
          break;
 
       default:
@@ -669,12 +673,12 @@ void MemMgr_OnListAdd (PMEMCHUNK pCopy)
    FormatBytes (szBytes, (double)pCopy->cbData);
 
    TCHAR szAddress[256];
-   wsprintf (szAddress, TEXT("0x%08lX"), (long)pCopy->pData);
+   wsprintf (szAddress, TEXT("0x%08p"), pCopy->pData);
 
    LPTSTR pszKey = NULL;
    switch (lr.iColSort)
       {
-      case 0:  pszKey = (LPTSTR)pCopy->dwTick;  break;
+      case 0:  pszKey = (LPTSTR)UlongToPtr(pCopy->dwTick);  break;
       case 1:  pszKey = (LPTSTR)szFlags;        break;
       case 2:  pszKey = (LPTSTR)szExpr;         break;
       case 3:  pszKey = (LPTSTR)szLocation;     break;
@@ -854,10 +858,10 @@ void MemMgr_OnRefresh (void)
 {
    // Fill in the statistics at the top of the manager dialog
    //
-   SetDlgItemInt (l.hManager, IDC_VALUE_CPP_COUNT, l.Stats.cAllocCpp, TRUE);
-   SetDlgItemInt (l.hManager, IDC_VALUE_OTHER_COUNT, l.Stats.cAllocDyna, TRUE);
-   SetDlgItemInt (l.hManager, IDC_VALUE_TARED_COUNT, l.Stats.cAllocTotalTared, TRUE);
-   SetDlgItemInt (l.hManager, IDC_VALUE_TOTAL_COUNT, l.Stats.cAllocTotal, TRUE);
+   SetDlgItemInt (l.hManager, IDC_VALUE_CPP_COUNT, (INT)l.Stats.cAllocCpp, TRUE);
+   SetDlgItemInt (l.hManager, IDC_VALUE_OTHER_COUNT, (INT)l.Stats.cAllocDyna, TRUE);
+   SetDlgItemInt (l.hManager, IDC_VALUE_TARED_COUNT, (INT)l.Stats.cAllocTotalTared, TRUE);
+   SetDlgItemInt (l.hManager, IDC_VALUE_TOTAL_COUNT, (INT)l.Stats.cAllocTotal, TRUE);
 
    SetDlgItemBytes (l.hManager, IDC_VALUE_CPP_SIZE, (double)l.Stats.cbAllocCpp);
    SetDlgItemBytes (l.hManager, IDC_VALUE_OTHER_SIZE, (double)l.Stats.cbAllocDyna);
@@ -994,9 +998,9 @@ void MemMgr_OnPaint (void)
 
 void MemMgr_OnTimer (void)
 {
-   if (GetWindowLong (l.hManager, GWL_USERDATA))
+   if (GetWindowLongPtr (l.hManager, GWLP_USERDATA))
       {
-      SetWindowLong (l.hManager, GWL_USERDATA, 0);
+      SetWindowLongPtr (l.hManager, GWLP_USERDATA, 0);
       MemMgr_OnRefresh();
       }
 }
@@ -1004,7 +1008,7 @@ void MemMgr_OnTimer (void)
 
 void MemMgr_OnDelayedRefresh (void)
 {
-   SetWindowLong (l.hManager, GWL_USERDATA, 1);
+   SetWindowLongPtr (l.hManager, GWLP_USERDATA, 1);
 }
 
 
@@ -1016,7 +1020,7 @@ BOOL CALLBACK MemMgr_DlgProc (HWND hDlg, UINT msg, WPARAM wp, LPARAM lp)
          MemMgr_StoreSettings();
          l.hManager = NULL;
          KillTimer (hDlg, l.idTimer);
-         l.idTimer = -1;
+         l.idTimer = (UINT_PTR)-1;
          break;
 
       case WM_SIZE:
@@ -1108,7 +1112,7 @@ BOOL CALLBACK MemMgr_DlgProc (HWND hDlg, UINT msg, WPARAM wp, LPARAM lp)
          break;
       }
 
-   return DefWindowProc (hDlg, msg, wp, lp);
+   return (BOOL) DefWindowProc (hDlg, msg, wp, lp);
 }
 
 
@@ -1138,7 +1142,7 @@ BOOL MemMgr_Initialize (void)
 }
 
 
-void MemMgr_TrackAllocation (PVOID pData, size_t cb, LPSTR pszExpr, LPSTR pszFile, DWORD dwLine, BOOL fSig)
+void MemMgr_TrackAllocation (PVOID pData, size_t cb, LPTSTR pszExpr, LPTSTR pszFile, DWORD dwLine, BOOL fSig)
 {
    if (!pData)
       return;
@@ -1166,7 +1170,7 @@ void MemMgr_TrackAllocation (PVOID pData, size_t cb, LPSTR pszExpr, LPSTR pszFil
             if ((pChunk = (PMEMCHUNK)l.pHeap->GetAt(iChunk)) == NULL)
                continue;
 
-            size_t iBucket = HASH(pChunk->pData,l.cBuckets);
+            size_t iBucket = HASH(PtrToUlong(pChunk->pData),l.cBuckets);
             if ((pChunk->iNext = l.aBuckets[iBucket].iFirst) != iINVALID)
                {
                PMEMCHUNK pNext;
@@ -1187,7 +1191,7 @@ void MemMgr_TrackAllocation (PVOID pData, size_t cb, LPSTR pszExpr, LPSTR pszFil
    // Prepare a MEMCHUNK entry and shove it in our array
    //
    size_t iChunk = l.cChunks;
-   size_t iBucket = HASH(pData,l.cBuckets);
+   size_t iBucket = HASH(PtrToUlong(pData),l.cBuckets);
    BOOL fLinkIn = TRUE;
 
    MEMCHUNK Chunk;
@@ -1265,7 +1269,7 @@ void MemMgr_TrackAllocation (PVOID pData, size_t cb, LPSTR pszExpr, LPSTR pszFil
 }
 
 
-BOOL MemMgr_TrackDestruction (PVOID pData, LPSTR pszFile, DWORD dwLine)
+BOOL MemMgr_TrackDestruction (PVOID pData, LPTSTR pszFile, DWORD dwLine)
 {
    if (MemMgr_Initialize())
       {
@@ -1274,7 +1278,7 @@ BOOL MemMgr_TrackDestruction (PVOID pData, LPSTR pszFile, DWORD dwLine)
       // Find the memchunk associated with this pData. That's what our
       // hash table is for.
       //
-      size_t iBucket = HASH(pData,l.cBuckets);
+      size_t iBucket = HASH(PtrToUlong(pData),l.cBuckets);
 
       PMEMCHUNK pChunk = NULL;
       for (size_t iChunk = l.aBuckets[iBucket].iFirst; iChunk != iINVALID; )
@@ -1295,8 +1299,8 @@ BOOL MemMgr_TrackDestruction (PVOID pData, LPSTR pszFile, DWORD dwLine)
          MEMCHUNK Sim;
          memset (&Sim, 0x00, sizeof(MEMCHUNK));
          Sim.pData = pData;
-         Sim.pszExpr = "(unknown)";
-         Sim.pszFile = "(unknown)";
+         Sim.pszExpr = TEXT("(unknown)");
+         Sim.pszFile = TEXT("(unknown)");
          MemMgr_ShowWarning (&Sim, pszFile, dwLine, TEXT("An invalid memory address was freed."));
          }
       else if (pChunk->dwEndSig && (*(DWORD*)((PBYTE)pData + pChunk->cbData) != pChunk->dwEndSig))
@@ -1408,7 +1412,7 @@ void MEMMGR_CALLCONV ShowMemoryManager (void)
          {
          MemMgr_RestoreSettings();
          l.hManager = MakeWindow (TEXT("Static"), cszTITLE, WS_OVERLAPPED | WS_THICKFRAME | WS_SYSMENU, &lr.rManager, 0, 0);
-         SetWindowLong (l.hManager, GWL_WNDPROC, (LONG)MemMgr_DlgProc);
+         SetWindowLongPtr (l.hManager, GWLP_WNDPROC, (LONG)PtrToUlong(MemMgr_DlgProc));
          PostMessage (l.hManager, WM_COMMAND, IDC_INITIALIZE, 0);
          ShowWindow (l.hManager, SW_SHOW);
          }
@@ -1455,7 +1459,7 @@ BOOL MEMMGR_CALLCONV IsMemoryManagerMessage (MSG *pMsg)
 }
 
 
-PVOID MEMMGR_CALLCONV MemMgr_AllocateMemory (size_t cb, LPSTR pszExpr, LPSTR pszFile, DWORD dwLine)
+PVOID MEMMGR_CALLCONV MemMgr_AllocateMemory (size_t cb, LPTSTR pszExpr, LPTSTR pszFile, DWORD dwLine)
 {
    PVOID pData = GlobalAlloc (GMEM_FIXED, cb + sizeof(DWORD));
    MemMgr_TrackAllocation (pData, cb, pszExpr, pszFile, dwLine, TRUE);
@@ -1463,7 +1467,7 @@ PVOID MEMMGR_CALLCONV MemMgr_AllocateMemory (size_t cb, LPSTR pszExpr, LPSTR psz
 }
 
 
-void MEMMGR_CALLCONV MemMgr_FreeMemory (PVOID pData, LPSTR pszFile, DWORD dwLine)
+void MEMMGR_CALLCONV MemMgr_FreeMemory (PVOID pData, LPTSTR pszFile, DWORD dwLine)
 {
    if (MemMgr_TrackDestruction (pData, pszFile, dwLine))
       {
@@ -1472,14 +1476,14 @@ void MEMMGR_CALLCONV MemMgr_FreeMemory (PVOID pData, LPSTR pszFile, DWORD dwLine
 }
 
 
-PVOID MEMMGR_CALLCONV MemMgr_TrackNew (PVOID pData, size_t cb, LPSTR pszExpr, LPSTR pszFile, DWORD dwLine)
+PVOID MEMMGR_CALLCONV MemMgr_TrackNew (PVOID pData, size_t cb, LPTSTR pszExpr, LPTSTR pszFile, DWORD dwLine)
 {
    MemMgr_TrackAllocation (pData, cb, pszExpr, pszFile, dwLine, FALSE);
    return pData;
 }
 
 
-void MEMMGR_CALLCONV MemMgr_TrackDelete (PVOID pData, LPSTR pszFile, DWORD dwLine)
+void MEMMGR_CALLCONV MemMgr_TrackDelete (PVOID pData, LPTSTR pszFile, DWORD dwLine)
 {
    MemMgr_TrackDestruction (pData, pszFile, dwLine);
 }