Exemplo n.º 1
0
static void test_IMalloc(void)
{
    LPVOID lpMem;
    ULONG ulRef;
    int iRet;
    HRESULT hRet;
    LPMALLOC lpMalloc;
    LPVOID lpVoid;

    pMAPIGetDefaultMalloc = (void*)GetProcAddress(hMapi32,
                            "MAPIGetDefaultMalloc@0");
    if (!pMAPIGetDefaultMalloc)
    {
        win_skip("MAPIGetDefaultMalloc is not available\n");
        return;
    }

    lpMalloc = pMAPIGetDefaultMalloc();
    ok(lpMalloc != NULL, "Expected MAPIGetDefaultMalloc to return non-NULL\n");
    if (!lpMalloc)
    {
        skip("MAPIGetDefaultMalloc failed\n");
        return;
    }

    lpVoid = NULL;
    hRet = IMalloc_QueryInterface(lpMalloc, &IID_IUnknown, &lpVoid);
    ok (hRet == S_OK && lpVoid != NULL,
        "IID_IUnknown: expected S_OK, non-null, got 0x%08x, %p\n",
        hRet, lpVoid);

    lpVoid = NULL;
    hRet = IMalloc_QueryInterface(lpMalloc, &IID_IMalloc, &lpVoid);
    ok (hRet == S_OK && lpVoid != NULL,
        "IID_IIMalloc: expected S_OK, non-null, got 0x%08x, %p\n",
        hRet, lpVoid);

    /* Prove that native mapi uses LocalAlloc/LocalFree */
    lpMem = IMalloc_Alloc(lpMalloc, 61);
    ok (lpMem && IMalloc_GetSize(lpMalloc, lpMem) == LocalSize(lpMem),
        "Expected non-null, same size, got %p, %s size\n", lpMem,
        lpMem ? "different" : "same");

    iRet = IMalloc_DidAlloc(lpMalloc, lpMem);
    ok (iRet == -1, "DidAlloc, expected -1. got %d\n", iRet);

    IMalloc_HeapMinimize(lpMalloc);

    LocalFree(lpMem);

    ulRef = IMalloc_AddRef(lpMalloc);
    ok (ulRef == 1u, "AddRef expected 1, returned %d\n", ulRef);

    ulRef = IMalloc_Release(lpMalloc);
    ok (ulRef == 1u, "AddRef expected 1, returned %d\n", ulRef);

    IMalloc_Release(lpMalloc);
}
Exemplo n.º 2
0
static void set_buffer(LPWSTR *buffer, LPCWSTR string)
{
    static const WCHAR empty_string[] = {0};
    IMalloc *malloc;
    ULONG cb;

    if (string == NULL)
        string = empty_string;
    CoGetMalloc(1, &malloc);

    cb = (strlenW(string) + 1)*sizeof(WCHAR);
    if (*buffer == NULL || cb > IMalloc_GetSize(malloc, *buffer))
        *buffer = IMalloc_Realloc(malloc, *buffer, cb);
    memcpy(*buffer, string, cb);
}
Exemplo n.º 3
0
int
main(int argc, char **argv)
{
    void *p;
    com_result_t r;
    int n;
    size_t s;
    IMalloc *allocator;

    (void) argc;
    (void) argv;

    com_init("com.googlecode.libcom.test-1");

    p = CoTaskMemAlloc(64);
    if(NULL == p)
    {
        fprintf(stderr, "Failed to allocate 64 bytes via CoTaskMemAlloc()\n");
        exit(EXIT_FAILURE);
    }
    if(COM_S_OK != (r = CoGetMalloc(1, &allocator)))
    {
        fprintf(stderr, "Failed to obtain reference to task allocator; result = 0x%08x\n", r);
        exit(EXIT_FAILURE);
    }
    if(1 != (n = IMalloc_DidAlloc(allocator, p)))
    {
        if(0 == n)
        {
            fprintf(stderr, "IMalloc::DidAlloc() claims task allocator was not responsible for allocated block\n");
        }
        else
        {
            fprintf(stderr, "IMalloc::DidAlloc() could not determine responsibility for allocated block\n");
        }
        exit(EXIT_FAILURE);
    }
    if(64 != (s = IMalloc_GetSize(allocator, p)))
    {
        fprintf(stderr, "IMalloc::GetSize() returned an incorrect size (%ul bytes)\n", s);
        exit(EXIT_FAILURE);
    }
    IMalloc_Free(allocator, p);
    puts("PASS");

    com_shutdown();
    return 0;
}