mempool.h revision ad27e1d5e48ca41245120c331cc88b50464813ce
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson/** @file
7d32c065c7bb56f281651ae3dd2888f32ce4f1d9Bob Halley * IPRT - Memory Allocation Pool.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson */
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson/*
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * Copyright (C) 2009 Oracle Corporation
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson *
15a44745412679c30a6d022733925af70a38b715David Lawrence * This file is part of VirtualBox Open Source Edition (OSE), as
15a44745412679c30a6d022733925af70a38b715David Lawrence * available from http://www.virtualbox.org. This file is free software;
15a44745412679c30a6d022733925af70a38b715David Lawrence * you can redistribute it and/or modify it under the terms of the GNU
15a44745412679c30a6d022733925af70a38b715David Lawrence * General Public License (GPL) as published by the Free Software
15a44745412679c30a6d022733925af70a38b715David Lawrence * Foundation, in version 2 as it comes in the "COPYING" file of the
15a44745412679c30a6d022733925af70a38b715David Lawrence * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15a44745412679c30a6d022733925af70a38b715David Lawrence * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
15a44745412679c30a6d022733925af70a38b715David Lawrence *
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * The contents of this file may alternatively be used under the terms
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * of the Common Development and Distribution License Version 1.0
15a44745412679c30a6d022733925af70a38b715David Lawrence * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
9c3531d72aeaad6c5f01efe6a1c82023e1379e4dDavid Lawrence * VirtualBox OSE distribution, in which case the provisions of the
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * CDDL are applicable instead of those of the GPL.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson *
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * You may elect to license modified versions of this file under the
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * terms and conditions of either the GPL or the CDDL or both.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson */
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#ifndef ___iprt_mempool_h
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#define ___iprt_mempool_h
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#include <iprt/types.h>
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas GustafssonRT_C_DECLS_BEGIN
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson/**
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * Creates a new memory pool.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson *
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * @returns IPRT status code.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson *
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * @param phMemPool Where to return the handle to the new memory
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * pool.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * @param pszName The name of the pool (for debug purposes).
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson */
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas GustafssonRTDECL(int) RTMemPoolCreate(PRTMEMPOOL phMemPool, const char *pszName);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson/**
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * Destroys the specified pool, freeing all the memory it contains.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson *
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * @returns IPRT status code.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson *
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * @param hMemPool The handle to the pool. The nil handle and
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * RTMEMPOOL_DEFAULT are quietly ignored (retval
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * VINF_SUCCESS).
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson */
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas GustafssonRTDECL(int) RTMemPoolDestroy(RTMEMPOOL hMemPool);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson/**
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * Allocates memory.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson *
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * @returns Pointer to the allocated memory.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * @returns NULL on failure.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson *
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * @param hMemPool Handle to the pool to allocate the memory from.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * @param cb Size in bytes of the memory block to allocated.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson */
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas GustafssonRTDECL(void *) RTMemPoolAlloc(RTMEMPOOL hMemPool, size_t cb) RT_NO_THROW;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson/**
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrence * Allocates zero'd memory.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson *
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * Instead of memset(pv, 0, sizeof()) use this when you want zero'd
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * memory. This keeps the code smaller and the heap can skip the memset
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * in about 0.42% of calls :-).
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson *
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * @returns Pointer to the allocated memory.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * @returns NULL on failure.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson *
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * @param hMemPool Handle to the pool to allocate the memory from.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * @param cb Size in bytes of the memory block to allocated.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson */
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas GustafssonRTDECL(void *) RTMemPoolAllocZ(RTMEMPOOL hMemPool, size_t cb) RT_NO_THROW;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson/**
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * Duplicates a chunk of memory into a new heap block.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson *
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * @returns New heap block with the duplicate data.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * @returns NULL if we're out of memory.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson *
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * @param hMemPool Handle to the pool to allocate the memory from.
c1a2cfef7f9156ffd15ded6cca6429a174319cc1Michael Graff * @param pvSrc The memory to duplicate.
c1a2cfef7f9156ffd15ded6cca6429a174319cc1Michael Graff * @param cb The amount of memory to duplicate.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson */
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas GustafssonRTDECL(void *) RTMemPoolDup(RTMEMPOOL hMemPool, const void *pvSrc, size_t cb) RT_NO_THROW;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson/**
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * Duplicates a chunk of memory into a new heap block with some
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * additional zeroed memory.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson *
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * @returns New heap block with the duplicate data.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * @returns NULL if we're out of memory.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson *
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * @param hMemPool Handle to the pool to allocate the memory from.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * @param pvSrc The memory to duplicate.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * @param cbSrc The amount of memory to duplicate.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * @param cbExtra The amount of extra memory to allocate and zero.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson */
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas GustafssonRTDECL(void *) RTMemPoolDupEx(RTMEMPOOL hMemPool, const void *pvSrc, size_t cbSrc, size_t cbExtra) RT_NO_THROW;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson/**
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * Reallocates memory.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson *
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * @returns Pointer to the allocated memory.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * @returns NULL on failure.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson *
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * @param hMemPool Handle to the pool containing the old memory.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * @param pvOld The memory block to reallocate.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * @param cbNew The new block size (in bytes).
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson */
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas GustafssonRTDECL(void *) RTMemPoolRealloc(RTMEMPOOL hMemPool, void *pvOld, size_t cbNew) RT_NO_THROW;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson/**
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * Frees memory allocated from a pool.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson *
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * @param hMemPool Handle to the pool containing the memory. Passing
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * NIL here is fine, but it may come at a slight
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * performance cost.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * @param pv Pointer to memory block.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson *
97404c1965ae83ecbfe9cf7b06f67dce5e28c588Andreas Gustafsson * @remarks This is the same a RTMemPoolRelease but included here as a separate
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * function to simplify code migration.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson */
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas GustafssonRTDECL(void) RTMemPoolFree(RTMEMPOOL hMemPool, void *pv) RT_NO_THROW;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson/**
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * Retains a reference to a memory block in a pool.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson *
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * @returns New reference count, UINT32_MAX on error (asserted).
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson *
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * @param pv Pointer to memory block.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson */
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas GustafssonRTDECL(uint32_t) RTMemPoolRetain(void *pv) RT_NO_THROW;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson/**
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * Releases a reference to a memory block in a pool.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson *
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * @returns New reference count, UINT32_MAX on error (asserted).
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson *
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * @param hMemPool Handle to the pool containing the memory. Passing
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * NIL here is fine, but it may come at a slight
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * performance cost.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * @param pv Pointer to memory block.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson */
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas GustafssonRTDECL(uint32_t) RTMemPoolRelease(RTMEMPOOL hMemPool, void *pv) RT_NO_THROW;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson/**
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * Get the current reference count.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson *
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * @returns The reference count, UINT32_MAX on error (asserted).
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * @param pv Pointer to memory block.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson */
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas GustafssonRTDECL(uint32_t) RTMemPoolRefCount(void *pv) RT_NO_THROW;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
c1a2cfef7f9156ffd15ded6cca6429a174319cc1Michael GraffRT_C_DECLS_END
c1a2cfef7f9156ffd15ded6cca6429a174319cc1Michael Graff
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#endif
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson