⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 functions.h

📁 新版本TR1的stl
💻 H
📖 第 1 页 / 共 2 页
字号:
/* /////////////////////////////////////////////////////////////////////////
 * File:        winstl/shell/memory/functions.h
 *
 * Purpose:     Shell memory functions.
 *
 * Created:     2nd March 1996
 * Updated:     14th January 2007
 *
 * Home:        http://stlsoft.org/
 *
 * Copyright (c) 1996-2007, Matthew Wilson and Synesis Software
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice, this
 *   list of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright notice,
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 * - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of
 *   any contributors may be used to endorse or promote products derived from
 *   this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * ////////////////////////////////////////////////////////////////////// */


/** \file winstl/shell/memory/functions.h
 *
 * \brief [C, C++] Shell memory functions
 *   (\ref group__library__memory "Memory" Library).
 */

#ifndef WINSTL_INCL_WINSTL_SHELL_MEMORY_H_FUNCTIONS
#define WINSTL_INCL_WINSTL_SHELL_MEMORY_H_FUNCTIONS

#ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
# define WINSTL_VER_WINSTL_SHELL_MEMORY_H_FUNCTIONS_MAJOR       6
# define WINSTL_VER_WINSTL_SHELL_MEMORY_H_FUNCTIONS_MINOR       0
# define WINSTL_VER_WINSTL_SHELL_MEMORY_H_FUNCTIONS_REVISION    2
# define WINSTL_VER_WINSTL_SHELL_MEMORY_H_FUNCTIONS_EDIT        50
#endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */

/* /////////////////////////////////////////////////////////////////////////
 * Compatibility
 */

/*
[DocumentationStatus:Ready]
 */

/* /////////////////////////////////////////////////////////////////////////
 * Includes
 */

#ifndef WINSTL_INCL_WINSTL_H_WINSTL
# include <winstl/winstl.h>
#endif /* !WINSTL_INCL_WINSTL_H_WINSTL */
#include <shlobj.h>

/* /////////////////////////////////////////////////////////////////////////
 * Namespace
 */

#if !defined(_WINSTL_NO_NAMESPACE) && \
    !defined(STLSOFT_DOCUMENTATION_SKIP_SECTION)
