mempool-generic.cpp revision 20593760b116c90f3e439552763eef632a3bbb17
/* $Id$ */
/** @file
* IPRT - Memory Allocation Pool.
*/
/*
* Copyright (C) 2009 Sun Microsystems, Inc.
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
* Clara, CA 95054 USA or visit http://www.sun.com if you need
* additional information or have any questions.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#include <iprt/spinlock.h>
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/** Pointer to a memory pool instance. */
typedef struct RTMEMPOOLINT *PRTMEMPOOLINT;
/** Pointer to a memory pool entry. */
typedef struct RTMEMPOOLENTRY *PRTMEMPOOLENTRY;
/**
* Memory pool entry.
*/
typedef struct RTMEMPOOLENTRY
{
/** Pointer to the pool */
/** Pointer to the next entry. */
PRTMEMPOOLENTRY volatile pNext;
/** Pointer to the previous entry. */
PRTMEMPOOLENTRY volatile pPrev;
/** The number of references to the pool entry. */
/**
* Memory pool instance data.
*/
typedef struct RTMEMPOOLINT
{
/** Magic number (RTMEMPOOL_MAGIC). */
/** Spinlock protecting the pool entry list updates. */
/** Head entry pointer. */
PRTMEMPOOLENTRY volatile pHead;
/** The number of entries in the pool (for statistical purposes). */
/** User data assoicated with the pool. */
void *pvUser;
/** The pool name. (variable length) */
char szName[8];
} RTMEMPOOLINT;
/*******************************************************************************
* Defined Constants And Macros *
*******************************************************************************/
/** Validates a memory pool handle, translating RTMEMPOOL_DEFAULT when found,
* and returns rc if not valid. */
do { \
if (pMemPool == RTMEMPOOL_DEFAULT) \
pMemPool = &g_rtMemPoolDefault; \
else \
{ \
} \
} while (0)
/** Validates a memory pool entry and returns rc if not valid. */
do { \
} while (0)
/*******************************************************************************
* Global Variables *
*******************************************************************************/
/** The */
static RTMEMPOOLINT g_rtMemPoolDefault =
{
/* .u32Magic = */ RTMEMPOOL_MAGIC,
/* .hSpinLock = */ NIL_RTSPINLOCK,
/* .pHead = */ NULL,
/* .cEntries = */ 0,
/* .pvUser = */ NULL,
/* .szName = */ "default"
};
{
if (!pMemPool)
return VERR_NO_MEMORY;
if (RT_SUCCESS(rc))
{
return VINF_SUCCESS;
}
return rc;
}
{
return VERR_NOT_IMPLEMENTED;
}
{
{
if (pHead)
else
}
}
{
{
else
}
else
}
{
if (!pEntry)
return NULL;
return pEntry + 1;
}
{
if (!pEntry)
return NULL;
return pEntry + 1;
}
{
if (!pEntry)
return NULL;
return pEntry + 1;
}
RTDECL(void *) RTMemPoolDupEx(RTMEMPOOL hMemPool, const void *pvSrc, size_t cbSrc, size_t cbExtra) RT_NO_THROW
{
if (!pEntry)
return NULL;
return pEntry + 1;
}
{
/*
* Fend off the odd cases.
*/
if (!cbNew)
{
return NULL;
}
if (!pvOld)
/*
* Real realloc.
*/
/*
* Unlink it from the current pool and try reallocate it.
*/
if (!pEntry)
{
return NULL;
}
return pEntry + 1;
}
{
}
{
return cRefs;
}
{
if (!pv)
return 0;
if (!cRefs)
{
}
return cRefs;
}