summaryrefslogblamecommitdiffstats
path: root/private/ole32/ole232/util/global.cpp
blob: 25c95487f99376f3703abc33aabb670b49fc5cce (plain) (tree)
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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821




















































































































































































































































































































































































































































































































































































































































































































































































































































                                                                                        
//+----------------------------------------------------------------------------
//
//	File:
//		global.cpp
//
//	Contents:
//		Ut functions that deal with HGlobals for debugging;
//		see le2int.h
//
//	Classes:
//
//	Functions:
//		UtGlobalAlloc
//		UtGlobalReAlloc
//		UtGlobalLock
//		UtGlobalUnlock
//		UtGlobalFree
//              UtGlobalFlush
//              UtSetClipboardData
//
//	History:
//		12/20/93 - ChrisWe - created
//		01/11/94 - alexgo  - added VDATEHEAP macros to every function
//              02/25/94 AlexT      Add some generic integrity checking
//              03/30/94 AlexT      Add UtSetClipboardData
//
//  Notes:
//
//  These routines are designed to catch bugs that corrupt GlobalAlloc memory.
//  We cannot guarantee that all global memory will be manipulated with these
//  routines (e.g.  OLE might allocate a handle and the client application
//  might free it), so we can't require that these routines be used in pairs.
//
//-----------------------------------------------------------------------------


#include <le2int.h>

#if DBG==1 && defined(WIN32)
#include <olesem.hxx>

ASSERTDATA

// undefine these, so we don't call ourselves recursively
// if this module is used, these are defined in le2int.h to replace
// the existing allocator with the functions here
#undef GlobalAlloc
#undef GlobalReAlloc
#undef GlobalLock
#undef GlobalUnlock
#undef GlobalFree
#undef SetClipboardData

//  Same ones as in memapi.cxx
#define OLEMEM_ALLOCBYTE       0xde
#define OLEMEM_FREEBYTE        0xed

typedef struct s_GlobalAllocInfo
{
    HGLOBAL hGlobal;                        //  A GlobalAlloc'd HGLOBAL
    ULONG   cbGlobalSize;                   //  GlobalSize(hGlobal)
    ULONG   cbUser;                         //  size requested by caller
    ULONG   ulIndex;                        //  allocation index (1st, 2nd...)
    struct s_GlobalAllocInfo *pNext;
} SGLOBALALLOCINFO, *PSGLOBALALLOCINFO;

//+-------------------------------------------------------------------------
//
//  Class:      CGlobalTrack
//
//  Purpose:    GlobalAlloc memory tracking
//
//  History:    25-Feb-94 AlexT     Created
//
//  Notes:
//
//--------------------------------------------------------------------------

class CGlobalTrack
{
  public:

    //
    //  We only have a constructor for debug builds, to ensure this object
    //  is statically allocated. Statically allocated objects are initialized
    //  to all zeroes, which is what we need.
    //

    CGlobalTrack();

    HGLOBAL cgtGlobalAlloc(UINT uiFlag, DWORD cbUser);
    HGLOBAL cgtGlobalReAlloc(HGLOBAL hGlobal, DWORD cbUser, UINT uiFlag);
    HGLOBAL cgtGlobalFree(HGLOBAL hGlobal);
    LPVOID  cgtGlobalLock(HGLOBAL hGlobal);
    BOOL    cgtGlobalUnlock(HGLOBAL hGlobal);

    void    cgtVerifyAll(void);
    void    cgtFlushTracking(void);
    BOOL    cgtStopTracking(HGLOBAL hGlobal);

  private:
    ULONG CalculateAllocSize(ULONG cbUser);
    void InitializeRegion(HGLOBAL hGlobal, ULONG cbStart, ULONG cbEnd);
    void Track(HGLOBAL hGlobal, ULONG cbUser);
    void Retrack(HGLOBAL hOld, HGLOBAL hNew);
    void VerifyHandle(HGLOBAL hGlobal);

    ULONG _ulIndex;
    PSGLOBALALLOCINFO _pRoot;
    static COleStaticMutexSem _mxsGlobalMemory;
};

