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 | */
|
---|
26 | extern void *crAlloc( unsigned int bytes );
|
---|
27 | void *crAlloc( unsigned int bytes )
|
---|
28 | {
|
---|
29 | return NULL;
|
---|
30 | }
|
---|
31 |
|
---|
32 | extern void *crCalloc( unsigned int bytes );
|
---|
33 | void *crCalloc( unsigned int bytes )
|
---|
34 | {
|
---|
35 | return NULL;
|
---|
36 | }
|
---|
37 | #endif /* DEBUG_MEM */
|
---|
38 |
|
---|
39 |
|
---|
40 |
|
---|
41 | #if DEBUG_MEM
|
---|
42 | static void *_crAlloc( unsigned int nbytes )
|
---|
43 | #else
|
---|
44 | DECLEXPORT(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 |
|
---|
59 | void *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 | RT_NOREF2(file, line);
|
---|
67 | return crAlloc(nbytes);
|
---|
68 | #endif
|
---|
69 | }
|
---|
70 |
|
---|
71 | #if DEBUG_MEM
|
---|
72 | static void *_crCalloc( unsigned int nbytes )
|
---|
73 | #else
|
---|
74 | DECLEXPORT(void) *crCalloc( unsigned int nbytes )
|
---|
75 | #endif
|
---|
76 | {
|
---|
77 | #ifdef VBOX
|
---|
78 | void *ret = RTMemAlloc( nbytes );
|
---|
79 | #else
|
---|
80 | void *ret = malloc( nbytes );
|
---|
81 | #endif
|
---|
82 | if (!ret) {
|
---|
83 | crError( "Out of memory trying to (c)allocate %d bytes!", nbytes );
|
---|
84 | abort();
|
---|
85 | }
|
---|
86 | crMemset( ret, 0, nbytes );
|
---|
87 | return ret;
|
---|
88 | }
|
---|
89 |
|
---|
90 | void *crCallocDebug( unsigned int nbytes, const char *file, int line )
|
---|
91 | {
|
---|
92 | #if DEBUG_MEM
|
---|
93 | if (nbytes >= THRESHOLD)
|
---|
94 | fprintf(stderr, "crCallocDebug(%d bytes) in %s at %d\n", nbytes, file, line);
|
---|
95 | return _crCalloc(nbytes);
|
---|
96 | #else
|
---|
97 | RT_NOREF2(file, line);
|
---|
98 | return crCalloc(nbytes);
|
---|
99 | #endif
|
---|
100 | }
|
---|
101 |
|
---|
102 | DECLEXPORT(void) crRealloc( void **ptr, unsigned int nbytes )
|
---|
103 | {
|
---|
104 | if ( *ptr == NULL )
|
---|
105 | {
|
---|
106 | #if DEBUG_MEM
|
---|
107 | *ptr = _crAlloc( nbytes );
|
---|
108 | #else
|
---|
109 | *ptr = crAlloc( nbytes );
|
---|
110 | #endif
|
---|
111 | }
|
---|
112 | else
|
---|
113 | {
|
---|
114 | #ifdef VBOX
|
---|
115 | *ptr = RTMemRealloc( *ptr, nbytes );
|
---|
116 | #else
|
---|
117 | *ptr = realloc( *ptr, nbytes );
|
---|
118 | #endif
|
---|
119 | if (*ptr == NULL)
|
---|
120 | crError( "Couldn't realloc %d bytes!", nbytes );
|
---|
121 | }
|
---|
122 | }
|
---|
123 |
|
---|
124 | DECLEXPORT(void) crFree( void *ptr )
|
---|
125 | {
|
---|
126 | #ifdef VBOX
|
---|
127 | if (ptr)
|
---|
128 | RTMemFree(ptr);
|
---|
129 | #else
|
---|
130 | if (ptr)
|
---|
131 | free(ptr);
|
---|
132 | #endif
|
---|
133 | }
|
---|
134 |
|
---|
135 | DECLEXPORT(void) crMemcpy( void *dst, const void *src, unsigned int bytes )
|
---|
136 | {
|
---|
137 | CRASSERT(dst || 0==bytes);
|
---|
138 | CRASSERT(src || 0==bytes);
|
---|
139 | (void) memcpy( dst, src, bytes );
|
---|
140 | }
|
---|
141 |
|
---|
142 | DECLEXPORT(void) crMemset( void *ptr, int value, unsigned int bytes )
|
---|
143 | {
|
---|
144 | CRASSERT(ptr);
|
---|
145 | memset( ptr, value, bytes );
|
---|
146 | }
|
---|
147 |
|
---|
148 | DECLEXPORT(void) crMemZero( void *ptr, unsigned int bytes )
|
---|
149 | {
|
---|
150 | CRASSERT(ptr);
|
---|
151 | memset( ptr, 0, bytes );
|
---|
152 | }
|
---|
153 |
|
---|
154 | DECLEXPORT(int) crMemcmp( const void *p1, const void *p2, unsigned int bytes )
|
---|
155 | {
|
---|
156 | CRASSERT(p1);
|
---|
157 | CRASSERT(p2);
|
---|
158 | return memcmp( p1, p2, bytes );
|
---|
159 | }
|
---|
160 |
|
---|