VirtualBox

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

Last change on this file since 24107 was 23614, checked in by vboxsync, 15 years ago

crOpenGL: remove src ptr assert when memcpy 0 bytes public #5158

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.6 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
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 */
25extern void *crAlloc( unsigned int bytes );
26void *crAlloc( unsigned int bytes )
27{
28 return NULL;
29}
30
31extern void *crCalloc( unsigned int bytes );
32void *crCalloc( unsigned int bytes )
33{
34 return NULL;
35}
36#endif /* DEBUG_MEM */
37
38
39
40#if DEBUG_MEM
41static void *_crAlloc( unsigned int nbytes )
42#else
43DECLEXPORT(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
54void *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
66static void *_crCalloc( unsigned int nbytes )
67#else
68DECLEXPORT(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
80void *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
91DECLEXPORT(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
109DECLEXPORT(void) crFree( void *ptr )
110{
111 if (ptr)
112 free(ptr);
113}
114
115DECLEXPORT(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
122DECLEXPORT(void) crMemset( void *ptr, int value, unsigned int bytes )
123{
124 CRASSERT(ptr);
125 memset( ptr, value, bytes );
126}
127
128DECLEXPORT(void) crMemZero( void *ptr, unsigned int bytes )
129{
130 CRASSERT(ptr);
131 memset( ptr, 0, bytes );
132}
133
134DECLEXPORT(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
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