feat(core): Makes spanwriter resizable (#376)

SpanWriter now has an argument that allows it to be resizable.
```cs
public SpanWriter(Span<byte> initialBuffer, bool resize = false)
public SpanWriter(int initialCapacity, bool resize = false)
```

If a SpanWriter is set to be resizable, or given an initial capacity instead of a buffer, it must be disposed:
```cs
public static void SomeMethod()
{
    using var writer = new SpanWriter(stackalloc byte[512], true);
    // stuff
    
    // Automatic Dispose of writer
}
```

This is because the SpanWriter uses `ArrayPool<byte>.Shared` _rented buffers_ to resize. If the writer is not disposed, those buffers will never be reused resulting in a _memory leak_.

It is possible that the SpanWriter will outright ditch the initial buffer if resize is set to true and growing is needed. To check/account for that we can do the following:

```cs
public static void SomeMethod()
{
    Span<byte> span = stackalloc byte[512];
    using var writer = new SpanWriter(span, true);
    // write some stuff that causes the span to grow
    
    span = writer.RawBuffer;
    // Do stuff with the span
}
```

Make sure you don't accidentally `Dispose()` the writer or use the `RawBuffer` outside of the `using` block. If you do, bad things will happen! (NullPointerException, or a fresh SpanWriter with no buffer, depending on the situation).

SpanWriter can also now be used with a fixed statement since it has a `PinnableReference()` function.
This commit is contained in:
Kamron Batman 2020-12-31 19:57:02 -08:00 • committed by GitHub
parent 2a2af424ad
commit 582e1877b8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
18 changed files with 298 additions and 134 deletions

View file

@ -29,24 +29,22 @@ namespace Server.Items
public override void OnDoubleClick(Mobile from)
{
var pm = from as PlayerMobile;
if (!IsChildOf(from.Backpack))
{
from.SendLocalizedMessage(1042001); // That must be in your pack for you to use it.
}
else if (pm == null || from.Skills.Alchemy.Base < 100.0)
else if (from is not PlayerMobile pm || from.Skills.Alchemy.Base < 100.0)
{
pm.SendMessage("Only a Grandmaster Alchemist can learn from this book.");
from.SendMessage("Only a Grandmaster Alchemist can learn from this book.");
}
else if (pm.Glassblowing)
{
pm.SendMessage("You have already learned this information.");
from.SendMessage("You have already learned this information.");
}
else
{
pm.Glassblowing = true;
pm.SendMessage(
from.SendMessage(
"You have learned to make items from glass. You will need to find miners to mine find sand for you to make these items."
);
Delete();

View file

@ -29,24 +29,22 @@ namespace Server.Items
public override void OnDoubleClick(Mobile from)
{
var pm = from as PlayerMobile;
if (!IsChildOf(from.Backpack))
{
from.SendLocalizedMessage(1042001); // That must be in your pack for you to use it.
}
else if (pm == null || from.Skills.Carpentry.Base < 100.0)
else if (from is not PlayerMobile pm || from.Skills.Carpentry.Base < 100.0)
{
pm.SendMessage("Only a Grandmaster Carpenter can learn from this book.");
from.SendMessage("Only a Grandmaster Carpenter can learn from this book.");
}
else if (pm.Masonry)
{
pm.SendMessage("You have already learned this information.");
from.SendMessage("You have already learned this information.");
}
else
{
pm.Masonry = true;
pm.SendMessage(
from.SendMessage(
"You have learned to make items from stone. You will need miners to gather stones for you to make these items."
);
Delete();

View file

@ -29,24 +29,22 @@ namespace Server.Items
public override void OnDoubleClick(Mobile from)
{
var pm = from as PlayerMobile;
if (!IsChildOf(from.Backpack))
{
from.SendLocalizedMessage(1042001); // That must be in your pack for you to use it.
}
else if (pm == null || from.Skills.Mining.Base < 100.0)
else if (from is not PlayerMobile pm || from.Skills.Mining.Base < 100.0)
{
pm.SendMessage("Only a Grandmaster Miner can learn from this book.");
from.SendMessage("Only a Grandmaster Miner can learn from this book.");
}
else if (pm.SandMining)
{
pm.SendMessage("You have already learned this information.");
from.SendMessage("You have already learned this information.");
}
else
{
pm.SandMining = true;
pm.SendMessage(
from.SendMessage(
"You have learned how to mine fine sand. Target sand areas when mining to look for fine sand."
);
Delete();