// ************************************
        // alloc / free
        // ************************************
        public virtual IntPtr Alloc(ulong length)
        {
#pragma warning disable 162
            if (Debug.On)
            {
                Logger.LogDebug("attempting to alloc length {length}", length);
            }
#pragma warning restore 162

            libc.getrlimit(GetMemLockLimit(), out var rlim);
            if (rlim.rlim_max != rlimit.UNLIMITED && rlim.rlim_max < length)
            {
                throw new MemoryLimitException(
                          $"Requested MemLock length exceeds resource limit max of {rlim.rlim_max}");
            }

            // Some platforms may require fd to be -1 even if using anonymous
            IntPtr protectedMemory = libc.mmap(
                IntPtr.Zero, length, GetProtReadWrite(), GetPrivateAnonymousFlags(), -1, 0);

            CheckIntPtr(protectedMemory, "mmap");
            try
            {
                CheckZero(libc.mlock(protectedMemory, length), "mlock");

                try
                {
                    SetNoDump(protectedMemory, length);
                }
                catch (Exception e)
                {
                    CheckZero(libc.munlock(protectedMemory, length), "munlock", e);
                    throw;
                }
            }
            catch (Exception e)
            {
                CheckZero(libc.munmap(protectedMemory, length), "munmap", e);
                throw;
            }

            return(protectedMemory);
        }
Example #2
0
        // ************************************
        // alloc / free
        // ************************************
        public virtual IntPtr Alloc(ulong length)
        {
            if (Interlocked.Read(ref memoryLocked) + (long)length > resourceLimit)
            {
                throw new MemoryLimitException(
                          $"Requested MemLock length exceeds resource limit max of {resourceLimit}");
            }

            // Some platforms may require fd to be -1 even if using anonymous
            IntPtr protectedMemory = libc.mmap(
                IntPtr.Zero, length, GetProtReadWrite(), GetPrivateAnonymousFlags(), -1, 0);

            Check.IntPtr(protectedMemory, "mmap");
            try
            {
                Check.Zero(libc.mlock(protectedMemory, length), "mlock");

                try
                {
                    Interlocked.Add(ref memoryLocked, (long)length);
                    SetNoDump(protectedMemory, length);
                }
                catch (Exception e)
                {
                    Check.Zero(libc.munlock(protectedMemory, length), "munlock", e);
                    Interlocked.Add(ref memoryLocked, 0 - (long)length);
                    throw;
                }
            }
            catch (Exception e)
            {
                Check.Zero(libc.munmap(protectedMemory, length), "munmap", e);
                throw;
            }

            return(protectedMemory);
        }