[Mono-dev] Mono.Unix.Native

Greg Young gregoryyoung1 at gmail.com
Mon Jan 12 04:59:31 UTC 2015


To answer my own question in case anyone runs into this.

Try this:

        private byte* Align(IntPtr buf, uint alignTo)
        {
            //The buffer must originally be at least one alignment bigger!
            var diff = alignTo - (buf.ToInt64() % alignTo);
            var aligned = (IntPtr)(buf.ToInt64() + diff);
            return (byte*) aligned;
        }

On Mon, Jan 12, 2015 at 4:19 AM, Greg Young <gregoryyoung1 at gmail.com> wrote:
> Oops read is failing as well.
>
> Aside from having aligned buffer sizes do I need to do something like memalign?
>
> Cheers,
>
> Greg
>
> On Mon, Jan 12, 2015 at 3:55 AM, Greg Young <gregoryyoung1 at gmail.com> wrote:
>> I have been trying to use Syscalls a bit but am getting an exception I
>> can't figure out why.
>>
>> Opening code throws no exceptions:
>>             var flags = OpenFlags.O_RDWR | OpenFlags.O_DIRECT |
>> OpenFlags.O_CREAT;
>>             //var f = NativeConvert.FromOpenFlags(flags); not needed?
>>             var han = Syscall.open(path, flags, FilePermissions.S_IRWXU);
>>             if(han < 0)
>>                 throw new Win32Exception();
>>
>>             var handle = new SafeFileHandle((IntPtr) han, true);
>>             if(handle.IsInvalid) throw new Exception("Invalid handle");
>>             return handle;
>>
>> Then I call write:
>>
>>             fixed (byte* b = buffer)
>>             {
>>                 long ret = 0;
>>                  do {
>>                     ret = Syscall.write
>> (handle.DangerousGetHandle().ToInt32(), b ,count);
>>                 } while (Mono.Unix.UnixMarshal.ShouldRetrySyscall ((int) ret));
>>                 if(ret == -1)
>>                     Mono.Unix.UnixMarshal.ThrowExceptionForLastErrorIf
>> ((int) ret);
>>             }
>>
>> I get an invalid parameter exception. Read seems to work following a
>> similar pattern so not quite sure whats wrong.
>>
>> My writes are of size 4096. Perhaps the issue is needing to call
>> memalign? I would expect reads to fail as well then though
>>
>> Cheers,
>>
>> Greg
>>
>>
>> 13) Test Error :
>> EventStore.Core.Tests.TransactionLog.Unbuffered.UnbufferedTests.when_writing_then_seeking_exact_to_alignment_and_writing_again
>>    System.ArgumentException : Invalid argument
>>   ----> Mono.Unix.UnixIOException : Invalid argument [EINVAL].
>>   at Mono.Unix.UnixMarshal.ThrowExceptionForLastError () [0x00000] in
>> <filename unknown>:0
>>   at Mono.Unix.UnixMarshal.ThrowExceptionForLastErrorIf (Int32 retval)
>> [0x00000] in <filename unknown>:0
>>   at EventStore.Core.TransactionLog.Unbuffered.NativeFile.Write
>> (Microsoft.Win32.SafeHandles.SafeFileHandle handle, System.Byte[]
>> buffer, UInt32 count, System.Int32& written) [0x00000] in <filename
>> unknown>:0
>>   at EventStore.Core.TransactionLog.Unbuffered.UnbufferedIOFileStream.InternalWrite
>> (System.Byte[] buffer, UInt32 count) [0x00000] in <filename unknown>:0
>>   at EventStore.Core.TransactionLog.Unbuffered.UnbufferedIOFileStream.Flush
>> () [0x00000] in <filename unknown>:0
>>   at EventStore.Core.TransactionLog.Unbuffered.UnbufferedIOFileStream.Dispose
>> (Boolean disposing) [0x00000] in <filename unknown>:0
>>   at System.IO.Stream.Close () [0x00000] in <filename unknown>:0
>>   at System.IO.Stream.Dispose () [0x00000] in <filename unknown>:0
>>   at EventStore.Core.Tests.TransactionLog.Unbuffered.UnbufferedTests.when_writing_then_seeking_exact_to_alignment_and_writing_again
>> () [0x00000] in <filename unknown>:0
>>   at (wrapper managed-to-native)
>> System.Reflection.MonoMethod:InternalInvoke
>> (System.Reflection.MonoMethod,object,object[],System.Exception&)
>>   at System.Reflection.MonoMethod.Invoke (System.Object obj,
>> BindingFlags invokeAttr, System.Reflection.Binder binder,
>> System.Object[] parameters, System.Globalization.CultureInfo culture)
>> [0x00000] in <filename unknown>:0
>> --UnixIOException
>>
>>
>>
>> --
>> Studying for the Turing test
>
>
>
> --
> Studying for the Turing test



-- 
Studying for the Turing test


More information about the Mono-devel-list mailing list