Which type to use for pointers?
Which type to use for pointers?
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.
It seems like people are using different schemes.
// Skeen
// Developing a yet unnamed microkernel in C++14.
// Developing a yet unnamed microkernel in C++14.
Re: Which type to use for pointers?
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.
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.
Learn to read.
Re: Which type to use for pointers?
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).
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?
It really depends.
uint8_t* will address memory in terms of bytes;
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.
uint8_t* will address memory in terms of bytes;
Code: Select all
uint8_t* a = <memory_address>
uint8_t byte = *(a + 1);
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.
[nx] kernel: http://github.com/zhiayang/nx
- gravaera
- Member
- Posts: 737
- Joined: Tue Jun 02, 2009 4:35 pm
- Location: Supporting the cause: Use \tabs to indent code. NOT \x20 spaces.
Re: Which type to use for pointers?
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
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
17:56 < sortie> Paging is called paging because you need to draw it on pages in your notebook to succeed at it.
-
- Member
- Posts: 595
- Joined: Mon Jul 05, 2010 4:15 pm
Re: Which type to use for pointers?
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*.
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?
This is a pointer to 64 bit word, with corresponding arithmetics, perhaps not something you would want.requimrar wrote:The safest and laziest way would be to use uint64_t*.
Learn to read.
Re: Which type to use for pointers?
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.
Fudge - Simplicity, clarity and speed.
http://github.com/Jezze/fudge/
http://github.com/Jezze/fudge/
Re: Which type to use for pointers?
Hi,
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
Agreed.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.
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
For all things; perfection is, and will always remain, impossible to achieve in practice. However; by striving for perfection we create things that are as perfect as practically possible. Let the pursuit of perfection be our guide.
- Combuster
- Member
- Posts: 9301
- Joined: Wed Oct 18, 2006 3:45 am
- Libera.chat IRC: [com]buster
- Location: On the balcony, where I can actually keep 1½m distance
- Contact:
Re: Which type to use for pointers?
If the type varies between executions, use void *. If you don't know, you're doing it wrongJezze wrote:if you dont know use *void.
Re: Which type to use for pointers?
I use compiler to determinate pointer size.
But where I don't manipulate with pointer address I use void *, int *, ... etc.
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
Hobby stuff (suckless libs, compilators, game engines, kernels): github. Work @ zabbix: arseniuss@zabbix
- Griwes
- Member
- Posts: 374
- Joined: Sat Jul 30, 2011 10:07 am
- Libera.chat IRC: Griwes
- Location: Wrocław/Racibórz, Poland
- Contact:
Re: Which type to use for pointers?
That's exactly what `uintptr_t` from <stdint.h> (or <cstdint>) is for.
Reaver Project :: Repository :: Ohloh project page
<klange> This is a horror story about what happens when you need a hammer and all you have is the skulls of the damned.
<drake1> as long as the lock is read and modified by atomic operations
<klange> This is a horror story about what happens when you need a hammer and all you have is the skulls of the damned.
<drake1> as long as the lock is read and modified by atomic operations
- Owen
- Member
- Posts: 1700
- Joined: Fri Jun 13, 2008 3:21 pm
- Location: Cambridge, United Kingdom
- Contact:
Re: Which type to use for pointers?
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!
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?
But in that case "char" is good?Owen wrote:It's as if you think architectures don't exist with __SIZEOF_POINTER__ == 1 and CHAR_BITS == 32!
Learn to read.