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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
//! Memory allocation for Unix-like systems.
use crate::;
use ;
use nstdapi;
/// Allocates a block of memory on the heap, returning a pointer to it.
///
/// # Parameters:
///
/// - `NSTDAllocLayout layout` - Describes the memory layout to allocate for.
///
/// # Returns
///
/// `NSTDAnyMut ptr` - A pointer to the newly allocated block of memory, or null on error.
///
/// # Safety
///
/// - Behavior is undefined if `layout`'s size is zero.
///
/// - The new memory buffer should be considered uninitialized.
///
/// # Example
///
/// ```
/// use nstd_sys::{
/// core::alloc::nstd_core_alloc_layout_new,
/// os::unix::alloc::{nstd_os_unix_alloc_allocate, nstd_os_unix_alloc_deallocate},
/// };
///
/// unsafe {
/// let size = core::mem::size_of::<u128>();
/// let align = core::mem::align_of::<u128>();
/// let layout = nstd_core_alloc_layout_new(size, align).unwrap();
/// let mem = nstd_os_unix_alloc_allocate(layout);
/// assert!(!mem.is_null());
/// nstd_os_unix_alloc_deallocate(mem);
/// }
/// ```
pub unsafe
/// Allocates a block of zero initialized memory on the heap, returning a pointer to it.
///
/// # Parameters:
///
/// - `NSTDAllocLayout layout` - Describes the memory layout to allocate for.
///
/// # Returns
///
/// `NSTDAnyMut ptr` - A pointer to the newly allocated block of memory, or null on error.
///
/// # Safety
///
/// Behavior is undefined if `layout`'s size is zero.
///
/// # Example
///
/// ```
/// use nstd_sys::{
/// core::alloc::nstd_core_alloc_layout_new,
/// os::unix::alloc::{nstd_os_unix_alloc_allocate_zeroed, nstd_os_unix_alloc_deallocate},
/// };
///
/// unsafe {
/// let size = core::mem::size_of::<isize>();
/// let align = core::mem::align_of::<isize>();
/// let layout = nstd_core_alloc_layout_new(size, align).unwrap();
/// let mem = nstd_os_unix_alloc_allocate_zeroed(layout);
/// assert!(!mem.is_null());
/// assert!(*mem.cast::<isize>() == 0);
/// nstd_os_unix_alloc_deallocate(mem);
/// }
/// ```
pub unsafe
/// Reallocates a block of memory previously allocated by `nstd_os_unix_alloc_allocate[_zeroed]`.
///
/// # Parameters:
///
/// - `NSTDAnyMut *ptr` - A pointer to the block of memory to reallocate.
///
/// - `NSTDAllocLayout old_layout` - Describes the previous memory layout.
///
/// - `NSTDAllocLayout new_layout` - Describes the new memory layout to allocate for.
///
/// # Returns
///
/// `NSTDAllocError errc` - The allocation operation error code.
///
/// # Safety
///
/// - Behavior is undefined if `new_layout`'s size is zero.
///
/// - `ptr` must point to memory previously allocated with `old_layout`.
///
/// # Example
///
/// ```
/// use nstd_sys::{
/// core::alloc::{nstd_core_alloc_layout_new, NSTDAllocError::NSTD_ALLOC_ERROR_NONE},
/// os::unix::alloc::{
/// nstd_os_unix_alloc_allocate_zeroed, nstd_os_unix_alloc_deallocate,
/// nstd_os_unix_alloc_reallocate,
/// },
/// };
///
///
/// unsafe {
/// let mut size = core::mem::size_of::<u64>();
/// let mut align = core::mem::align_of::<u64>();
/// let layout = nstd_core_alloc_layout_new(size, align).unwrap();
/// let mut mem = nstd_os_unix_alloc_allocate_zeroed(layout);
/// assert!(!mem.is_null());
/// size = core::mem::size_of::<u32>();
/// align = core::mem::align_of::<u32>();
/// let new_layout = nstd_core_alloc_layout_new(size, align).unwrap();
/// let errc = nstd_os_unix_alloc_reallocate(&mut mem, layout, new_layout);
/// assert!(errc == NSTD_ALLOC_ERROR_NONE);
/// assert!(*mem.cast::<u32>() == 0);
/// nstd_os_unix_alloc_deallocate(mem);
/// }
/// ```
pub unsafe
/// Deallocates a block of memory previously allocated by `nstd_os_unix_alloc_allocate[_zeroed]`.
///
/// # Parameters:
///
/// - `NSTDAnyMut ptr` - A pointer to the block of memory to free.
///
/// # Safety
///
/// Behavior is undefined if `ptr` does not point to memory allocated by
/// `nstd_os_unix_alloc_allocate[_zeroed]`.
///
/// # Example
///
/// ```
/// use nstd_sys::{
/// core::alloc::nstd_core_alloc_layout_new,
/// os::unix::alloc::{nstd_os_unix_alloc_allocate, nstd_os_unix_alloc_deallocate},
/// };
///
/// unsafe {
/// let size = core::mem::size_of::<[i16; 8]>();
/// let align = core::mem::align_of::<[i16; 8]>();
/// let layout = nstd_core_alloc_layout_new(size, align).unwrap();
/// let mem = nstd_os_unix_alloc_allocate(layout);
/// assert!(!mem.is_null());
/// nstd_os_unix_alloc_deallocate(mem);
/// }
/// ```
pub unsafe