Page 1 of 1

Which type to use for pointers?

Posted: Fri Jul 19, 2013 8:28 am
by skeen
My question is somewhat simple, should i use; 'uintptr_t', 'uint32_t', 'void*' or something else, for passing around pointers in my kernel? - and why?

It seems like people are using different schemes.

Re: Which type to use for pointers?

Posted: Fri Jul 19, 2013 8:41 am
by dozniak
Pointers are usually declared as pointers, e.g. char*, void*.

Pointer-type-compatible addresses can be represented via uintptr_t.

uint32_t is a non-portable way to represent values similar to uintptr_t, it will only work for 32 bits systems.

Re: Which type to use for pointers?

Posted: Fri Jul 19, 2013 9:01 am
by bluemoon
For address manipulation where a physical address field is needed, i use custom type phyaddr_t which is defined as uint32_t or uint64_t depends on the architecture.
In case a logical address is needed, i use void* (or better yet, typed pointer).

Note that uintptr_t may be 64-bit even on 32-bit target(VC++ do this).

Re: Which type to use for pointers?

Posted: Fri Jul 19, 2013 9:04 am
by zhiayang
It really depends.

uint8_t* will address memory in terms of bytes;

Code: Select all

uint8_t* a = <memory_address>
uint8_t byte = *(a + 1);
useful for byte-fields/structures in memory.

Similar thing for uint16_t/32_t.

Don't assume uint32_t for pointers; many make the jump to long-mode; If you don't you'll need to live with the limitations of a 32-bit address space. So if (when) you move, you'd have a hell-of-a-time changing all those pointer references.


The safest and laziest way would be to use uint64_t*. A more appropriate type would be uintptr_t; it's always guaranteed to be *at least* the size of a pointer, on any system.

void* might also work, but honestly what's wrong with uintptr_t? It was conceived for only one purpose. Use it.

Re: Which type to use for pointers?

Posted: Fri Jul 19, 2013 11:01 am
by gravaera
Yo:

If it's a pointer, you should declare it as a pointer; if it's an integer, declare it as an integer.

--Peace out,
gravaera

Re: Which type to use for pointers?

Posted: Fri Jul 19, 2013 11:31 am
by OSwhatever
I have two types:

vaddr_t for virtual addresses
paddr_t for physical addresses

I could have chosen char* for vaddr_t but char* is limited for strange pointer arithmetics, something that you do a lot in the memory management so vaddr_t is an uint32_t or uint64_t depending on system. When I don't need vaddr_t benefits, I use void*, char* or MyVerySpecialType_t*.

Re: Which type to use for pointers?

Posted: Sat Jul 20, 2013 1:07 am
by dozniak
requimrar wrote:The safest and laziest way would be to use uint64_t*.
This is a pointer to 64 bit word, with corresponding arithmetics, perhaps not something you would want.

Re: Which type to use for pointers?

Posted: Sat Jul 20, 2013 1:44 am
by Jezze
What a weird collection of answers. How you define a pointer depends on what it is pointing to. If it points to a char use *char, if it is an int use *int or if you dont know use *void.

Re: Which type to use for pointers?

Posted: Sat Jul 20, 2013 2:09 am
by Brendan
Hi,
Jezze wrote:What a weird collection of answers. How you define a pointer depends on what it is pointing to. If it points to a char use *char, if it is an int use *int or if you dont know use *void.
Agreed.

Also; (assuming paging is involved) physical addresses are not pointers (they're unsigned integers) and you should use something like "uint64_t" for them (even on 32-bit kernels if PAE is involved).


Cheers,

Brendan

Re: Which type to use for pointers?

Posted: Sat Jul 20, 2013 10:14 am
by Combuster
Jezze wrote:if you dont know use *void.
If the type varies between executions, use void *. If you don't know, you're doing it wrong :wink:

Re: Which type to use for pointers?

Posted: Sat Jul 20, 2013 10:31 am
by arseniuss
I use compiler to determinate pointer size.

Code: Select all

#if __SIZEOF_POINTER__ == 1
typedef u8_t addr_t;
# define p_max u8_max
# define __BITS__ 8
#elif __SIZEOF_POINTER__ == 2
typedef u16_t addr_t;
# define p_max u16_max
# define __BITS__ 16
#elif __SIZEOF_POINTER__ == 4
typedef u32_t addr_t;
# define p_max u32_max
# define __BITS__ 32
#elif __SIZEOF_POINTER__ == 8
typedef u64_t addr_t;
# define p_max u64_max
# define __BITS__ 64
#else
# error Unknown pointer size!
#endif
But where I don't manipulate with pointer address I use void *, int *, ... etc.

Re: Which type to use for pointers?

Posted: Sat Jul 20, 2013 4:56 pm
by Griwes
That's exactly what `uintptr_t` from <stdint.h> (or <cstdint>) is for.

Re: Which type to use for pointers?

Posted: Sun Jul 21, 2013 6:13 pm
by Owen
Also, __SIZEOF_POINTER__ checks while ignoring CHAR_BITS? :-)

It's as if you think architectures don't exist with __SIZEOF_POINTER__ == 1 and CHAR_BITS == 32!

Re: Which type to use for pointers?

Posted: Mon Jul 22, 2013 1:08 am
by dozniak
Owen wrote:It's as if you think architectures don't exist with __SIZEOF_POINTER__ == 1 and CHAR_BITS == 32!
But in that case "char" is good?