VirtualBox

source: vbox/trunk/src/VBox/Runtime/r3/alloc.cpp@ 3672

Last change on this file since 3672 was 3672, checked in by vboxsync, 17 years ago

RT_OS_* and RT_ARCH_* for Runtime/ and Support/

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.5 KB
Line 
1/* $Id: alloc.cpp 3672 2007-07-17 12:39:30Z 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 * If you received this file as part of a commercial VirtualBox
18 * distribution, then only the terms of your commercial VirtualBox
19 * license agreement apply instead of the previous paragraph.
20 */
21
22
23/*******************************************************************************
24* Header Files *
25*******************************************************************************/
26#include "alloc-ef.h"
27#include <iprt/alloc.h>
28#include <iprt/assert.h>
29#include <iprt/param.h>
30#include <iprt/string.h>
31
32#include <stdlib.h>
33
34
35/**
36 * Allocates temporary memory.
37 *
38 * Temporary memory blocks are used for not too large memory blocks which
39 * are believed not to stick around for too long. Using this API instead
40 * of RTMemAlloc() not only gives the heap manager room for optimization
41 * but makes the code easier to read.
42 *
43 * @returns Pointer to the allocated memory.
44 * @returns NULL on failure.
45 * @param cb Size in bytes of the memory block to allocate.
46 */
47RTDECL(void *) RTMemTmpAlloc(size_t cb)
48{
49 return RTMemAlloc(cb);
50}
51
52
53/**
54 * Allocates zero'ed temporary memory.
55 *
56 * Same as RTMemTmpAlloc() but the memory will be zero'ed.
57 *
58 * @returns Pointer to the allocated memory.
59 * @returns NULL on failure.
60 * @param cb Size in bytes of the memory block to allocate.
61 */
62RTDECL(void *) RTMemTmpAllocZ(size_t cb)
63{
64 return RTMemAllocZ(cb);
65}
66
67
68/**
69 * Free temporary memory.
70 *
71 * @param pv Pointer to memory block.
72 */
73RTDECL(void) RTMemTmpFree(void *pv)
74{
75 RTMemFree(pv);
76}
77
78
79/**
80 * Allocates memory.
81 *
82 * @returns Pointer to the allocated memory.
83 * @returns NULL on failure.
84 * @param cb Size in bytes of the memory block to allocate.
85 */
86RTDECL(void *) RTMemAlloc(size_t cb)
87{
88#ifdef RTALLOC_USE_EFENCE
89 void *pv = rtMemAlloc("Alloc", RTMEMTYPE_RTMEMALLOC, cb, ((void **)&cb)[-1], 0, NULL, NULL);
90
91#else /* !RTALLOC_USE_EFENCE */
92
93 AssertMsg(cb, ("Allocating ZERO bytes is really not a good idea! Good luck with the next assertion!\n"));
94 void *pv = malloc(cb);
95 AssertMsg(pv, ("malloc(%d) failed!!!\n", cb));
96#ifdef RT_OS_OS2 /* temporary workaround until libc062. */
97 AssertMsg( cb < 32
98 || !((uintptr_t)pv & (RTMEM_ALIGNMENT - 1)), ("pv=%p RTMEM_ALIGNMENT=%#x\n", pv, RTMEM_ALIGNMENT));
99#else
100 AssertMsg( cb < RTMEM_ALIGNMENT
101 || !((uintptr_t)pv & (RTMEM_ALIGNMENT - 1)), ("pv=%p RTMEM_ALIGNMENT=%#x\n", pv, RTMEM_ALIGNMENT));
102#endif
103#endif /* !RTALLOC_USE_EFENCE */
104 return pv;
105}
106
107
108/**
109 * Allocates zero'ed memory.
110 *
111 * Instead of memset(pv, 0, sizeof()) use this when you want zero'ed
112 * memory. This keeps the code smaller and the heap can skip the memset
113 * in about 0.42% of the calls :-).
114 *
115 * @returns Pointer to the allocated memory.
116 * @returns NULL on failure.
117 * @param cb Size in bytes of the memory block to allocate.
118 */
119RTDECL(void *) RTMemAllocZ(size_t cb)
120{
121#ifdef RTALLOC_USE_EFENCE
122 void *pv = rtMemAlloc("AllocZ", RTMEMTYPE_RTMEMALLOCZ, cb, ((void **)&cb)[-1], 0, NULL, NULL);
123
124#else /* !RTALLOC_USE_EFENCE */
125
126 AssertMsg(cb, ("Allocating ZERO bytes is really not a good idea! Good luck with the next assertion!\n"));
127
128 void *pv = calloc(1, cb);
129 AssertMsg(pv, ("calloc(1,%d) failed!!!\n", cb));
130#ifdef RT_OS_OS2 /* temporary workaround until libc062. */
131 AssertMsg( cb < 32
132 || !((uintptr_t)pv & (RTMEM_ALIGNMENT - 1)), ("pv=%p RTMEM_ALIGNMENT=%#x\n", pv, RTMEM_ALIGNMENT));
133#else
134 AssertMsg( cb < RTMEM_ALIGNMENT
135 || !((uintptr_t)pv & (RTMEM_ALIGNMENT - 1)), ("pv=%p RTMEM_ALIGNMENT=%#x\n", pv, RTMEM_ALIGNMENT));
136#endif
137#endif /* !RTALLOC_USE_EFENCE */
138 return pv;
139}
140
141
142/**
143 * Reallocates memory.
144 *
145 * @returns Pointer to the allocated memory.
146 * @returns NULL on failure.
147 * @param pvOld The memory block to reallocate.
148 * @param cbNew The new block size (in bytes).
149 */
150RTDECL(void *) RTMemRealloc(void *pvOld, size_t cbNew)
151{
152#ifdef RTALLOC_USE_EFENCE
153 void *pv = rtMemRealloc("Realloc", RTMEMTYPE_RTMEMREALLOC, pvOld, cbNew, ((void **)&pvOld)[-1], 0, NULL, NULL);
154
155#else /* !RTALLOC_USE_EFENCE */
156
157 void *pv = realloc(pvOld, cbNew);
158 AssertMsg(pv && cbNew, ("realloc(%p, %d) failed!!!\n", pvOld, cbNew));
159#ifdef RT_OS_OS2 /* temporary workaround until libc062. */
160 AssertMsg( cbNew < 32
161 || !((uintptr_t)pv & (RTMEM_ALIGNMENT - 1)), ("pv=%p RTMEM_ALIGNMENT=%#x\n", pv, RTMEM_ALIGNMENT));
162#else
163 AssertMsg( cbNew < RTMEM_ALIGNMENT
164 || !((uintptr_t)pv & (RTMEM_ALIGNMENT - 1)), ("pv=%p RTMEM_ALIGNMENT=%#x\n", pv, RTMEM_ALIGNMENT));
165#endif
166#endif /* !RTALLOC_USE_EFENCE */
167 return pv;
168}
169
170
171/**
172 * Free memory related to a virtual machine
173 *
174 * @param pv Pointer to memory block.
175 */
176RTDECL(void) RTMemFree(void *pv)
177{
178 if (pv)
179#ifdef RTALLOC_USE_EFENCE
180 rtMemFree("Free", RTMEMTYPE_RTMEMFREE, pv, ((void **)&pv)[-1], 0, NULL, NULL);
181#else
182 free(pv);
183#endif
184}
185
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette