Skip to content

Remove unneeded padding field from MemoryDescriptor struct #628

Closed
@liferooter

Description

@liferooter

The purpose of adding this field is to enforce correct alignment. But #[repr(C)] already enforces it.

Let's see structure alignment with and without this field:

With padding field:

pub struct MemoryDescriptor {
    // size: 4, alignment: 4, offset: 0
    pub ty: MemoryType,
    // size: 4, alignment: 4, offset: 4
    padding: u32,
    // size: 8, alignment: 8, offset: 8
    pub phys_start: PhysicalAddress,
    // size: 8, alignment: 8, offset: 16
    pub virt_start: VirtualAddress,
    // size: 8, alignment: 8, offset: 24
    pub page_count: u64,
    // size: 8, alignment: 8, offset: 32
    pub att: MemoryAttribute,
}

Without padding field:

pub struct MemoryDescriptor {
    // size: 4, alignment: 4, offset: 0
    pub ty: MemoryType,
    // size: 8, alignment: 8, offset: 8
    pub phys_start: PhysicalAddress,
    // size: 8, alignment: 8, offset: 16
    pub virt_start: VirtualAddress,
    // size: 8, alignment: 8, offset: 24
    pub page_count: u64,
    // size: 8, alignment: 8, offset: 32
    pub att: MemoryAttribute,
}

As you can see, these memory layouts are the same one. So there's no need for a useless private field that makes working with the type harder (e.g. you can't use structure declaration to create an instance of this struct). So I suggest to remove the padding field.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions