/// Create a new heap. If any parameter is invalid, this will return a [HeapError].
Create a new heap. If any parameter is invalid, this will return a [HeapError].
Create a new heap . If any parameter is invalid this will return a [ HeapError ] .
pub unsafe fn new(heap_base: NonNull<u8>, heap_size: usize) -> Result<Self, HeapError> {
        // Calculate our minimum block size based on the number of free
        // lists we have available.
        let min_block_size = heap_size >> (N - 1);
        // The heap must be aligned on a 4K bounday.
        if heap_base.as_ptr() as usize & (MIN_HEAP_ALIGN - 1) != 0 {
            return Err(HeapError::BadBaseAlignment);
        }
        // The heap must be big enough to contain at least one block.
        if heap_size < min_block_size {
            return Err(HeapError::BadHeapSize);
        }
        // The smallest possible heap block must be big enough to contain
        // the block header.
        if min_block_size < size_of::<FreeBlock>() {
            return Err(HeapError::MinBlockTooSmall);
        }
        // The heap size must be a power of 2.
        if !heap_size.is_power_of_two() {
            return Err(HeapError::BadSizeAlignment);
        }
        // We must have one free list per possible heap block size.
        // FIXME: Can this assertion even be hit?
        // assert_eq!(
        //     min_block_size * (2u32.pow(N as u32 - 1)) as usize,
        //     heap_size
        // );
        // assert!(N > 0);
        Ok(Self::new_unchecked(heap_base.as_ptr(), heap_size))
    }
pub unsafe fn new ( heap_base : NonNull < u8 > , heap_size : usize ) -> Result < Self , HeapError > { // Calculate our minimum block size based on the number of free // lists we have available. let min_block_size = heap_size >> ( N - 1 ) ; // The heap must be aligned on a 4K bounday. if heap_base . as_ptr ( ) as usize & ( MIN_HEAP_ALIGN - 1 ) != 0 { return Err ( HeapError :: BadBaseAlignment ) ; } // The heap must be big enough to contain at least one block. if heap_size < min_block_size { return Err ( HeapError :: BadHeapSize ) ; } // The smallest possible heap block must be big enough to contain // the block header. if min_block_size < size_of :: < FreeBlock > ( ) { return Err ( HeapError :: MinBlockTooSmall ) ; } // The heap size must be a power of 2. if ! heap_size . is_power_of_two ( ) { return Err ( HeapError :: BadSizeAlignment ) ; } // We must have one free list per possible heap block size. // FIXME: Can this assertion even be hit? // assert_eq!( //     min_block_size * (2u32.pow(N as u32 - 1)) as usize, //     heap_size // ); // assert!(N > 0); Ok ( Self :: new_unchecked ( heap_base . as_ptr ( ) , heap_size ) ) }
Create a new heap.
Create a new heap .
heap_base
NonNull<u8>
heap_size
usize