Marshaling, second part, and QNX

english — Tags: , , — @ 00:17

After testing the class I noticed that works with 2 bytes… and should work with greather lengths, but I still don’t like its performance… too many loops… needs further reviewing. BTW, I’ve fixed the one MonoCanvas bug, now the DnD works in a better way, first release is closer!!

Yesterday I went to bed too late… I was compiling mono in QNX, first, there are missing some details for compiling the Boehm GC and then.. I couldn’t finish its compiling due to some -lgthread missing in gcc. Still need “to dive” into the code for fixing these issues.

Marshaling: C’s struct’s bit fields

english — Tags: , , — @ 23:02

If you are C programmer you might have noticed the existence of the bit fields in structs:

typedef struct {
int Some : 1;
int Bit : 2;
int Fields : 5;
} MyStruct;

Marshaling that code could be a pain in the ass. Before all, read Jonathan’s doc, then forget about the C’s sizeof, sum all the bit fields and get the value, in our sample struct we need to get that as 1 byte (1 + 2 + 5 = 8 bits = 1 byte) and then use the System.Collections.BitArray class, and convert that to their data type value. (I wrote a wrapper for doing this in a easiest way)

See the graphical explanation for understanding this.

See this sample

/* libsample.h */

typedef struct {
unsigned int Some : 1;
unsigned int Bit : 2;
unsigned int Fields : 5;
} MyStruct;

MyStruct get_mystruct (void);

int some (void);

/* libsample.c */
#include
#include "libsample.h"

MyStruct get_mystruct (void)
{
MyStruct a;
a.Some = 0x01; //One bit: 1 - 0
a.Bit = 0x02; //Two bits: 10 - 2
a.Fields = 0x1A; //Five bits: 0001 1010 - 26
//Full return: 1101 1010
return a;
}

int some (void)
{
return 0;
}

Then compiling


gcc -fPIC -Wall -g -c libsample.c
gcc -g -shared -Wl,-soname,libsample.so.0 \
-o libsample.so.0.0 libsample.o -lc
/sbin/ldconfig -n .
ln -sf libsample.so.0 libsample.so

Will create the shared libraries, let’s see the C# PInvoke.

using System;
using System.Collections;
using System.Runtime.InteropServices;

public class Sample
{
[StructLayout (LayoutKind.Sequential, Size=1, Pack=4)]
public struct MyStruct
{
public byte Value;

public uint GetSome ()
{
BitArray bArray = new BitArray (new byte[] { Value });
BitFieldFormater a = new BitFieldFormater (bArray);
return a.GetInt (0, 1);
}

public uint GetBit ()
{
BitArray bArray = new BitArray (new byte[] { Value });
BitFieldFormater a = new BitFieldFormater (bArray);
return a.GetInt (1, 2);
}

public uint GetFields ()
{
BitArray bArray = new BitArray (new byte[] { Value });
BitFieldFormater a = new BitFieldFormater (bArray);
return a.GetInt (3, 5);
}

public override string ToString ()
{
return "Some "+GetSome ()+" Bit "+GetBit ()+" Fields "+GetFields ();
}
}

[DllImport ("libsample.so")]
public static unsafe extern MyStruct get_mystruct (); //notice the return value

[DllImport ("libsample.so")]
public static unsafe extern int some (); //notice the return value

public static void Main ()
{
MyStruct myStruct = get_mystruct ();
System.Console.WriteLine ("some (): "+some ());
System.Console.WriteLine ("get_mystruct (): "+myStruct);
}
}

I’m using thre a class called BitFieldFormater for doing the magic:

using System;
using System.Collections;

public class BitFieldFormater
{
public BitFieldFormater (BitArray bitArray)
{
_bitArray = bitArray;
}

public uint GetUInt (int beginBit, int bitLength)
{
if ((beginBit + bitLength) > _bitArray.Count)
throw new ArgumentException ("Begin bit plus bit length is greater than array's length");

string requestedString = "";

//Getting bit values from the BitArray
for (int i = 0; i < bitLength; i++)
requestedString = Convert.ToInt32 (_bitArray [beginBit + i]) + requestedString;

return (uint) BinaryToInteger (requestedString);
}

private int BinaryToInteger (string binary)
{
char []elements = binary.ToCharArray ();
int result = 0, i = 0, pow = 0;
for (i = elements.Length - 1; i >= 0; i--) {
if (elements[i] == '1')
result += (int) Math.Pow (2, pow);
pow++;
}
return result;
}

private BitArray _bitArray;
}

Any bugs, please, let me know. I haven’t yet tested this class with structs with byte-size greater than 1… further testing is required.

UPDATED. Feb 3rd. Lot of useless code removed… what the fuck was I thinking!?

Nowadays

english — Tags: , — @ 23:13

I’m sure that isn’t only me, but sometimes, while working, I think and imagine the processes in my head… and then somebody touchs my shoulder or calls me… that really freaks me out… may be too many cups of coffee… but my heart starts beating very fast…

