Newer
Older
#include <stdio.h>
#include <stdlib.h>
#ifdef WIN32
# include "winjunk.h"
#else
# include <unistd.h>
#endif
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include "shmem.h"
#if CAVE
#include <cave_ogl.h>
static int (*shm_purge_func)( void *data, int nbytes, void *arena ) = NULL;
static void *shm_purge_data;
void *aarena;
void shminit( int nbytes ) {
aarena = CAVEUserSharedMemory( (unsigned int) nbytes );
if(aarena == NULL) {
fprintf(stderr, "Couldn't create CAVEUserSharedMemory of %dMbytes!\n",
((unsigned int)nbytes) >> 20);
exit(1);
}
amallopt(M_DEBUG, 1, aarena);
}
void shmusearena( void *a ) {
aarena = a;
}
int shm_malloc_zero;
void *shmalloc( int nbytes ) {
void *p;
if(aarena == NULL) {
fprintf(stderr, "shmalloc: Never called shminit( arenasize )!\n");
exit(1);
}
if(nbytes <= 0) {
shm_malloc_zero++;
nbytes = 1;
}
do {
p = amalloc( nbytes, aarena );
if(p != NULL)
return p;
} while (shm_purge_func != NULL
&& 0 != (*shm_purge_func)( shm_purge_data, nbytes, aarena ));
fprintf(stderr, "Couldn't shmalloc %d bytes! Pausing for debugging (pid %d)\n", nbytes, getpid());
sleep(10);
exit(1);
return NULL;
}
void *shmrealloc( void *oldp, int nbytes ) {
void *p;
if(aarena == NULL) {
fprintf(stderr, "shmrealloc: Never called shminit( arenasize )!\n");
exit(1);
}
do {
p = arealloc( oldp, nbytes, aarena );
if(p != NULL)
return p;
} while (shm_purge_func != NULL
&& 0 != (*shm_purge_func)( shm_purge_data, nbytes, aarena ));
fprintf(stderr, "Couldn't shmrealloc %d bytes! Pausing for debugging (pid %d)\n", nbytes, getpid());
sleep(10);
exit(1);
return NULL;
}
void shmfree( void *p ) {
if(aarena && p)
afree( p, aarena );
}
char *shmstrdup( char *str ) {
char *s;
if(str == NULL) return NULL;
s = shmalloc(strlen(str)+1);
if(s == NULL) return NULL;
strcpy(s,str);
return s;
}
void shmrecycler( int (*func)(void *, int, void *), void *data )
{
shm_purge_func = func;
shm_purge_data = data;
}
#else
/* non-CAVE -- ordinary single process address space? */
char *shmstrdup( CONST char *str ) {
return str ? strdup(str) : NULL;
}
#endif /*CAVEMENU*/