-
Notifications
You must be signed in to change notification settings - Fork 0
/
xalloc.c
214 lines (192 loc) · 4.49 KB
/
xalloc.c
1
2
3
4
5
6
7
8
9
10
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/* -*- mode: C; mode: folding; fill-column: 70; -*- */
/* Copyright 2010-2011, Georgia Institute of Technology, USA. */
/* See COPYING for license. */
#include "compat.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <errno.h>
#include <limits.h>
#include <unistd.h>
#include <time.h>
#include <fcntl.h>
#include <sys/types.h>
/* #include <sys/mman.h> */
#include "mman/mman.h"
#if defined(HAVE_LIBNUMA)
#include <numa.h>
#endif
#if !defined(MAP_POPULATE)
#define MAP_POPULATE 0
#endif
#if !defined(MAP_NOSYNC)
#define MAP_NOSYNC 0
#endif
/* Unused... for now. */
#if !defined(MAP_HUGETLB)
#define MAP_HUGETLB 0
#endif
extern void *xmalloc (size_t);
#if defined(__MTA__)||defined(USE_MMAP_LARGE)||defined(USE_MMAP_LARGE_EXT)
#define MAX_LARGE 32
static int n_large_alloc = 0;
static struct {
void * p;
size_t sz;
int fd;
} large_alloc[MAX_LARGE];
static int installed_handler = 0;
static void (*old_abort_handler)(int);
static void
exit_handler (void)
{
int k;
for (k = 0; k < n_large_alloc; ++k) {
if (large_alloc[k].p)
munmap (large_alloc[k].p, large_alloc[k].sz);
if (large_alloc[k].fd >= 0)
close (large_alloc[k].fd);
large_alloc[k].p = NULL;
large_alloc[k].fd = -1;
}
}
static void
abort_handler (int passthrough)
{
exit_handler ();
if (old_abort_handler) old_abort_handler (passthrough);
}
#endif
#if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
#define MAP_ANONYMOUS MAP_ANON
#endif
void *
xmalloc_large (size_t sz)
{
#if defined(__MTA__)||defined(USE_MMAP_LARGE)
void *out;
int which = n_large_alloc++;
if (n_large_alloc > MAX_LARGE) {
fprintf (stderr, "Too many large allocations. %d %d\n", n_large_alloc, MAX_LARGE);
--n_large_alloc;
abort ();
}
large_alloc[which].p = NULL;
large_alloc[which].fd = -1;
out = mmap (NULL, sz, PROT_READ|PROT_WRITE,
MAP_PRIVATE|MAP_ANONYMOUS|MAP_POPULATE, 0, 0);
if (out == MAP_FAILED || !out) {
perror ("mmap failed");
abort ();
}
large_alloc[which].p = out;
large_alloc[which].sz = sz;
return out;
#else
return xmalloc (sz);
#endif
}
void
xfree_large (void *p)
{
#if defined(__MTA__)||defined(USE_MMAP_LARGE)||defined(USE_MMAP_LARGE_EXT)
int k, found = 0;
for (k = 0; k < n_large_alloc; ++k) {
if (p == large_alloc[k].p) {
munmap (p, large_alloc[k].sz);
large_alloc[k].p = NULL;
if (large_alloc[k].fd >= 0) {
close (large_alloc[k].fd);
large_alloc[k].fd = -1;
}
found = 1;
break;
}
}
if (found) {
--n_large_alloc;
for (; k < n_large_alloc; ++k)
large_alloc[k] = large_alloc[k+1];
} else
free (p);
#else
free (p);
#endif
}
void *
xmalloc_large_ext (size_t sz)
{
#if !defined(__MTA__)&&defined(USE_MMAP_LARGE_EXT)
char extname[PATH_MAX+1];
char *tmppath;
void *out;
int fd, which;
if (getenv ("TMPDIR"))
tmppath = getenv ("TMPDIR");
else if (getenv ("TEMPDIR"))
tmppath = getenv ("TEMPDIR");
else
tmppath = "/tmp";
sprintf (extname, "%s/graph500-ext-XXXXXX", tmppath);
which = n_large_alloc++;
if (n_large_alloc > MAX_LARGE) {
fprintf (stderr, "Out of large allocations.\n");
abort ();
}
large_alloc[which].p = 0;
large_alloc[which].fd = -1;
fd = mkstemp (extname);
if (fd < 0) {
perror ("xmalloc_large_ext failed to make a file");
abort ();
}
if (unlink (extname)) {
perror ("UNLINK FAILED!");
goto errout;
}
#if _XOPEN_SOURCE >= 500
if (pwrite (fd, &fd, sizeof (fd), sz - sizeof(fd)) != sizeof (fd)) {
perror ("resizing pwrite failed");
goto errout;
}
#else
if (lseek (fd, sz - sizeof(fd), SEEK_SET) < 0) {
perror ("lseek failed");
goto errout;
}
if (write (fd, &fd, sizeof(fd)) != sizeof (fd)) {
perror ("resizing write failed");
goto errout;
}
#endif
fcntl (fd, F_SETFD, O_ASYNC);
out = mmap (NULL, sz, PROT_READ|PROT_WRITE,
MAP_SHARED|MAP_POPULATE|MAP_NOSYNC, fd, 0);
if (MAP_FAILED == out || !out) {
perror ("mmap ext failed");
goto errout;
}
if (!installed_handler) {
installed_handler = 1;
if (atexit (exit_handler)) {
perror ("failed to install exit handler");
goto errout;
}
old_abort_handler = signal (SIGABRT, abort_handler);
if (SIG_ERR == old_abort_handler) {
perror ("failed to install cleanup handler");
goto errout;
}
}
large_alloc[which].p = out;
large_alloc[which].sz = sz;
large_alloc[which].fd = fd;
return out;
errout:
if (fd >= 0) close (fd);
abort ();
#else
return xmalloc_large (sz);
#endif
}