nvptx: Add _ssize_t as _READ_WRITE_RETURN_TYPE in newlib/libc/include/sys/config.h

Message ID 20250714024141.99939-1-arijitkdgit.official@gmail.com
State New
Headers
Series nvptx: Add _ssize_t as _READ_WRITE_RETURN_TYPE in newlib/libc/include/sys/config.h |

Commit Message

Arijit Kumar Das July 14, 2025, 2:41 a.m. UTC
  Signed-off-by: Arijit Kumar Das <arijitkdgit.official@gmail.com>
---
 newlib/libc/include/sys/config.h | 4 ++++
 1 file changed, 4 insertions(+)
  

Comments

Thomas Schwinge July 14, 2025, 9:31 a.m. UTC | #1
Hi Arijit!

Thanks for looking into this!

On 2025-07-14T08:11:41+0530, Arijit Kumar Das via Newlib <newlib@sourceware.org> wrote:
> --- a/newlib/libc/include/sys/config.h
> +++ b/newlib/libc/include/sys/config.h
> @@ -12,6 +12,10 @@
>  #define __DYNAMIC_REENT__
>  #endif
>  
> +#ifdef __nvptx__
> +#define _READ_WRITE_RETURN_TYPE _ssize_t
> +#endif
> +
>  /* exceptions first */
>  #if defined(__H8500__) || defined(__W65__)
>  #define __SMALL_BITFIELDS

