When casting between a pointer and an integer of a different size, GCC
emits a warning (which is escalated to a build failure by -Werror).
Indeed, if what you start with is a pointer, which you then cast to a
shorter integer and then back again, you're going to cut off some bits
of the pointer.
But if you start with an integer (such as mach_port_t), then cast it to
a longer pointer (void *), and then back to a shorter integer, you are
fine. To keep GCC happy, cast through an intermediary uintptr_t, which
is always the same size as a pointer.
Signed-off-by: Sergey Bugaev <bugaevc@gmail.com>
Message-Id: <
20230212111044.610942-4-bugaevc@gmail.com>
__cthread_detach (__cthread_t thread)
{
int err;
+ pthread_t pthread = (pthread_t) (uintptr_t) thread;
- err = __pthread_detach ((pthread_t) thread);
+ err = __pthread_detach (pthread);
assert_perror (err);
}
weak_alias (__cthread_detach, cthread_detach)
err = __pthread_create (&thread, NULL, func, arg);
assert_perror (err);
- return (__cthread_t) thread;
+ return (__cthread_t) (uintptr_t) thread;
}
weak_alias (__cthread_fork, cthread_fork)
mach_msg_type_number_t nread;
error_t err;
char *bufp = buf;
+ io_t io = (io_t) (uintptr_t) cookie;
nread = n;
- if (err = __io_read ((io_t) cookie, &bufp, &nread, -1, n))
+ if (err = __io_read (io, &bufp, &nread, -1, n))
return __hurd_fail (err);
if (bufp != buf)
{
vm_size_t wrote;
error_t err;
+ io_t io = (io_t) (uintptr_t) cookie;
- if (err = __io_write ((io_t) cookie, buf, n, -1, &wrote))
+ if (err = __io_write (io, buf, n, -1, &wrote))
return __hurd_fail (err);
return wrote;
off64_t *pos,
int whence)
{
- error_t err = __io_seek ((file_t) cookie, *pos, whence, pos);
+ io_t io = (io_t) (uintptr_t) cookie;
+ error_t err = __io_seek (io, *pos, whence, pos);
return err ? __hurd_fail (err) : 0;
}
static int
closeio (void *cookie)
{
+ io_t io = (io_t) (uintptr_t) cookie;
error_t error = __mach_port_deallocate (__mach_task_self (),
- (mach_port_t) cookie);
+ io);
if (error)
return __hurd_fail (error);
return 0;
return NULL;
}
- return fopencookie ((void *) port, mode, funcsio);
+ return fopencookie ((void *) (uintptr_t) port,
+ mode, funcsio);
}
weak_alias (__fopenport, fopenport)
if (result != MACH_PORT_NULL)
{
link->cleanup = &_hurd_port_cleanup;
- link->cleanup_data = (void *) result;
+ link->cleanup_data = (void *) (uintptr_t) result;
_hurd_userlink_link (&port->users, link);
}
__spin_unlock (&port->lock);
void
_hurd_port_cleanup (void *cleanup_data, jmp_buf env, int val)
{
- __mach_port_deallocate (__mach_task_self (), (mach_port_t) cleanup_data);
+ mach_port_t port = (mach_port_t) (uintptr_t) cleanup_data;
+ __mach_port_deallocate (__mach_task_self (), port);
}
/* We were cancelled while using a port, and called from the cleanup unwinding.
static ssize_t
do_write (void *cookie, const char *buf, size_t n)
{
+ io_t io = (io_t) (uintptr_t) cookie;
vm_size_t amount = n;
- error_t error = __io_write ((io_t) cookie, buf, n, -1, &amount);
+ error_t error = __io_write (io, buf, n, -1, &amount);
if (error)
return __hurd_fail (error);
return n;
#endif
_IO_cookie_init (&temp_f.cfile, _IO_NO_READS,
- (void *) port, (cookie_io_functions_t) { write: do_write });
+ (void *) (uintptr_t) port,
+ (cookie_io_functions_t) { write: do_write });
done = __vfprintf_internal (&temp_f.cfile.__fp.file, format, arg, 0);
static ssize_t
devstream_write (void *cookie, const char *buffer, size_t n)
{
- const device_t dev = (device_t) cookie;
+ const device_t dev = (device_t) (uintptr_t) cookie;
int write_some (const char *p, size_t to_write)
{
static ssize_t
devstream_read (void *cookie, char *buffer, size_t to_read)
{
- const device_t dev = (device_t) cookie;
+ const device_t dev = (device_t) (uintptr_t) cookie;
kern_return_t err;
mach_msg_type_number_t nread = to_read;
static int
dealloc_ref (void *cookie)
{
- if (__mach_port_deallocate (mach_task_self (), (mach_port_t) cookie))
+ const device_t dev = (device_t) (uintptr_t) cookie;
+ if (__mach_port_deallocate (mach_task_self (), dev))
{
errno = EINVAL;
return -1;
return NULL;
}
- stream = _IO_fopencookie ((void *) dev, mode,
+ stream = _IO_fopencookie ((void *) (uintptr_t) dev, mode,
(cookie_io_functions_t) { write: devstream_write,
read: devstream_read,
close: dealloc_ref });