Skip to content

Commit

Permalink
Improved code implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
mahara committed Mar 17, 2023
1 parent d41fe5d commit 09ae359
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,7 @@ private void OnEndRequest(object sender, EventArgs e)
{
var session = (ISession) HttpContext.Current.Items[SessionKey];

if (session != null)
{
session.Dispose();
}
session?.Dispose();
}

private static IWindsorContainer ObtainContainer()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,24 @@ public class ObjectPersister<T> : IObjectPersister<T>
public T Read(string filePath, FileMode mode = FileMode.OpenOrCreate)
{
using var stream = new FileStream(filePath, mode);
using var allocation = ArrayPool<byte>.Shared.Allocate((int) stream.Length);
var bytes = allocation.Values;
#if NETFRAMEWORK
stream.Read(bytes, 0, bytes.Length);
var @object = (T) SerializationHelper.Deserialize(bytes);
using var allocation = ArrayPool<byte>.Shared.AllocateByte((int) stream.Length, true);
var buffer = allocation.Buffer;
#if NET
stream.Read(buffer.AsSpan());
#else
var bytesAsSpan = bytes.AsSpan();
stream.Read(bytesAsSpan);
var @object = (T) SerializationHelper.Deserialize(bytesAsSpan.ToArray());
stream.Read(buffer, 0, buffer.Length);
#endif
return @object;
return (T) SerializationHelper.Deserialize(buffer);
}

public void Write(T @object, string filePath, FileMode mode = FileMode.OpenOrCreate)
{
using var stream = new FileStream(filePath, mode);
var bytes = SerializationHelper.Serialize(@object);
#if NETFRAMEWORK
stream.Write(bytes, 0, bytes.Length);
var buffer = SerializationHelper.Serialize(@object);
#if NET
stream.Write(buffer.AsSpan());
#else
var bytesAsSpan = bytes.AsSpan();
stream.Write(bytesAsSpan);
stream.Write(buffer, 0, buffer.Length);
#endif
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@

namespace Castle.Facilities.NHibernateIntegration.SessionStores
{
#if NET
using System;
#endif
using System.Collections.Generic;

#if NETFRAMEWORK
using System.Runtime.Remoting.Messaging;

Expand All @@ -27,6 +28,7 @@ namespace Castle.Facilities.NHibernateIntegration.SessionStores
/// which relies on <see cref="CallContext" />.
/// </summary>
#else

/// <summary>
/// An implementation of <see cref="ISessionStore" />
/// which relies on .NET Framework CallContext.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,19 @@

namespace Castle.Facilities.NHibernateIntegration.SessionStores
{
#if NET
using System;
#endif
using System.Collections.Generic;


#if NETFRAMEWORK
using System.Runtime.Remoting.Messaging;
#endif
#if NETFRAMEWORK

/// <summary>
/// An implementation of <see cref="ISessionStore" />
/// which relies on logical <see cref="CallContext" />.
/// </summary>
#else

/// <summary>
/// An implementation of <see cref="ISessionStore" />
/// which relies on .NET Framework logical CallContext.
Expand All @@ -37,7 +37,6 @@ namespace Castle.Facilities.NHibernateIntegration.SessionStores
/// This is not supported anymore in .NET.
/// </exception>
#endif

public class LogicalCallContextSessionStore : AbstractDictionaryStackSessionStore
{
protected override IDictionary<string, Stack<SessionDelegate>> GetSessionDictionary()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ namespace Castle.Facilities.NHibernateIntegration.SessionStores
#endif

using MicroKernel.Facilities;

#if NET

using Microsoft.AspNetCore.Http;
#endif

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,86 @@ namespace Castle.Facilities.NHibernateIntegration.Util
{
using System;
using System.Buffers;
#if NET
using System.Security.Cryptography;
#endif

public static class ArrayPoolExtensions
{
public static ArrayPoolAllocation<T> Allocate<T>(this ArrayPool<T> pool, int minimumSize)
public static ArrayPoolAllocation<T> Allocate<T>(this ArrayPool<T> pool,
int minimumLength,
bool clearBufferContents = false)
{
return new ArrayPoolAllocation<T>(pool, minimumSize);
return new ArrayPoolAllocation<T>(pool, minimumLength, clearBufferContents);
}

public static ArrayPoolByteAllocation AllocateByte(this ArrayPool<byte> pool,
int minimumLength,
bool clearBufferContents = false)
{
return new ArrayPoolByteAllocation(pool, minimumLength, clearBufferContents);
}
}

public readonly struct ArrayPoolAllocation<T> : IDisposable
{
private readonly ArrayPool<T> _pool;
private readonly bool _clearBufferContents;

internal ArrayPoolAllocation(ArrayPool<T> pool,
int minimumLength,
bool clearBufferContents)
{
_pool = pool ?? throw new ArgumentNullException(nameof(pool));
_clearBufferContents = clearBufferContents;

Buffer = _pool.Rent(minimumLength);
}

public T[] Buffer { get; }

internal ArrayPoolAllocation(ArrayPool<T> pool, int minimumSize)
public void Dispose()
{
if (_clearBufferContents)
{
// https://github.com/dotnet/runtime/discussions/48697
Buffer.AsSpan(0, Buffer.Length).Clear();
}

_pool.Return(Buffer);
}
}

public readonly struct ArrayPoolByteAllocation : IDisposable
{
private readonly ArrayPool<byte> _pool;
private readonly bool _clearBufferContents;

internal ArrayPoolByteAllocation(ArrayPool<byte> pool,
int minimumLength,
bool clearBufferContents)
{
_pool = pool ?? throw new ArgumentNullException(nameof(pool));
_clearBufferContents = clearBufferContents;

Values = _pool.Rent(minimumSize);
Buffer = _pool.Rent(minimumLength);
}

public T[] Values { get; }
public byte[] Buffer { get; }

public void Dispose()
{
_pool.Return(Values);
if (_clearBufferContents)
{
// https://github.com/dotnet/runtime/discussions/48697
#if NET
CryptographicOperations.ZeroMemory(Buffer.AsSpan(0, Buffer.Length));
#else
Buffer.AsSpan(0, Buffer.Length).Clear();
#endif
}

_pool.Return(Buffer);
}
}
}

0 comments on commit 09ae359

Please sign in to comment.