__attribute__ ((aligned(2)) vs. __attribute__ ( (__packed__))

29 03 2008

Because alignment, the size of this data structure is 28, instead of 26.


typedef struct tWAVEFORMATEX
{
    u_int32_t   fcc;
    u_int32_t   cb;
    u_int16_t   wFormatTag;         /* format type */
    u_int16_t   nChannels;          /* number of channels (i.e. mono, stereo...) */
    u_int32_t   nSamplesPerSec;     /* sample rate */
    u_int32_t   nAvgBytesPerSec;    /* for buffer estimation */
    u_int16_t   nBlockAlign;        /* block size of data */
    u_int16_t   wBitsPerSample;     /* number of bits per sample of mono data */
    u_int16_t   cbSize;             /* the count in bytes of the size of */
    /* extra information (after cbSize) */
} WAVEFORMATEX;

 
However, you want to read data from a file into the structure. Then how to do so?
What makes it worse is when there are following set of data which should be read into another data structure. Because the size of above structure is 28 due to the alignment, it starts reading next data from the 29th bytes instead of 27th bytes.

Then how to solve it?
You can tell the compiler to abide by original data layout which is same to the one denoted by the structure.
So, you will try issue :


typedef struct.... {
 ...
} WAVEFORMATEX __attribute__ ( (aligned(2)) );

or you may try adding the __attribute__ to each data field declaration like this.


typedef struct tWAVEFORMATEX
{
    u_int32_t   fcc __attribute__ ( (aligned(2)) );
    u_int32_t   cb __attribute__ ( (aligned(2)) );
    u_int16_t   wFormatTag __attribute__ ( (aligned(2)) );         /* format type */
    u_int16_t   nChannels __attribute__ ( (aligned(2)) );          /* number of channels (i.e. mono, stereo...) */
    u_int32_t   nSamplesPerSec __attribute__ ( (aligned(2)) );     /* sample rate */
    u_int32_t   nAvgBytesPerSec;    /* for buffer estimation */
    u_int16_t   nBlockAlign;        /* block size of data */
    u_int16_t   wBitsPerSample __attribute__ ( (aligned(2)) );     /* number of bits per sample of mono data */
    u_int16_t   cbSize __attribute__ ( (aligned(2)) );             /* the count in bytes of the size of */
    /* extra information (after cbSize) */
} WAVEFORMATEX;

(Actually, what matters is the last field, cbSize. )
However, they don’t work.

Then how to work around this problem?

The minimum size of structure, ie. 26, can be ensured by writing it this way : 


typedef struct __attribute__ ( (__packed__)) tWAVEFORMATEX
{
    u_int32_t   fcc;
    u_int32_t   cb;
    u_int16_t   wFormatTag;         /* format type */
    u_int16_t   nChannels;          /* number of channels (i.e. mono, stereo...) */
    u_int32_t   nSamplesPerSec;     /* sample rate */
    u_int32_t   nAvgBytesPerSec;    /* for buffer estimation */
    u_int16_t   nBlockAlign;        /* block size of data */
    u_int16_t   wBitsPerSample;     /* number of bits per sample of mono data */
    u_int16_t   cbSize;             /* the count in bytes of the size of */
    /* extra information (after cbSize) */
} WAVEFORMATEX;




GNAT vs. GNATS : Crazy open source names

22 03 2008

I wanted to compile a GHDL for my PowerPC iMac G4 17″.
However, instead of compiling it for myself, I tried the MacPort to download prebuilt binaries for the necessary tools.
I knew that gnatmake is necessary. It seems to me that it is in GNAT project. So, I searched the MacPort. Oh.. there is the GNATS.
So, I downloaded it. But there was no gnatmake.

So, probably the gnats has something wrong, and decided to build for myself. Hmm.. I downloaded the GNAT. and it complains some conflicted declaration on wswap, and so on.
I didn’t want to waste my time, although I tried modifying troubled source codes at least. So, I searched where I can get the gnatmake from the MacPort.

Wow.. what I found out from searching is that GNAT is different from GNATS.

GNAT is a GNU bug tracking software, while the GNATS is the Ada compiler.

Wow…………………..
People should be careful about naming projects…





Stabs is deprecated

10 03 2008

According to Lap Cat Software Blog, the Stabs is deprecated.

So, it is time to move on to the DWARF or DWARF with dSYM.





Is Steve Jobs OK??

9 03 2008

Last night, I watched the iPhone SDK announcement. It is very brilliant and I could see the strategy behind it. There are lots of things to talk about it.

However, I would like to talk about Mr. Jobs himself. When the QuickTime broadcast started, I was surprised because how he looked. Something must be wrong with him. When a person get old, his/her muscle collapses, or diminishes. However as far as I know it happens after his/her mid 50s. Nowadays it happens even later because people eat well and they exercise well nowadays. So, I was so surprised.Well.. one thing.. I can think about it.. the cancer in pancreas…. Will it be the cause? I don’t know.. Isn’t it cured? I don’t know. One problem with western medicine is that M.Ds say very definitely, but sometimes the nature shows that it is not really. 
I hope he will be very OK. I really worry about his health and thus Apple’s sound foundation.  

In recent stock holders’ meeting, it is said that a successor to Mr. Jobs was mentioned. I doubt why…? Well.. Yeah.. probably they need to train and raise the next Mr.Jobs, or Jobs 2.0 for the future Apple. According to him, there are many brilliant people at the Apple, however I don’t think it is enough. Before, Mr. Sculley was an outstanding CEO. Dr. Amellio was said to be a quite good leader. There are more people that I remember. However they didn’t make the Apple as successful as Mr. Jobs. How good they are is one important factor to make a company great, but not all. There is something small - I mean, big - , but critical thing missing.

I wish he would be OK.Really… 





Big endian vs. Little endian : Why I prefer big endian

7 03 2008

Most of the processors are big endian : Sun sparc processorc, PowerPCs, Motorola 680×0, MIPS, and so on.

However, the one which has the most of the market share produces little endian processors, the Intel. Well, the AMD can be added to the list.

Isn’t it strange why others make big endian processors? I would like to start by defending Intel/AMD first. If a number is represented in binary, the left most part is the highest bit. It is very straight forward and reasonable. So, locating the higher part to higher address location is logical too. Probably the original x86 processor designers also thought that way.

However, in other aspect, it starts not being reasonable.

For example, let’s assume that you make a bit stream buffer and save it as a file. Let’s also assume that it is saved in the unit of 4 bytes.

Here are the data : 0×0A0B0102.

If it is saved to a file on a intel platform, it becomes 02 01 0B 0A.

If it is saved on a big endian system, it becomes 0A 0B 01 02

Now, if they are read into memory, they will be read as 0A0B0102 on the two platform. So far, there is no problem.

However let’s save 0×11223344 additionally.

On little endian systems, it is 02 01 0B 0A 44 33 22 11, while it becomes 0A 0B 01 02 11 22 33 44 on big endian systems. Because they are save in the unit of 4 bytes, they should be read into memory in the unit of 4 bytes to ensure the original semantics on little endian system.

When you make a kind of bit stream buffer, for example, you feed in additional data into the buffer. Let’s assume that you want to fill the buffer whenever there is enough empty slot in the bit stream buffer. So, someone already consumed 2 bytes from the buffer, so 2 more bytes are going to be fed into the buffer. After the first 4 bytes were read, the current file position is at the 5th bytes. You can say, “Oh.. now let’s read additional bytes.” Then from where will you read the “additional” bytes? On little endian system, it is the 8th bytes not the 5th bytes. So, you should read the next 4 bytes, and feed in the high 2 bytes into the bit stream buffer. So, to read the 4 bytes, and take out the high 2 bytes, an intermediate storage is necessary.

On the other hand, you can read actual next 2 bytes from the file on big endian systems. You don’t need any additional buffer, and you can read any data in whatever unit you want. So, to manipulate data, big endian system is more reasonable.

If we still write in assembly language, probably the x86 system would not be as popular as it does nowadays. :)





