Skip to content

Commit 4ce5514

Browse files
author
Vyacheslav
committed
feat: resolve #33
1 parent f2bbd55 commit 4ce5514

File tree

1 file changed

+114
-106
lines changed

1 file changed

+114
-106
lines changed

Src/StackMemoryCollections/GenerateMemory.cs

Lines changed: 114 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -9,110 +9,184 @@ private void GenerateMemory(
99
in GeneratorExecutionContext context,
1010
in StringBuilder builder
1111
)
12+
{
13+
GenerateMemory(in context, in builder, "Class");
14+
GenerateMemory(in context, in builder, "Struct");
15+
}
16+
17+
private void GenerateMemory(
18+
in GeneratorExecutionContext context,
19+
in StringBuilder builder,
20+
in string memoryNamespace
21+
)
1222
{
1323
builder.Clear();
24+
MemoryStart(in builder, in memoryNamespace);
25+
26+
MemoryConstructor1(in builder);
27+
MemoryProperties(in builder);
28+
MemoryAllocateMemory(in builder);
29+
MemoryTryAllocateMemory(in builder);
30+
MemoryFreeMemory(in builder);
31+
MemoryTryFreeMemory(in builder);
32+
MemoryDispose(in builder, in memoryNamespace);
33+
34+
MemoryEnd(in builder);
35+
context.AddSource($"StackMemory{memoryNamespace}.g.cs", builder.ToString());
36+
}
1437

