Skip to content

Commit fe3511e

Browse files
committed
Reformat code in README.md with codeblocks and 4 spaces
1 parent 7b48fd4 commit fe3511e

File tree

1 file changed

+68
-66
lines changed

1 file changed

+68
-66
lines changed

README.md

Lines changed: 68 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,19 @@ limits the program to only a few megs of dynamically allocated data
3131

3232
Example:
3333

34-
```
35-
34+
```rust
3635
// First define a struct to hold all the array on the stack.
3736
declare_stack_allocator_struct!(StackAllocatedFreelist4, 4, stack);
3837
// since generics cannot be used, the actual struct to hold the memory must be defined with a macro
3938
...
4039

41-
// in the code where the memory must be used, first the array needs to be readied
42-
let mut stack_buffer = define_allocator_memory_pool!(4, u8, [0; 65536], stack);
43-
// then an allocator needs to be made and pointed to the stack_buffer on the stack
44-
// the final argument tells the system if free'd data should be zero'd before being
45-
// reused by a subsequent call to alloc_cell
46-
let mut ags = StackAllocatedFreelist4::<u8>::new_allocator(&mut stack_buffer, bzero);
47-
{
40+
// in the code where the memory must be used, first the array needs to be readied
41+
let mut stack_buffer = define_allocator_memory_pool!(4, u8, [0; 65536], stack);
42+
// then an allocator needs to be made and pointed to the stack_buffer on the stack
43+
// the final argument tells the system if free'd data should be zero'd before being
44+
// reused by a subsequent call to alloc_cell
45+
let mut ags = StackAllocatedFreelist4::<u8>::new_allocator(&mut stack_buffer, bzero);
46+
{
4847
// now we can get memory dynamically
4948
let mut x = ags.alloc_cell(9999);
5049
x.slice_mut()[0] = 4;
@@ -61,61 +60,64 @@ declare_stack_allocator_struct!(StackAllocatedFreelist4, 4, stack);
6160
### On the heap
6261
This uses the standard Box facilities to allocate memory
6362

64-
let mut halloc = HeapAlloc::<u8>::new(0);
65-
for _i in 1..10 { // heap test
66-
let mut x = halloc.alloc_cell(100000);
67-
x[0] = 4;
68-
let mut y = halloc.alloc_cell(110000);
69-
y[0] = 5;
70-
let mut z = halloc.alloc_cell(120000);
71-
z[0] = 6;
72-
assert_eq!(y[0], 5);
73-
halloc.free_cell(y);
74-
assert_eq!(x[0], 4);
75-
assert_eq!(x[9], 0);
76-
assert_eq!(z[0], 6);
77-
}
63+
```rust
64+
let mut halloc = HeapAlloc::<u8>::new(0);
65+
for _i in 1..10 { // heap test
66+
let mut x = halloc.alloc_cell(100000);
67+
x[0] = 4;
68+
let mut y = halloc.alloc_cell(110000);
69+
y[0] = 5;
70+
let mut z = halloc.alloc_cell(120000);
71+
z[0] = 6;
72+
assert_eq!(y[0], 5);
73+
halloc.free_cell(y);
74+
assert_eq!(x[0], 4);
75+
assert_eq!(x[9], 0);
76+
assert_eq!(z[0], 6);
77+
}
78+
```
7879

7980
### On the heap, but uninitialized
8081
This does allocate data every time it is requested, but it does not allocate the
8182
memory, so naturally it is unsafe. The caller must initialize the memory properly
83+
```rust
84+
let mut halloc = unsafe{HeapAllocUninitialized::<u8>::new()};
85+
{ // heap test
86+
let mut x = halloc.alloc_cell(100000);
87+
x[0] = 4;
88+
let mut y = halloc.alloc_cell(110000);
89+
y[0] = 5;
90+
let mut z = halloc.alloc_cell(120000);
91+
z[0] = 6;
92+
assert_eq!(y[0], 5);
93+
halloc.free_cell(y);
94+
assert_eq!(x[0], 4);
95+
assert_eq!(x[9], 0);
96+
assert_eq!(z[0], 6);
97+
...
98+
}
8299
```
83-
let mut halloc = unsafe{HeapAllocUninitialized::<u8>::new()};
84-
{ // heap test
85-
let mut x = halloc.alloc_cell(100000);
86-
x[0] = 4;
87-
let mut y = halloc.alloc_cell(110000);
88-
y[0] = 5;
89-
let mut z = halloc.alloc_cell(120000);
90-
z[0] = 6;
91-
assert_eq!(y[0], 5);
92-
halloc.free_cell(y);
93-
assert_eq!(x[0], 4);
94-
assert_eq!(x[9], 0);
95-
assert_eq!(z[0], 6);
96-
...
97-
}
98100

99101

100102
### On the heap in a single pool allocation
101103
This does a single big allocation on the heap, after which no further usage of the stdlib
102104
will happen. This can be useful for a jailed application that wishes to restrict syscalls
103105
at this point
104106

105-
```
107+
```rust
106108
use alloc_no_stdlib::HeapPrealloc;
107109
...
108-
let mut heap_global_buffer = define_allocator_memory_pool!(4096, u8, [0; 6 * 1024 * 1024], heap);
109-
let mut ags = HeapPrealloc::<u8>::new_allocator(4096, &mut heap_global_buffer, uninitialized);
110-
{
110+
let mut heap_global_buffer = define_allocator_memory_pool!(4096, u8, [0; 6 * 1024 * 1024], heap);
111+
let mut ags = HeapPrealloc::<u8>::new_allocator(4096, &mut heap_global_buffer, uninitialized);
112+
{
111113
let mut x = ags.alloc_cell(9999);
112114
x.slice_mut()[0] = 4;
113115
let mut y = ags.alloc_cell(4);
114116
y[0] = 5;
115117
ags.free_cell(y);
116118

117119
//y.mem[0] = 6; // <-- this is an error (use after free)
118-
}
120+
}
119121
```
120122

121123

@@ -126,20 +128,20 @@ will happen. This can be useful for a jailed application that wishes to restrict
126128
at this point. This option keep does not set the memory to a valid value, so it is
127129
necessarily marked unsafe
128130

129-
```
131+
```rust
130132
use alloc_no_stdlib::HeapPrealloc;
131133
...
132-
let mut heap_global_buffer = unsafe{HeapPrealloc::<u8>::new_uninitialized_memory_pool(6 * 1024 * 1024)};
133-
let mut ags = HeapPrealloc::<u8>::new_allocator(4096, &mut heap_global_buffer, uninitialized);
134-
{
134+
let mut heap_global_buffer = unsafe{HeapPrealloc::<u8>::new_uninitialized_memory_pool(6 * 1024 * 1024)};
135+
let mut ags = HeapPrealloc::<u8>::new_allocator(4096, &mut heap_global_buffer, uninitialized);
136+
{
135137
let mut x = ags.alloc_cell(9999);
136138
x.slice_mut()[0] = 4;
137139
let mut y = ags.alloc_cell(4);
138140
y[0] = 5;
139141
ags.free_cell(y);
140142

141143
//y.mem[0] = 6; // <-- this is an error (use after free)
142-
}
144+
}
143145
```
144146

145147
### With calloc
@@ -148,28 +150,28 @@ It does invoke the C calloc function and hence must invoke unsafe code.
148150
In this version, the number of cells are fixed to the parameter specified in the struct definition
149151
(4096 in this example)
150152

151-
```
153+
```rust
152154
extern {
153-
fn calloc(n_elem : usize, el_size : usize) -> *mut u8;
154-
fn malloc(len : usize) -> *mut u8;
155-
fn free(item : *mut u8);
155+
fn calloc(n_elem : usize, el_size : usize) -> *mut u8;
156+
fn malloc(len : usize) -> *mut u8;
157+
fn free(item : *mut u8);
156158
}
157159

158160
declare_stack_allocator_struct!(CallocAllocatedFreelist4096, 4096, calloc);
159161
...
160162

161-
// the buffer is defined with 200 megs of zero'd memory from calloc
162-
let mut calloc_global_buffer = unsafe {define_allocator_memory_pool!(4096, u8, [0; 200 * 1024 * 1024], calloc)};
163-
// and assigned to a new_allocator
164-
let mut ags = CallocAllocatedFreelist4096::<u8>::new_allocator(&mut calloc_global_buffer.data, bzero);
165-
{
163+
// the buffer is defined with 200 megs of zero'd memory from calloc
164+
let mut calloc_global_buffer = unsafe {define_allocator_memory_pool!(4096, u8, [0; 200 * 1024 * 1024], calloc)};
165+
// and assigned to a new_allocator
166+
let mut ags = CallocAllocatedFreelist4096::<u8>::new_allocator(&mut calloc_global_buffer.data, bzero);
167+
{
166168
let mut x = ags.alloc_cell(9999);
167169
x.slice_mut()[0] = 4;
168170
let mut y = ags.alloc_cell(4);
169171
y[0] = 5;
170172
ags.free_cell(y);
171173
//y.mem[0] = 6; // <-- this is an error (use after free)
172-
}
174+
}
173175
```
174176

175177
### With a static, mutable buffer
@@ -185,25 +187,25 @@ If it is used from two places or at different times, undefined behavior may resu
185187
since multiple allocators may get access to global_buffer.
186188

187189

188-
```
190+
```rust
189191
declare_stack_allocator_struct!(GlobalAllocatedFreelist, 16, global);
190192
define_allocator_memory_pool!(16, u8, [0; 1024 * 1024 * 100], global, global_buffer);
191193

192194
...
193-
// this references a global buffer
194-
let mut ags = GlobalAllocatedFreelist::<u8>::new_allocator(bzero);
195-
unsafe {
196-
bind_global_buffers_to_allocator!(ags, global_buffer, u8);
197-
}
198-
{
195+
// this references a global buffer
196+
let mut ags = GlobalAllocatedFreelist::<u8>::new_allocator(bzero);
197+
unsafe {
198+
bind_global_buffers_to_allocator!(ags, global_buffer, u8);
199+
}
200+
{
199201
let mut x = ags.alloc_cell(9999);
200202
x.slice_mut()[0] = 4;
201203
let mut y = ags.alloc_cell(4);
202204
y[0] = 5;
203205
ags.free_cell(y);
204206

205207
//y.mem[0] = 6; // <-- this is an error (use after free)
206-
}
208+
}
207209
```
208210

209211

0 commit comments

Comments
 (0)