public object GetObject(Serializer binfmt, DataType datatype) { if (Size == 0) return null; if (datatype == DataType.UInt) { staticalloc((int)Size); Marshal.Copy(Ptr, staticdata, 0, (int)Size); byte[] d = staticdata; uint val = (uint)d[0] + ((uint)d[1] << 8) + ((uint)d[2] << 16) + ((uint)d[3] << 24); return val; } else if (datatype == DataType.IntArray) { int[] data = new int[(int)Size/4]; Marshal.Copy(Ptr, data, 0, data.Length); return data; /*} else if (datatype == DataType.String && false) { char[] data = new char[(int)Size/2]; Marshal.Copy(Ptr, data, 0, data.Length); return new String(data);*/ } else { staticalloc((int)Size); Marshal.Copy(Ptr, staticdata, 0, (int)Size); return binfmt.Deserialize(staticdata); } }
public BDB46(string file, string table, bool create, DBFormat format, bool allowDuplicates, Env environment) { if (format == DBFormat.Recno) KeyType = DataType.UInt; this.env = environment; int offset = 296; if (IntPtr.Size == 8) offset = 504; db_create(out dbp, environment.envptr, 0); funcs = (dbstruct)Marshal.PtrToStructure((IntPtr)((int)dbp + offset), typeof(dbstruct)); this.m_Format = format; this.m_File = file; int type = (int)format; uint flags = DB_DIRTY_READ; // | DB_AUTO_COMMIT; int chmod_mode = 0; if (create) flags |= DB_CREATE; /* needed for my renumber stuff. this shit is so hacked, i dont even care about * maintaining compat at this point. oh well. * * -- Christian */ this.funcs.set_flags (dbp, DB_RENUMBER); // if file & database are null, db is held in-memory // on file is taken as UTF8 (is this call right?) int ret = funcs.open(dbp, env.Txn, file, table, type, flags, chmod_mode); CheckError(ret); binfmt = new Serializer(); }
public static Data New(object data, Serializer binfmt, DataType datatype) { Data ret = new Data(); if (data == null) { ret.Ptr = IntPtr.Zero; ret.Size = 0; } else if (datatype == DataType.UInt) { staticalloc(4); uint value = (uint)data; byte[] d = staticdata; d[0] = (byte)((value) & 0xFF); d[1] = (byte)((value >> 8) & 0xFF); d[2] = (byte)((value >> 16) & 0xFF); d[3] = (byte)((value >> 24) & 0xFF); ret.Ptr = BytesToAlloc(d, 4, 1); ret.Size = (uint)4; } else if (datatype == DataType.IntArray) { int[] values = (int[])data; ret.Size = (uint)(4*values.Length); ret.Ptr = BytesToAlloc(values, values.Length, 4); /*} else if (datatype == DataType.String && false) { // Somehow this is slower than the below path. char[] values = ((string)data).ToCharArray(); ret.Size = (uint)(2*values.Length); ret.Ptr = BytesToAlloc(values, values.Length, 2);*/ } else { MemoryStream binary = binfmt.Serialize(data); if (binary.Length > uint.MaxValue) throw new ArgumentException("data is too large"); ret.Ptr = BytesToAlloc(binary.GetBuffer(), (int)binary.Length, 1); ret.Size = (uint)binary.Length; } return ret; }