顯示具有 Coding 標籤的文章。 顯示所有文章
顯示具有 Coding 標籤的文章。 顯示所有文章

2010年12月12日 星期日

Population Count of a Uint32

When studying Android audio system, I saw a interest function code in google’s AndioSystem class:

uint32_t AudioSystem::popCount(uint32_t u)

{

    u = ((u&0x55555555) + ((u>>1)&0x55555555));

    u = ((u&0x33333333) + ((u>>2)&0x33333333));

    u = ((u&0x0f0f0f0f) + ((u>>4)&0x0f0f0f0f));

    u = ((u&0x00ff00ff) + ((u>>8)&0x00ff00ff));

    u = ( u&0x0000ffff) + (u>>16);

    return u;

}

Honestly, I cannot understand how this code come from. Fortunately, the code leave a reference link for us: http://www.df/lth/se/~john_e/gems/gem002d.html.

And telling the trust again, for me, it’s still hard to understand after reading the reference algorithm. So, I decide to make a node, and just copy the code when I need it ~ 吐舌頭.

2010年11月14日 星期日

Print enumeration name in C, C++

Print out enumeration name as string is useful for debugging, but I does not how to do it until I read a sample code from MSDN on last week. That’s really shame for being an engineer (害羞).

2010年10月26日 星期二

Cemail.vol backup / restore

The original post is on MSDN develop forums, cause the post there sometimes get lost, and the coding style is totally crash by HTML. So, I made one copy here for backup and clear the coding style for easy read.

Below link it the original post:

http://social.msdn.microsoft.com/Forums/en-US/vssmartdevicesnative/thread/d1d8c95a-9366-4acf-8bac-b6862bfc2f79

This post share source code of two functions: BackupMain() and RestoreMain() to backup and restore cemail.vol, The idea is very simple.

2010年10月15日 星期五

Enumerate database in volume

As I said in [Windows Mobile (CE) Database Study], the terminology of CEDB and EDB is almost killing me. The topic is enumerate  tables in database, if using all other common database engines.

I cannot find any document descripts CEDB and EDB which one used in Windows Mobile 6.1 (my test device). But I cannot use EDB only APIs on my develop environment (VS2008 + WM 6.1 SDK) , so the sample code here is using CEDB.

Here is the code piece, follow the sample code in [Enumerate DB Volumes], we got the DB volume’s CEGUID (in variable ‘guid’) and then try to enumerate all database (tables) in that volume.

