Typecasting a void pointer to unsigned long in zsmalloc.c

Sotir Danailov sndanailov at gmail.com
Thu Jan 23 10:52:24 EST 2025


While looking through the zsmalloc.c file I stumbled upon something.

On the following line, in the zs_malloc() function:
handle = cache_alloc_handle(pool, gfp);
https://elixir.bootlin.com/linux/v6.13-rc3/source/mm/zsmalloc.c#L1356

the handle is a result of a typecast from a void pointer to an unsigned long.

return (unsigned long)kmem_cache_alloc(...);
https://elixir.bootlin.com/linux/v6.13-rc3/source/mm/zsmalloc.c#L333

What's the guarantee that an unsigned long can hold the data
from the void pointer? Is this safe to do? Shouldn't there be a special type
for doing this type of conversion?

I understand the reason for not using the void pointer directly and why
they need a "handle" instead, but I'm just a bit confused
by the method that has been chosen here.



More information about the Kernelnewbies mailing list