38+
private void MemoryStart(
39+
in StringBuilder builder,
40+
in string memoryNamespace
41+
)
42+
{
1543
builder.Append($@"
1644
using System;
1745
using System.Runtime.InteropServices;
1846
19-
namespace StackMemoryCollections.Class
47+
namespace StackMemoryCollections.{memoryNamespace}
2048
{{
21-
public unsafe class StackMemory : IDisposable
49+
public unsafe {memoryNamespace.ToLowerInvariant()} StackMemory : IDisposable
2250
{{
23-
private nuint _offsetBytes;
51+
");
52+
}
2453

54+
private void MemoryConstructor1(
55+
in StringBuilder builder
56+
)
57+
{
58+
builder.Append($@"
2559
public StackMemory(nuint byteCount)
2660
{{
2761
Start = NativeMemory.Alloc(byteCount);
2862
Current = Start;
2963
ByteCount = byteCount;
30-
_offsetBytes = 0;
64+
FreeByteCount = byteCount;
65+
_disposed = false;
3166
}}
67+
");
68+
}
3269

70+
private void MemoryProperties(
71+
in StringBuilder builder
72+
)
73+
{
74+
builder.Append($@"
3375
public void* Start {{ get; init; }}
3476
public void* Current {{ get; private set; }}
3577
public nuint ByteCount {{ get; init; }}
36-
public nuint FreeByteCount => ByteCount - _offsetBytes;
78+
public nuint FreeByteCount {{ get; private set; }}
79+
");
80+
}
3781

82+
private void MemoryAllocateMemory(
83+
in StringBuilder builder
84+
)
85+
{
86+
builder.Append($@"
3887
public void* AllocateMemory(nuint allocateBytes)
3988
{{
4089
if (_disposed)
4190
{{
4291
throw new ObjectDisposedException(nameof(StackMemory));
4392
}}
4493
45-
if (ByteCount - _offsetBytes < allocateBytes)
94+
if (FreeByteCount < allocateBytes)
4695
{{
4796
throw new ArgumentException(""Can't allocate memory"");
4897
}}
4998
50-
_offsetBytes += allocateBytes;
99+
FreeByteCount -= allocateBytes;
51100
var start = Current;
52101
Current = (byte*)start + allocateBytes;
53102
return start;
54103
}}
104+
");
105+
}
55106

107+
private void MemoryTryAllocateMemory(
108+
in StringBuilder builder
109+
)
110+
{
111+
builder.Append($@"
56112
public bool TryAllocateMemory(nuint allocateBytes, out void* ptr)
57113
{{
58114
if (_disposed)
59115
{{
60116
throw new ObjectDisposedException(nameof(StackMemory));
61117
}}
62118
63-
if (ByteCount - _offsetBytes < allocateBytes)
119+
if (FreeByteCount < allocateBytes)
64120
{{
65121
ptr = null;
66122
return false;
67123
}}
68124
69-
_offsetBytes += allocateBytes;
125+
FreeByteCount -= allocateBytes;
70126
var start = Current;
71127
Current = (byte*)start + allocateBytes;
72128
ptr = start;
73129
74130
return true;
75131
}}
132+
");
133+
}
76134

135+
private void MemoryFreeMemory(
136+
in StringBuilder builder
137+
)
138+
{
139+
builder.Append($@"
77140
public void FreeMemory(nuint reducingBytes)
78141
{{
79142
if(_disposed)
80143
{{
81144
throw new ObjectDisposedException(nameof(StackMemory));
82145
}}
83146
84-
var start = new IntPtr(Start);
85-
var newCurrent = new IntPtr((byte*)Current - reducingBytes);
86-
87-
if (newCurrent.CompareTo(start) < 0)
147+
if (ByteCount - FreeByteCount < reducingBytes)
88148
{{
89149
throw new Exception(""Unable to free memory, it is out of available memory"");
90150
}}
91151
92-
_offsetBytes -= reducingBytes;
93-
Current = newCurrent.ToPointer();
152+
FreeByteCount += reducingBytes;
153+
Current = (byte*)Current - reducingBytes;
94154
}}
155+
");
156+
}
95157

158+
private void MemoryTryFreeMemory(
159+
in StringBuilder builder
160+
)
161+
{
162+
builder.Append($@"
96163
public bool TryFreeMemory(nuint reducingBytes)
97164
{{
98165
if (_disposed)
99166
{{
100167
throw new ObjectDisposedException(nameof(StackMemory));
101168
}}
102169
103-
var start = new IntPtr(Start);
104-
var newCurrent = new IntPtr((byte*)Current - reducingBytes);
105-
106-
if (newCurrent.CompareTo(start) < 0)
170+
if (ByteCount - FreeByteCount < reducingBytes)
107171
{{
108172
return false;
109173
}}
110174
111-
_offsetBytes -= reducingBytes;
112-
Current = newCurrent.ToPointer();
175+
FreeByteCount += reducingBytes;
176+
Current = (byte*)Current - reducingBytes;
113177
return true;
114178
}}
179+
");
180+
}
115181

182+
private void MemoryDispose(
183+
in StringBuilder builder,
184+
in string memoryNamespace
185+
)
186+
{
187+
if(memoryNamespace == "Class")
188+
{
189+
builder.Append($@"
116190
#region IDisposable
117191
118192
private bool _disposed;
@@ -129,109 +203,43 @@ protected virtual void Dispose(bool disposing)
129203
{{
130204
if (!_disposed)
131205
{{
132-
if (disposing)
133-
{{
134-
135-
}}
136-
137206
NativeMemory.Free(Start);
138207
_disposed = true;
139208
}}
140209
}}
141210
142211
#endregion
143-
}}
144-
}}
145-
146212
");
147-
context.AddSource($"StackMemoryClass.g.cs", builder.ToString());
148-
149-
builder.Clear();
150-
builder.Append($@"
151-
using System.Runtime.CompilerServices;
152-
using System.Runtime.InteropServices;
153-
using System;
154-
155-
namespace StackMemoryCollections.Struct
156-
{{
157-
public unsafe struct StackMemory : IDisposable
158-
{{
159-
private nuint _offsetBytes;
160-
161-
public StackMemory()
162-
{{
163-
throw new ArgumentException(""Default constructor not supported"");
164-
}}
165-
166-
public StackMemory(nuint byteCount)
167-
{{
168-
Start = NativeMemory.Alloc(byteCount);
169-
Current = Start;
170-
ByteCount = byteCount;
171-
_offsetBytes = 0;
172-
}}
173-
174-
public void* Start {{ get; init; }}
175-
public void* Current {{ get; private set; }}
176-
public nuint ByteCount {{ get; init; }}
177-
public nuint FreeByteCount => ByteCount - _offsetBytes;
178-
179-
public void* AllocateMemory(nuint allocateBytes)
180-
{{
181-
if (ByteCount - _offsetBytes < allocateBytes)
182-
{{
183-
throw new ArgumentException(""Can't allocate memory"");
184-
}}
185-
186-
_offsetBytes += allocateBytes;
187-
var start = Current;
188-
Current = (byte*)start + allocateBytes;
189-
return start;
190-
}}
191-
192-
public bool TryAllocateMemory(nuint allocateBytes, out void* ptr)
193-
{{
194-
if (ByteCount - _offsetBytes < allocateBytes)
195-
{{
196-
ptr = null;
197-
return false;
198-
}}
199-
200-
_offsetBytes += allocateBytes;
201-
var start = Current;
202-
Current = (byte*)start + allocateBytes;
203-
ptr = start;
213+
}
214+
else
215+
{
216+
builder.Append($@"
217+
#region IDisposable
204218
205-
return true;
206-
}}
219+
private bool _disposed;
207220
208-
public void FreeMemory(nuint reducingBytes)
221+
public void Dispose()
209222
{{
210-
var start = new IntPtr(Start);
211-
var newCurrent = new IntPtr((byte*)Current - reducingBytes);
212-
213-
if (newCurrent.CompareTo(start) < 0)
223+
if(!_disposed)
214224
{{
215-
throw new Exception(""Unable to free memory, it is out of available memory"");
225+
NativeMemory.Free(Start);
226+
_disposed = true;
216227
}}
217-
218-
_offsetBytes -= reducingBytes;
219-
Current = newCurrent.ToPointer();
220-
}}
221-
222-
#region IDisposable
223-
224-
public void Dispose()
225-
{{
226-
NativeMemory.Free(Start);
227228
}}
228229
229230
#endregion
231+
");
232+
}
233+
}
234+
235+
private void MemoryEnd(
236+
in StringBuilder builder
237+
)
238+
{
239+
builder.Append($@"
230240
}}
231241
}}
232-
233242
");
234-
context.AddSource($"StackMemoryStruct.g.cs", builder.ToString());
235243
}
236244
}
237245
}

0 commit comments

Comments
 (0)