| 
 | 1 | +// Licensed to the .NET Foundation under one or more agreements.  | 
 | 2 | +// The .NET Foundation licenses this file to you under the MIT license.  | 
 | 3 | + | 
 | 4 | +#include <Windows.h>  | 
 | 5 | +#include <heapapi.h>  | 
 | 6 | +#include <intsafe.h>  | 
 | 7 | +#include <crtdbg.h> /* _ASSERTE */  | 
 | 8 | + | 
 | 9 | +#include <zlib_allocator.h>  | 
 | 10 | + | 
 | 11 | +/* A custom allocator for zlib that uses a dedicated heap.  | 
 | 12 | +   This provides better performance and avoids fragmentation  | 
 | 13 | +   that can occur on Windows when using the default process heap.  | 
 | 14 | + */  | 
 | 15 | + | 
 | 16 | +// Gets the special heap we'll allocate from.  | 
 | 17 | +HANDLE GetZlibHeap()  | 
 | 18 | +{  | 
 | 19 | +    static HANDLE s_hPublishedHeap = NULL;  | 
 | 20 | + | 
 | 21 | +    // If already initialized, return immediately.  | 
 | 22 | +    // We don't need a volatile read here since the publish is performed with release semantics.  | 
 | 23 | +    if (s_hPublishedHeap != NULL) { return s_hPublishedHeap; }  | 
 | 24 | + | 
 | 25 | +    // Attempt to create a new heap. The heap will be dynamically sized.  | 
 | 26 | +    HANDLE hNewHeap = HeapCreate(0, 0, 0);  | 
 | 27 | + | 
 | 28 | +    if (hNewHeap != NULL)  | 
 | 29 | +    {  | 
 | 30 | +        // We created a new heap. Attempt to publish it.  | 
 | 31 | +        if (InterlockedCompareExchangePointer(&s_hPublishedHeap, hNewHeap, NULL) != NULL)  | 
 | 32 | +        {  | 
 | 33 | +            HeapDestroy(hNewHeap); // Somebody published before us. Destroy our heap.  | 
 | 34 | +            hNewHeap = NULL; // Guard against accidental use later in the method.  | 
 | 35 | +        }  | 
 | 36 | +    }  | 
 | 37 | +    else  | 
 | 38 | +    {  | 
 | 39 | +        // If we can't create a new heap, fall back to the process default heap.  | 
 | 40 | +        InterlockedCompareExchangePointer(&s_hPublishedHeap, GetProcessHeap(), NULL);  | 
 | 41 | +    }  | 
 | 42 | + | 
 | 43 | +    // Some thread - perhaps us, perhaps somebody else - published the heap. Return it.  | 
 | 44 | +    // We don't need a volatile read here since the publish is performed with release semantics.  | 
 | 45 | +    _ASSERTE(s_hPublishedHeap != NULL);  | 
 | 46 | +    return s_hPublishedHeap;  | 
 | 47 | +}  | 
 | 48 | + | 
 | 49 | +voidpf z_custom_calloc(opaque, items, size)  | 
 | 50 | +    voidpf opaque;  | 
 | 51 | +    unsigned items;  | 
 | 52 | +    unsigned size;  | 
 | 53 | +{  | 
 | 54 | + | 
 | 55 | +    SIZE_T cbRequested;  | 
 | 56 | +    if (sizeof(items) + sizeof(size) <= sizeof(cbRequested))  | 
 | 57 | +    {  | 
 | 58 | +        // multiplication can't overflow; no need for safeint  | 
 | 59 | +        cbRequested = (SIZE_T)items * (SIZE_T)size;  | 
 | 60 | +    }  | 
 | 61 | +    else  | 
 | 62 | +    {  | 
 | 63 | +        // multiplication can overflow; go through safeint  | 
 | 64 | +        if (FAILED(SIZETMult(items, size, &cbRequested))) { return NULL; }  | 
 | 65 | +    }  | 
 | 66 | + | 
 | 67 | +    return HeapAlloc(GetZlibHeap(), 0, cbRequested);  | 
 | 68 | +}  | 
 | 69 | + | 
 | 70 | +void z_custom_cfree(opaque, ptr)  | 
 | 71 | +    voidpf opaque;  | 
 | 72 | +    voidpf ptr;  | 
 | 73 | +{  | 
 | 74 | +    if (ptr == NULL) { return; } // ok to free nullptr  | 
 | 75 | + | 
 | 76 | +    if (!HeapFree(GetZlibHeap(), 0, ptr)) { goto Fail; }  | 
 | 77 | +    return;  | 
 | 78 | + | 
 | 79 | +Fail:  | 
 | 80 | +    __fastfail(FAST_FAIL_HEAP_METADATA_CORRUPTION);  | 
 | 81 | +}  | 
0 commit comments