COleStaticMutexSem CGlobalTrack::_mxsGlobalMemory;

CGlobalTrack gGlobalTrack;



//+-------------------------------------------------------------------------
//
//  Member:     CGlobalTrack::CGlobalTrack, public
//
//  Synopsis:   constructor
//
//  History:    28-Feb-94 AlexT     Created
//
//--------------------------------------------------------------------------

CGlobalTrack::CGlobalTrack()
{
    Win4Assert (g_fDllState == DLL_STATE_STATIC_CONSTRUCTING);
    Win4Assert (_pRoot == NULL && _ulIndex == 0);
}



//+-------------------------------------------------------------------------
//
//  Member:     CGlobalTrack::cgtGlobalAlloc, public
//
//  Synopsis:   Debugging version of GlobalAlloc
//
//  Arguments:  [uiFlag] -- allocation flags
//              [cbUser] -- requested allocation size
//
//  Requires:   We must return a "real" GlobalAlloc'd pointer, because
//              we may not necessarily be the ones to free it.
//
//  Returns:    HGLOBAL
//
//  Algorithm:  We allocate an extra amount to form a tail and initialize it
//              to a known value.
//
//  History:    25-Feb-94 AlexT     Added this prologue
//
//  Notes:
//
//--------------------------------------------------------------------------

HGLOBAL CGlobalTrack::cgtGlobalAlloc(UINT uiFlag, DWORD cbUser)
{
    VDATEHEAP();

    ULONG cbAlloc;
    HGLOBAL hGlobal;

    cbAlloc = CalculateAllocSize(cbUser);
    hGlobal = GlobalAlloc(uiFlag, cbAlloc);
    if (NULL == hGlobal)
    {
        LEDebugOut((DEB_WARN, "GlobalAlloc(%ld) failed - %lx\n", cbAlloc,
                   GetLastError()));
    }
    else
    {
        if (uiFlag & GMEM_ZEROINIT)
        {
            //   Caller asked for zeroinit, so we only initialize the tail
            InitializeRegion(hGlobal, cbUser, cbAlloc);
        }
        else
        {
            //  Caller did not ask for zeroinit, so we initialize the whole
            //  region
            InitializeRegion(hGlobal, 0, cbAlloc);
        }

        Track(hGlobal, cbUser);
    }

    return(hGlobal);
}

//+-------------------------------------------------------------------------
//
//  Member:     CGlobalTrack::cgtGlobalReAlloc, public
//
//  Synopsis:   Debugging version of GlobalReAlloc
//
//  Arguments:  [hGlobal] -- handle to reallocate
//              [cbUser] -- requested allocation size
//              [uiFlag] -- allocation flags
//
//  Returns:    reallocated handle
//
//  Algorithm:
//
//    if (modify only)
//      reallocate
//    else
//      reallocate with tail
//      initialize tail
//
//    update tracking information
//
//  History:    25-Feb-94 AlexT     Added this prologue
//
//  Notes:
//
//--------------------------------------------------------------------------

HGLOBAL CGlobalTrack::cgtGlobalReAlloc(HGLOBAL hGlobal, DWORD cbUser, UINT uiFlag)
{
    VDATEHEAP();

    HGLOBAL hNew;
    ULONG cbAlloc;

    VerifyHandle(hGlobal);

    if (uiFlag & GMEM_MODIFY)
    {
        //  We're not changing sizes, so there's no work for us to do

        LEDebugOut((DEB_WARN, "UtGlobalReAlloc modifying global handle\n"));
        hNew = GlobalReAlloc(hGlobal, cbUser, uiFlag);
    }
    else
    {
        cbAlloc = CalculateAllocSize(cbUser);
        hNew = GlobalReAlloc(hGlobal, cbAlloc, uiFlag);
        if (NULL == hNew)
        {
            LEDebugOut((DEB_WARN, "GlobalReAlloc failed - %lx\n",
                        GetLastError()));
        }
        else
        {
            InitializeRegion(hNew, cbUser, cbAlloc);
        }
    }

    if (NULL != hNew)
    {
        if (uiFlag & GMEM_MODIFY)
        {
            //  Retrack will only track hNew if we were tracking hGlobal
            Retrack(hGlobal, hNew);
        }
        else
        {
            //  We've allocated a new block, so we always want to track the
            //  new one
            cgtStopTracking(hGlobal);
            Track(hNew, cbUser);
        }
    }

    return(hNew);
}

