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
121 /* Using mmap instead of malloc */121 /* Using mmap instead of malloc */
122 void *prw;122 void *prw;
123 static const char *tmpdir;123 static const char *tmpdir;
124 char tmpfname[256];124 int fd = -1;
125 int fd;
126 125
127 if (tmpdir == NULL) {126 if (tmpdir == NULL) {
128 tmpdir = getenv("TMPDIR");127 tmpdir = getenv("TMPDIR");
129 if (tmpdir == NULL || *tmpdir == 0)128 if (tmpdir == NULL || *tmpdir == 0)
130 tmpdir = "/tmp";129 tmpdir = "/tmp";
131 }130 }
132 snprintf(tmpfname, sizeof(tmpfname), "%s/.tccrunXXXXXX", tmpdir);131#ifdef O_TMPFILE
133 fd = mkstemp(tmpfname);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 }
134 if (fd < 0)141 if (fd < 0)
135 return tcc_error_noabort("tccrun: could not create temp file");142 return tcc_error_noabort("tccrun: could not create temp file");
136 unlink(tmpfname);
137 if (ftruncate(fd, size) < 0)143 if (ftruncate(fd, size) < 0)
138 return tcc_error_noabort("tccrun: could not resize temp file");144 return tcc_error_noabort("tccrun: could not resize temp file");
139 ptr = mmap(NULL, size * 2, PROT_READ|PROT_EXEC, MAP_SHARED, fd, 0);145 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