Skip to content

Update linux.Map_Flags_Bits #5152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 12, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Update linux.Map_Flags_Bits
Fixes #5151

- Removes `SHARED_VALIDATE` from the enum and turns it into `Map_Shared_Validate :: Map_Flags{.SHARED, .PRIVATE}` so it has the proper value of 0x03.
- Adds `DROPPABLE`.
- Adds constants `MAP_HUGE_SHIFT` and `MAP_HUGE_MASK`.
- Adds the huge page precomputed constants from `mman.h`, defined as the log2 of the size shifted left by `MAP_HUGE_SHIFT`:
	Map_Huge_16KB
	Map_Huge_64KB
	Map_Huge_512KB
	Map_Huge_1MB
	Map_Huge_2MB
	Map_Huge_8MB
	Map_Huge_16MB
	Map_Huge_32MB
	Map_Huge_256MB
	Map_Huge_512MB
	Map_Huge_1GB
	Map_Huge_2GB
	Map_Huge_16GB
  • Loading branch information
Kelimion committed May 12, 2025
commit dec3d6959df1a7a876782f3e233d0320d41a0fad
8 changes: 6 additions & 2 deletions core/sys/linux/bits.odin
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ Inotify_Event_Bits :: enum u32 {
/*
Bits for Mem_Protection bitfield
*/
Mem_Protection_Bits :: enum{
Mem_Protection_Bits :: enum {
READ = 0,
WRITE = 1,
EXEC = 2,
Expand All @@ -598,7 +598,7 @@ Mem_Protection_Bits :: enum{
Map_Flags_Bits :: enum {
SHARED = 0,
PRIVATE = 1,
SHARED_VALIDATE = 2,
DROPPABLE = 3,
FIXED = 4,
ANONYMOUS = 5,
// platform-dependent section start
Expand All @@ -619,6 +619,10 @@ Map_Flags_Bits :: enum {
UNINITIALIZED = 26,
}

// Not actually flags, but a shift and mask for when HUGETLB is defined
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These should go into constants.odin and prob good to stay consistent with the comment style used in the package.

MAP_HUGE_SHIFT :: 26
MAP_HUGE_MASK :: 63

/*
Bits for MLock_Flags
*/
Expand Down
15 changes: 15 additions & 0 deletions core/sys/linux/types.odin
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,21 @@ Mem_Protection :: bit_set[Mem_Protection_Bits; i32]
*/
Map_Flags :: bit_set[Map_Flags_Bits; i32]

Map_Shared_Validate :: Map_Flags{.SHARED, .PRIVATE}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are constants right? Should go into constants.odin and be in upper snake case.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's not very usable, though. Who's going to look for Map_Shared_Validate in constants.odin instead of next to either the enum or the bit_set?

Copy link
Member Author

@Kelimion Kelimion May 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And when it comes to os.open flag combinations, we don't put them in another file either.

File_Flags :: distinct bit_set[File_Flag; uint]
File_Flag :: enum {
	Read,
	Write,
	Append,
	Create,
	Excl,
	Sync,
	Trunc,
	Sparse,
	Inheritable,

	Unbuffered_IO,
}

O_RDONLY  :: File_Flags{.Read}
O_WRONLY  :: File_Flags{.Write}
O_RDWR    :: File_Flags{.Read, .Write}
O_APPEND  :: File_Flags{.Append}
O_CREATE  :: File_Flags{.Create}
O_EXCL    :: File_Flags{.Excl}
O_SYNC    :: File_Flags{.Sync}
O_TRUNC   :: File_Flags{.Trunc}
O_SPARSE  :: File_Flags{.Sparse}

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean that's how the entirety of this package is structured

Copy link
Member Author

@Kelimion Kelimion May 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True. The core:os/os2 way is much more convenient, but it's also much newer.

I'll make it UPPER_SNAKE_CASE and move it to constants.odin for consistency, but leave a comment under the bit_set in types.odin that some precomputed huge page sizes are available over there.

We may at some point want to give core:sys the core:os/os2 convenience treatment and keep related info together.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, I am not saying I fully agree with the structure here but to be at least consistent on the package level is a good thing.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made the changes and left a comment where to find it for people who expect it next to the enum or bit_set.

Map_Huge_16KB :: transmute(Map_Flags)(u32(14) << MAP_HUGE_SHIFT)
Map_Huge_64KB :: transmute(Map_Flags)(u32(16) << MAP_HUGE_SHIFT)
Map_Huge_512KB :: transmute(Map_Flags)(u32(19) << MAP_HUGE_SHIFT)
Map_Huge_1MB :: transmute(Map_Flags)(u32(20) << MAP_HUGE_SHIFT)
Map_Huge_2MB :: transmute(Map_Flags)(u32(21) << MAP_HUGE_SHIFT)
Map_Huge_8MB :: transmute(Map_Flags)(u32(23) << MAP_HUGE_SHIFT)
Map_Huge_16MB :: transmute(Map_Flags)(u32(24) << MAP_HUGE_SHIFT)
Map_Huge_32MB :: transmute(Map_Flags)(u32(25) << MAP_HUGE_SHIFT)
Map_Huge_256MB :: transmute(Map_Flags)(u32(28) << MAP_HUGE_SHIFT)
Map_Huge_512MB :: transmute(Map_Flags)(u32(29) << MAP_HUGE_SHIFT)
Map_Huge_1GB :: transmute(Map_Flags)(u32(30) << MAP_HUGE_SHIFT)
Map_Huge_2GB :: transmute(Map_Flags)(u32(31) << MAP_HUGE_SHIFT)
Map_Huge_16GB :: transmute(Map_Flags)(u32(34) << MAP_HUGE_SHIFT)

/*
Flags for mlock(2).
*/
Expand Down