Here are a couple of patches that clean up the internal File API and related things a bit:

0001-Update-types-in-File-API.patch

    Make the argument types of the File API match stdio better:

    - Change the data buffer to void *, from char *.
    - Change FileWrite() data buffer to const on top of that.
    - Change amounts to size_t, from int.

    In passing, change the FilePrefetch() amount argument from int to
    off_t, to match the underlying posix_fadvise().

0002-Remove-unnecessary-casts.patch

    Some code carefully cast all data buffer arguments for
    BufFileWrite() and BufFileRead() to void *, even though the
    arguments are already void * (and AFAICT were never anything else).
    Remove this unnecessary clutter.

(I had initially thought these casts were related to the first patch, but as I said the BufFile API never used char * arguments, so this turned out to be unrelated, but still weird.)
From 0ca35b75e383272356ba49211913d8aead5ca10d Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <pe...@eisentraut.org>
Date: Thu, 1 Dec 2022 08:36:09 +0100
Subject: [PATCH 1/2] Update types in File API

Make the argument types of the File API match stdio better:

- Change the data buffer to void *, from char *.
- Change FileWrite() data buffer to const on top of that.
- Change amounts to size_t, from int.

In passing, change the FilePrefetch() amount argument from int to
off_t, to match the underlying posix_fadvise().
---
 src/backend/storage/file/fd.c | 8 ++++----
 src/include/storage/fd.h      | 6 +++---
 2 files changed, 7 insertions(+), 7 deletions(-)

diff --git a/src/backend/storage/file/fd.c b/src/backend/storage/file/fd.c
index 4151cafec547..f6c938202309 100644
--- a/src/backend/storage/file/fd.c
+++ b/src/backend/storage/file/fd.c
@@ -1980,7 +1980,7 @@ FileClose(File file)
  * to read into.
  */
 int
-FilePrefetch(File file, off_t offset, int amount, uint32 wait_event_info)
+FilePrefetch(File file, off_t offset, off_t amount, uint32 wait_event_info)
 {
 #if defined(USE_POSIX_FADVISE) && defined(POSIX_FADV_WILLNEED)
        int                     returnCode;
@@ -2031,7 +2031,7 @@ FileWriteback(File file, off_t offset, off_t nbytes, 
uint32 wait_event_info)
 }
 
 int
