mirror of
				https://github.com/RGBCube/serenity
				synced 2025-10-31 04:22:45 +00:00 
			
		
		
		
	 b847541ee8
			
		
	
	
		b847541ee8
		
	
	
	
	
		
			
			The C++ standard says that it's legal to call the `delete` operator with a null pointer argument, in which case it should be a no-op. I encountered this issue when running a kernel that's compiled with Clang. I assume this fact was used for some kind of optimization.
		
			
				
	
	
		
			43 lines
		
	
	
	
		
			1.9 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
			
		
		
	
	
			43 lines
		
	
	
	
		
			1.9 KiB
		
	
	
	
		
			C++
		
	
	
	
	
	
| /*
 | |
|  * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
 | |
|  *
 | |
|  * SPDX-License-Identifier: BSD-2-Clause
 | |
|  */
 | |
| 
 | |
| #pragma once
 | |
| 
 | |
| #include <AK/Function.h>
 | |
| #include <AK/Types.h>
 | |
| 
 | |
| namespace Kernel {
 | |
| 
 | |
| #define SLAB_ALLOC_SCRUB_BYTE 0xab
 | |
| #define SLAB_DEALLOC_SCRUB_BYTE 0xbc
 | |
| 
 | |
| void* slab_alloc(size_t slab_size);
 | |
| void slab_dealloc(void*, size_t slab_size);
 | |
| void slab_alloc_init();
 | |
| void slab_alloc_stats(Function<void(size_t slab_size, size_t allocated, size_t free)>);
 | |
| 
 | |
| #define MAKE_SLAB_ALLOCATED(type)                                            \
 | |
| public:                                                                      \
 | |
|     [[nodiscard]] void* operator new(size_t)                                 \
 | |
|     {                                                                        \
 | |
|         void* ptr = slab_alloc(sizeof(type));                                \
 | |
|         VERIFY(ptr);                                                         \
 | |
|         return ptr;                                                          \
 | |
|     }                                                                        \
 | |
|     [[nodiscard]] void* operator new(size_t, const std::nothrow_t&) noexcept \
 | |
|     {                                                                        \
 | |
|         return slab_alloc(sizeof(type));                                     \
 | |
|     }                                                                        \
 | |
|     void operator delete(void* ptr) noexcept                                 \
 | |
|     {                                                                        \
 | |
|         if (!ptr)                                                            \
 | |
|             return;                                                          \
 | |
|         slab_dealloc(ptr, sizeof(type));                                     \
 | |
|     }                                                                        \
 | |
|                                                                              \
 | |
| private:
 | |
| 
 | |
| }
 |