Skip to content

Commit 69db08c

Browse files
Update README.md
1 parent 4dcd9fa commit 69db08c

File tree

1 file changed

+54
-37
lines changed

1 file changed

+54
-37
lines changed

README.md

Lines changed: 54 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -29,35 +29,60 @@ Supported collections:
2929
- List
3030
- Queue
3131

32-
General idea (on the example of a list):
33-
34-
Allocate memory for all your collections.
35-
36-
![1](https://user-images.githubusercontent.com/43916814/188752538-cff787a0-2c92-4d86-8439-6c9efec3eb57.png)
37-
38-
In our example, we will allocate a list of 5 elements on this memory.
32+
Usage:
3933

40-
![2](https://user-images.githubusercontent.com/43916814/188752689-bbc509e0-05be-4ea2-847f-5ba04ca5b066.png)
34+
```C#
35+
unsafe
36+
{
37+
using (var memory = new Struct.StackMemory(sizeof(int) * 100))//Allocate memory for all your collections.
38+
{
39+
{
40+
using var listOfInt32 = new Struct.ListOfInt32(50, &memory);
41+
list.ExpandCapacity(50);
42+
//Do whatever you want with list of Int32 items
43+
}//return memory
4144
42-
If we need to increase the capacity, and at the same time, the collection whose capacity increases is the last element in memory, then to increase the capacity, you just need to indicate that there is more available memory for the collection. No copying or reallocation.
45+
var listOfInt64 = new Struct.ListOfInt64(50, &memory);//get memory
46+
//Do whatever you want with list of Int64 items
47+
}//free all memory
48+
}
4349

44-
![3](https://user-images.githubusercontent.com/43916814/188752910-11f87ccc-2384-4a9a-909c-91d85c2e67fa.png)
50+
```
4551

46-
If we need to allocate a collection of elements of a different type on the same memory (and we do not need the old collection), then we do not have to allocate new memory, we can allocate it on the already allocated one.
52+
In our example, we will allocate a list of 50 elements on this memory.
53+
Then we increase the capacity to the 100 elements. No copying or reallocation.
54+
Then we free old collection and allocate new collection of Int64 on the same memory.
4755

48-
If something else is written to memory after the collection, then the collection becomes sealed.
49-
In the future, you can compress memory if there are areas that are no longer used, thereby not sealing the collection.
56+
In the future(TODO), you can compress memory if there are areas that are no longer used, thereby not sealing the collection.
5057
This can be useful for allocating memory for an entire method if we know approximately how much memory it can consume at the maximum.
5158

52-
Usage:
59+
_____
60+
Stack of composite type example:
5361

5462
```C#
5563
//Marking a class/struct with attributes is all that is required of you.
5664
[GenerateStack]
5765
[GenerateWrapper]
58-
public struct JobStruct
66+
public struct SimpleStruct
67+
{
68+
public SimpleStruct(
69+
int int32,
70+
long int64
71+
)
72+
{
73+
Int32 = int32;
74+
Int64 = int64;
75+
}
76+
77+
public long Int64;
78+
public int Int32;
79+
}
80+
81+
[GenerateStack]
82+
[GenerateWrapper]
83+
public class SimpleClass
5984
{
60-
public JobStruct(
85+
public SimpleClass(
6186
int int32,
6287
long int64
6388
)
@@ -76,57 +101,49 @@ public struct JobStruct
76101
//Stack of pointers
77102
unsafe
78103
{
79-
using (var memory = new Struct.StackMemory(JobStructHelper.SizeOf + (nuint)sizeof(IntPtr)))
104+
using (var memory = new Struct.StackMemory(SimpleStructHelper.SizeOf + (nuint)sizeof(IntPtr)))
80105
{
81106
using var stack = new Struct.StackOfIntPtr(1, &memory);
82107
{
83-
var item = new Struct.JobStructWrapper(&memory);
108+
var item = new Struct.SimpleStructWrapper(&memory);
84109
item.Int32 = 456;
85110
*stack.TopFuture() = new IntPtr(item.Ptr);
86111
stack.PushFuture();
87112
}
88-
var item2 = new Struct.JobStructWrapper(stack.Top().ToPointer());
113+
var item2 = new Struct.SimpleStructWrapper(stack.Top().ToPointer());
89114
//item2 point to same memory as is item
90115
}
91116
}
92117
```
93118

94119
```C#
95-
//Stack of structures
96-
//All alocate memory = JobStructHelper.SizeOf * 100
120+
//All alocate memory = SimpleStructHelper.SizeOf * 100 = 12* 100 = 1200 byte
97121
unsafe
98122
{
99123
using (var memory = new Struct.StackMemory(JobStructHelper.SizeOf * (nuint)100))//allocate memory
100-
{
101-
var item = new Struct.JobStructWrapper(memory.Start, false);
102-
var js2W = new Struct.JobStruct2Wrapper(memory.Start, false);
103-
124+
{
104125
{
105-
using var stack = new Struct.StackOfJobStruct((nuint)100, &memory);//get memory
126+
var item = new Struct.SimpleStructWrapper(memory.Start, false);
127+
using var stackOfSimpleStruct = new Struct.StackOfSimpleStruct((nuint)100, &memory);//get memory
106128
for (int i = 0; i < 100; i++)
107129
{
108-
item.ChangePtr(stack.TopFuture());
130+
item.ChangePtr(stackOfSimpleStruct.TopFuture());
109131
item.Int32 = i;
110132
item.Int64 = i * 2;
111-
js2W.ChangePtr(item.JobStruct2Ptr);
112-
js2W.Int32 = 777;
113-
js2W.Int64 = 111;
114-
stack.PushFuture();
133+
stackOfSimpleStruct.PushFuture();
115134
}
116135

117136
//Do whatever you want with stack
118137
}//return memory
119138
120-
var stack2 = new Struct.StackOfJobStruct((nuint)100, &memory);//get memory
139+
var item = new Struct.SimpleClassWrapper(memory.Start, false);
140+
var stackOfSimpleClass = new Struct.StackOfSimpleClass((nuint)100, &memory);//get memory
121141
for (int i = 0; i < 100; i++)
122142
{
123-
item.ChangePtr(stack2.TopFuture());
143+
item.ChangePtr(stackOfSimpleClass.TopFuture());
124144
item.Int32 = i;
125145
item.Int64 = i * 2;
126-
js2W.ChangePtr(item.JobStruct2Ptr);
127-
js2W.Int32 = 465;
128-
js2W.Int64 = 7898721;
129-
stack2.PushFuture();
146+
stackOfSimpleClass.PushFuture();
130147
}
131148
}//free all memory
132149
}

0 commit comments

Comments
 (0)