Skip to content

Commit eb13723

Browse files
committed
Bug 1879433 - Scoped.h use removed from mozglue. r=glandium
Differential Revision: https://phabricator.services.mozilla.com/D201174
1 parent 6a68496 commit eb13723

File tree

4 files changed

+24
-33
lines changed

4 files changed

+24
-33
lines changed

mozglue/linker/ElfLoader.cpp

+12-7
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <cstdlib>
88
#include <cstdio>
99
#include <dlfcn.h>
10+
#include <optional>
1011
#include <unistd.h>
1112
#include <errno.h>
1213
#include <algorithm>
@@ -18,6 +19,7 @@
1819
#include "Logging.h"
1920
#include "Utils.h"
2021
#include <inttypes.h>
22+
#include "mozilla/ScopeExit.h"
2123

2224
// From Utils.h
2325
mozilla::Atomic<size_t, mozilla::ReleaseAcquire> gPageSize;
@@ -152,17 +154,17 @@ class DlIteratePhdrHelper {
152154
DlIteratePhdrHelper() {
153155
int pipefd[2];
154156
valid_pipe = (pipe(pipefd) == 0);
155-
read_fd.reset(pipefd[0]);
156-
write_fd.reset(pipefd[1]);
157+
read_fd.emplace(pipefd[0]);
158+
write_fd.emplace(pipefd[1]);
157159
}
158160

159161
int fill_and_call(dl_phdr_cb callback, const void* l_addr, const char* l_name,
160162
void* data);
161163

162164
private:
163165
bool valid_pipe;
164-
AutoCloseFD read_fd;
165-
AutoCloseFD write_fd;
166+
std::optional<AutoCloseFD> read_fd;
167+
std::optional<AutoCloseFD> write_fd;
166168
};
167169

168170
// This function is called for each shared library iterated over by
@@ -199,7 +201,7 @@ int DlIteratePhdrHelper::fill_and_call(dl_phdr_cb callback, const void* l_addr,
199201
static_assert(sizeof(raw_ehdr) < PIPE_BUF, "PIPE_BUF is too small");
200202
do {
201203
// writes are atomic when smaller than PIPE_BUF, per POSIX.1-2008.
202-
ret = write(write_fd, l_addr, sizeof(raw_ehdr));
204+
ret = write(*write_fd, l_addr, sizeof(raw_ehdr));
203205
} while (ret == -1 && errno == EINTR);
204206
if (ret != sizeof(raw_ehdr)) {
205207
if (ret == -1 && errno == EFAULT) {
@@ -212,7 +214,7 @@ int DlIteratePhdrHelper::fill_and_call(dl_phdr_cb callback, const void* l_addr,
212214
do {
213215
// Per POSIX.1-2008, interrupted reads can return a length smaller
214216
// than the given one instead of failing with errno EINTR.
215-
ret = read(read_fd, raw_ehdr + nbytes, sizeof(raw_ehdr) - nbytes);
217+
ret = read(*read_fd, raw_ehdr + nbytes, sizeof(raw_ehdr) - nbytes);
216218
if (ret > 0) nbytes += ret;
217219
} while ((nbytes != sizeof(raw_ehdr) && ret > 0) ||
218220
(ret == -1 && errno == EINTR));
@@ -834,7 +836,10 @@ class EnsureWritable {
834836
/* The interesting part of the /proc/self/maps format looks like:
835837
* startAddr-endAddr rwxp */
836838
int result = 0;
837-
AutoCloseFILE f(fopen("/proc/self/maps", "r"));
839+
FILE* const f = fopen("/proc/self/maps", "r");
840+
const auto cleanup = mozilla::MakeScopeExit([&]() {
841+
if (f) fclose(f);
842+
});
838843
while (f) {
839844
unsigned long long startAddr, endAddr;
840845
char perms[5];

mozglue/linker/Mappable.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,20 @@ Mappable* Mappable::Create(const char* path) {
1515

1616
MemoryRange Mappable::mmap(const void* addr, size_t length, int prot, int flags,
1717
off_t offset) {
18-
MOZ_ASSERT(fd != -1);
18+
MOZ_ASSERT(fd && *fd != -1);
1919
MOZ_ASSERT(!(flags & MAP_SHARED));
2020
flags |= MAP_PRIVATE;
2121

22-
return MemoryRange::mmap(const_cast<void*>(addr), length, prot, flags, fd,
22+
return MemoryRange::mmap(const_cast<void*>(addr), length, prot, flags, *fd,
2323
offset);
2424
}
2525

2626
void Mappable::finalize() {
2727
/* Close file ; equivalent to close(fd.forget()) */
28-
fd = -1;
28+
fd.emplace(-1);
2929
}
3030

3131
size_t Mappable::GetLength() const {
3232
struct stat st;
33-
return fstat(fd, &st) ? 0 : st.st_size;
33+
return fstat(*fd, &st) ? 0 : st.st_size;
3434
}

mozglue/linker/Mappable.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#ifndef Mappable_h
66
#define Mappable_h
77

8+
#include <optional>
89
#include "mozilla/RefCounted.h"
910
#include "Utils.h"
1011

@@ -48,7 +49,7 @@ class Mappable : public mozilla::RefCounted<Mappable> {
4849

4950
private:
5051
/* File descriptor */
51-
AutoCloseFD fd;
52+
std::optional<AutoCloseFD> fd;
5253
};
5354

5455
#endif /* Mappable_h */

mozglue/linker/Utils.h

+6-21
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#include <unistd.h>
1313
#include "mozilla/Assertions.h"
1414
#include "mozilla/Atomics.h"
15-
#include "mozilla/Scoped.h"
1615

1716
/**
1817
* On architectures that are little endian and that support unaligned reads,
@@ -75,29 +74,15 @@ typedef le_to_cpu<unsigned char> le_uint16;
7574
typedef le_to_cpu<le_uint16> le_uint32;
7675
#endif
7776

78-
/**
79-
* AutoCloseFD is a RAII wrapper for POSIX file descriptors
80-
*/
81-
struct AutoCloseFDTraits {
82-
typedef int type;
83-
static int empty() { return -1; }
84-
static void release(int fd) {
85-
if (fd != -1) close(fd);
86-
}
87-
};
88-
typedef mozilla::Scoped<AutoCloseFDTraits> AutoCloseFD;
77+
struct AutoCloseFD {
78+
const int fd;
8979

90-
/**
91-
* AutoCloseFILE is a RAII wrapper for POSIX streams
92-
*/
93-
struct AutoCloseFILETraits {
94-
typedef FILE* type;
95-
static FILE* empty() { return nullptr; }
96-
static void release(FILE* f) {
97-
if (f) fclose(f);
80+
MOZ_IMPLICIT AutoCloseFD(int fd) : fd(fd) {}
81+
~AutoCloseFD() {
82+
if (fd != -1) close(fd);
9883
}
84+
operator int() const { return fd; }
9985
};
100-
typedef mozilla::Scoped<AutoCloseFILETraits> AutoCloseFILE;
10186

10287
extern mozilla::Atomic<size_t, mozilla::ReleaseAcquire> gPageSize;
10388

0 commit comments

Comments
 (0)