1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// This file is part of Bizinikiwi.
// Copyright (C) Parity Technologies (UK) Ltd. and Dijital Kurdistan Tech Institute
// SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use ;
/// The type used to store the offset between the real pointer and the returned pointer.
type Offset = u16;
/// The length of [`Offset`].
const OFFSET_LENGTH: usize = ;
/// Allocator used by Bizinikiwi from within the runtime.
///
/// The allocator needs to align the returned pointer to given layout. We assume that on the host
/// side the freeing-bump allocator is used with a fixed alignment of `8` and a `HEADER_SIZE` of
/// `8`. The freeing-bump allocator is storing the header in the 8 bytes before the actual pointer
/// returned by `alloc`. The problem is that the runtime not only sees pointers allocated by this
/// `RuntimeAllocator`, but also pointers allocated by the host. The header is stored as a
/// little-endian `u64`. The allocation header consists of 8 bytes. The first four bytes (as written
/// in memory) are used to store the order of the allocation (or the link to the next slot, if
/// unallocated). Then the least significant bit of the next byte determines whether a given slot is
/// occupied or free, and the last three bytes are unused.
///
/// The `RuntimeAllocator` aligns the pointer to the required alignment before returning it to the
/// user code. As we are assuming the freeing-bump allocator that already aligns by `8` by default,
/// we only need to take care of alignments above `8`. The offset is stored in two bytes before the
/// pointer that we return to the user. Depending on the alignment, we may write into the header,
/// but given the assumptions above this should be no problem.
;
static ALLOCATOR: RuntimeAllocator = RuntimeAllocator;
unsafe