//+-------------------------------------------------------------------------
//
//  Member:     CGlobalTrack::cgtGlobalFree, public
//
//  Synopsis:   Debugging version of GlobalReAlloc
//
//  Arguments:  [hGlobal] -- global handle to free
//
//  Returns:    Same as GlobalFree
//
//  Algorithm:
//
//  History:    25-Feb-94 AlexT     Created
//
//  Notes:
//
//--------------------------------------------------------------------------

HGLOBAL CGlobalTrack::cgtGlobalFree(HGLOBAL hGlobal)
{
    VDATEHEAP();

    HGLOBAL hReturn;

    VerifyHandle(hGlobal);

    hReturn = GlobalFree(hGlobal);

    if (NULL == hReturn)
    {
        cgtStopTracking(hGlobal);
    }
    else
    {
        LEDebugOut((DEB_WARN, "GlobalFree did not free %lx\n", hGlobal));
    }

    return(hReturn);
}

//+-------------------------------------------------------------------------
//
//  Member:     CGlobalTrack::cgtGlobalLock, public
//
//  Synopsis:   Debugging version of GlobalLock
//
//  Arguments:  [hGlobal] -- global memory handle
//
//  Returns:    Same as GlobalLock
//
//  History:    25-Feb-94 AlexT     Created
//
//  Notes:
//
//--------------------------------------------------------------------------

LPVOID CGlobalTrack::cgtGlobalLock(HGLOBAL hGlobal)
{
    VDATEHEAP();

    VerifyHandle(hGlobal);
    return(GlobalLock(hGlobal));
}

//+-------------------------------------------------------------------------
//
//  Member:     CGlobalTrack::cgtGlobalUnlock, public
//
//  Synopsis:   Debugging version of GlobalUnlock
//
//  Arguments:  [hGlobal] -- global memory handle
//
//  Returns:    Same as GlobalUnlock
//
//  History:    25-Feb-94 AlexT     Created
//
//  Notes:
//
//--------------------------------------------------------------------------

BOOL CGlobalTrack::cgtGlobalUnlock(HGLOBAL hGlobal)
{
    VDATEHEAP();

    VerifyHandle(hGlobal);
    return(GlobalUnlock(hGlobal));
}

//+-------------------------------------------------------------------------
//
//  Member:     CGlobalTrack::cgtVerifyAll, public
//
//  Synopsis:   Verify all tracked handles
//
//  History:    28-Feb-94 AlexT     Created
//
//  Notes:
//
//--------------------------------------------------------------------------

void CGlobalTrack::cgtVerifyAll(void)
{
    VerifyHandle(NULL);
}

//+-------------------------------------------------------------------------
//
//  Member:     CGlobalTrack::cgtFlushTracking
//
//  Synopsis:   Stops all tracking
//
//  Effects:    Frees all internal memory
//
//  History:    28-Feb-94 AlexT     Created
//
//  Notes:
//
//--------------------------------------------------------------------------

void CGlobalTrack::cgtFlushTracking(void)
{
    COleStaticLock lck(_mxsGlobalMemory);
    BOOL bResult;

    while (NULL != _pRoot)
    {
        bResult = cgtStopTracking(_pRoot->hGlobal);
        Assert(bResult && "CGT::cgtFlushTracking problem");
    }
}

//+-------------------------------------------------------------------------
//
//  Member:     CGlobalTrack::CalculateAllocSize, private
//
//  Synopsis:   calculate total allocation size (inluding tail)
//
//  Arguments:  [cbUser] -- requested size
//
//  Returns:    total count of bytes to allocate
//
//  Algorithm:  calculate bytes needed to have at least one guard page at the
//              end
//
//  History:    28-Feb-94 AlexT     Created
//
//  Notes:      By keeping this calculation in one location we make it
//              easier to maintain.
//
//--------------------------------------------------------------------------

