History

relocate with mmap: use open with O_TMPFILE if possible

This is better than mkstemp+unlink in every aspect.

Sigrid Solveig Haflínudóttir committed 9 months ago

Commit dab704d1 parent 0680cb51 file · +11 −5Browse files

Unified · Split

tccrun.c+11−5
@@ -121,19 +121,25 @@ static int rt_mem(TCCState *s1, int size) · 120 unmodified lines
121121 /* Using mmap instead of malloc */
122122 void *prw;
123123 static const char *tmpdir;
124− char tmpfname[256];
125− int fd;
124+ int fd = -1;
126125
127126 if (tmpdir == NULL) {
128127 tmpdir = getenv("TMPDIR");
129128 if (tmpdir == NULL || *tmpdir == 0)
130129 tmpdir = "/tmp";
131130 }
132− snprintf(tmpfname, sizeof(tmpfname), "%s/.tccrunXXXXXX", tmpdir);
133− fd = mkstemp(tmpfname);
131+#ifdef O_TMPFILE
132+ fd = open(tmpdir, O_TMPFILE|O_RDWR|O_EXCL, 0700);
133+#endif
134+ if (fd < 0) {
135+ char tmpfname[256];
136+ snprintf(tmpfname, sizeof(tmpfname), "%s/.tccrunXXXXXX", tmpdir);
137+ fd = mkstemp(tmpfname);
138+ if (fd >= 0)
139+ unlink(tmpfname);
140+ }
134141 if (fd < 0)
135142 return tcc_error_noabort("tccrun: could not create temp file");
136− unlink(tmpfname);
137143 if (ftruncate(fd, size) < 0)
138144 return tcc_error_noabort("tccrun: could not resize temp file");
139145 ptr = mmap(NULL, size * 2, PROT_READ|PROT_EXEC, MAP_SHARED, fd, 0);

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t