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 |
|
---|
15 | #if DEBUG_MEM
|
---|
16 | #include <stdio.h>
|
---|
17 | #define THRESHOLD 100 * 1024
|
---|
18 |
|
---|
19 | #undef crAlloc
|
---|
20 | #undef crCalloc
|
---|
21 |
|
---|
22 | /* Need these stubs because util.def says we're always exporting crAlloc
|
---|
23 | * and crCalloc.
|
---|
24 | */
|
---|
25 | extern void *crAlloc( unsigned int bytes );
|
---|
26 | void *crAlloc( unsigned int bytes )
|
---|
27 | {
|
---|
28 | return NULL;
|
---|
29 | }
|
---|
30 |
|
---|
31 | extern void *crCalloc( unsigned int bytes );
|
---|
32 | void *crCalloc( unsigned int bytes )
|
---|
33 | {
|
---|
34 | return NULL;
|
---|
35 | }
|
---|
36 | #endif /* DEBUG_MEM */
|
---|
37 |
|
---|
38 |
|
---|
39 |
|
---|
40 | #if DEBUG_MEM
|
---|
41 | static void *_crAlloc( unsigned int nbytes )
|
---|
42 | #else
|
---|
43 | DECLEXPORT(void) *crAlloc( unsigned int nbytes )
|
---|
44 | #endif
|
---|
45 | {
|
---|
46 | void *ret = malloc( nbytes );
|
---|
47 | if (!ret) {
|
---|
48 | crError( "Out of memory trying to allocate %d bytes!", nbytes );
|
---|
49 | abort();
|
---|
50 | }
|
---|
51 | return ret;
|
---|
52 | }
|
---|
53 |
|
---|
54 | void *crAllocDebug( unsigned int nbytes, const char *file, int line )
|
---|
55 | {
|
---|
56 | #if DEBUG_MEM
|
---|
57 | if (nbytes >= THRESHOLD)
|
---|
58 | fprintf(stderr, "crAllocDebug(%d bytes) in %s at %d\n", nbytes, file, line);
|
---|
59 | return _crAlloc(nbytes);
|
---|
60 | #else
|
---|
61 | return crAlloc(nbytes);
|
---|
62 | #endif
|
---|
63 | }
|
---|
64 |
|
---|
65 | #if DEBUG_MEM
|
---|
66 | static void *_crCalloc( unsigned int nbytes )
|
---|
67 | #else
|
---|
68 | DECLEXPORT(void) *crCalloc( unsigned int nbytes )
|
---|
69 | #endif
|
---|
70 | {
|
---|
71 | void *ret = malloc( nbytes );
|
---|
72 | if (!ret) {
|
---|
73 | crError( "Out of memory trying to (c)allocate %d bytes!", nbytes );
|
---|
74 | abort();
|
---|
75 | }
|
---|
76 | crMemset( ret, 0, nbytes );
|
---|
77 | return ret;
|
---|
78 | }
|
---|
79 |
|
---|
80 | void *crCallocDebug( unsigned int nbytes, const char *file, int line )
|
---|
81 | {
|
---|
82 | #if DEBUG_MEM
|
---|
83 | if (nbytes >= THRESHOLD)
|
---|
84 | fprintf(stderr, "crCallocDebug(%d bytes) in %s at %d\n", nbytes, file, line);
|
---|
85 | return _crCalloc(nbytes);
|
---|
86 | #else
|
---|
87 | return crCalloc(nbytes);
|
---|
88 | #endif
|
---|
89 | }
|
---|
90 |
|
---|
91 | DECLEXPORT(void) crRealloc( void **ptr, unsigned int nbytes )
|
---|
92 | {
|
---|
93 | if ( *ptr == NULL )
|
---|
94 | {
|
---|
95 | #if DEBUG_MEM
|
---|
96 | *ptr = _crAlloc( nbytes );
|
---|
97 | #else
|
---|
98 | *ptr = crAlloc( nbytes );
|
---|
99 | #endif
|
---|
100 | }
|
---|
101 | else
|
---|
102 | {
|
---|
103 | *ptr = realloc( *ptr, nbytes );
|
---|
104 | if (*ptr == NULL)
|
---|
105 | crError( "Couldn't realloc %d bytes!", nbytes );
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 | DECLEXPORT(void) crFree( void *ptr )
|
---|
110 | {
|
---|
111 | if (ptr)
|
---|
112 | free(ptr);
|
---|
113 | }
|
---|
114 |
|
---|
115 | DECLEXPORT(void) crMemcpy( void *dst, const void *src, unsigned int bytes )
|
---|
116 | {
|
---|
117 | CRASSERT(dst || 0==bytes);
|
---|
118 | CRASSERT(src || 0==bytes);
|
---|
119 | (void) memcpy( dst, src, bytes );
|
---|
120 | }
|
---|
121 |
|
---|
122 | DECLEXPORT(void) crMemset( void *ptr, int value, unsigned int bytes )
|
---|
123 | {
|
---|
124 | CRASSERT(ptr);
|
---|
125 | memset( ptr, value, bytes );
|
---|
126 | }
|
---|
127 |
|
---|
128 | DECLEXPORT(void) crMemZero( void *ptr, unsigned int bytes )
|
---|
129 | {
|
---|
130 | CRASSERT(ptr);
|
---|
131 | memset( ptr, 0, bytes );
|
---|
132 | }
|
---|
133 |
|
---|
134 | DECLEXPORT(int) crMemcmp( const void *p1, const void *p2, unsigned int bytes )
|
---|
135 | {
|
---|
136 | CRASSERT(p1);
|
---|
137 | CRASSERT(p2);
|
---|
138 | return memcmp( p1, p2, bytes );
|
---|
139 | }
|
---|
140 |
|
---|