Skip to content
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

[Closes #371] Use const Default trait in arena. #469

Merged
merged 2 commits into from
Apr 1, 2021

Conversation

travis1829
Copy link
Collaborator

@travis1829 travis1829 commented Mar 31, 2021

Closes #371

Changes

ArrayArena/MruArenanew method를 변경했습니다.
기존:

pub const fn new(entries: [RcCell<T>; CAPACITY]) -> Self; // ArrayArena
pub const fn new(entries: [MruEntry<T>; CAPACITY]) -> Self; // MruArena

변경 후:

pub const fn new<D: Default>() -> Self; // ArrayArena
pub const fn new<D: Default>() -> Self; // MruArena

  • 기존에는 caller가 직접 초기화된 배열을 제공하는 형태였기 때문에, caller가 malicious한 배열을 제공하게 되면 문제가 생길 수 있었습니다.
  • 변경 후에는 ArrayArena/MruArena가 직접 배열을 만들고 초기화합니다.
    • 이를 위해서, Default trait을 요구하도록 변경했습니다. (const fn안에서는 closure나 function pointer를 사용할 수 없는 것으로 보입니다.)
    • 특히, Default trait을 const fn안에서 사용하기 위해서, #![feature(const_impl_trait)]을 사용했습니다.

kernel-rs/src/arena.rs Outdated Show resolved Hide resolved
kernel-rs/src/arena.rs Outdated Show resolved Hide resolved
/// ```rust,no_run
/// let arr_arena = ArrayArena::<D, 100>::new_locked("arr_arena");
/// ```
pub const fn new_locked<D: Default>(name: &'static str) -> Spinlock<ArrayArena<D, CAPACITY>> {
Copy link
Member

Choose a reason for hiding this comment

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

new가 아니라 new_locked를 만드는 이유는?

Copy link
Collaborator Author

@travis1829 travis1829 Mar 31, 2021

Choose a reason for hiding this comment

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

  • ...Arena 자체를 접근하는건 arena.rs 내부에서만 해야하고, 외부에서는 항상 Spinlock에 감싸진 형태로 접근해야합니다.
  • 추가로, 외부에서는 ...Arena를 가지고서는 할 수 있는게 없습니다. (사용할 수 있는 API가 없습니다.)

newSelf를 리턴하는 것보다 Spinlock<Self>를 리턴하는게 위 사항을 더 잘 나타낸다고 생각하여 변경했습니다.
다만, 혹시 이전으로 다시 되돌리기를 원하시면 말씀해주세요.

Copy link
Member

Choose a reason for hiding this comment

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

* 외부에서는 항상 `Spinlock`에 감싸진 형태로 접근해야합니다.

왜 그런가요? Arena 자체는 spinlock 없이도 API를 잘 만들 수 있는게 아닌가 싶어서 질문드립니다.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

다시 new로 변경했습니다.

지금의 ArrayArena/MruArena는 multi thread에서는 사용할 수 없으므로, 항상 Spinlock에 감싸져있어야해서 이렇게 생각했는데, 말씀하신것처럼 arena를 single thread에서 사용할 수도 있고 이미 MruArena::init같은 API가 존재하므로, 다시 변경했습니다.

kernel-rs/src/bio.rs Outdated Show resolved Hide resolved
/// ```rust,no_run
/// let arr_arena = ArrayArena::<D, 100>::new_locked("arr_arena");
/// ```
pub const fn new_locked<D: Default>(name: &'static str) -> Spinlock<ArrayArena<D, CAPACITY>> {
Copy link
Member

Choose a reason for hiding this comment

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

* 외부에서는 항상 `Spinlock`에 감싸진 형태로 접근해야합니다.

왜 그런가요? Arena 자체는 spinlock 없이도 API를 잘 만들 수 있는게 아닌가 싶어서 질문드립니다.

kernel-rs/src/arena.rs Outdated Show resolved Hide resolved
kernel-rs/src/bio.rs Outdated Show resolved Hide resolved
Copy link
Member

@jeehoonkang jeehoonkang left a comment

Choose a reason for hiding this comment

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

bors r+

@kaist-cp-bors
Copy link
Contributor

kaist-cp-bors bot commented Apr 1, 2021

Build succeeded:

@kaist-cp-bors kaist-cp-bors bot merged commit 15937d8 into kaist-cp:riscv Apr 1, 2021
@travis1829 travis1829 mentioned this pull request Apr 12, 2021
kaist-cp-bors bot added a commit that referenced this pull request Apr 12, 2021
484: Small fix r=Medowhill a=travis1829

매우 간단한 PR입니다. 바로 merge해주셔도 될 것 같습니다.

* Fix for #480 (comment)
* Fix for #469 (comment)
  * rustfmt version이 1.4.36-nightly로 업데이트되어, 더 이상 `#[rustfmt::skip]`을 붙일 필요가 없습니다.

Co-authored-by: travis1829 <travis1829@naver.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Safety in ArrayArena::new() and MruArena::new()
3 participants