|
5 | 5 | //! C header: [`include/linux/drm/drm_device.h`](srctree/include/linux/drm/drm_device.h) |
6 | 6 |
|
7 | 7 | use crate::{ |
| 8 | + alloc::allocator::Kmalloc, |
8 | 9 | bindings, device, drm, |
9 | 10 | drm::driver::AllocImpl, |
10 | 11 | error::from_err_ptr, |
11 | 12 | error::Result, |
12 | 13 | prelude::*, |
13 | 14 | types::{ARef, AlwaysRefCounted, Opaque}, |
14 | 15 | }; |
15 | | -use core::{mem, ops::Deref, ptr, ptr::NonNull}; |
| 16 | +use core::{alloc::Layout, mem, ops::Deref, ptr, ptr::NonNull}; |
16 | 17 |
|
17 | 18 | #[cfg(CONFIG_DRM_LEGACY)] |
18 | 19 | macro_rules! drm_legacy_fields { |
@@ -96,14 +97,18 @@ impl<T: drm::Driver> Device<T> { |
96 | 97 |
|
97 | 98 | /// Create a new `drm::Device` for a `drm::Driver`. |
98 | 99 | pub fn new(dev: &device::Device, data: impl PinInit<T::Data, Error>) -> Result<ARef<Self>> { |
| 100 | + // `__drm_dev_alloc` uses `kmalloc()` to allocate memory, hence ensure a `kmalloc()` |
| 101 | + // compatible `Layout`. |
| 102 | + let layout = Kmalloc::aligned_layout(Layout::new::<Self>()); |
| 103 | + |
99 | 104 | // SAFETY: |
100 | 105 | // - `VTABLE`, as a `const` is pinned to the read-only section of the compilation, |
101 | 106 | // - `dev` is valid by its type invarants, |
102 | 107 | let raw_drm: *mut Self = unsafe { |
103 | 108 | bindings::__drm_dev_alloc( |
104 | 109 | dev.as_raw(), |
105 | 110 | &Self::VTABLE, |
106 | | - mem::size_of::<Self>(), |
| 111 | + layout.size(), |
107 | 112 | mem::offset_of!(Self, dev), |
108 | 113 | ) |
109 | 114 | } |
|
0 commit comments