Let’s breath and restart…

MonoCanvas’ DnD

english — Tags: , — @ 18:20

Using graphics for representing ideas is quite usefull and the “DnD idea” is really good. Dragging and dropping represents the idea of adding, moving or sending information from a source to a target. I’ve been playing with this idea this week and I’m happy with this preview. There are bugs, I found two, Cursor bug, doesn’t change when starting dragging and drag-begin bug that doesn’t call the IDraggable.DragBegin when should be.

I did an implementation based on a mix of Gtk#‘s and System.Windows.Forms‘ DnD methods, comments are allowed and hearing/reading them would be very nice. The idea using two interfaces that represent the Draggable and the Droppable shapes, current implementation is

MonoCanvas'

which means:

MonoCanvas' DnD sample

I did a sample screencast.

Beagle from sources

english — Tags: , — @ 23:49

Time ago I played with Beagle (apt-get install beagle), but after its 0.20 release decided to build it from sources. I don’t do apt-get because I’m running mono from svn, yes, I know can have parallel installations but like living in the edge ;-) . Compiling takes sometime, allmost all the time because of the dependencies, if you want to enable everything you must compile allmost all. Here’s my quick-and-dirty guide for building Beagle 0.20 from sources.

I’m running linux 2.6.12-9-686 from deb package with Ubuntu Breezy. Guided from beagle’s wiki-page I began installing. Remember that using another –prefix rather than /usr is a very good idea, I use /opt/beagle/, remember to set $PATH, $LD_CONFIG_PATH and $PKG_CONFIG_PATH while installing the packages,

  1. sqlite-2.8.17.tar.gz
  2. gmime-2.1.19.tar.gz
  3. XFree86 libs, apt-get install libxss-dev
  4. evolution-sharp
  5. galago, here you need dbus-sharp… then you need dbus even if you already have it.
  6. libexif, apt-get install libexif-dev
  7. gsf-sharp

Then, beagled, and beagle-search, and done.

Beagle

Thursday

english — Tags: , — @ 01:16

Lot of laughing. It is all related. Conspiracy or too much paranoia?

In the meanwhile. We need to improve MonoUML‘s UI. When finishing MonoCanvas’ first release will start with that.

Lovely

english — Tags: , — @ 00:45

Lovely GNOME, lovely.

Everytime when I upgrade to a newer GNOME version I got impressed, very good work. Really enjoy the desktop.

GNOME

Ideas

english — Tags: , , , — @ 23:45

I spent this weekend thinking about some many things, monouml-things. It all began after reading (days ago) alo’s post when I notice that maybe taking his effort and wrapping the libraries would be nice for allowing GroupWare in MonoUML, of course in the near future… we haven’t yet finished MonoCanvas, but the ideas are nice. I talked with Rodolfo, telling this idea for using the WebServices-based communication, yes I know, WS aren’t the best either… He suggested a better solution: CORBA. I agree with him, but using CORBA adds another dependency, so does cherokee-wrapping… Anyway I just need to think more while implementing MonoCanvas.

There are some things that a want to upgrade, some of then are related to usability, such as better DnD within the Views, nicer UIs, better usage space… MonoUML needs love.

Información de interés

Uncategorized — Tags: , — @ 18:27

“En la década de 1970 la palabra metodología comenzó a utilizarse en el sentido de “una manera de desarrollar un sistema de información”; la palabra en realidad significa la ciencia de los métodos. Luego, en la década de 1980, la palabra paradigma se puso de moda en el mundo empresarial, como en la frase: “Es un paradigma completamente nuevo”. La industria de los sistemas de información pronto comenzó a utilizar la palabra paradigma en las frases paradigma orientado a objetos y paradigma tradicional (o clásico) para referirse a “un estilo de desarrollo de sistemas de información”. Ésta fue otra desacertada elección de terminología, porque paradigma significa modelo o patrón.”
Stephen R. Schach
– Análisis y diseño orientado a objetos con UML y el Proceso Unificado.

Usualmente peleo por los términos que utilizamos diariamente para usarse para/con los usuarios finales, uno de ellos es catálogo, pero bueno, más que tener razón es simplemente costumbre, costumbre de que alguien tiempo atrás seleccionó un término que le pareció adecuado para tal situación pero que ahora, al menos en mi punto de vista esta fuera del entendimiento para los usuarios finales tradicionales. Las cosas han cambiado y nosotros debemos de cambiar, términos viejos deben mantenerse como sugerencias no reglas.

Simple innovación, simple evolución.

Tenemos que cambiar.

Enjoying life

english — Tags: , — @ 00:32

I’m trying to port Mono to QNX, hope to finish this effort sooner. In the meanwhile lets enjoy some beers.

XX Lager

Next Page »
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License.
(c) 2004-2010 Mario Carrion | powered by WordPress with Barecity