Ejemplo n.º 1
0
        public static unsafe int CallArrayDoubleArrayDouble(DataStructure *data)
        {
            if (data->printf_fct != null)
            {
                return(CaptureStdOutput(CallArrayDoubleArrayDouble, data));
            }

            CallArrayDoubleArrayDoubleIO *input = (CallArrayDoubleArrayDoubleIO *)data->inputs;
            var     vec = new double[input->nb];
            double *src = (double *)input->p;

            for (int i = 0; i < vec.Length; ++i)
            {
                vec[i] = src[i];
            }

            Int64 *p_fct     = (Int64 *)data->exc;
            var    fctref    = ObjectStorage.Inst.Get(*p_fct);
            var    fct       = fctref.MethodInfo;
            var    cs_output = fct.Invoke(null, new object[] { vec }) as double[];
            CallArrayDoubleArrayDoubleIO *output = (CallArrayDoubleArrayDoubleIO *)data->outputs;

            output->nb = cs_output.Length;
            if (cs_output.Length > 0)
            {
                NativeAllocation allocate = MarshalDelegate <NativeAllocation>(data->allocate_fct);
                int size = cs_output.Length * sizeof(double);
                allocate(size, out output->p);
                Marshal.Copy(cs_output, 0, (IntPtr)output->p, cs_output.Length);
            }
            return(0);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Creates a new VCDIFF Encoder. The input streams will not be closed once this object is disposed.
        /// </summary>
        /// <param name="source">The dictionary (sourceStream file).</param>
        /// <param name="target">The target to create the diff from.</param>
        /// <param name="outputStream">The stream to write the diff into.</param>
        /// <param name="maxBufferSize">The maximum buffer size for window chunking in megabytes (MiB).</param>
        /// <param name="blockSize">
        /// The block size to use. Must be a power of two. No match smaller than this block size will be identified.
        /// Increasing blockSize by a factor of two will halve the amount of memory needed for the next block table, and will halve the setup time
        /// for a new BlockHash.  However, it also doubles the minimum match length that is guaranteed to be found.
        ///
        /// Blocksizes that are n mod 32 = 0 are AVX2 accelerated. Blocksizes that are n mod 16 = 0 are SSE2 accelerated, if supported. 16 is a good default
        /// for most scenarios, but you should use a block size of 32 or 64 for very similar data, or to optimize for speed.
        /// </param>
        /// <param name="chunkSize">
        /// The minimum size of a string match that is worth putting into a COPY. This must be bigger than twice the block size.</param>
        /// <param name="rollingHash">
        /// Manually provide a <see cref="RollingHash"/> instance that can be reused for multiple encoding instances
        /// of the same block size.
        ///
        /// If you provide a <see cref="RollingHash"/> instance, you must dispose of it yourself.
        /// </param>
        /// <exception cref="ArgumentException">If an invalid blockSize or chunkSize is used..</exception>
        public unsafe VcEncoder(Stream source, Stream target, Stream outputStream, int maxBufferSize = 1, int blockSize = 16, int chunkSize = 0, RollingHash?rollingHash = null)
        {
            _nativeAllocation = new NativeAllocation <byte>((int)source.Length);
            source.Read(_nativeAllocation.AsSpan());
            this.oldData = new ByteBuffer(_nativeAllocation.AsSpan());

            InitializeEncoder(target, outputStream, maxBufferSize, blockSize, chunkSize, rollingHash);
        }
Ejemplo n.º 3
0
        public void Return(byte *ptr, long size, NativeMemory.ThreadStats allocatingThread, long generation)
        {
            if (ptr == null)
            {
                return;
            }

            Interlocked.Add(ref _currentlyInUseBytes, -size);

            Sodium.sodium_memzero(ptr, (UIntPtr)size);

            var numberOfPages = size / Constants.Storage.PageSize;

            if (Disabled || numberOfPages > MaxNumberOfPagesToCache || (_isLowMemory.IsRaised() && generation < Generation))
            {
                // - don't want to pool large buffers
                // - release all the buffers that were created before we got the low memory event
                ForTestingPurposes?.OnFree4KbAlignedMemory?.Invoke(size);
                PlatformSpecific.NativeMemory.Free4KbAlignedMemory(ptr, size, allocatingThread);
                return;
            }

            var index      = Bits.MostSignificantBit(size);
            var allocation = new NativeAllocation
            {
                Ptr         = ptr,
                Size        = size,
                InPoolSince = DateTime.UtcNow
            };

            var addToPerCorePool = ForTestingPurposes == null || ForTestingPurposes.CanAddToPerCorePool;
            var success          = addToPerCorePool ? _items[index].TryPush(allocation) : false;

            if (success)
            {
                // updating the thread allocations since we released the memory back to the pool
                ForTestingPurposes?.OnUpdateMemoryStatsForThread?.Invoke(size);
                NativeMemory.UpdateMemoryStatsForThread(allocatingThread, size);
                return;
            }

            var addToGlobalPool = ForTestingPurposes == null || ForTestingPurposes.CanAddToGlobalPool;

            var currentGlobalStack = _globalStacks[index];

            if (addToGlobalPool && currentGlobalStack.Count < _maxNumberOfAllocationsToKeepInGlobalStackPerSlot)
            {
                // updating the thread allocations since we released the memory back to the pool
                ForTestingPurposes?.OnUpdateMemoryStatsForThread?.Invoke(size);
                NativeMemory.UpdateMemoryStatsForThread(allocatingThread, size);
                currentGlobalStack.Push(allocation);
                return;
            }

            ForTestingPurposes?.OnFree4KbAlignedMemory?.Invoke(size);
            PlatformSpecific.NativeMemory.Free4KbAlignedMemory(ptr, size, allocatingThread);
        }
Ejemplo n.º 4
0
        public static unsafe int RandomString(DataStructure *data)
        {
            string           text     = "Français";
            var              raw      = StringToNullTerminatedBytesUTF8(text);
            NativeAllocation allocate = MarshalDelegate <NativeAllocation>(data->allocate_fct);

            allocate(raw.Length, out data->outputs);
            data->exc = null;
            Marshal.Copy(raw, 0, (IntPtr)data->outputs, raw.Length);
            return(0);
        }
Ejemplo n.º 5
0
        public static unsafe int CsUpper(DataStructure *data)
        {
            string text = BytesToString((sbyte *)data->inputs);

            text = text.ToUpper();
            var raw = StringToNullTerminatedBytesUTF8(text);
            NativeAllocation allocate = MarshalDelegate <NativeAllocation>(data->allocate_fct);

            allocate(raw.Length, out data->outputs);
            data->exc = null;
            Marshal.Copy(raw, 0, (IntPtr)data->outputs, raw.Length);
            return(0);
        }
Ejemplo n.º 6
0
        public static unsafe int CallArrayInt32String(DataStructure *data)
        {
            if (data->printf_fct != null)
            {
                return(CaptureStdOutput(CallArrayInt32String, data));
            }
            string content   = BytesToString((sbyte *)data->inputs);
            Int64 *p_fct     = (Int64 *)data->exc;
            var    fctref    = ObjectStorage.Inst.Get(*p_fct);
            var    fct       = fctref.MethodInfo;
            var    cs_output = fct.Invoke(null, new object[] { content }) as int[];
            CallArrayInt32StringOutput *output = (CallArrayInt32StringOutput *)data->outputs;

            output->nb = cs_output.Length;
            if (cs_output.Length > 0)
            {
                NativeAllocation allocate = MarshalDelegate <NativeAllocation>(data->allocate_fct);
                int size = cs_output.Length * sizeof(int);
                allocate(size, out output->p);
                Marshal.Copy(cs_output, 0, (IntPtr)output->p, cs_output.Length);
            }
            return(0);
        }
Ejemplo n.º 7
0
        public static unsafe int CreateFunction(DataStructure *data)
        {
            CreateFunctionInput *inputPtr = (CreateFunctionInput *)data->inputs;
            Int64 *outputPtr = (Int64 *)data->outputs;
            string name      = BytesToString((sbyte *)inputPtr->namePointer);
            string code      = BytesToString((sbyte *)inputPtr->codePointer);
            string clrPath   = BytesToString((sbyte *)inputPtr->clrPath);

            sbyte **c_usings = (sbyte **)inputPtr->usingsPointer;
            var     usings   = new List <string>();

            while (*c_usings != null)
            {
                usings.Add(BytesToString(*c_usings));
                c_usings++;
            }

            sbyte **c_dependencies = (sbyte **)inputPtr->dependenciesPointer;
            var     dependencies   = new List <string>();

            while (*c_dependencies != null)
            {
                dependencies.Add(BytesToString(*c_dependencies));
                c_dependencies++;
            }

            string     text = "";
            MethodInfo meth;

            try
            {
                meth = DynamicFunction.CreateFunction(name, code, usings.ToArray(), dependencies.ToArray(), clrPath);
            }
            catch (Exception exc)
            {
                meth = null;
                text = exc.ToString();
                text = string.Format("Unable to compile function '{0}' due to {1}\n---CODE---\n{2}\n---USINGS---\n{3}\n---DEPENDENCIES---\n{4}\n---",
                                     name, exc.ToString(), code, string.Join("\n", usings), string.Join("\n", dependencies));
            }

            if (meth != null)
            {
                try
                {
                    text = DynamicFunction.MethodSignature(meth);
                }
                catch (Exception exc)
                {
                    meth = null;
                    text = string.Format("Unable to get the signature due to: {0}.", exc.ToString());
                }
            }
            else if (string.IsNullOrEmpty(text))
            {
                text = string.Format("Method '{0}' is null\n---CODE---\n{1}\n---USINGS---\n{2}\n---DEPENDENCIES---\n{3}\n---",
                                     name, code, string.Join("\n", usings), string.Join("\n", dependencies));
            }

            *outputPtr = meth == null ? -1 : ObjectStorage.Inst.AddIncref(meth);
            if (meth == null)
            {
                text = text.Replace("\r", "").Replace("\n\n", "\n");
            }
            var raw = StringToNullTerminatedBytesUTF8(text);
            NativeAllocation allocate = MarshalDelegate <NativeAllocation>(data->allocate_fct);

            allocate(raw.Length, out data->exc);
            Marshal.Copy(raw, 0, (IntPtr)data->exc, raw.Length);
            return(0);
        }