Quite often seen in somewhere but you don’t remember where..

4 03 2008

There are some predefined macros  you would want to use fequently.

However some of them are hard to find, when you want to use them but don’t remember the exact spelling.

So.. let’s briefly list it here.

  • __OBJC__
  • __i386__
  • for Intel (32-bit)

  • __x86_64__
  • for Intel (64-bit)

  • __ppc__
  • for PowerPC (32-bit)

  • __ppc64__
  • for PowerPC (64-bit)

  • __BIG_ENDIAN__
  • for Big endian CPU

  • __LITTLE_ENDIAN__
  • for Little endian CPU

  • __LP64__
  • for The LP64 (64-bit) data model





Duh….MSDN document AVIMAINHEADER vs. MainAVIHeader

3 03 2008

I don’t understand the mechanism MS people think.

Try finding AVIMAINHEADER and MainAVIHeader on the MSDN site.

They are very similar structure, but the MSDN document doesn’t explain what they are, how they are different, and when to use what.

AVI format is also very bad.

There is a so-called original AVI format, called AVI 1.0, while there is another one called, OpenDML.

They are not compatible, and there seems to be no way to detect what format a specific AVI file is stored as by looking at the beginning part of the header.

Also, OpenDML document mentions “hybrid” format, but there is no explanation on how the hybrid format looks like.