# if defined(_STLSOFT_NO_NAMESPACE)
/* There is no stlsoft namespace, so must define ::winstl */
namespace winstl
{
# else
/* Define stlsoft::winstl_project */

namespace stlsoft
{

namespace winstl_project
{

# endif /* _STLSOFT_NO_NAMESPACE */
#endif /* !_WINSTL_NO_NAMESPACE */

/* /////////////////////////////////////////////////////////////////////////
 * C functions
 */

/** \brief [C only] Allocates a block of shell memory.
 *
 * \ingroup group__library__memory
 *
 * This function uses the shell allocator to allocate a memory block.
 *
 * \param cb The size, in bytes, of the memory block to be allocated.
 * \return Pointer to the allocated memory block, or NULL if the request
 *   failed.
 *
 * \remarks On failure, the function will set the thread error information,
 *   which may be retrieved by the Win32 API function
 *   <code>GetLastError()</code>
 *
 * \note [C++] This function is wrapped by the winstl::SHMemAlloc()
 *   function.
 */
STLSOFT_INLINE void *winstl__SHMemAlloc(ws_size_t cb)
{
    LPMALLOC    lpmalloc;
    void        *pv;
    HRESULT     hr  =   STLSOFT_NS_GLOBAL(SHGetMalloc)(&lpmalloc);

    if(SUCCEEDED(hr))
    {
        pv = WINSTL_ITF_CALL(lpmalloc)->Alloc(WINSTL_ITF_THIS(lpmalloc) cb);
        WINSTL_ITF_CALL(lpmalloc)->Release(WINSTL_ITF_THIS0(lpmalloc));

        if(NULL == pv)
        {
            STLSOFT_NS_GLOBAL(SetLastError)(ERROR_NOT_ENOUGH_MEMORY);
        }
    }
    else
    {
        STLSOFT_NS_GLOBAL(SetLastError)(stlsoft_static_cast(DWORD, hr));
        pv = NULL;
    }

    return pv;
}

/** \brief [C only] Deallocates a block of shell memory previously allocated
 *    by a call to winstl__SHMemAlloc(), winstl__SHMemRealloc() or through
 *    the <code>IMalloc::Alloc()</code> or <code>IMalloc::Realloc()</code>
 *    methods on the shell allocator (obtained via <code>SHGetMalloc()</code>).
 *
 * \ingroup group__library__memory
 *
 * This function uses the shell allocator to allocate a memory block.
 *
 * \param pv Pointer to the memory block to be deallocated
 *
 * \remarks On failure, the function will set the thread error information,
 *   which may be retrieved by the Win32 API function
 *   <code>GetLastError()</code>
 *
 * \note [C++] This function is wrapped by the winstl::SHMemFree()
 *   function.
 */
STLSOFT_INLINE void winstl__SHMemFree(void *pv)
{
    LPMALLOC    lpmalloc;
    HRESULT     hr  =   STLSOFT_NS_GLOBAL(SHGetMalloc)(&lpmalloc);

    if(SUCCEEDED(hr))
    {
        WINSTL_ITF_CALL(lpmalloc)->Free(WINSTL_ITF_THIS(lpmalloc) pv);
        WINSTL_ITF_CALL(lpmalloc)->Release(WINSTL_ITF_THIS0(lpmalloc));
    }
    else
    {
        STLSOFT_NS_GLOBAL(SetLastError)(stlsoft_static_cast(DWORD, hr));
    }
}

#if defined(STLSOFT_COMPILER_IS_GCC) && \
    __GNUC__ < 4 && \
    __GNUC_MINOR__ < 3

/* GCC pre 3.3 contains an invalid definition of IMalloc, where
 * the Realloc() method is called ReAlloc().
 *
 * We use a bit of casting to get round it here.
 */

# undef INTERFACE
# define INTERFACE IMallocGcc32
DECLARE_INTERFACE_(IMallocGcc32,IUnknown)
{
    STDMETHOD(QueryInterface)(THIS_ REFIID,PVOID*) PURE;
    STDMETHOD_(ULONG,AddRef)(THIS) PURE;
    STDMETHOD_(ULONG,Release)(THIS) PURE;
    STDMETHOD_(void*,Alloc)(THIS_ ULONG) PURE;
    STDMETHOD_(void*,Realloc)(THIS_ void*,ULONG) PURE;
    STDMETHOD_(void,Free)(THIS_ void*) PURE;
    STDMETHOD_(ULONG,GetSize)(THIS_ void*) PURE;
    STDMETHOD_(int,DidAlloc)(THIS_ void*) PURE;
    STDMETHOD_(void,HeapMinimize)(THIS) PURE;
};

#endif /* compiler */





/** \brief [C only] Rellocates a block of shell memory.
 *
 * \ingroup group__library__memory
 *
 * This function uses the shell allocator to allocate a memory block.
 *
 * \param pv Pointer to the memory block to be reallocated. Can be NULL, in
 *   which case the function acts like SHMemAlloc()
 * \param cb The size, in bytes, of the memory block to be reallocated. Can
 *   be 0, in which case the function acts like SHMemFree() (if pv is not
 *   NULL), or like SHMemAlloc() (if pv is NULL).
 * \return Pointer to the allocated memory block, or NULL if the request
 *   failed or cb is 0 and pv is not NULL.
 *
 * \remarks On failure, the function will set the thread error information,
 *   which may be retrieved by the Win32 API function
 *   <code>GetLastError()</code>
 *
 * \note [C++] This function is wrapped by the winstl::SHMemRealloc()
 *   function.
 */

STLSOFT_INLINE void *winstl__SHMemRealloc(void *pv, ws_size_t cb)
{
#if defined(STLSOFT_COMPILER_IS_GCC) && \
    __GNUC__ < 4 && \
    __GNUC_MINOR__ < 3

    IMallocGcc32    *lpmalloc;
#else /* ? compiler */
    LPMALLOC        lpmalloc;
#endif /* compiler */
    void            *pvNew;
    HRESULT         hr  =   STLSOFT_NS_GLOBAL(SHGetMalloc)(stlsoft_reinterpret_cast(LPMALLOC*, &lpmalloc));

    if(SUCCEEDED(hr))
    {
        pvNew = WINSTL_ITF_CALL(lpmalloc)->Realloc(WINSTL_ITF_THIS(lpmalloc) pv, cb);
        WINSTL_ITF_CALL(lpmalloc)->Release(WINSTL_ITF_THIS0(lpmalloc));

        if( NULL == pvNew &&
            0 != cb &&
            NULL != pv)
        {
            STLSOFT_NS_GLOBAL(SetLastError)(ERROR_NOT_ENOUGH_MEMORY);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -