VirtualBox

source: vbox/trunk/src/VBox/Runtime/r0drv/alloc-r0drv.cpp@ 4867

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

Biggest check-in ever. New source code headers for all (C) innotek files.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 5.0 KB
Line 
1/* $Id: alloc-r0drv.cpp 4071 2007-08-07 17:07:59Z vboxsync $ */
2/** @file
3 * innotek Portable Runtime - Memory Allocation, Ring-0 Driver.
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/string.h>
23#include <iprt/alloc.h>
24#include <iprt/assert.h>
25#include <iprt/param.h>
26#include "r0drv/alloc-r0drv.h"
27
28
29/**
30 * Allocates temporary memory.
31 *
32 * Temporary memory blocks are used for not too large memory blocks which
33 * are believed not to stick around for too long. Using this API instead
34 * of RTMemAlloc() not only gives the heap manager room for optimization
35 * but makes the code easier to read.
36 *
37 * @returns Pointer to the allocated memory.
38 * @returns NULL on failure.
39 * @param cb Size in bytes of the memory block to allocated.
40 */
41RTDECL(void *) RTMemTmpAlloc(size_t cb)
42{
43 return RTMemAlloc(cb);
44}
45
46
47/**
48 * Allocates zero'ed temporary memory.
49 *
50 * Same as RTMemTmpAlloc() but the memory will be zero'ed.
51 *
52 * @returns Pointer to the allocated memory.
53 * @returns NULL on failure.
54 * @param cb Size in bytes of the memory block to allocated.
55 */
56RTDECL(void *) RTMemTmpAllocZ(size_t cb)
57{
58 return RTMemAllocZ(cb);
59}
60
61
62/**
63 * Free temporary memory.
64 *
65 * @param pv Pointer to memory block.
66 */
67RTDECL(void) RTMemTmpFree(void *pv)
68{
69 return RTMemFree(pv);
70}
71
72
73/**
74 * Allocates memory.
75 *
76 * @returns Pointer to the allocated memory.
77 * @returns NULL on failure.
78 * @param cb Size in bytes of the memory block to allocated.
79 */
80RTDECL(void *) RTMemAlloc(size_t cb)
81{
82 PRTMEMHDR pHdr = rtMemAlloc(cb, 0);
83 if (pHdr)
84 return pHdr + 1;
85 return NULL;
86}
87
88
89/**
90 * Allocates zero'ed memory.
91 *
92 * Instead of memset(pv, 0, sizeof()) use this when you want zero'ed
93 * memory. This keeps the code smaller and the heap can skip the memset
94 * in about 0.42% of calls :-).
95 *
96 * @returns Pointer to the allocated memory.
97 * @returns NULL on failure.
98 * @param cb Size in bytes of the memory block to allocated.
99 */
100RTDECL(void *) RTMemAllocZ(size_t cb)
101{
102 PRTMEMHDR pHdr = rtMemAlloc(cb, RTMEMHDR_FLAG_ZEROED);
103 if (pHdr)
104 return memset(pHdr + 1, 0, pHdr->cb);
105 return NULL;
106}
107
108
109/**
110 * Reallocates memory.
111 *
112 * @returns Pointer to the allocated memory.
113 * @returns NULL on failure.
114 * @param pvOld The memory block to reallocate.
115 * @param cbNew The new block size (in bytes).
116 */
117RTDECL(void *) RTMemRealloc(void *pvOld, size_t cbNew)
118{
119 if (!cbNew)
120 RTMemFree(pvOld);
121 else if (!pvOld)
122 return RTMemAlloc(cbNew);
123 else
124 {
125 PRTMEMHDR pHdrOld = (PRTMEMHDR)pvOld - 1;
126 if (pHdrOld->u32Magic == RTMEMHDR_MAGIC)
127 {
128 PRTMEMHDR pHdrNew;
129 if (pHdrOld->cb >= cbNew && pHdrOld->cb - cbNew <= 128)
130 return pvOld;
131 pHdrNew = rtMemAlloc(cbNew, 0);
132 if (pHdrNew)
133 {
134 size_t cbCopy = RT_MIN(pHdrOld->cb, pHdrNew->cb);
135 memcpy(pHdrNew + 1, pvOld, cbCopy);
136 rtMemFree(pHdrOld);
137 return pHdrNew + 1;
138 }
139 }
140 else
141 AssertMsgFailed(("pHdrOld->u32Magic=%RX32 pvOld=%p cbNew=%#zx\n", pHdrOld->u32Magic, pvOld, cbNew));
142 }
143
144 return NULL;
145}
146
147
148/**
149 * Free memory related to an virtual machine
150 *
151 * @param pv Pointer to memory block.
152 */
153RTDECL(void) RTMemFree(void *pv)
154{
155 PRTMEMHDR pHdr;
156 if (!pv)
157 return;
158 pHdr = (PRTMEMHDR)pv - 1;
159 if (pHdr->u32Magic == RTMEMHDR_MAGIC)
160 {
161 Assert(!(pHdr->fFlags & RTMEMHDR_FLAG_EXEC));
162 rtMemFree(pHdr);
163 }
164 else
165 AssertMsgFailed(("pHdr->u32Magic=%RX32 pv=%p\n", pHdr->u32Magic, pv));
166}
167
168
169/**
170 * Allocates memory which may contain code.
171 *
172 * @returns Pointer to the allocated memory.
173 * @returns NULL on failure.
174 * @param cb Size in bytes of the memory block to allocate.
175 */
176RTDECL(void *) RTMemExecAlloc(size_t cb)
177{
178 PRTMEMHDR pHdr = rtMemAlloc(cb, RTMEMHDR_FLAG_EXEC);
179 if (pHdr)
180 return pHdr + 1;
181 return NULL;
182}
183
184
185/**
186 * Free executable/read/write memory allocated by RTMemExecAlloc().
187 *
188 * @param pv Pointer to memory block.
189 */
190RTDECL(void) RTMemExecFree(void *pv)
191{
192 PRTMEMHDR pHdr;
193 if (!pv)
194 return;
195 pHdr = (PRTMEMHDR)pv - 1;
196 if (pHdr->u32Magic == RTMEMHDR_MAGIC)
197 rtMemFree(pHdr);
198 else
199 AssertMsgFailed(("pHdr->u32Magic=%RX32 pv=%p\n", pHdr->u32Magic, pv));
200}
201
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