-FileRead(File file, char *buffer, int amount, off_t offset,
+FileRead(File file, void *buffer, size_t amount, off_t offset,
                 uint32 wait_event_info)
 {
        int                     returnCode;
@@ -2039,7 +2039,7 @@ FileRead(File file, char *buffer, int amount, off_t 
offset,
 
        Assert(FileIsValid(file));
 
-       DO_DB(elog(LOG, "FileRead: %d (%s) " INT64_FORMAT " %d %p",
+       DO_DB(elog(LOG, "FileRead: %d (%s) " INT64_FORMAT " %zu %p",
                           file, VfdCache[file].fileName,
                           (int64) offset,
                           amount, buffer));
@@ -2087,7 +2087,7 @@ FileRead(File file, char *buffer, int amount, off_t 
offset,
 }
 
 int
-FileWrite(File file, char *buffer, int amount, off_t offset,
+FileWrite(File file, const void *buffer, size_t amount, off_t offset,
                  uint32 wait_event_info)
 {
        int                     returnCode;
diff --git a/src/include/storage/fd.h b/src/include/storage/fd.h
index c0a212487d92..7144fc9f6050 100644
--- a/src/include/storage/fd.h
+++ b/src/include/storage/fd.h
@@ -102,9 +102,9 @@ extern File PathNameOpenFile(const char *fileName, int 
fileFlags);
 extern File PathNameOpenFilePerm(const char *fileName, int fileFlags, mode_t 
fileMode);
 extern File OpenTemporaryFile(bool interXact);
 extern void FileClose(File file);
-extern int     FilePrefetch(File file, off_t offset, int amount, uint32 
wait_event_info);
-extern int     FileRead(File file, char *buffer, int amount, off_t offset, 
uint32 wait_event_info);
-extern int     FileWrite(File file, char *buffer, int amount, off_t offset, 
uint32 wait_event_info);
+extern int     FilePrefetch(File file, off_t offset, off_t amount, uint32 
wait_event_info);
+extern int     FileRead(File file, void *buffer, size_t amount, off_t offset, 
uint32 wait_event_info);
+extern int     FileWrite(File file, const void *buffer, size_t amount, off_t 
offset, uint32 wait_event_info);
 extern int     FileSync(File file, uint32 wait_event_info);
 extern off_t FileSize(File file);
 extern int     FileTruncate(File file, off_t offset, uint32 wait_event_info);

base-commit: 43351557d0d2b9c5e20298b5fee2849abef86aff
-- 
2.38.1

From 8d0530fbf9095bc16fa3465b69aef2c31c4c3c9b Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <pe...@eisentraut.org>
Date: Thu, 1 Dec 2022 08:36:09 +0100
Subject: [PATCH 2/2] Remove unnecessary casts

Some code carefully cast all data buffer arguments for BufFileWrite()
and BufFileRead() to void *, even though the arguments are already
void * (and AFAICT were never anything else).  Remove this unnecessary
clutter.
---
 src/backend/executor/nodeHashjoin.c |  8 ++++----
 src/backend/utils/sort/tuplestore.c | 12 ++++++------
 2 files changed, 10 insertions(+), 10 deletions(-)

diff --git a/src/backend/executor/nodeHashjoin.c 
b/src/backend/executor/nodeHashjoin.c
index 2718c2113f58..3e1a997f920d 100644
--- a/src/backend/executor/nodeHashjoin.c
+++ b/src/backend/executor/nodeHashjoin.c
@@ -1227,8 +1227,8 @@ ExecHashJoinSaveTuple(MinimalTuple tuple, uint32 
hashvalue,
                *fileptr = file;
        }
 
-       BufFileWrite(file, (void *) &hashvalue, sizeof(uint32));
-       BufFileWrite(file, (void *) tuple, tuple->t_len);
+       BufFileWrite(file, &hashvalue, sizeof(uint32));
+       BufFileWrite(file, tuple, tuple->t_len);
 }
 
 /*
@@ -1260,7 +1260,7 @@ ExecHashJoinGetSavedTuple(HashJoinState *hjstate,
         * we can read them both in one BufFileRead() call without any type
         * cheating.
         */
-       nread = BufFileRead(file, (void *) header, sizeof(header));
+       nread = BufFileRead(file, header, sizeof(header));
        if (nread == 0)                         /* end of file */
        {
                ExecClearTuple(tupleSlot);
@@ -1275,7 +1275,7 @@ ExecHashJoinGetSavedTuple(HashJoinState *hjstate,
        tuple = (MinimalTuple) palloc(header[1]);
        tuple->t_len = header[1];
        nread = BufFileRead(file,
-                                               (void *) ((char *) tuple + 
sizeof(uint32)),
+                                               ((char *) tuple + 
sizeof(uint32)),
                                                header[1] - sizeof(uint32));
        if (nread != header[1] - sizeof(uint32))
                ereport(ERROR,
diff --git a/src/backend/utils/sort/tuplestore.c 
b/src/backend/utils/sort/tuplestore.c
index f605ece721eb..cc884ab11649 100644
--- a/src/backend/utils/sort/tuplestore.c
+++ b/src/backend/utils/sort/tuplestore.c
@@ -1468,7 +1468,7 @@ getlen(Tuplestorestate *state, bool eofOK)
        unsigned int len;
        size_t          nbytes;
 
-       nbytes = BufFileRead(state->myfile, (void *) &len, sizeof(len));
+       nbytes = BufFileRead(state->myfile, &len, sizeof(len));
        if (nbytes == sizeof(len))
                return len;
        if (nbytes != 0 || !eofOK)
@@ -1512,10 +1512,10 @@ writetup_heap(Tuplestorestate *state, void *tup)
        /* total on-disk footprint: */
        unsigned int tuplen = tupbodylen + sizeof(int);
 
-       BufFileWrite(state->myfile, (void *) &tuplen, sizeof(tuplen));
-       BufFileWrite(state->myfile, (void *) tupbody, tupbodylen);
+       BufFileWrite(state->myfile, &tuplen, sizeof(tuplen));
+       BufFileWrite(state->myfile, tupbody, tupbodylen);
        if (state->backward)            /* need trailing length word? */
-               BufFileWrite(state->myfile, (void *) &tuplen, sizeof(tuplen));
+               BufFileWrite(state->myfile, &tuplen, sizeof(tuplen));
 
        FREEMEM(state, GetMemoryChunkSpace(tuple));
        heap_free_minimal_tuple(tuple);
@@ -1533,7 +1533,7 @@ readtup_heap(Tuplestorestate *state, unsigned int len)
        USEMEM(state, GetMemoryChunkSpace(tuple));
        /* read in the tuple proper */
        tuple->t_len = tuplen;
-       nread = BufFileRead(state->myfile, (void *) tupbody, tupbodylen);
+       nread = BufFileRead(state->myfile, tupbody, tupbodylen);
        if (nread != (size_t) tupbodylen)
                ereport(ERROR,
                                (errcode_for_file_access(),
@@ -1541,7 +1541,7 @@ readtup_heap(Tuplestorestate *state, unsigned int len)
                                                nread, (size_t) tupbodylen)));
        if (state->backward)            /* need trailing length word? */
        {
-               nread = BufFileRead(state->myfile, (void *) &tuplen, 
sizeof(tuplen));
+               nread = BufFileRead(state->myfile, &tuplen, sizeof(tuplen));
                if (nread != sizeof(tuplen))
                        ereport(ERROR,
                                        (errcode_for_file_access(),
-- 
2.38.1

Reply via email to