VirtualBox

source: vbox/trunk/src/VBox/GuestHost/OpenGL/util/mem.c@ 57541

Last change on this file since 57541 was 57541, checked in by vboxsync, 9 years ago

SharedOpenGL: Use RTMemAlloc and friends to allocate memory, so we can make use of the electrical fence for debugging everywhere

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 2.8 KB
Line 
1/* Copyright (c) 2001, Stanford University
2 * All rights reserved
3 *
4 * See the file LICENSE.txt for information on redistributing this software.
5 */
6
7#include "cr_mem.h"
8#include "cr_error.h"
9
10#include <stdlib.h>
11#include <memory.h>
12
13#include <iprt/types.h>
14#include <iprt/mem.h>
15
16#if DEBUG_MEM
17#include <stdio.h>
18#define THRESHOLD 100 * 1024
19
20#undef crAlloc
21#undef crCalloc
22
23/* Need these stubs because util.def says we're always exporting crAlloc
24 * and crCalloc.
25 */
26extern void *crAlloc( unsigned int bytes );
27void *crAlloc( unsigned int bytes )
28{
29 return NULL;
30}
31
32extern void *crCalloc( unsigned int bytes );
33void *crCalloc( unsigned int bytes )
34{
35 return NULL;
36}
37#endif /* DEBUG_MEM */
38
39
40
41#if DEBUG_MEM
42static void *_crAlloc( unsigned int nbytes )
43#else
44DECLEXPORT(void) *crAlloc( unsigned int nbytes )
45#endif
46{
47#ifdef VBOX
48 void *ret = RTMemAlloc( nbytes );
49#else
50 void *ret = malloc( nbytes );
51#endif
52 if (!ret) {
53 crError( "Out of memory trying to allocate %d bytes!", nbytes );
54 abort();
55 }
56 return ret;
57}
58
59void *crAllocDebug( unsigned int nbytes, const char *file, int line )
60{
61#if DEBUG_MEM
62 if (nbytes >= THRESHOLD)
63 fprintf(stderr, "crAllocDebug(%d bytes) in %s at %d\n", nbytes, file, line);
64 return _crAlloc(nbytes);
65#else
66 return crAlloc(nbytes);
67#endif
68}
69
70#if DEBUG_MEM
71static void *_crCalloc( unsigned int nbytes )
72#else
73DECLEXPORT(void) *crCalloc( unsigned int nbytes )
74#endif
75{
76#ifdef VBOX
77 void *ret = RTMemAlloc( nbytes );
78#else
79 void *ret = malloc( nbytes );
80#endif
81 if (!ret) {
82 crError( "Out of memory trying to (c)allocate %d bytes!", nbytes );
83 abort();
84 }
85 crMemset( ret, 0, nbytes );
86 return ret;
87}
88
89void *crCallocDebug( unsigned int nbytes, const char *file, int line )
90{
91#if DEBUG_MEM
92 if (nbytes >= THRESHOLD)
93 fprintf(stderr, "crCallocDebug(%d bytes) in %s at %d\n", nbytes, file, line);
94 return _crCalloc(nbytes);
95#else
96 return crCalloc(nbytes);
97#endif
98}
99
100DECLEXPORT(void) crRealloc( void **ptr, unsigned int nbytes )
101{
102 if ( *ptr == NULL )
103 {
104#if DEBUG_MEM
105 *ptr = _crAlloc( nbytes );
106#else
107 *ptr = crAlloc( nbytes );
108#endif
109 }
110 else
111 {
112 *ptr = realloc( *ptr, nbytes );
113 if (*ptr == NULL)
114 crError( "Couldn't realloc %d bytes!", nbytes );
115 }
116}
117
118DECLEXPORT(void) crFree( void *ptr )
119{
120#ifdef VBOX
121 if (ptr)
122 RTMemFree(ptr);
123#else
124 if (ptr)
125 free(ptr);
126#endif
127}
128
129DECLEXPORT(void) crMemcpy( void *dst, const void *src, unsigned int bytes )
130{
131 CRASSERT(dst || 0==bytes);
132 CRASSERT(src || 0==bytes);
133 (void) memcpy( dst, src, bytes );
134}
135
136DECLEXPORT(void) crMemset( void *ptr, int value, unsigned int bytes )
137{
138 CRASSERT(ptr);
139 memset( ptr, value, bytes );
140}
141
142DECLEXPORT(void) crMemZero( void *ptr, unsigned int bytes )
143{
144 CRASSERT(ptr);
145 memset( ptr, 0, bytes );
146}
147
148DECLEXPORT(int) crMemcmp( const void *p1, const void *p2, unsigned int bytes )
149{
150 CRASSERT(p1);
151 CRASSERT(p2);
152 return memcmp( p1, p2, bytes );
153}
154
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