1 | /* $Id: alloc.cpp 4071 2007-08-07 17:07:59Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * innotek Portable Runtime - Memory Allocation.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 innotek GmbH
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License as published by the Free Software Foundation,
|
---|
13 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
14 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
15 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 |
|
---|
19 | /*******************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *******************************************************************************/
|
---|
22 | #include <iprt/alloc.h>
|
---|
23 | #include <iprt/assert.h>
|
---|
24 | #include <iprt/string.h>
|
---|
25 |
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Duplicates a chunk of memory into a new heap block.
|
---|
29 | *
|
---|
30 | * @returns New heap block with the duplicate data.
|
---|
31 | * @returns NULL if we're out of memory.
|
---|
32 | * @param pvSrc The memory to duplicate.
|
---|
33 | * @param cb The amount of memory to duplicate.
|
---|
34 | */
|
---|
35 | RTDECL(void *) RTMemDup(const void *pvSrc, size_t cb)
|
---|
36 | {
|
---|
37 | void *pvDst = RTMemAlloc(cb);
|
---|
38 | if (pvDst)
|
---|
39 | memcpy(pvDst, pvSrc, cb);
|
---|
40 | return pvDst;
|
---|
41 | }
|
---|
42 |
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Duplicates a chunk of memory into a new heap block with some
|
---|
46 | * additional zeroed memory.
|
---|
47 | *
|
---|
48 | * @returns New heap block with the duplicate data.
|
---|
49 | * @returns NULL if we're out of memory.
|
---|
50 | * @param pvSrc The memory to duplicate.
|
---|
51 | * @param cbSrc The amount of memory to duplicate.
|
---|
52 | * @param cbExtra The amount of extra memory to allocate and zero.
|
---|
53 | */
|
---|
54 | RTDECL(void *) RTMemDupEx(const void *pvSrc, size_t cbSrc, size_t cbExtra)
|
---|
55 | {
|
---|
56 | void *pvDst = RTMemAlloc(cbSrc + cbExtra);
|
---|
57 | if (pvDst)
|
---|
58 | {
|
---|
59 | memcpy(pvDst, pvSrc, cbSrc);
|
---|
60 | memset((uint8_t *)pvDst + cbSrc, 0, cbExtra);
|
---|
61 | }
|
---|
62 | return pvDst;
|
---|
63 | }
|
---|
64 |
|
---|
65 |
|
---|