I don’t like explanation and writing style on the MSDN.

P.S. I’m sorry that I posted a few complaints recently. I want to write only technical issue, but sometimes I need to complain.





So religious fight between Windows people and Mac people

1 03 2008

What a great day!

I posted my inquiry to the Apple’s mailing list, darwin-drivers@lists.apple.com, and a USENET newsgroup, microsoft.public.development.device.drivers.

My question is how to get information on H/W to write a device driver. That question is very platform independent and specific to devices.

One of the response I got from the MS newsgroup is :

You bought a MacBook to run Windows, interesting idea. And you bought a
notebook (MacBook)because you want to write drvice drivers for those devices
(designed for Mac)not supported by Windows. Don’t quite get it. I thought
Apple is eviler than Microsoft.

Windows HC miniport is completely undocumented, good luck.

Calvin

Many developers buy Mac H/W because they can use tools which are only available or better on  one platform. There are many developers who should write their code for the two platforms, and so on. I’m one of them. The reason I want to write drivers for the MacBook is that I want to make my devices recognized, and actually I want to learn how to develop drivers.

Another response is from the Apple’s mailing list.

On Feb 29, 2008, at 1:42 PM, JongAm Park wrote:

> I’m trying to write my own mouse driver and external HDD driver for the Windows XP x64 installed on my MacBook.
> Although I understand that this mailing list, darwin-drivers@lists.apple.com, is for device driver developers for the MacOS X, I think my inquiry is common to any driver writer. So, please understand my post.

“I understand that this mailing list is not for the questions I ask, but I am going to ask them anyway”.

You’re right, it’s not.

> I found out that the MS HID-compatible device driver doesn’t work with mice connected to the MacBook on Windows XP x64. Also, My external HDD, more specifically the enclosure manufactured by the Vantek, doesn’t work with it.
> So, I decided to write my own device driver for them. ( Anyway, the reason I bought a MacBook was to study writing device drivers. )
> So, I need H/W information for the Apple’s own H/W.

Why? And what “Apple’s own H/W”?

> For example, what address are allocated for devices like the USB hub controller and so on, what I/O model they use, i.e. memory mapped I/O or port I/O, what IRP do they expect, etc.

The USB host controller works fine with your vendor-supplied driver; the “advice” you got from the “MS device driver newsgroup” is bad.

Make sure that you have the most recent BootCamp driver pack installed, and contact your operating system vendor for support. It’s what you paid them for, right?

= Mike

People say that the Mike, or Michael Smith, is a developer at the Apple.
How unprofessional response it is!!!
Does he even understand what device drivers are?
I wanted to know information on Apple’s own H/W which you should know to write device drivers. He maybe a too strong pro-Apple, but he should understand that “Windows”-installable Mac actually helps selling the Mac, and it becomes sound foundation for the Apple’s success.

I’m not going to pirate things and do hacking.

Why do they act so childish?