I did wonder why placing this new '#define' here is a good choice -- but
then, I've just looked at the current 'config.h' file, and found that
placement of most of its content appears to be rather disorganized, so
this placement here indeed should be as good as (almost) any other.
(We shall of course be happy to hear from the newlib maintainers, if
they've got any better advice.)


But: won't that change in isolation break the build, or at least warn,
given that we've still got 'newlib/libc/machine/nvptx/misc.c':

    int
    read(int fd, void *buf, size_t count) {

..., that is, 'int' instead of the desire 'ssize_t' return type?

In 'newlib/libc/machine/nvptx/write.c' we have:

    _READ_WRITE_RETURN_TYPE write (int fd, const void *buf, size_t count)
    {

Apparently, there isn't any diagnostic about the 'int' vs. 'ssize_t' for
'read', but I suggest that you change the return types of both 'read' and
'write' to the actual type, 'ssize_t'.  (Actually, I don't see any
'-W[...]' flags get used for an combined-tree GCC/newlib build, huh...)


Another thing I've now noticed: nvptx' 'read' and 'write' implementations
correctly use 'size_t count' (see cited above) -- but in 'config.h', as
far as I can tell, we use the default 'int':

    /* Define `count' parameter of read/write routines.  In POSIX, the `count'
       parameter is "size_t" but legacy newlib code has been using "int" for some
       time.  If not specified, "int" is defaulted.  */
    #ifndef _READ_WRITE_BUFSIZE_TYPE
    #define _READ_WRITE_BUFSIZE_TYPE int
    #endif

I suggest that for nvptx, next to your new '_READ_WRITE_RETURN_TYPE', you
also add '#define _READ_WRITE_BUFSIZE_TYPE __size_t' (double underscore,
per 'newlib/libc/include/sys/_types.h'), what do you think?
(I'm slightly confused, as no configuration apart from Cygwin,
'winsup/cygwin/include/cygwin/config.h', appears to be doing this, but it
still seems the right thing to do?)

Make that either an additional commit, or as part of this one, as you
like.


I'll meanwhile test all this, too.


Grüße
 Thomas
  
Arijit Kumar Das July 15, 2025, 8:39 a.m. UTC | #2
Hi Thomas!

Sorry for the late response. I had seen your email earlier yesterday,
but thought of replying
after I got back to my laptop (which I didn't, until today). And for
some reason, Slack fails to connect from the mobile app (could be an
ISP issue because I can connect from my laptop)
so I couldn't respond from my phone.

> I did wonder why placing this new '#define' here is a good choice -- but
> then, I've just looked at the current 'config.h' file, and found that
> placement of most of its content appears to be rather disorganized, so
> this placement here indeed should be as good as (almost) any other.
> (We shall of course be happy to hear from the newlib maintainers, if
> they've got any better advice.)

Yes, and I placed it there because the #define for __AMDGCN__ was just
above that, and both
are kind of related so mainly that's why. Moreover, __nvptx__ wasn't
#define -d anywhere else
so that location seemed plausible to me. I'd of course like to hear
from the newlib maintainers,
as you said.

> But: won't that change in isolation break the build, or at least warn,
> given that we've still got 'newlib/libc/machine/nvptx/misc.c':
>
>     int
>     read(int fd, void *buf, size_t count) {
>
> ..., that is, 'int' instead of the desire 'ssize_t' return type?
>
> In 'newlib/libc/machine/nvptx/write.c' we have:
>
>     _READ_WRITE_RETURN_TYPE write (int fd, const void *buf, size_t count)
>     {

That's a very real possibility, and in fact after some speculation
it's likely to occur since the
commit does not change the return type of read() to _READ_WRITE_RETURN_TYPE (aka
_ssize_t) whereas that's the one for write() in write.c. This causes
both to have a different
function signature, which will most definitely cause a CUDA error, if
not a compile time
error or warning, as per my earlier experience.

> Apparently, there isn't any diagnostic about the 'int' vs. 'ssize_t' for
> 'read', but I suggest that you change the return types of both 'read' and
> 'write' to the actual type, 'ssize_t'.  (Actually, I don't see any
> '-W[...]' flags get used for an combined-tree GCC/newlib build, huh...)

Okay, so I change it directly to ssize_t instead of _ssize_t?
(Actually, I followed the convention
of the other #define -s for _READ_WRITE_RETURN_TYPE from config.h).

> Another thing I've now noticed: nvptx' 'read' and 'write' implementations
> correctly use 'size_t count' (see cited above) -- but in 'config.h', as
> far as I can tell, we use the default 'int':
>
>     /* Define `count' parameter of read/write routines.  In POSIX, the `count'
>        parameter is "size_t" but legacy newlib code has been using "int" for some
>        time.  If not specified, "int" is defaulted.  */
>     #ifndef _READ_WRITE_BUFSIZE_TYPE
>     #define _READ_WRITE_BUFSIZE_TYPE int
>     #endif
>
> I suggest that for nvptx, next to your new '_READ_WRITE_RETURN_TYPE', you
> also add '#define _READ_WRITE_BUFSIZE_TYPE __size_t' (double underscore,
> per 'newlib/libc/include/sys/_types.h'), what do you think?
> (I'm slightly confused, as no configuration apart from Cygwin,
> 'winsup/cygwin/include/cygwin/config.h', appears to be doing this, but it
> still seems the right thing to do?)

Yeah, __size_t is indeed correct and I verified that from _types.h as
you said. And I'll be
incorporating this new #define too, and update count as in read() and write().

> Make that either an additional commit, or as part of this one, as you
> like.

Alright.

Another thing which I was thinking is that, now that my work in
newlib/libc/machine/nvptx/misc.c
is functional (albeit, not optimized or following some expected
standards per se) could I add this
one too, in my commit? That doesn't break the build, adds some
functionality, and I can still
continue working on it and sending my commits without breaking stuff.

If the above idea isn't possible at the moment, I can also just update
the function signatures
in newlib/libc/machine/nvptx/misc.c and
newlib/libc/machine/nvptx/write.c and leave everything
as it is.

Let me know what's your take on the above.

Best regards,
Arijit
  
Thomas Schwinge July 16, 2025, 7:47 a.m. UTC | #3
Hi Arijit!

On 2025-07-15T14:09:03+0530, Arijit Kumar Das via Newlib <newlib@sourceware.org> wrote:
>> But: won't that change in isolation break the build, or at least warn,
>> given that we've still got 'newlib/libc/machine/nvptx/misc.c':
>>
>>     int
>>     read(int fd, void *buf, size_t count) {
>>
>> ..., that is, 'int' instead of the desire 'ssize_t' return type?
>>
>> In 'newlib/libc/machine/nvptx/write.c' we have:
>>
>>     _READ_WRITE_RETURN_TYPE write (int fd, const void *buf, size_t count)
>>     {
>
> That's a very real possibility, and in fact after some speculation
> it's likely to occur since the
> commit does not change the return type of read() to _READ_WRITE_RETURN_TYPE (aka
> _ssize_t) whereas that's the one for write() in write.c. This causes
> both to have a different
> function signature, which will most definitely cause a CUDA error, if
> not a compile time
> error or warning, as per my earlier experience.
>
>> Apparently, there isn't any diagnostic about the 'int' vs. 'ssize_t' for
>> 'read', but I suggest that you change the return types of both 'read' and
>> 'write' to the actual type, 'ssize_t'.  (Actually, I don't see any
>> '-W[...]' flags get used for an combined-tree GCC/newlib build, huh...)
>
> Okay, so I change it directly to ssize_t instead of _ssize_t?
> (Actually, I followed the convention
> of the other #define -s for _READ_WRITE_RETURN_TYPE from config.h).

I've not researched all the context/history here, but usually it goes
similar to this:

Generic code (for example: 'newlib/libc/include/sys/config.h',
'newlib/libc/stdio/stdio.c'), which has to work for all possible newlib
targets/configurations, is often using some indirections/abstractions
(for example: '_ssize_t' corresponding to the actual 'ssize_t', or
'_READ_WRITE_RETURN_TYPE' corresponding to the actual 'read', 'write'
return type), so that certain targets/configurations are able to "fix up
things" if necessary.

In contrast, the target-specific implementations (for example:
'newlib/libc/machine/nvptx/write.c') can directly use the actual types
(for example: 'ssize_t' instead of '_ssize_t' or
'_READ_WRITE_RETURN_TYPE'); they (mostly) don't need the
indirections/abstractions, as they define (implement) code for the actual
target/configuration.


> Another thing which I was thinking is that, now that my work in
> newlib/libc/machine/nvptx/misc.c
> is functional (albeit, not optimized or following some expected
> standards per se) could I add this
> one too, in my commit? That doesn't break the build, adds some
> functionality, and I can still
> continue working on it and sending my commits without breaking stuff.

:-) I understand you're eager to get your new code out there, but let's
first sort out the '_READ_WRITE_RETURN_TYPE', '_READ_WRITE_BUFSIZE_TYPE'
thing, and then continue to work on the new code some more, test it
properly (via the GCC test suite, for example), and clean it up, etc.
We'll get there.  :-)


Grüße
 Thomas
  
Arijit Kumar Das July 16, 2025, 5:15 p.m. UTC | #4
Hi Thomas!

On Wed, Jul 16, 2025 at 1:17 PM Thomas Schwinge <tschwinge@baylibre.com> wrote:

> > Okay, so I change it directly to ssize_t instead of _ssize_t?
> > (Actually, I followed the convention
> > of the other #define -s for _READ_WRITE_RETURN_TYPE from config.h).
>
> I've not researched all the context/history here, but usually it goes
> similar to this:
>
> Generic code (for example: 'newlib/libc/include/sys/config.h',
> 'newlib/libc/stdio/stdio.c'), which has to work for all possible newlib
> targets/configurations, is often using some indirections/abstractions
> (for example: '_ssize_t' corresponding to the actual 'ssize_t', or
> '_READ_WRITE_RETURN_TYPE' corresponding to the actual 'read', 'write'
> return type), so that certain targets/configurations are able to "fix up
> things" if necessary.
>
> In contrast, the target-specific implementations (for example:
> 'newlib/libc/machine/nvptx/write.c') can directly use the actual types
> (for example: 'ssize_t' instead of '_ssize_t' or
> '_READ_WRITE_RETURN_TYPE'); they (mostly) don't need the
> indirections/abstractions, as they define (implement) code for the actual
> target/configuration.
>

Got it! I'll be sending the patch soon. Here's what I'll add to config.h:

#ifdef __nvptx__
#define _READ_WRITE_RETURN_TYPE    ssize_t
#define _READ_WRITE_BUFSIZE_TYPE    size_t

Does that look good?


>
> > Another thing which I was thinking is that, now that my work in
> > newlib/libc/machine/nvptx/misc.c
> > is functional (albeit, not optimized or following some expected
> > standards per se) could I add this
> > one too, in my commit? That doesn't break the build, adds some
> > functionality, and I can still
> > continue working on it and sending my commits without breaking stuff.
>
> :-) I understand you're eager to get your new code out there, but let's
> first sort out the '_READ_WRITE_RETURN_TYPE', '_READ_WRITE_BUFSIZE_TYPE'
> thing, and then continue to work on the new code some more, test it
> properly (via the GCC test suite, for example), and clean it up, etc.
> We'll get there.  :-)
>

Alright! But can I also update the function signatures in write.c and misc.c to
_READ_WRITE_RETURN_TYPE and _READ_WRITE_BUFSIZE_TYPE
wherever applicable and leave the rest of the things as it is (i.e. not include
my filesystem code yet)? Otherwise the function signatures for read()
and write()
become different since write() uses_READ_WRITE_RETURN_TYPE and
read() uses int which can cause a run time CUDA error.

Let me know what do you think and I'll be glad to send my first commit
to be merged :-)

Best regards,
Arijit
  
Thomas Schwinge July 16, 2025, 6:12 p.m. UTC | #5
Hi Arijit!

On 2025-07-16T22:45:39+0530, Arijit Kumar Das <arijitkdgit.official@gmail.com> wrote:
> On Wed, Jul 16, 2025 at 1:17 PM Thomas Schwinge <tschwinge@baylibre.com> wrote:
>> Generic code (for example: 'newlib/libc/include/sys/config.h',
>> 'newlib/libc/stdio/stdio.c'), which has to work for all possible newlib
>> targets/configurations, is often using some indirections/abstractions
>> (for example: '_ssize_t' corresponding to the actual 'ssize_t', or
>> '_READ_WRITE_RETURN_TYPE' corresponding to the actual 'read', 'write'
>> return type), so that certain targets/configurations are able to "fix up
>> things" if necessary.
>>
>> In contrast, the target-specific implementations (for example:
>> 'newlib/libc/machine/nvptx/write.c') can directly use the actual types
>> (for example: 'ssize_t' instead of '_ssize_t' or
>> '_READ_WRITE_RETURN_TYPE'); they (mostly) don't need the
>> indirections/abstractions, as they define (implement) code for the actual
>> target/configuration.
>>
>
> Got it! I'll be sending the patch soon. Here's what I'll add to config.h:
>
> #ifdef __nvptx__
> #define _READ_WRITE_RETURN_TYPE    ssize_t
> #define _READ_WRITE_BUFSIZE_TYPE    size_t

No, in the (generic) 'newlib/libc/include/sys/config.h', you should use
'_ssize_t' and '__size_t'.  It won't make a difference in practice, but
for consistency.

>> [...] let's
>> first sort out the '_READ_WRITE_RETURN_TYPE', '_READ_WRITE_BUFSIZE_TYPE'
>> thing, [...]
>
> Alright! But can I also update the function signatures in write.c and misc.c to
> _READ_WRITE_RETURN_TYPE and _READ_WRITE_BUFSIZE_TYPE
> wherever applicable and leave the rest of the things as it is (i.e. not include
> my filesystem code yet)?

Do not include your new code yet, just update:

> Otherwise the function signatures for read()
> and write()
> become different since write() uses_READ_WRITE_RETURN_TYPE and
> read() uses int which can cause a run time CUDA error.

... these to 'ssize_t' return type.


Grüße
 Thomas
  
Arijit Kumar Das July 16, 2025, 7:57 p.m. UTC | #6
Hi Thomas!

Here's the final commit. I hope it's okay :-)

Best regards,
Arijit


From 3089acec0f3b5606bffcbca3b6864023ccd2afa3 Mon Sep 17 00:00:00 2001
From: Arijit Kumar Das <arijitkdgit.official@gmail.com>
Date: Thu, 17 Jul 2025 01:01:03 +0530
Subject: [PATCH] [nvptx]: Use _READ_WRITE_RETURN_TYPE for return type and
 _READ_WRITE_BUFSIZE_TYPE for count in read() and write()

- Changed the return type of read() from int to
_READ_WRITE_RETURN_TYPE as per conventions.
- Changed the 'count' parameter of read() and write() to
_READ_WRITE_BUFSIZE_TYPE.
- Defined both of the above macros in libc/include/sys/config.h under __nvptx__.
- _READ_WRITE_RETURN_TYPE defined as _ssize_t (ssize_t).
- _READ_WRITE_BUFSIZE_TYPE defined as __size_t (size_t).

Signed-off-by: Arijit Kumar Das <arijitkdgit.official@gmail.com>
---
 newlib/libc/include/sys/config.h  | 5 +++++
 newlib/libc/machine/nvptx/misc.c  | 4 ++--
 newlib/libc/machine/nvptx/write.c | 3 ++-
 3 files changed, 9 insertions(+), 3 deletions(-)

diff --git a/newlib/libc/include/sys/config.h b/newlib/libc/include/sys/config.h
index 4c9acc55c..c3cd51e86 100644
--- a/newlib/libc/include/sys/config.h
+++ b/newlib/libc/include/sys/config.h
@@ -12,6 +12,11 @@
 #define __DYNAMIC_REENT__
 #endif

+#ifdef __nvptx__
+#define _READ_WRITE_RETURN_TYPE _ssize_t
+#define _READ_WRITE_BUFSIZE_TYPE __size_t
+#endif
+
 /* exceptions first */
 #if defined(__H8500__) || defined(__W65__)
 #define __SMALL_BITFIELDS
diff --git a/newlib/libc/machine/nvptx/misc.c b/newlib/libc/machine/nvptx/misc.c
index 56e66b9f3..329804f88 100644
--- a/newlib/libc/machine/nvptx/misc.c
+++ b/newlib/libc/machine/nvptx/misc.c
@@ -62,8 +62,8 @@ open (const char *pathname, int flags, ...) {
   return -1;
 }

-int
-read(int fd, void *buf, size_t count) {
+_READ_WRITE_RETURN_TYPE
+read(int fd, void *buf, _READ_WRITE_BUFSIZE_TYPE count) {
   return 0;
 }

diff --git a/newlib/libc/machine/nvptx/write.c
b/newlib/libc/machine/nvptx/write.c
index 0544dd05e..49fb65944 100644
--- a/newlib/libc/machine/nvptx/write.c
+++ b/newlib/libc/machine/nvptx/write.c
@@ -18,7 +18,8 @@
 #include <unistd.h>
 #include <errno.h>

-_READ_WRITE_RETURN_TYPE write (int fd, const void *buf, size_t count)
+_READ_WRITE_RETURN_TYPE
+write (int fd, const void *buf, _READ_WRITE_BUFSIZE_TYPE count)
 {
   size_t i;
   char *b = (char *)buf;
  
Thomas Schwinge July 17, 2025, 8:19 a.m. UTC | #7
Hi Arijit!

On 2025-07-17T01:27:41+0530, Arijit Kumar Das <arijitkdgit.official@gmail.com> wrote:
> Here's the final commit. I hope it's okay :-)

Almost.  ;-)

> From 3089acec0f3b5606bffcbca3b6864023ccd2afa3 Mon Sep 17 00:00:00 2001
> From: Arijit Kumar Das <arijitkdgit.official@gmail.com>
> Date: Thu, 17 Jul 2025 01:01:03 +0530
> Subject: [PATCH] [nvptx]: Use _READ_WRITE_RETURN_TYPE for return type and
>  _READ_WRITE_BUFSIZE_TYPE for count in read() and write()

The important thing about this commit is the changes of types.  I'd say:

    nvptx: Change 'read' and 'write' to 'ssize_t' return type, and 'size_t count'

Your following text is just textually describing the commit:

> - Changed the return type of read() from int to _READ_WRITE_RETURN_TYPE as per conventions.
> - Changed the 'count' parameter of read() and write() to _READ_WRITE_BUFSIZE_TYPE.
> - Defined both of the above macros in libc/include/sys/config.h under __nvptx__.
> - _READ_WRITE_RETURN_TYPE defined as _ssize_t (ssize_t).
> - _READ_WRITE_BUFSIZE_TYPE defined as __size_t (size_t).

..., so I personally wouldn't include that.  Instead, you could add some
rationale, perhaps:

    ... to match their specification in POSIX, and enable large files.

> Signed-off-by: Arijit Kumar Das <arijitkdgit.official@gmail.com>
> ---
>  newlib/libc/include/sys/config.h  | 5 +++++
>  newlib/libc/machine/nvptx/misc.c  | 4 ++--
>  newlib/libc/machine/nvptx/write.c | 3 ++-
>  3 files changed, 9 insertions(+), 3 deletions(-)

> --- a/newlib/libc/include/sys/config.h
> +++ b/newlib/libc/include/sys/config.h
> @@ -12,6 +12,11 @@
>  #define __DYNAMIC_REENT__
>  #endif
>  
> +#ifdef __nvptx__
> +#define _READ_WRITE_RETURN_TYPE _ssize_t
> +#define _READ_WRITE_BUFSIZE_TYPE __size_t
> +#endif
> +
>  /* exceptions first */
>  #if defined(__H8500__) || defined(__W65__)
>  #define __SMALL_BITFIELDS

ACK.

But for the following two nvptx-specific implementation files:

> --- a/newlib/libc/machine/nvptx/misc.c
> +++ b/newlib/libc/machine/nvptx/misc.c

> -int
> -read(int fd, void *buf, size_t count) {
> +_READ_WRITE_RETURN_TYPE
> +read(int fd, void *buf, _READ_WRITE_BUFSIZE_TYPE count) {

> --- a/newlib/libc/machine/nvptx/write.c
> +++ b/newlib/libc/machine/nvptx/write.c

> -_READ_WRITE_RETURN_TYPE write (int fd, const void *buf, size_t count)
> +_READ_WRITE_RETURN_TYPE
> +write (int fd, const void *buf, _READ_WRITE_BUFSIZE_TYPE count)
>  {

..., I'd like you to use the actual types.  So, please here replace
'_READ_WRITE_RETURN_TYPE' with 'ssize_t', and revert back
'_READ_WRITE_BUFSIZE_TYPE' to 'size_t'.


Grüße
 Thomas
  
Arijit Kumar Das July 17, 2025, 11:30 a.m. UTC | #8
Hi Thomas!

Here's the updated version and (hopefully) this one's done as you instructed :-)

Best regards,
Arijit


From b7ed7f8883053fd84c77a87e69ed757b55551b5f Mon Sep 17 00:00:00 2001
From: Arijit Kumar Das <arijitkdgit.official@gmail.com>
Date: Thu, 17 Jul 2025 16:21:16 +0530
Subject: [PATCH] nvptx: Change 'read' and 'write' to 'ssize_t' return type

This commit changes the return type of the read() and write() syscalls for
nvptx to ssize_t. This would allow large files to be handled properly by
these syscalls in situations where the read/write buffer length exceeds
INT_MAX, for example. This also makes the syscall signatures fully complaint
with their current POSIX specifications.

We additionally define two macros: '_READ_WRITE_RETURN_TYPE' as _ssize_t and
'_READ_WRITE_BUFSIZE_TYPE' as __size_t in libc/include/sys/config.h under
__nvptx__ for consistency.

Signed-off-by: Arijit Kumar Das <arijitkdgit.official@gmail.com>
---
 newlib/libc/include/sys/config.h  | 5 +++++
 newlib/libc/machine/nvptx/misc.c  | 2 +-
 newlib/libc/machine/nvptx/write.c | 3 ++-
 3 files changed, 8 insertions(+), 2 deletions(-)

diff --git a/newlib/libc/include/sys/config.h b/newlib/libc/include/sys/config.h
index 4c9acc55c..c3cd51e86 100644
--- a/newlib/libc/include/sys/config.h
+++ b/newlib/libc/include/sys/config.h
@@ -12,6 +12,11 @@
 #define __DYNAMIC_REENT__
 #endif

+#ifdef __nvptx__
+#define _READ_WRITE_RETURN_TYPE _ssize_t
+#define _READ_WRITE_BUFSIZE_TYPE __size_t
+#endif
+
 /* exceptions first */
 #if defined(__H8500__) || defined(__W65__)
 #define __SMALL_BITFIELDS
diff --git a/newlib/libc/machine/nvptx/misc.c b/newlib/libc/machine/nvptx/misc.c
index 56e66b9f3..829921ec2 100644
--- a/newlib/libc/machine/nvptx/misc.c
+++ b/newlib/libc/machine/nvptx/misc.c
@@ -62,7 +62,7 @@ open (const char *pathname, int flags, ...) {
   return -1;
 }

-int
+ssize_t
 read(int fd, void *buf, size_t count) {
   return 0;
 }
diff --git a/newlib/libc/machine/nvptx/write.c
b/newlib/libc/machine/nvptx/write.c
index 0544dd05e..38f086893 100644
--- a/newlib/libc/machine/nvptx/write.c
+++ b/newlib/libc/machine/nvptx/write.c
@@ -18,7 +18,8 @@
 #include <unistd.h>
 #include <errno.h>

-_READ_WRITE_RETURN_TYPE write (int fd, const void *buf, size_t count)
+ssize_t
+write (int fd, const void *buf, size_t count)
 {
   size_t i;
   char *b = (char *)buf;
  
Thomas Schwinge July 17, 2025, 1:30 p.m. UTC | #9
Hi!

On 2025-07-17T17:00:59+0530, Arijit Kumar Das <arijitkdgit.official@gmail.com> wrote:
> Here's the updated version and (hopefully) this one's done as you instructed :-)

Pushed to main branch commit 5d8c71af5e0fa5cdc99d9f741624920e34756418
"nvptx: Change 'read' and 'write' to 'ssize_t' return type".

Arijit, congratulations to your first commit in newlib!  :-D


Grüße
 Thomas


> From b7ed7f8883053fd84c77a87e69ed757b55551b5f Mon Sep 17 00:00:00 2001
> From: Arijit Kumar Das <arijitkdgit.official@gmail.com>
> Date: Thu, 17 Jul 2025 16:21:16 +0530
> Subject: [PATCH] nvptx: Change 'read' and 'write' to 'ssize_t' return type
>
> This commit changes the return type of the read() and write() syscalls for
> nvptx to ssize_t. This would allow large files to be handled properly by
> these syscalls in situations where the read/write buffer length exceeds
> INT_MAX, for example. This also makes the syscall signatures fully complaint
> with their current POSIX specifications.
>
> We additionally define two macros: '_READ_WRITE_RETURN_TYPE' as _ssize_t and
> '_READ_WRITE_BUFSIZE_TYPE' as __size_t in libc/include/sys/config.h under
> __nvptx__ for consistency.
>
> Signed-off-by: Arijit Kumar Das <arijitkdgit.official@gmail.com>
> ---
>  newlib/libc/include/sys/config.h  | 5 +++++
>  newlib/libc/machine/nvptx/misc.c  | 2 +-
>  newlib/libc/machine/nvptx/write.c | 3 ++-
>  3 files changed, 8 insertions(+), 2 deletions(-)
>
> diff --git a/newlib/libc/include/sys/config.h b/newlib/libc/include/sys/config.h
> index 4c9acc55c..c3cd51e86 100644
> --- a/newlib/libc/include/sys/config.h
> +++ b/newlib/libc/include/sys/config.h
> @@ -12,6 +12,11 @@
>  #define __DYNAMIC_REENT__
>  #endif
>  
> +#ifdef __nvptx__
> +#define _READ_WRITE_RETURN_TYPE _ssize_t
> +#define _READ_WRITE_BUFSIZE_TYPE __size_t
> +#endif
> +
>  /* exceptions first */
>  #if defined(__H8500__) || defined(__W65__)
>  #define __SMALL_BITFIELDS
> diff --git a/newlib/libc/machine/nvptx/misc.c b/newlib/libc/machine/nvptx/misc.c
> index 56e66b9f3..829921ec2 100644
> --- a/newlib/libc/machine/nvptx/misc.c
> +++ b/newlib/libc/machine/nvptx/misc.c
> @@ -62,7 +62,7 @@ open (const char *pathname, int flags, ...) {
>    return -1;
>  }
>  
> -int
> +ssize_t
>  read(int fd, void *buf, size_t count) {
>    return 0;
>  }
> diff --git a/newlib/libc/machine/nvptx/write.c b/newlib/libc/machine/nvptx/write.c
> index 0544dd05e..38f086893 100644
> --- a/newlib/libc/machine/nvptx/write.c
> +++ b/newlib/libc/machine/nvptx/write.c
> @@ -18,7 +18,8 @@
>  #include <unistd.h>
>  #include <errno.h>
>  
> -_READ_WRITE_RETURN_TYPE write (int fd, const void *buf, size_t count)
> +ssize_t
> +write (int fd, const void *buf, size_t count)
>  {
>    size_t i;
>    char *b = (char *)buf;
> -- 
> 2.39.5
  

Patch

diff --git a/newlib/libc/include/sys/config.h b/newlib/libc/include/sys/config.h
index 4c9acc55c..0ee9e672d 100644
--- a/newlib/libc/include/sys/config.h
+++ b/newlib/libc/include/sys/config.h
@@ -12,6 +12,10 @@ 
 #define __DYNAMIC_REENT__
 #endif
 
+#ifdef __nvptx__
+#define _READ_WRITE_RETURN_TYPE _ssize_t
+#endif
+
 /* exceptions first */
 #if defined(__H8500__) || defined(__W65__)
 #define __SMALL_BITFIELDS