ULONG CGlobalTrack::CalculateAllocSize(ULONG cbUser)
{
    SYSTEM_INFO si;
    ULONG cbAlloc;

#ifdef _CHICAGO_
    //  BUGBUG Chicago: GetSystemInfo not yet implemented
    si.dwPageSize = 0x1000;
#else
    GetSystemInfo(&si);
#endif

    //  Calculate how many pages are need to cover cbUser
    cbAlloc = ((cbUser + si.dwPageSize - 1) / si.dwPageSize) * si.dwPageSize;

    //  Add an extra page so that the tail is at least one page long
    cbAlloc += si.dwPageSize;

    return(cbAlloc);
}

//+-------------------------------------------------------------------------
//
//  Member:     CGlobalTrack::InitializeRegion, private
//
//  Synopsis:   initialize region to bad value
//
//  Effects:    fills in memory region
//
//  Arguments:  [hGlobal] -- global memory handle
//              [cbStart] -- count of bytes to skip
//              [cbEnd]   -- end offset (exclusive)
//
//  Requires:   cbEnd > cbStart
//
//  Algorithm:  fill in hGlobal from cbStart (inclusive) to cbEnd (exclusive)
//
//  History:    28-Feb-94 AlexT     Created
//
//  Notes:
//
//--------------------------------------------------------------------------

void CGlobalTrack::InitializeRegion(HGLOBAL hGlobal, ULONG cbStart, ULONG cbEnd)
{
    BYTE *pbStart;
    BYTE *pb;

    Assert(cbStart < cbEnd && "illogical parameters");
    Assert(cbEnd <= GlobalSize(hGlobal) && "global memory too small");

    //  GlobalLock on GMEM_FIXED memory is a nop, so this is a safe call
    pbStart = (BYTE *) GlobalLock(hGlobal);

    if (NULL == pbStart)
    {
        //  Shouldn't have failed - (we allocated > 0 bytes)

        LEDebugOut((DEB_WARN, "GlobalLock failed - %lx\n", GetLastError()));
        return;
    }

    //  Initialize the tail portion of the memory
    for (pb = pbStart + cbStart; pb < pbStart + cbEnd; pb++)
    {
        *pb = OLEMEM_ALLOCBYTE;
    }

    GlobalUnlock(hGlobal);
}

//+-------------------------------------------------------------------------
//
//  Member:     CGlobalTrack::Track, private
//
//  Synopsis:
//
//  Effects:
//
//  Arguments:  [hGlobal] -- global memory handle
//              [cbUser] -- user allocation size
//
//  Requires:
//
//  Returns:
//
//  Signals:
//
//  Modifies:
//
//  Derivation:
//
//  Algorithm:
//
//  History:    28-Feb-94 AlexT     Created
//
//  Notes:
//
//--------------------------------------------------------------------------

void CGlobalTrack::Track(HGLOBAL hGlobal, ULONG cbUser)
{
    COleStaticLock lck(_mxsGlobalMemory);
    PSGLOBALALLOCINFO pgi;

    if (cgtStopTracking(hGlobal))
    {
        //  If it's already in our list, it's possible that someone else
        //  freed the HGLOBAL without telling us - remove our stale one
        LEDebugOut((DEB_WARN, "CGT::Track - %lx was already in list!\n",
                    hGlobal));
    }

    pgi = (PSGLOBALALLOCINFO) PrivMemAlloc(sizeof(SGLOBALALLOCINFO));
    if (NULL == pgi)
    {
        LEDebugOut((DEB_WARN, "CGT::Insert - PrivMemAlloc failed\n"));

        //  Okay fine - we just won't track this one

        return;
    }

    pgi->hGlobal = hGlobal;
    pgi->cbGlobalSize = GlobalSize(hGlobal);
    Assert((0 == cbUser || pgi->cbGlobalSize > 0) && "GlobalSize failed - bad handle?");
    pgi->cbUser = cbUser;
    pgi->ulIndex = ++_ulIndex;
    pgi->pNext = _pRoot;
    _pRoot = pgi;
}

