Browse Source

Kernel: Allow kmalloc_aligned() alignment up to 4096

This allows us to get kmalloc() memory aligned to the VM page size.
Andreas Kling 4 years ago
parent
commit
40f2abf7c3
1 changed files with 5 additions and 5 deletions
  1. 5 5
      Kernel/Heap/kmalloc.h

+ 5 - 5
Kernel/Heap/kmalloc.h

@@ -58,18 +58,18 @@ inline void* operator new[](size_t, void* p) { return p; }
 template<size_t ALIGNMENT>
 template<size_t ALIGNMENT>
 [[gnu::malloc, gnu::returns_nonnull, gnu::alloc_size(1)]] inline void* kmalloc_aligned(size_t size)
 [[gnu::malloc, gnu::returns_nonnull, gnu::alloc_size(1)]] inline void* kmalloc_aligned(size_t size)
 {
 {
-    static_assert(ALIGNMENT > 1);
-    static_assert(ALIGNMENT < 255);
-    void* ptr = kmalloc(size + ALIGNMENT + sizeof(u8));
+    static_assert(ALIGNMENT > sizeof(ptrdiff_t));
+    static_assert(ALIGNMENT <= 4096);
+    void* ptr = kmalloc(size + ALIGNMENT + sizeof(ptrdiff_t));
     size_t max_addr = (size_t)ptr + ALIGNMENT;
     size_t max_addr = (size_t)ptr + ALIGNMENT;
     void* aligned_ptr = (void*)(max_addr - (max_addr % ALIGNMENT));
     void* aligned_ptr = (void*)(max_addr - (max_addr % ALIGNMENT));
-    ((u8*)aligned_ptr)[-1] = (u8)((u8*)aligned_ptr - (u8*)ptr);
+    ((ptrdiff_t*)aligned_ptr)[-1] = (ptrdiff_t)((u8*)aligned_ptr - (u8*)ptr);
     return aligned_ptr;
     return aligned_ptr;
 }
 }
 
 
 inline void kfree_aligned(void* ptr)
 inline void kfree_aligned(void* ptr)
 {
 {
-    kfree((u8*)ptr - ((u8*)ptr)[-1]);
+    kfree((u8*)ptr - ((const ptrdiff_t*)ptr)[-1]);
 }
 }
 
 
 void kmalloc_enable_expand();
 void kmalloc_enable_expand();