rust-for-linux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Alex Gaynor <alex.gaynor@gmail.com>
To: Fabio Aiuto <fabioaiuto83@gmail.com>
Cc: rust-for-linux <rust-for-linux@vger.kernel.org>
Subject: Re: Question on Box<T> smart pointer
Date: Tue, 27 Apr 2021 11:35:17 -0400	[thread overview]
Message-ID: <CAFRnB2UPfD--T6o+MYLi=qUxeowHFCQJLuu4601kHQx_h+JwpA@mail.gmail.com> (raw)
In-Reply-To: <20210427153311.GB4653@agape.jhs>

Yes, it overrides it for any APIs in the alloc crate that allocate
memory (Arc, Rc, Vec, String, etc.).

Technically https://github.com/Rust-for-Linux/linux/blob/rust/rust/kernel/lib.rs#L203-L204
is where the overriding really happens. The previous code I linked is
where the implementation is.

Alex

On Tue, Apr 27, 2021 at 11:33 AM Fabio Aiuto <fabioaiuto83@gmail.com> wrote:
>
> On Tue, Apr 27, 2021 at 11:22:23AM -0400, Alex Gaynor wrote:
> > Box<T> in the kernel allocates on the kernel heap, via kmalloc. This
> > is done by providing a Rust GlobalAllocator:
> > https://github.com/Rust-for-Linux/linux/blob/rust/rust/kernel/allocator.rs#L11
>
> so this code:
>
> pub struct KernelAllocator;
>
> unsafe impl GlobalAlloc for KernelAllocator {
>     unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
>         // `krealloc()` is used instead of `kmalloc()` because the latter is
>         // an inline function and cannot be bound to as a result.
>         bindings::krealloc(ptr::null(), layout.size(), bindings::GFP_KERNEL) as *mut u8
>     }
>
>     unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) {
>         bindings::kfree(ptr as *const c_types::c_void);
>     }
> }
>
> overrides the default behaviour of Box<T> defined
> in core::alloc::GlobalAlloc, doesn't it?
>
> So this code overrides the general heap allocation
> facility, not just Box<T>, right?
>
> thank you,
>
> fabio
>
> >
> > Alex
> >
> > On Tue, Apr 27, 2021 at 11:20 AM Fabio Aiuto <fabioaiuto83@gmail.com> wrote:
> > >
> > > Hi all,
> > >
> > > I have a question. There's one thing I miss.
> > >
> > > If Box<T> smart pointer allows allocating space on the heap,
> > > how is this behaviour overridden in linux kernel Rust?
> > >
> > > I mean, in c we have all API's to allocate heap space,
> > > but using "pure" Box<T> in kernel space what kind of
> > > allocation provides?
> > >
> > > If this question is stupid I apologize in advance,
> > >
> > > thank you,
> > >
> > > fabio
> >
> >
> >
> > --
> > All that is necessary for evil to succeed is for good people to do nothing.



-- 
All that is necessary for evil to succeed is for good people to do nothing.

      reply	other threads:[~2021-04-27 15:35 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-27 15:20 Question on Box<T> smart pointer Fabio Aiuto
2021-04-27 15:22 ` Alex Gaynor
2021-04-27 15:33   ` Fabio Aiuto
2021-04-27 15:35     ` Alex Gaynor [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAFRnB2UPfD--T6o+MYLi=qUxeowHFCQJLuu4601kHQx_h+JwpA@mail.gmail.com' \
    --to=alex.gaynor@gmail.com \
    --cc=fabioaiuto83@gmail.com \
    --cc=rust-for-linux@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).