//+-------------------------------------------------------------------------
//
//  Member:     CGlobalTrack::Retrack, private
//
//  Synopsis:
//
//  Effects:
//
//  Arguments:  [hOld] -- previous handle
//              [hNew] -- new handle
//
//  Modifies:
//
//  Algorithm:
//
//  History:    28-Feb-94 AlexT     Created
//
//  Notes:
//
//--------------------------------------------------------------------------

void CGlobalTrack::Retrack(HGLOBAL hOld, HGLOBAL hNew)
{
    COleStaticLock lck(_mxsGlobalMemory);
    PSGLOBALALLOCINFO pgi;

    if (hOld != hNew && cgtStopTracking(hNew))
    {
        //  If hNew was already in the list, it's possible that someone else
        //  freed the HGLOBAL without telling us so we removed the stale one
        LEDebugOut((DEB_WARN, "CGT::Retrack - %lx was already in list!\n", hNew));
    }

    for (pgi = _pRoot; NULL != pgi; pgi = pgi->pNext)
    {
        if (pgi->hGlobal == hOld)
        {
            pgi->hGlobal = hNew;
            break;
        }
    }

    if (NULL == pgi)
    {
        //  We didn't find hOld
        LEDebugOut((DEB_WARN, "CGT::Retrack - hOld (%lx) not found\n", hOld));
    }
}

//+-------------------------------------------------------------------------
//
//  Member:     CGlobalTrack::cgtStopTracking, public
//
//  Synopsis:
//
//  Effects:
//
//  Arguments:  [hGlobal] -- global handle
//
//  Modifies:
//
//  Algorithm:
//
//  History:    28-Feb-94 AlexT     Created
//
//  Notes:
//
//--------------------------------------------------------------------------

BOOL CGlobalTrack::cgtStopTracking(HGLOBAL hGlobal)
{
    COleStaticLock lck(_mxsGlobalMemory);
    PSGLOBALALLOCINFO *ppgi = &_pRoot;
    PSGLOBALALLOCINFO pgi;

    while (*ppgi != NULL && (*ppgi)->hGlobal != hGlobal)
    {
        ppgi = &((*ppgi)->pNext);
    }

    if (NULL == *ppgi)
    {
        return(FALSE);
    }

    pgi = *ppgi;
    Assert(pgi->hGlobal == hGlobal && "CGT::cgtStopTracking search problem");

    *ppgi = pgi->pNext;

    PrivMemFree(pgi);
    return(TRUE);
}

//+-------------------------------------------------------------------------
//
//  Member:     CGlobalTrack::VerifyHandle, private
//
//  Synopsis:   Verify global handle
//
//  Arguments:  [hGlobal] -- global memory handle
//
//  Signals:    Asserts if bad
//
//  Algorithm:
//
//  History:    28-Feb-94 AlexT     Created
//              22-Jun-94 AlexT     Allow for handle to have been freed and
//                                  reallocated under us
//
//--------------------------------------------------------------------------

void CGlobalTrack::VerifyHandle(HGLOBAL hGlobal)
{
    COleStaticLock lck(_mxsGlobalMemory);
    PSGLOBALALLOCINFO pgi, pgiNext;
    ULONG cbAlloc;
    BYTE *pbStart;
    BYTE *pb;

    //  Note that we use a while loop (recording pgiNext up front) instead
    //  of a for loop because pgi will get removed from the list if we call
    //  cgtStopTracking on it

    pgi = _pRoot;
    while (NULL != pgi)
    {
        pgiNext = pgi->pNext;

        if (NULL == hGlobal || pgi->hGlobal == hGlobal)
        {
            if (pgi->cbGlobalSize != GlobalSize(pgi->hGlobal))
            {
                //  pgi->hGlobal's size has changed since we started tracking
                //  it;  it must have been freed or reallocated by someone
                //  else.  Stop tracking it.

                //  This call will remove pgi from the list (so we NULL it to
                //  make sure we don't try reusing it)!

                cgtStopTracking(pgi->hGlobal);
                pgi = NULL;
            }
            else
            {
                cbAlloc = CalculateAllocSize(pgi->cbUser);

                pbStart = (BYTE *) GlobalLock(pgi->hGlobal);

                // it is legitimate to have a zero length (NULL memory) handle
                if (NULL == pbStart)
                {
                    LEDebugOut((DEB_WARN, "GlobalLock failed - %lx\n",
                               GetLastError()));
                }
                else
                {
                    for (pb = pbStart + pgi->cbUser;
                         pb < pbStart + cbAlloc;
                         pb++)
                    {
                        if (*pb != OLEMEM_ALLOCBYTE)
                            break;
                    }

                    if (pb < pbStart + cbAlloc)
                    {
                        //  In general an application may have freed and reallocated
                        //  any HGLOBAL, so we can only warn about corruption.

                        LEDebugOut((DEB_WARN, "HGLOBAL #%ld may be corrupt\n",
                                   pgi->ulIndex));
#ifdef GLOBALDBG
                        //  If GLOBALDBG is true, then all allocations should be
                        //  coming through these routines.  In this case we assert
                        //  if we've found corruption.
                        Assert(0 && "CGlobalTrack::VerifyHandle - HGLOBAL corrupt");
#endif
                    }

                    GlobalUnlock(pgi->hGlobal);
                }
            }
        }

        pgi = pgiNext;
    }
}

//+-------------------------------------------------------------------------
//
//  Function:   UtGlobalAlloc, ReAlloc, Free, Lock, Unlock
//
//  Synopsis:   Debug versions of Global memory routines
//
//  Arguments:  Same as Windows APIs
//
//  History:    28-Feb-94 AlexT     Created
//
//  Notes:      These entry points just call the worker routines
//
//--------------------------------------------------------------------------

extern "C" HGLOBAL WINAPI UtGlobalAlloc(UINT uiFlag, DWORD cbUser)
{
    return gGlobalTrack.cgtGlobalAlloc(uiFlag, cbUser);
}

extern "C" HGLOBAL WINAPI UtGlobalReAlloc(HGLOBAL hGlobal, DWORD cbUser, UINT uiFlag)
{
    return gGlobalTrack.cgtGlobalReAlloc(hGlobal, cbUser, uiFlag);
}

extern "C" LPVOID WINAPI UtGlobalLock(HGLOBAL hGlobal)
{
    return gGlobalTrack.cgtGlobalLock(hGlobal);
}

extern "C" BOOL WINAPI UtGlobalUnlock(HGLOBAL hGlobal)
{
    return gGlobalTrack.cgtGlobalUnlock(hGlobal);
}

extern "C" HGLOBAL WINAPI UtGlobalFree(HGLOBAL hGlobal)
{
    return gGlobalTrack.cgtGlobalFree(hGlobal);
}

extern "C" void UtGlobalFlushTracking(void)
{
    gGlobalTrack.cgtFlushTracking();
}

//+-------------------------------------------------------------------------
//
//  Function:   UtSetClipboardData
//
//  Synopsis:   Calls Windows SetClipboardData and stops tracking the handle
//
//  Arguments:  [uFormat] -- clipboard format
//              [hMem]    -- data handle
//
//  Returns:    Same as SetClipboard
//
//  Algorithm:  If SetClipboardData succeeds, stop tracking the handle
//
//  History:    30-Mar-94 AlexT     Created
//
//  Notes:
//
//--------------------------------------------------------------------------

extern "C" HANDLE WINAPI UtSetClipboardData(UINT uFormat, HANDLE hMem)
{
    HANDLE hRet;

    hRet = SetClipboardData(uFormat, hMem);

    if (NULL != hRet)
    {
        gGlobalTrack.cgtStopTracking(hMem);
    }

    return(hRet);
}

#endif  //  DBG==1 && defined(WIN32)