alloc.cpp revision 6cd47f43e2d859210588a870c49b1b122308b170
2N/A/* $Id$ */
2N/A/** @file
2N/A * innotek Portable Runtime - Memory Allocation.
2N/A */
2N/A
2N/A/*
2N/A * Copyright (C) 2006-2007 innotek GmbH
2N/A *
2N/A * This file is part of VirtualBox Open Source Edition (OSE), as
2N/A * available from http://www.virtualbox.org. This file is free software;
2N/A * you can redistribute it and/or modify it under the terms of the GNU
2N/A * General Public License as published by the Free Software Foundation,
2N/A * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
2N/A * distribution. VirtualBox OSE is distributed in the hope that it will
2N/A * be useful, but WITHOUT ANY WARRANTY of any kind.
2N/A */
2N/A
2N/A
2N/A/*******************************************************************************
2N/A* Header Files *
2N/A*******************************************************************************/
2N/A#include <iprt/alloc.h>
2N/A#include <iprt/assert.h>
2N/A#include <iprt/string.h>
2N/A
2N/A
2N/A/**
2N/A * Duplicates a chunk of memory into a new heap block.
2N/A *
2N/A * @returns New heap block with the duplicate data.
2N/A * @returns NULL if we're out of memory.
2N/A * @param pvSrc The memory to duplicate.
2N/A * @param cb The amount of memory to duplicate.
2N/A */
2N/ARTDECL(void *) RTMemDup(const void *pvSrc, size_t cb)
2N/A{
2N/A void *pvDst = RTMemAlloc(cb);
2N/A if (pvDst)
2N/A memcpy(pvDst, pvSrc, cb);
2N/A return pvDst;
2N/A}
2N/A
2N/A
2N/A/**
2N/A * Duplicates a chunk of memory into a new heap block with some
2N/A * additional zeroed memory.
2N/A *
2N/A * @returns New heap block with the duplicate data.
2N/A * @returns NULL if we're out of memory.
2N/A * @param pvSrc The memory to duplicate.
2N/A * @param cbSrc The amount of memory to duplicate.
2N/A * @param cbExtra The amount of extra memory to allocate and zero.
*/
RTDECL(void *) RTMemDupEx(const void *pvSrc, size_t cbSrc, size_t cbExtra)
{
void *pvDst = RTMemAlloc(cbSrc + cbExtra);
if (pvDst)
{
memcpy(pvDst, pvSrc, cbSrc);
memset((uint8_t *)pvDst + cbSrc, 0, cbExtra);
}
return pvDst;
}