git: 8bae22bbbe65 - main - fusefs: prefer new/delete over malloc/free

From: Alan Somers <asomers_at_FreeBSD.org>
Date: Wed, 17 Jan 2024 22:50:04 UTC
The branch main has been updated by asomers:

URL: https://cgit.FreeBSD.org/src/commit/?id=8bae22bbbe6571da9259e0d43ffa8a56f4b3e171

commit 8bae22bbbe6571da9259e0d43ffa8a56f4b3e171
Author:     Alan Somers <asomers@FreeBSD.org>
AuthorDate: 2024-01-15 23:49:47 +0000
Commit:     Alan Somers <asomers@FreeBSD.org>
CommitDate: 2024-01-17 22:49:41 +0000

    fusefs: prefer new/delete over malloc/free
    
    MFC after:      2 weeks
    Reviewed by:    kib
    Differential Revision: https://reviews.freebsd.org/D43464
---
 tests/sys/fs/fusefs/bmap.cc            |  5 +-
 tests/sys/fs/fusefs/copy_file_range.cc | 22 ++++----
 tests/sys/fs/fusefs/fallocate.cc       |  6 +--
 tests/sys/fs/fusefs/io.cc              | 30 +++++------
 tests/sys/fs/fusefs/read.cc            | 14 +++--
 tests/sys/fs/fusefs/setattr.cc         | 23 ++++-----
 tests/sys/fs/fusefs/write.cc           | 94 +++++++++++++++-------------------
 7 files changed, 85 insertions(+), 109 deletions(-)

diff --git a/tests/sys/fs/fusefs/bmap.cc b/tests/sys/fs/fusefs/bmap.cc
index 1ef3dfa00045..4c9edac9360a 100644
--- a/tests/sys/fs/fusefs/bmap.cc
+++ b/tests/sys/fs/fusefs/bmap.cc
@@ -188,7 +188,7 @@ TEST_P(BmapEof, eof)
 	const off_t filesize = 2 * m_maxbcachebuf;
 	const ino_t ino = 42;
 	mode_t mode = S_IFREG | 0644;
-	void *buf;
+	char *buf;
 	int fd;
 	int ngetattrs;
 
@@ -243,11 +243,12 @@ TEST_P(BmapEof, eof)
 		out.body.attr.attr.size = filesize / 2;
 	})));
 
-	buf = calloc(1, filesize);
+	buf = new char[filesize]();
 	fd = open(FULLPATH, O_RDWR);
 	ASSERT_LE(0, fd) << strerror(errno);
 	read(fd, buf, filesize);
 
+	delete[] buf;
 	leak(fd);
 }
 
diff --git a/tests/sys/fs/fusefs/copy_file_range.cc b/tests/sys/fs/fusefs/copy_file_range.cc
index 17b21b888736..806ecf3c3653 100644
--- a/tests/sys/fs/fusefs/copy_file_range.cc
+++ b/tests/sys/fs/fusefs/copy_file_range.cc
@@ -197,7 +197,7 @@ TEST_F(CopyFileRange, evicts_cache)
 	const char RELPATH1[] = "src.txt";
 	const char FULLPATH2[] = "mountpoint/dst.txt";
 	const char RELPATH2[] = "dst.txt";
-	void *buf0, *buf1, *buf;
+	char *buf0, *buf1, *buf;
 	const uint64_t ino1 = 42;
 	const uint64_t ino2 = 43;
 	const uint64_t fh1 = 0xdeadbeef1a7ebabe;
@@ -209,7 +209,7 @@ TEST_F(CopyFileRange, evicts_cache)
 	ssize_t len = m_maxbcachebuf;
 	int fd1, fd2;
 
-	buf0 = malloc(m_maxbcachebuf);
+	buf0 = new char[m_maxbcachebuf];
 	memset(buf0, 42, m_maxbcachebuf);
 
 	expect_lookup(RELPATH1, ino1, S_IFREG | 0644, fsize1, 1);
@@ -240,7 +240,7 @@ TEST_F(CopyFileRange, evicts_cache)
 	fd2 = open(FULLPATH2, O_RDWR);
 
 	// Prime cache
-	buf = malloc(m_maxbcachebuf);
+	buf = new char[m_maxbcachebuf];
 	ASSERT_EQ(m_maxbcachebuf, pread(fd2, buf, m_maxbcachebuf, start2))
 		<< strerror(errno);
 	EXPECT_EQ(0, memcmp(buf0, buf, m_maxbcachebuf));
@@ -249,7 +249,7 @@ TEST_F(CopyFileRange, evicts_cache)
 	ASSERT_EQ(len, copy_file_range(fd1, &start1, fd2, &start2, len, 0));
 
 	// Read again.  This should bypass the cache and read direct from server
-	buf1 = malloc(m_maxbcachebuf);
+	buf1 = new char[m_maxbcachebuf];
 	memset(buf1, 69, m_maxbcachebuf);
 	start2 -= len;
 	expect_read(ino2, start2, m_maxbcachebuf, m_maxbcachebuf, buf1, -1,
@@ -258,9 +258,9 @@ TEST_F(CopyFileRange, evicts_cache)
 		<< strerror(errno);
 	EXPECT_EQ(0, memcmp(buf1, buf, m_maxbcachebuf));
 
-	free(buf1);
-	free(buf0);
-	free(buf);
+	delete[] buf1;
+	delete[] buf0;
+	delete[] buf;
 	leak(fd1);
 	leak(fd2);
 }
@@ -343,8 +343,8 @@ TEST_F(CopyFileRange, mmap_write)
 	int fd;
 	const mode_t mode = 0644;
 
-	fbuf = (uint8_t*)calloc(1, fsize);
-	wbuf = (uint8_t*)malloc(wsize);
+	fbuf = new uint8_t[fsize]();
+	wbuf = new uint8_t[wsize];
 	memset(wbuf, 1, wsize);
 
 	expect_lookup(RELPATH, ino, S_IFREG | mode, fsize, 1);
@@ -383,8 +383,8 @@ TEST_F(CopyFileRange, mmap_write)
 	r = copy_file_range(fd, &offset2_in, fd, &offset2_out, copysize, 0);
 	ASSERT_EQ(copysize, (size_t)r) << strerror(errno);
 
-	free(wbuf);
-	free(fbuf);
+	delete[] wbuf;
+	delete[] fbuf;
 }
 
 
diff --git a/tests/sys/fs/fusefs/fallocate.cc b/tests/sys/fs/fusefs/fallocate.cc
index 251552ddc8d0..92e327be5ade 100644
--- a/tests/sys/fs/fusefs/fallocate.cc
+++ b/tests/sys/fs/fusefs/fallocate.cc
@@ -415,14 +415,14 @@ TEST_F(Fspacectl_7_18, ok)
 	const char FULLPATH[] = "mountpoint/some_file.txt";
 	const char RELPATH[] = "some_file.txt";
 	struct spacectl_range rqsr, rmsr;
-	void *buf;
+	char *buf;
 	uint64_t ino = 42;
 	uint64_t fsize = 2000;
 	uint64_t offset = 500;
 	uint64_t length = 1000;
 	int fd;
 
-	buf = malloc(length);
+	buf = new char[length];
 
 	expect_lookup(RELPATH, ino, S_IFREG | 0644, fsize, 1);
 	expect_open(ino, 0, 1);
@@ -437,7 +437,7 @@ TEST_F(Fspacectl_7_18, ok)
 	EXPECT_EQ((off_t)(offset + length), rmsr.r_offset);
 
 	leak(fd);
-	free(buf);
+	delete[] buf;
 }
 
 /*
diff --git a/tests/sys/fs/fusefs/io.cc b/tests/sys/fs/fusefs/io.cc
index 283b601c9e87..99b5eae34e09 100644
--- a/tests/sys/fs/fusefs/io.cc
+++ b/tests/sys/fs/fusefs/io.cc
@@ -283,7 +283,8 @@ void do_ftruncate(off_t offs)
 
 void do_mapread(off_t offs, ssize_t size)
 {
-	void *control_buf, *p;
+	char *control_buf;
+	void *p;
 	off_t pg_offset, page_mask;
 	size_t map_size;
 
@@ -295,8 +296,7 @@ void do_mapread(off_t offs, ssize_t size)
 	    offs - pg_offset);
 	ASSERT_NE(p, MAP_FAILED) << strerror(errno);
 
-	control_buf = malloc(size);
-	ASSERT_NE(nullptr, control_buf) << strerror(errno);
+	control_buf = new char[size];
 
 	ASSERT_EQ(size, pread(m_control_fd, control_buf, size, offs))
 		<< strerror(errno);
@@ -304,18 +304,16 @@ void do_mapread(off_t offs, ssize_t size)
 	compare((void*)((char*)p + pg_offset), control_buf, offs, size);
 
 	ASSERT_EQ(0, munmap(p, map_size)) << strerror(errno);
-	free(control_buf);
+	delete[] control_buf;
 }
 
 void do_read(off_t offs, ssize_t size)
 {
-	void *test_buf, *control_buf;
+	char *test_buf, *control_buf;
 	ssize_t r;
 
-	test_buf = malloc(size);
-	ASSERT_NE(nullptr, test_buf) << strerror(errno);
-	control_buf = malloc(size);
-	ASSERT_NE(nullptr, control_buf) << strerror(errno);
+	test_buf = new char[size];
+	control_buf = new char[size];
 
 	errno = 0;
 	r = pread(m_test_fd, test_buf, size, offs);
@@ -327,8 +325,8 @@ void do_read(off_t offs, ssize_t size)
 
 	compare(test_buf, control_buf, offs, size);
 
-	free(control_buf);
-	free(test_buf);
+	delete[] control_buf;
+	delete[] test_buf;
 }
 
 void do_mapwrite(off_t offs, ssize_t size)
@@ -343,8 +341,7 @@ void do_mapwrite(off_t offs, ssize_t size)
 	pg_offset = offs & page_mask;
 	map_size = pg_offset + size;
 
-	buf = (char*)malloc(size);
-	ASSERT_NE(nullptr, buf) << strerror(errno);
+	buf = new char[size];
 	for (i=0; i < size; i++)
 		buf[i] = random();
 
@@ -364,7 +361,7 @@ void do_mapwrite(off_t offs, ssize_t size)
 	ASSERT_EQ(size, pwrite(m_control_fd, buf, size, offs))
 		<< strerror(errno);
 
-	free(buf);
+	delete[] buf;
 	ASSERT_EQ(0, munmap(p, map_size)) << strerror(errno);
 }
 
@@ -373,8 +370,7 @@ void do_write(off_t offs, ssize_t size)
 	char *buf;
 	long i;
 
-	buf = (char*)malloc(size);
-	ASSERT_NE(nullptr, buf) << strerror(errno);
+	buf = new char[size];
 	for (i=0; i < size; i++)
 		buf[i] = random();
 
@@ -384,7 +380,7 @@ void do_write(off_t offs, ssize_t size)
 		<< strerror(errno);
 	m_filesize = std::max(m_filesize, offs + size);
 
-	free(buf);
+	delete[] buf;
 }
 
 };
diff --git a/tests/sys/fs/fusefs/read.cc b/tests/sys/fs/fusefs/read.cc
index 3df0420facb9..373f742d4fd3 100644
--- a/tests/sys/fs/fusefs/read.cc
+++ b/tests/sys/fs/fusefs/read.cc
@@ -1216,8 +1216,7 @@ TEST_F(Read, cache_block)
 	char buf[bufsize];
 	const char *contents1 = CONTENTS0 + bufsize;
 
-	contents = (char*)calloc(1, filesize);
-	ASSERT_NE(nullptr, contents);
+	contents = new char[filesize]();
 	memmove(contents, CONTENTS0, strlen(CONTENTS0));
 
 	expect_lookup(RELPATH, ino, filesize);
@@ -1235,7 +1234,7 @@ TEST_F(Read, cache_block)
 	ASSERT_EQ(bufsize, read(fd, buf, bufsize)) << strerror(errno);
 	ASSERT_EQ(0, memcmp(buf, contents1, bufsize));
 	leak(fd);
-	free(contents);
+	delete[] contents;
 }
 
 /* Reading with sendfile should work (though it obviously won't be 0-copy) */
@@ -1332,10 +1331,9 @@ TEST_P(ReadAhead, readahead) {
 	char *rbuf, *contents;
 	off_t offs;
 
-	contents = (char*)malloc(filesize);
-	ASSERT_NE(nullptr, contents);
+	contents = new char[filesize];
 	memset(contents, 'X', filesize);
-	rbuf = (char*)calloc(1, bufsize);
+	rbuf = new char[bufsize]();
 
 	expect_lookup(RELPATH, ino, filesize);
 	expect_open(ino, 0, 1);
@@ -1357,8 +1355,8 @@ TEST_P(ReadAhead, readahead) {
 	ASSERT_EQ(0, memcmp(rbuf, contents, bufsize));
 
 	leak(fd);
-	free(rbuf);
-	free(contents);
+	delete[] rbuf;
+	delete[] contents;
 }
 
 INSTANTIATE_TEST_SUITE_P(RA, ReadAhead,
diff --git a/tests/sys/fs/fusefs/setattr.cc b/tests/sys/fs/fusefs/setattr.cc
index 2502286c3f03..79559db33b12 100644
--- a/tests/sys/fs/fusefs/setattr.cc
+++ b/tests/sys/fs/fusefs/setattr.cc
@@ -448,7 +448,7 @@ TEST_F(Setattr, truncate) {
 TEST_F(Setattr, truncate_discards_cached_data) {
 	const char FULLPATH[] = "mountpoint/some_file.txt";
 	const char RELPATH[] = "some_file.txt";
-	void *w0buf, *r0buf, *r1buf, *expected;
+	char *w0buf, *r0buf, *r1buf, *expected;
 	off_t w0_offset = 0;
 	size_t w0_size = 0x30000;
 	off_t r0_offset = 0;
@@ -463,18 +463,13 @@ TEST_F(Setattr, truncate_discards_cached_data) {
 	int fd, r;
 	bool should_have_data = false;
 
-	w0buf = malloc(w0_size);
-	ASSERT_NE(nullptr, w0buf) << strerror(errno);
+	w0buf = new char[w0_size];
 	memset(w0buf, 'X', w0_size);
 
-	r0buf = malloc(r0_size);
-	ASSERT_NE(nullptr, r0buf) << strerror(errno);
-	r1buf = malloc(r1_size);
-	ASSERT_NE(nullptr, r1buf) << strerror(errno);
+	r0buf = new char[r0_size];
+	r1buf = new char[r1_size];
 
-	expected = malloc(r1_size);
-	ASSERT_NE(nullptr, expected) << strerror(errno);
-	memset(expected, 0, r1_size);
+	expected = new char[r1_size]();
 
 	expect_lookup(RELPATH, ino, mode, 0, 1);
 	expect_open(ino, O_RDWR, 1);
@@ -558,10 +553,10 @@ TEST_F(Setattr, truncate_discards_cached_data) {
 	r = memcmp(expected, r1buf, r1_size);
 	ASSERT_EQ(0, r);
 
-	free(expected);
-	free(r1buf);
-	free(r0buf);
-	free(w0buf);
+	delete[] expected;
+	delete[] r1buf;
+	delete[] r0buf;
+	delete[] w0buf;
 
 	leak(fd);
 }
diff --git a/tests/sys/fs/fusefs/write.cc b/tests/sys/fs/fusefs/write.cc
index 1fb9de70ec6c..f931f350a7c3 100644
--- a/tests/sys/fs/fusefs/write.cc
+++ b/tests/sys/fs/fusefs/write.cc
@@ -311,10 +311,8 @@ TEST_F(Write, append_to_cached)
 	uint64_t oldsize = m_maxbcachebuf / 2;
 	int fd;
 
-	oldcontents = (char*)calloc(1, oldsize);
-	ASSERT_NE(nullptr, oldcontents) << strerror(errno);
-	oldbuf = (char*)malloc(oldsize);
-	ASSERT_NE(nullptr, oldbuf) << strerror(errno);
+	oldcontents = new char[oldsize]();
+	oldbuf = new char[oldsize];
 
 	expect_lookup(RELPATH, ino, oldsize);
 	expect_open(ino, 0, 1);
@@ -332,8 +330,8 @@ TEST_F(Write, append_to_cached)
 	/* Write the new data.  There should be no more read operations */
 	ASSERT_EQ(BUFSIZE, write(fd, CONTENTS, BUFSIZE)) << strerror(errno);
 	leak(fd);
-	free(oldbuf);
-	free(oldcontents);
+	delete[] oldbuf;
+	delete[] oldcontents;
 }
 
 TEST_F(Write, append_direct_io)
@@ -659,7 +657,7 @@ TEST_P(WriteEofDuringVnopStrategy, eof_during_vop_strategy)
 	const char RELPATH[] = "some_file.txt";
 	Sequence seq;
 	const off_t filesize = 2 * m_maxbcachebuf;
-	void *contents;
+	char *contents;
 	uint64_t ino = 42;
 	uint64_t attr_valid = 0;
 	uint64_t attr_valid_nsec = 0;
@@ -668,7 +666,7 @@ TEST_P(WriteEofDuringVnopStrategy, eof_during_vop_strategy)
 	int ngetattrs;
 
 	ngetattrs = GetParam();
-	contents = calloc(1, filesize);
+	contents = new char[filesize]();
 
 	EXPECT_LOOKUP(FUSE_ROOT_ID, RELPATH)
 	.WillRepeatedly(Invoke(
@@ -742,14 +740,12 @@ TEST_F(Write, mmap)
 	void *p;
 	uint64_t offset = 10;
 	size_t len;
-	void *zeros, *expected;
+	char *zeros, *expected;
 
 	len = getpagesize();
 
-	zeros = calloc(1, len);
-	ASSERT_NE(nullptr, zeros);
-	expected = calloc(1, len);
-	ASSERT_NE(nullptr, expected);
+	zeros = new char[len]();
+	expected = new char[len]();
 	memmove((uint8_t*)expected + offset, CONTENTS, bufsize);
 
 	expect_lookup(RELPATH, ino, len);
@@ -774,8 +770,8 @@ TEST_F(Write, mmap)
 	ASSERT_EQ(0, munmap(p, len)) << strerror(errno);
 	close(fd);	// Write mmap'd data on close
 
-	free(expected);
-	free(zeros);
+	delete[] expected;
+	delete[] zeros;
 
 	leak(fd);
 }
@@ -867,8 +863,7 @@ TEST_F(WriteMaxWrite, write)
 	if (halfbufsize >= m_maxbcachebuf || halfbufsize >= m_maxphys)
 		GTEST_SKIP() << "Must lower m_maxwrite for this test";
 	bufsize = halfbufsize * 2;
-	contents = (int*)malloc(bufsize);
-	ASSERT_NE(nullptr, contents);
+	contents = new int[bufsize / sizeof(int)];
 	for (int i = 0; i < (int)bufsize / (int)sizeof(i); i++) {
 		contents[i] = i;
 	}
@@ -885,7 +880,7 @@ TEST_F(WriteMaxWrite, write)
 	ASSERT_EQ(bufsize, write(fd, contents, bufsize)) << strerror(errno);
 	leak(fd);
 
-	free(contents);
+	delete[] contents;
 }
 
 TEST_F(Write, write_nothing)
@@ -966,15 +961,13 @@ TEST_F(WriteCluster, clustering)
 	const char RELPATH[] = "some_file.txt";
 	uint64_t ino = 42;
 	int i, fd;
-	void *wbuf, *wbuf2x;
+	char *wbuf, *wbuf2x;
 	ssize_t bufsize = m_maxbcachebuf;
 	off_t filesize = 5 * bufsize;
 
-	wbuf = malloc(bufsize);
-	ASSERT_NE(nullptr, wbuf) << strerror(errno);
+	wbuf = new char[bufsize];
 	memset(wbuf, 'X', bufsize);
-	wbuf2x = malloc(2 * bufsize);
-	ASSERT_NE(nullptr, wbuf2x) << strerror(errno);
+	wbuf2x = new char[2 * bufsize];
 	memset(wbuf2x, 'X', 2 * bufsize);
 
 	expect_lookup(RELPATH, ino, filesize);
@@ -997,8 +990,8 @@ TEST_F(WriteCluster, clustering)
 			<< strerror(errno);
 	}
 	close(fd);
-	free(wbuf2x);
-	free(wbuf);
+	delete[] wbuf2x;
+	delete[] wbuf;
 }
 
 /* 
@@ -1015,12 +1008,11 @@ TEST_F(WriteCluster, cluster_write_err)
 	const char RELPATH[] = "some_file.txt";
 	uint64_t ino = 42;
 	int i, fd;
-	void *wbuf;
+	char *wbuf;
 	ssize_t bufsize = m_maxbcachebuf;
 	off_t filesize = 4 * bufsize;
 
-	wbuf = malloc(bufsize);
-	ASSERT_NE(nullptr, wbuf) << strerror(errno);
+	wbuf = new char[bufsize];
 	memset(wbuf, 'X', bufsize);
 
 	expect_lookup(RELPATH, ino, filesize);
@@ -1042,7 +1034,7 @@ TEST_F(WriteCluster, cluster_write_err)
 			<< strerror(errno);
 	}
 	close(fd);
-	free(wbuf);
+	delete[] wbuf;
 }
 
 /*
@@ -1179,11 +1171,11 @@ TEST_F(WriteBack, mmap_direct_io)
 	int fd;
 	size_t len;
 	ssize_t bufsize = strlen(CONTENTS);
-	void *p, *zeros;
+	char *zeros;
+	void *p;
 
 	len = getpagesize();
-	zeros = calloc(1, len);
-	ASSERT_NE(nullptr, zeros);
+	zeros = new char[len]();
 
 	expect_lookup(RELPATH, ino, len);
 	expect_open(ino, FOPEN_DIRECT_IO, 1);
@@ -1203,7 +1195,7 @@ TEST_F(WriteBack, mmap_direct_io)
 	ASSERT_EQ(0, munmap(p, len)) << strerror(errno);
 	close(fd);	// Write mmap'd data on close
 
-	free(zeros);
+	delete[] zeros;
 }
 
 /*
@@ -1252,10 +1244,9 @@ TEST_F(WriteBackAsync, direct_io_ignores_unrelated_cached)
 	ssize_t bufsize = strlen(CONTENTS0) + 1;
 	ssize_t fsize = 2 * m_maxbcachebuf;
 	char readbuf[bufsize];
-	void *zeros;
+	char *zeros;
 
-	zeros = calloc(1, m_maxbcachebuf);
-	ASSERT_NE(nullptr, zeros);
+	zeros = new char[m_maxbcachebuf]();
 
 	expect_lookup(RELPATH, ino, fsize);
 	expect_open(ino, 0, 1);
@@ -1282,7 +1273,7 @@ TEST_F(WriteBackAsync, direct_io_ignores_unrelated_cached)
 	ASSERT_STREQ(readbuf, CONTENTS0);
 
 	leak(fd);
-	free(zeros);
+	delete[] zeros;
 }
 
 /*
@@ -1298,20 +1289,15 @@ TEST_F(WriteBackAsync, direct_io_partially_overlaps_cached_block)
 	int fd;
 	off_t bs = m_maxbcachebuf;
 	ssize_t fsize = 3 * bs;
-	void *readbuf, *zeros, *ones, *zeroones, *onezeros;
-
-	readbuf = malloc(bs);
-	ASSERT_NE(nullptr, readbuf) << strerror(errno);
-	zeros = calloc(1, 3 * bs);
-	ASSERT_NE(nullptr, zeros);
-	ones = calloc(1, 2 * bs);
-	ASSERT_NE(nullptr, ones);
+	char *readbuf, *zeros, *ones, *zeroones, *onezeros;
+
+	readbuf = new char[bs];
+	zeros = new char[3 * bs]();
+	ones = new char[2 * bs];
 	memset(ones, 1, 2 * bs);
-	zeroones = calloc(1, bs);
-	ASSERT_NE(nullptr, zeroones);
+	zeroones = new char[bs]();
 	memset((uint8_t*)zeroones + bs / 2, 1, bs / 2);
-	onezeros = calloc(1, bs);
-	ASSERT_NE(nullptr, onezeros);
+	onezeros = new char[bs]();
 	memset(onezeros, 1, bs / 2);
 
 	expect_lookup(RELPATH, ino, fsize);
@@ -1356,11 +1342,11 @@ TEST_F(WriteBackAsync, direct_io_partially_overlaps_cached_block)
 	EXPECT_EQ(0, memcmp(ones, readbuf, bs / 2));
 
 	leak(fd);
-	free(zeroones);
-	free(onezeros);
-	free(ones);
-	free(zeros);
-	free(readbuf);
+	delete[] zeroones;
+	delete[] onezeros;
+	delete[] ones;
+	delete[] zeros;
+	delete[] readbuf;
 }
 
 /*