public static void CopyShallow(object source, object target) { if (source == null) { throw new ArgumentNullException("source"); } if (target == null) { throw new ArgumentNullException("target"); } Type t = source.GetType(); if (target.GetType() != t) { throw new ArgumentException("Object types must be equal."); } byte[] data = new byte[UnsafeTools.BaseInstanceSizeOf(t) - IntPtr.Size]; InteropTools.Pin( source, o1 => { var ptr = UnsafeTools.GetAddress(o1); Marshal.Copy(ptr, data, 0, data.Length); } ); InteropTools.Pin( target, o2 => { var ptr = UnsafeTools.GetAddress(o2); Marshal.Copy(data, 0, ptr, data.Length); } ); }
public static unsafe T Init(void *target) { IntPtr *vtable = ((IntPtr *)target) + 1; *vtable = type.TypeHandle.Value; return((T)UnsafeTools.GetObject((IntPtr)(vtable))); }
static T[] FindInstance(ref ByValArray <T> arr) { T[] orig = arr.array; return(UnsafeTools.GetPointer( //Obtain the address from the reference. //It uses a lambda to minimize the chance of the reference //being moved around by the GC. out arr, ptr => { WeakReference <T[]> wref; T[] inst; if (Cache.TryGetValue(ptr, out wref) && wref.TryGetTarget(out inst)) { //An object is found on this address. if (inst != orig) { //This address was overwritten with a new value, //clone the instance. inst = (T[])orig.Clone(); Cache[ptr] = new WeakReference <T[]>(inst); } return inst; } else { //No object was found on this address, //clone the instance. inst = (T[])orig.Clone(); Cache[ptr] = new WeakReference <T[]>(inst); return inst; } } )); }
/// <summary> /// Atomically exchanges two values. /// </summary> public static void Exchange <T>(ref T location1, ref T location2) { if (UnsafeTools.SizeOf(TypeOf <T> .TypeID) > IntPtr.Size) { throw new ArgumentException("Value is longer than native reference size."); } _Exchange(__makeref(location1), __makeref(location2)); }
public static byte[] MemorySerialize <T>(T?value) where T : struct { Type t = TypeOf <T?> .TypeID; int size = UnsafeTools.SizeOf(t); byte[] arr = new byte[size]; Copier <T?> .Copy(arr, value); return(arr); }
public static void Exchange(TypedReference location1, TypedReference location2) { if (__reftype(location1) != __reftype(location2)) { throw new ArgumentException("Reference types do not match."); } if (UnsafeTools.SizeOf(__reftype(location1)) > IntPtr.Size) { throw new ArgumentException("Value is longer than native reference size."); } _Exchange(location1, location2); }
/// <summary> /// Process the filter on the specified image. /// </summary> /// /// <param name="image">Source image data.</param> /// protected unsafe override void ProcessFilter(UnmanagedImage image) { int width = image.Width; int height = image.Height; int stride = image.Stride; int offset = stride - width; int b = blockSize; int blocksX = width - b; int blocksY = height - b; double count = b * b; double[,] mean = new double[blocksY, blocksX]; double[,] var = new double[blocksY, blocksX]; byte *src = (byte *)image.ImageData.ToPointer(); for (int y = 0; y < height - b; y++) { for (int x = 0; x < width - b; x++, src++) { mean[y, x] = UnsafeTools.Sum(src, b, b, stride) / count; var[y, x] = UnsafeTools.Scatter(src, b, b, stride, mean[y, x]); } src += offset + b; } src = (byte *)image.ImageData.ToPointer() + b * stride + b; for (int y = b; y < height - b - 1; y++) { for (int x = b; x < width - b - 1; x++, src++) { // variances double va = var[y - b, x - b], vb = var[y - b, x + 1]; double vc = var[y + 1, x - b], vd = var[y + 1, x + 1]; // means double ma = mean[y - b, x - b], mb = mean[y - b, x + 1]; double mc = mean[y + 1, x - b], md = mean[y + 1, x + 1]; double value = min(va, vb, vc, vd, ma, mb, mc, md); *src = (byte)(value > 255 ? 255 : (value < 0 ? 0 : value)); } src += offset + 2 * b + 1; } }
public bool MoveNext() { ArgIterator arglist = (ArgIterator)Arglist; state = 0; if (arglist.GetRemainingCount() == 0) { state = 1; return(false); } current = TypedReference.ToObject(arglist.GetNextArg()); Arglist = UnsafeTools.Box(arglist); return(true); }
public static T Clone(T instance) { if (instance == null) { throw new ArgumentNullException("source"); } Type t = TypeOf <T> .TypeID; if (t.IsValueType) { return(instance); } var copy = (T)UnsafeTools.GetUninitializedObject(t); Copier.CopyShallow(instance, copy); return(copy); }
/// <summary> /// Processes the filter. /// </summary> /// protected unsafe override void ProcessFilter(Signal sourceData, Signal destinationData) { SampleFormat format = sourceData.SampleFormat; int channels = sourceData.Channels; int length = sourceData.Length; if (format == SampleFormat.Format32BitIeeeFloat) { float *src = (float *)sourceData.Data.ToPointer(); float *ovl = (float *)OverlaySignal.Data.ToPointer(); float *dst = (float *)destinationData.Data.ToPointer(); for (int i = 0; i < length; i++) { for (int j = 0; j < channels; j++, src++, dst++, ovl++) { *dst = *src + *ovl; } } if (Normalize) { dst = (float *)destinationData.Data.ToPointer(); float[] min, max; float[] len = UnsafeTools.GetRange(dst, channels, length, out min, out max); for (int i = 0; i < length; i++) { for (int j = 0; j < channels; j++, dst++) { *dst = ((*dst - min[j]) / len[j]) * 2 - 1; } } #if DEBUG dst = (float *)destinationData.Data.ToPointer(); len = UnsafeTools.GetRange(dst, channels, length, out min, out max); Accord.Diagnostics.Debug.Assert(len.IsEqual(2, atol: 1e-6f)); Accord.Diagnostics.Debug.Assert(min.IsEqual(-1, atol: 1e-6f)); Accord.Diagnostics.Debug.Assert(max.IsEqual(+1, atol: 1e-6f)); #endif } } }
public TRet GetReference <TRet>(Reference.RefFunc <T, TRet> func) { return(UnsafeTools.GetReference(this, func)); }
private static void FormatterGeneric <T>(StringBuffer stringBuffer, byte *valuePtr, StringView view, UnmanagedFormatterDelegate <T> typedFormatter) where T : unmanaged { typedFormatter?.Invoke(ref UnsafeTools.AsRef <T>(valuePtr), stringBuffer, view); }
public ArgListEnumerable(ArgIterator arglist) { Arglist = UnsafeTools.Box(arglist); Initial = UnsafeTools.Box(arglist); }