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 | return crAlloc(nbytes);
|
---|
67 | #endif
|
---|
68 | }
|
---|
69 |
|
---|
70 | #if DEBUG_MEM
|
---|
71 | static void *_crCalloc( unsigned int nbytes )
|
---|
72 | #else
|
---|
73 | DECLEXPORT(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 |
|
---|
89 | void *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 |
|
---|
100 | DECLEXPORT(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 | #ifdef VBOX
|
---|
113 | *ptr = RTMemRealloc( *ptr, nbytes );
|
---|
114 | #else
|
---|
115 | *ptr = realloc( *ptr, nbytes );
|
---|
116 | #endif
|
---|
117 | if (*ptr == NULL)
|
---|
118 | crError( "Couldn't realloc %d bytes!", nbytes );
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 | DECLEXPORT(void) crFree( void *ptr )
|
---|
123 | {
|
---|
124 | #ifdef VBOX
|
---|
125 | if (ptr)
|
---|
126 | RTMemFree(ptr);
|
---|
127 | #else
|
---|
128 | if (ptr)
|
---|
129 | free(ptr);
|
---|
130 | #endif
|
---|
131 | }
|
---|
132 |
|
---|
133 | DECLEXPORT(void) crMemcpy( void *dst, const void *src, unsigned int bytes )
|
---|
134 | {
|
---|
135 | CRASSERT(dst || 0==bytes);
|
---|
136 | CRASSERT(src || 0==bytes);
|
---|
137 | (void) memcpy( dst, src, bytes );
|
---|
138 | }
|
---|
139 |
|
---|
140 | DECLEXPORT(void) crMemset( void *ptr, int value, unsigned int bytes )
|
---|
141 | {
|
---|
142 | CRASSERT(ptr);
|
---|
143 | memset( ptr, value, bytes );
|
---|
144 | }
|
---|
145 |
|
---|
146 | DECLEXPORT(void) crMemZero( void *ptr, unsigned int bytes )
|
---|
147 | {
|
---|
148 | CRASSERT(ptr);
|
---|
149 | memset( ptr, 0, bytes );
|
---|
150 | }
|
---|
151 |
|
---|
152 | DECLEXPORT(int) crMemcmp( const void *p1, const void *p2, unsigned int bytes )
|
---|
153 | {
|
---|
154 | CRASSERT(p1);
|
---|
155 | CRASSERT(p2);
|
---|
156 | return memcmp( p1, p2, bytes );
|
---|
157 | }
|
---|
158 |
|
---|