Commit made by Daniel, including the implementation of the daemon.
[vdi_driver.git] / src / alloc.cpp
blob6480cdc7656306ec98563af497be7dd8adb22669
1 /* $Id: alloc.cpp 23517 2007-08-07 17:07:59Z umoeller $ */
2 /** @file
3 * innotek Portable Runtime - Memory Allocation.
4 */
6 /*
7 * Copyright (C) 2006-2007 innotek GmbH
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.
19 /*******************************************************************************
20 * Header Files *
21 *******************************************************************************/
22 #include "alloc.h"
23 #include <cstdlib>
24 #include <cstdio>
26 /**
27 * Allocates temporary memory.
29 * Temporary memory blocks are used for not too large memory blocks which
30 * are believed not to stick around for too long. Using this API instead
31 * of RTMemAlloc() not only gives the heap manager room for optimization
32 * but makes the code easier to read.
34 * @returns Pointer to the allocated memory.
35 * @returns NULL on failure.
36 * @param cb Size in bytes of the memory block to allocate.
38 void* RTMemTmpAlloc(size_t cb)
40 return RTMemAlloc(cb);
44 /**
45 * Allocates zero'ed temporary memory.
47 * Same as RTMemTmpAlloc() but the memory will be zero'ed.
49 * @returns Pointer to the allocated memory.
50 * @returns NULL on failure.
51 * @param cb Size in bytes of the memory block to allocate.
53 void* RTMemTmpAllocZ(size_t cb)
55 return RTMemAllocZ(cb);
59 /**
60 * Free temporary memory.
62 * @param pv Pointer to memory block.
64 void RTMemTmpFree(void *pv)
66 RTMemFree(pv);
70 /**
71 * Allocates memory.
73 * @returns Pointer to the allocated memory.
74 * @returns NULL on failure.
75 * @param cb Size in bytes of the memory block to allocate.
77 void* RTMemAlloc(size_t cb)
79 void *pv = malloc(cb);
81 if (!pv)
83 printf("malloc(%d) failed!!!\n", cb);
86 return pv;
90 /**
91 * Allocates zero'ed memory.
93 * Instead of memset(pv, 0, sizeof()) use this when you want zero'ed
94 * memory. This keeps the code smaller and the heap can skip the memset
95 * in about 0.42% of the calls :-).
97 * @returns Pointer to the allocated memory.
98 * @returns NULL on failure.
99 * @param cb Size in bytes of the memory block to allocate.
101 void* RTMemAllocZ(size_t cb)
103 void *pv = calloc(1, cb);
105 if (!pv)
107 printf("calloc(1,%d) failed!!!\n", cb);
110 return pv;
115 * Reallocates memory.
117 * @returns Pointer to the allocated memory.
118 * @returns NULL on failure.
119 * @param pvOld The memory block to reallocate.
120 * @param cbNew The new block size (in bytes).
122 void* RTMemRealloc(void *pvOld, size_t cbNew)
124 void *pv = realloc(pvOld, cbNew);
126 if (!pv)
128 printf("realloc(%p, %d) failed!!!\n", pvOld, cbNew);
131 return pv;
136 * Free memory related to a virtual machine
138 * @param pv Pointer to memory block.
140 void RTMemFree(void *pv)
142 if (pv)
143 free(pv);