コード例 #1
0
ファイル: Context.cs プロジェクト: bonomali/Ibasa
        public Context(Dictionary <IntPtr, IntPtr> properties, DeviceType deviceType, Action <string, byte[], object> notify, object user_data)
            : this()
        {
            unsafe
            {
                int property_count = properties == null ? 0 : properties.Count;

                IntPtr *properties_ptr = stackalloc IntPtr[property_count * 2 + 1];

                int index = 0;
                if (properties != null)
                {
                    foreach (var pair in properties)
                    {
                        properties_ptr[index++] = pair.Key;
                        properties_ptr[index++] = pair.Value;
                    }
                    properties_ptr[index] = IntPtr.Zero;
                }
                else
                {
                    properties_ptr = null;
                }

                var function_ptr = IntPtr.Zero;
                var data_handle  = new GCHandle();
                var data_ptr     = IntPtr.Zero;

                if (notify != null)
                {
                    var data = Tuple.Create(notify, user_data);
                    data_handle = GCHandle.Alloc(data);
                    data_ptr    = GCHandle.ToIntPtr(data_handle);

                    function_ptr = Marshal.GetFunctionPointerForDelegate(new Action <IntPtr, IntPtr, UIntPtr, IntPtr>(Callback));
                }

                try
                {
                    int errcode = 0;
                    Handle = Cl.CreateContextFromType(properties_ptr, (int)deviceType,
                                                      function_ptr, data_ptr.ToPointer(), &errcode);
                    ClHelper.GetError(errcode);

                    CallbackPointers.Add(Handle, data_handle);
                }
                catch (Exception)
                {
                    if (data_handle.IsAllocated)
                    {
                        data_handle.Free();
                    }
                    throw;
                }
            }
        }