HANDLE hFind = CeFindFirstDatabaseEX(&guid, 0);
CEOID oid = 0;
while ((oid = CeFindNextDatabaseEx(hFind, NULL)) != 0) 
{
        CEOIDINFOEX oidinfo;
        memset(&oidinfo, 0, sizeof(CEOIDINFOEX));
        oidinfo.wVersion = CEOIDINFO_VERSION;

        if ((CeOidGetInfoEx2(&guid, oid, &oidinfo))
        {
                /// TODO: print out oidinfo.infDatabase.szDbaseName
        }
}
CloseHandle(hFind);

Back to enumeration, you can use CREATE_INVALIDGUID to initialize variable 'guid’, then the code will enumerate all database (tables) in all mounted volume (I am not sure the order)..

And you can specified a PCEGUID in the second parameter of CeFindNextDatabaseEx() function to get the volume’s GUID which the database belongs to.

Below is the enumeration of my device, just for reference.

VOL: \Documents and Settings\default.vol
        ScheduleU
        ScheduleS
        ScheduleGroupU
        ScheduleGroupS
VOL: \ReplStorVol
        ReplStor
VOL: \mxip_notify.vol
        DB_notify_queue
        DB_notify_events
VOL: \mxip_swmgmt.vol
        SwMgmtMetadataStore
VOL: \cemail.vol
        fldr3100001d
        pmailVolumes
        pmailNamedProps
        pmailMsgClasses
        pmailOldTables
        pmailMsgs
        pmailFolders
VOL: \mxip_system.vol
        \ConfigMetabase
VOL: \mxip_lang.vol
        \MetabaseOptions
        \MetabaseLabels

2010年10月14日 星期四

Enumerate EDB volumes

I wrote a test application to enumerates all EDB volumes on my WM6.1 devices. Here is the code piece:

CEGUID guid;
DWORD  dwErr;
WCHAR  wszMsg[255];
WCHAR  wszName[255];

CREATE_INVALIDGUID(&guid);

while (CeEnumDBVolumes(&guid, wszName, 255))
{
        StringCchPrintfW(
                wszMsg, 255, L"VOL: %s\n", wszName);
        OutputDebugStringW(wszMsg);
}

if (GetLastError != ERROR_NO_MORE_ITEMS)
{
        OutputDebugString(L”Unexpected stop\n”);       
}   

According the MSDN, the size of wszName should be enough to CEDB_MAXDBASENAMELEN. But actually I got ERROR_INSUFFICIENT_BUFFER error when using that size.

After increase the size to 255, the enumeration finish normally, and here is the output in my device.

VOL: \Documents and Settings\default.vol
VOL: \ReplStorVol
VOL: \mxip_notify.vol
VOL: \mxip_swmgmt.vol
VOL: \cemail.vol
VOL: \mxip_system.vol
VOL: \mxip_lang.vol

It’s more than I expected….嚎啕大哭

And notice the first one, the length is absolutely larger then CEDB_MAXDBASENAMELEN (32), that means the reference guide of CeEnumDBVolumes() was wrong.

Back to the list I got, there are more than that when I searching the *.vol files on root file system. Cause CeEnumDBVolumes() only list the mounted database volumes.

And of course, the volumes listed has been locked after my mobile device boot up, there is a long road for me to understand how to release them for backup/restore purpose.

2010年10月11日 星期一

Fstream read performance issue on WinCE

I meet this issue when develop a window mobile application which needs huge file IO operations on our company's handheld device. Until now, I still don't know this issue cause by official windows mobile STD library or our company's OS image only.

The target application randomly read binary files like a RO-database. I using STD ofstream API to read data from file and found the performance was badly. I try to debug this performance issue on that application and found that the root cause is fstream API.

I made a 100MB dummy file and one simple application to read the file sequentially (a for-loop), the total ready time using fstream is almost ten times of another one using fopen/fread interface.

I am lazy to test the same behavior on other company's device (actually, I don't have other device….), so I just re-write the code to use fread() API and the problem solved.

PS. I got the same behavior on two models of our company's handheld device, and they are use Window Mobile 6.1 system.

2010年9月20日 星期一

ACTION_UP missing?

I am try to implement a custom VIEW for practice, and I got a strange issue: I want to handle the ACTION_UP event in onTouchEvent() method, but I cannot see the ACTION_UP event.

After some testing, I found the root course of this issue: ACTION_DOWN event must be handled before you received the ACTION_UP event.

Cause my view does not need to care about ACTION_DOWN, so I just put ACTION_UP check and return TRUE for it, otherwise return super.onTouchEvent() instead.

Seems Android makes a assumption that if you don’t care about ACTION_DOWN, you will not need ACTION_UP. And that’s why I cannot get the ACTION_UP event.

After I add a ACTION_DOWN checking and return TRUE, I can receive ACTION_UP as normal.

I don’t know if my guess is correct, I cannot find any document mention this assumption. But that’s the behavior I can see in my test code, and that really solve my issue.

Layout 元件的大小單位轉換

Android 上有幾個在 layout 上可用的單位:

px

Pixels - 對應到螢幕的幾個像素(pixel)

in

Inches - 對應到實際的距離為幾呎

pt

Points - 通常用在 font size,一個 point 為 1/72 inch

換算公式:pt = px * dpi /72

dp (dip)

Density-Independent Pixels - 對應到在 160 dpi 的螢幕上的幾個像素,為 android 用來處理支援不同大小的螢幕的 layout 單位,詳細定義可以參考 android SDK 的文件。在 android 的 layout 中,應該都使用 dp 來給定元件大小,而不應使用 px。

換算公式:dp = px * dpi / 160

sp

Scale-Independent Pixels - 跟 dip 一樣,加上 pt 的觀念。簡單的說,就是對應在 160 dpi 的螢幕上的幾個 pt。在 android 中,文字大小應使用 sp 而非 pt。

換算公式:sp = pt * dpi / 160

換算公式:sp = dp * dpi / 72


舉個例子說明:你有一個 49 dp 的按鈕,在 120 dpi 的螢幕上,裡面的文字大小最大應為: 49 * 120 / 160 = 36.75 sp。