C++: Disallow evil constructors.

I have not write C++ code for years, recently because some reason, also because c++11, have start touch c++.

Today found a very funny macro in google’s android c++ code, it maybe very populary in c++ world, but I still want to write some about this.

The macro is like this:

From the comment, you can see this macro declear the copy constructor and assign operator.

If you want to disabllow these two “help” function generated by c++ complier, you can do the macro the private, like this:

This macro will do this for you, generally, you want do this in all you c++ class, except you really need it.

This macro was in many project, like chromium, regex-re2, skia, srec, webrtc, android.

 

Handy One-liners for AWK

This post copy a very handy text about awk, for I can reference in further.
The copyright belongs to the origin auther: Eric Pement.

 

 

最近Google搜索打不开?看这里

最近由于一些总所周知的原因,大陆地区的Google的搜索结果打不开(或者说很难打开)了。 原因是因为DNS污染之类的, 详见: http://www.williamlong.info/archives/2263.html

解决办法倒是有一些的,针对不同的浏览器, 下载不同的插件去掉Google搜索结果打开需要的Google Forward 的URL即可:

Chrome: 这里:https://chrome.google.com/webstore/detail/remove-google-redirects/ccenmflbeofaceccfhhggbagkblihpoh

Opera: https://addons.opera.com/en/extensions/details/remove-google-redirects/

Safari: 无。我已经提交了一个插件,希望能够尽快出现

Firefox: 无, 这个有时间也弄一个。

这个插件的原理就是去掉Gogole Forward的URL, 让你直接打开你搜索到的结果, 这样没有被墙的内容还是可以打开的。

Rails: Dynamic Finders

 

 

Rails’s ActiveRecord can use a dymatic way to finder

Like this model class in rails:

It don’t have much query function, but you can use query function like:

It because it use Ruby’s method_missing functionality to automatically create methods that don’t exist until runtime. Using dynamic finders, you can include the attribute you’re looking for directly in the method name.

There are several variations of dynamic finders, which are summarized in the Table:

 
Finder Example
find_by_*(cond) find_by_title(‘Hello’) #=>; Article
find_all_by_*(cond) find_all_by_title(‘Hello’) #=>; Array
find_by_*_and_*(cond1, cond2) find_by_title_and_published_on(‘Hello’, ’2012-03-12′) #=>;Articlelike creating a record. You can update attributes one at a time and
then save the result, or you can update attributes in one fell swoop. When you update a
record, a SQL UPDATE statement is constructed behind the scenes. First, you use a find
operation to retrieve the record you
find_all_by_*_and_*(cond1, cond2) find_all_by_title_and_published_on(‘Hello’, ’2012-03-12′) #=>;Array

Amazing…

 

ARM V6 和V6+架构中的原子指令

ARM V6

ARM V6中原子指令是:

SWP 指令

该指令可以实现寄存器和内存之间的原子交换, 但是由于该指令要lock住BUS, 所以会影响系统性能.
例子:
所以在内核中, V6以前的架构, atomic_add/sub 之类的指令都是用

来实现的.

ARM V6 以后, V7, V8中

提供LDREX和STREX来替换SWP指令, 实现对原子的内存操作.
所以Linux内核就可以利用这个指令来做原子操作, 内核中的atomic_add 实现就是
下面是一些ARM V6+架构对该指令的内部实现,(参考:http://www.keil.com/support/man/docs/armasmref/armasmref_Cihbghef.htm)

LDREX loads data from memory.If the physical address has the Shared TLB attribute, LDREX tags the physical address as exclusive access for the current processor, and clears any exclusive access tag for this processor for any other physical address.

Otherwise, it tags the fact that the executing processor has an outstanding tagged physical address.

(如果物理地址在TLB中, 那么LDREX就标识该地址为当前CORE的排他使用(exclusive access), 而且清楚掉该Core对其他的地址的exclusive access.
如果不在TLB中, 那么就标志当前CPU有一个outstanding tagged 物理地址, 我想应该是一个特殊的物理地址访问的意思.)

STREX performs a conditional store to memory. The conditions are as follows:If the physical address does not have the Shared TLB attribute, and the executing processor has an outstanding tagged physical address, the store takes place, the tag is cleared, and the value 0 is returned in Rd.

(如果该物理地址没有share TBL 属性, 也就是没有load到TLB中, 当前执行的CPU有一个”outstanding tagged physical address”, 那么存储会直接存进去, 清除tag, 然后再Rd里面返回0)

    If the physical address does not have the Shared TLB attribute, and the executing processor does not have an outstanding tagged physical address, the store does not take place, and the value 1 is returned in Rd. (如果物理地址没有在TBL中, 而且当前执行CPU没有一个outstanding tagged physical address, 那么这次store不发生, 并且返回1在Rd中).If the physical address has the Shared TLB attribute, and the physical address is tagged as exclusive access for the executing processor, the store takes place, the tag is cleared, and the value 0 is returned in Rd.

(如果物理地址已经有一个TLB的属性, 而且该物理地址已经被标识为exclusive access, 那么存储发生, 并且清掉TAG, 在Rd中返回0)

If the physical address has the Shared TLB attribute, and the physical address is not tagged as exclusive access for the executing processor, the store does not take place, and the value 1 is returned in Rd.
(如果该地址已经有TLB的属性, 但是物理地址没有tag成exclusive access, 存储不发生, 并且在Rd中返回0.

Blog终于走上了独立的道路

最近想开始写东西了,可惜appspot很不给力, 由于被评论机器人看上了, 被评论了20,000多个, 总是一会就不能用了。

想想也好, 也想要开始写一些网络应用,租一个VPS还是比较值得的。 用上了正宗的Wordpress, 也比以前的山寨货好用多了。

Powered by Qumana

Time Machine Eat My HD…

今天忽然发现自己的MBP上面的320G的硬盘用完了, 慢的叫个没话说。
可是东西感觉没安多少, 基本上就是iTune U上面下了很多视频, 但是也不至于把硬盘全部用完把。
所以还是先到根目录下面找找是什么占了内存了:
  

惊讶的发现删掉的很多视频竟然出现在 /.MbileBackups 里面。
计算了一下大小:
发现竟然有48G, My God, 必须回收回来。。
Google了一会发现原来发现这个目录是Time Machine用来做本地备份的,
Apple considered to implement a new “feature” to Lion that no one seems to be aware of: Local Backups if your Time Machine Drive is not connected to you computer. It seems to affect only Mac Laptops, not the desktop Macs. So every time your Time Machine backup starts without the destination drive it will copy all data in a secure folder on your local startup volume called “.MobileBackups”.
已经有很多天备份了, 而且已经把Music目录排除出去了,估计这些文件是在很早以前下载的。 于是查到了通过这个命令关掉这些本地备份, 重启以后就会全部删掉。
然后发现重启以后就多了48G空间
不过Time Machine还是有必要的,于是又重新打开

adb & gdbserver: remote gdb debugging under android

 

Most of operations are reference from this link, but with some more refine, and detail.

http://www.kandroid.org/online-pdk/guide/debugging_native.html

This page show how to use gdb in android env, it will use adb to forward tcp to gbd port, so no network needed, and also show how to load the symbol information from an android build dir, also use sshfs to mapping the build env in server to local PC.

first, the commands in this page:

# the command under target board, adb
$ the command on host.
(god) the command under gdb shell.

to debug use gdb, tell adb forward all tcp to gdb port.

$ adb forward tcp:5039 tcp:5039

start gdb server at board.
# gbdserver :5039 –attach 257 & 
or 
# gdbserver :5039 /system/bin/surfacefligner

start debugging
$ prebuilt/linux-x86/toolchain/arm-eabi-4.4.3/bin/arm-eabi-gdb out/target/product/sabresd_6q/symbols/system/bin/surfaceflinger

(gdb) set solib-absolute-prefix /home/xxxx/proj/ics/out/target/product/sabresd_6q/symbols/
(gdb) set solib-search-path /home/xxxx/proj/ics/out/target/product/sabresd_6q/symbols/system/lib
(gdb) target remote :5039

// don’t let other scheduler during your debugging.
(gdb) set scheduler-locking on

//  continue can start remote debugging.
(gbd) continue

Tips:

0. use sshfs to remap the build dir to your local PC:

//  use adb require a usb cable connect to your board, so for some guy build the code on server, you can use sshfs to let all file system in server mapping to your PC.
// in my case, i use 

sshfs  b33xxx@10.192.224.xx:/home/b33xxx/  ~/shdroid1

start gdb command become:
prebuilt/linux-x86/toolchain/arm-eabi-4.4.3/bin/arm-eabi-gdb /home/xxx/shdroid1/proj/ics/out/target/product/sabresd_6q/symbols/system/bin/surfaceflinger

and the path in gbd ’s input is become:

set solib-absolute-prefix /home/xxx/shdroid1/proj/ics/out/target/product/sabresd_6q/symbols/
set solib-search-path /home/xxx/shdroid1/proj/ics/out/target/product/sabresd_6q/symbols/system/lib

1. how to keep gdb run
modify init.freescale.rc, change this line from:
setprop persist.sys.usb.config mtp
–> setprop persist.sys.usb.config gdb

2. How to let gdb load all symbol

gdbserver will stop at linker:begin.S when start,   before continue, you should set a break point at main() function, after you stop at main, the all symbol will loaded, you can set break point by function’s name
(gdb) b main
(gdb) continue
<— after stop at main —>
b SurfaceFlinger::publishAndJoinThreadPool() # set break point by function name

3. how to restart debuging, restart debugging like this can keep the break points.

(gbd) kill 
# gdbserver xxxx // needs to restart server
(gbd)  target remote :5039    // reconnect.
(gbd) set scheduler-locking on

4. how to extract the stack dump quickly,

(going to android build dir)
$ . build/envsetup.sh
$ lunch 
( choose your product)
(make sure this was full build dir)

run 
$ ./development/scripts/stack
when you see this:
Reading native crash info from stdin
you can copy the stack dump from logcat, begin with 

I/DEBUG   ( 2167): *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***

such as:

I/DEBUG   ( 2167): *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***

I/DEBUG   ( 2167): Build fingerprint: ‘freescale/sabresd_6q/sabresd_6q:4.0.3/R13.2-rc3/eng.b33651.20120327.172909:eng/test-keys’

I/DEBUG   ( 2167): pid: 2649, tid: 2649  >>> /system/bin/surfaceflinger <<<

I/DEBUG   ( 2167): signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 002e0064

I/DEBUG   ( 2167):  r0 ffffffff  r1 002e0064  r2 401167d0  r3 0066791c

I/DEBUG   ( 2167):  r4 ffffffff  r5 002e0064  r6 bec42a50  r7 40208e85

I/DEBUG   ( 2167):  r8 bec429f0  r9 00000a59  10 000003e8  fp 00000000

I/DEBUG   ( 2167):  ip 40196078  sp bec429b8  lr 4004d0e8  pc 4004d0e8  cpsr 00000010

I/DEBUG   ( 2167):  d0  0000000000000000  d1  0000000000000000

I/DEBUG   ( 2167):  d2  0000000000000000  d3  0000000000000000

I/DEBUG   ( 2167):  d4  0000000000000000  d5  0000000000000000

I/DEBUG   ( 2167):  d6  3f80000000000000  d7  000000003f800000

I/DEBUG   ( 2167):  d8  0000000000000000  d9  0000000000000000

I/DEBUG   ( 2167):  d10 0000000000000000  d11 0000000000000000

I/DEBUG   ( 2167):  d12 0000000000000000  d13 0000000000000000

I/DEBUG   ( 2167):  d14 0000000000000000  d15 0000000000000000

I/DEBUG   ( 2167):  d16 41a93d96b0b645a2  d17 3f50624dd2f1a9fc

I/DEBUG   ( 2167):  d18 41c5dae992000000  d19 0000000000000000

I/DEBUG   ( 2167):  d20 0000000000000000  d21 0000000000000000

I/DEBUG   ( 2167):  d22 0000000000000000  d23 0000000000000000

I/DEBUG   ( 2167):  d24 0000000000000000  d25 0000000000000000

I/DEBUG   ( 2167):  d26 0000000000000000  d27 0000000000000000

I/DEBUG   ( 2167):  d28 0000000000000000  d29 0000000000000000

I/DEBUG   ( 2167):  d30 0000000000000000  d31 0000000000000000

I/DEBUG   ( 2167):  scr 00000010

I/DEBUG   ( 2167):

I/DEBUG   ( 2167):       #00  pc 000050e8  /system/lib/libcutils.so (android_atomic_add)

I/DEBUG   ( 2167):       #01  pc 0001a058  /system/lib/libutils.so (_ZNK7android7RefBase9decStrongEPKv)

I/DEBUG   ( 2167):       #02  pc 00019218  /system/lib/libbinder.so (_ZN7android2spINS_21IPermissionControllerEED1Ev)

I/DEBUG   ( 2167):       #03  pc 0001b1fa  /system/lib/libbinder.so (_ZN7android14IPCThreadState14executeCommandEi)

I/DEBUG   ( 2167):       #04  pc 0001b3c6  /system/lib/libbinder.so (_ZN7android14IPCThreadState14joinThreadPoolEb)

I/DEBUG   ( 2167):

I/DEBUG   ( 2167): code around pc:

I/DEBUG   ( 2167): 4004d0c8 e1853f92 e3530000 1afffffa e8bd8070  .?….S…..p…

I/DEBUG   ( 2167): 4004d0d8 e92d4070 e1a04000 e1a05001 ebffffd1  p@-..@…P……

I/DEBUG   ( 2167): 4004d0e8 e1950f9f e0802004 e1853f92 e3530000  ….. …?….S.

I/DEBUG   ( 2167): 4004d0f8 1afffffa e8bd8070 e1a01000 e3e00000  ….p………..

I/DEBUG   ( 2167): 4004d108 eafffff2 e1a01000 e3a00001 eaffffef  …………….

I/DEBUG   ( 2167):

I/DEBUG   ( 2167): memory map around addr 002e0064:

I/DEBUG   ( 2167): 00009000-0000a000 /system/bin/surfaceflinger

I/DEBUG   ( 2167): (no map for address)

I/DEBUG   ( 2167): 00662000-00668000 [heap]

I/DEBUG   ( 2167):

I/DEBUG   ( 2167): stack:

I/DEBUG   ( 2167): bec42978  bec42a20  [stack]

I/DEBUG   ( 2167): bec4297c  bec429f0  [stack]

I/DEBUG   ( 2167): bec42980  00000004  

I/DEBUG   ( 2167): bec42984  bec42a20  [stack]

I/DEBUG   ( 2167): bec42988  00000a59  

I/DEBUG   ( 2167): bec4298c  000003e8  

I/DEBUG   ( 2167): bec42990  00000000  

I/DEBUG   ( 2167): bec42994  4010ed0d  /system/lib/libsurfaceflinger.so

I/DEBUG   ( 2167): bec42998  00000010  

I/DEBUG   ( 2167): bec4299c  00000004  

I/DEBUG   ( 2167): bec429a0  bec42a20  [stack]

I/DEBUG   ( 2167): bec429a4  bec429f0  [stack]

I/DEBUG   ( 2167): bec429a8  0066791c  [heap]

I/DEBUG   ( 2167): bec429ac  00000004  

I/DEBUG   ( 2167): bec429b0  df0027ad  

I/DEBUG   ( 2167): bec429b4  00000000  

I/DEBUG   ( 2167): #00 bec429b8  002e0064  

I/DEBUG   ( 2167): bec429bc  00667f68  [heap]

I/DEBUG   ( 2167): bec429c0  bec42a50  [stack]

I/DEBUG   ( 2167): bec429c4  4018205b  /system/lib/libutils.so

I/DEBUG   ( 2167): #01 bec429c8  bec42a50  [stack]

I/DEBUG   ( 2167): bec429cc  00000000  

I/DEBUG   ( 2167): bec429d0  4021932c  /system/lib/libbinder.so

I/DEBUG   ( 2167): bec429d4  4020a21b  /system/lib/libbinder.so

then you type CTRL-D to tell the script EOF,

then you will see the full stack dump with source and lines like this;

Reading symbols from /home/b33651/proj/ics/out/target/product/sabresd_6q/symbols

pid: 2649, tid: 2649  >>> /system/bin/surfaceflinger <<<

signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 002e0064

 r0 ffffffff  r1 002e0064  r2 401167d0  r3 0066791c

 r4 ffffffff  r5 002e0064  r6 bec42a50  r7 40208e85

 r8 bec429f0  r9 00000a59  10 000003e8  fp 00000000

 ip 40196078  sp bec429b8  lr 4004d0e8  pc 4004d0e8

Stack Trace:

 RELADDR   FUNCTION                                        FILE:LINE

 000050e8  android_atomic_add+16                           /home/b33651/proj/ics/system/core/include/cutils/atomic-arm.h:158

 0001a058  android::RefBase::decStrong(void const*) const+12  /home/b33651/proj/ics/frameworks/base/libs/utils/RefBase.cpp:351

 v——>  ~sp                                             /home/b33651/proj/ics/frameworks/base/include/utils/StrongPointer.h:149

 00019218  android::sp<android::IMemoryHeap>::~sp()+20     /home/b33651/proj/ics/frameworks/base/include/utils/StrongPointer.h:149

 0001b1fa  android::IPCThreadState::executeCommand(int)+538   /home/b33651/proj/ics/frameworks/base/libs/binder/IPCThreadState.cpp:1029

 0001b3c6  android::IPCThreadState::joinThreadPool(bool)+186  /home/b33651/proj/ics/frameworks/base/libs/binder/IPCThreadState.cpp:468

Stack Data:

 ADDR   VALUE FILE:LINE/FUNCTION

 bec42978  bec42a20  

 bec4297c  bec429f0  

 bec42980  00000004  

 bec42984  bec42a20  

 bec42988  00000a59  

 bec4298c  000003e8  

 bec42990  00000000  

 bec42994  4010ed0d  

 bec42998  00000010  

 bec4299c  00000004  

 bec429a0  bec42a20  

 bec429a4  bec429f0  

 bec429a8  0066791c  

 bec429ac  00000004  

 bec429b0  df0027ad  

 bec429b4  00000000  

 bec429b8  002e0064  

 bec429bc  00667f68  

 bec429c0  bec42a50  

 bec429c4  4018205b  

 bec429c8  bec42a50  

 bec429cc  00000000  

 bec429d0  4021932c  

 bec429d4  4020a21b  

#####—————————-
set solib-absolute-prefix /home/kzj/shdroid1/proj/ics/out/target/product/sabresd_6q/symbols/
set solib-search-path /home/kzj/shdroid1/proj/ics/out/target/product/sabresd_6q/symbols/system/lib

 

谈谈closure

 

closure是functional programming中的很重要的一个概念,Scheme首创这个概念, 大致可以理解为一个东西可以以自己为开始并且结束, 最常见的形式莫过于回调函数了。这句话很难理解。

 

举个生活中的例子:

 

刚来上海的时候, 经常到一家小饭馆吃饭, 发现这个饭馆点菜很有方法。 这是一个小饭馆, 每个人点菜都不是很多, 点菜再前台, 点完以后拿到小票给服务员, 服务员走到桌子前, 打气桌子上写着桌号的小夹子加载小票上, 一起交给厨师。

 

然后上菜的时候另外一个服务员就可以通过夹子找到客人, 然后在把夹子放在桌子上。

 

全过程中, 这个菜单和夹子就是一个object, 它自我闭包, 不需要任何起其他记录(全局变量), 就可以完成完全不知道的两个服务员正确的送菜(多线程)。

 

从这个例子可以看到, 这个点菜过程完全是闭包的, 其中的对象可以说就是这个夹子(夹子上有桌号和菜单)。正因为这种closure, 这个饭馆可以随时更换增加和减少服务员和厨师数量的数量, 每个厨师和服务员都不需要记住任何客人的地方和菜单。只要这个夹子就可以知道所有上菜与做菜的东西。

 

其实在高度并发的环境下,增加程序的闭包性是最优雅,也是最快的解决办法。不需要上下文, 不需要复杂的记录object的数据, 完全由并发的个体自我完成。 

 

 

Kindle Touch几天来的感受

Kindle Touch 已经入手一个星期了,对于一个书呆子来说,这个东西的魅力绝对是无法形容的。

它比纸书轻也方便带,直接放在上衣口袋里面就行了,也不会因为带着一本厚厚的书在地铁上拿出来招来异样的眼神。也不会像看其他的屏幕那样半个小时以后眼睛都睁不开,估计没看几本书眼镜就又多了100度闪光。

e-ink的屏幕看书的感觉和纸质的书几乎完全一样,但是有几个纸质书无法达到的地方:

  • 可以做标记, 我们看书的时候总是喜欢把自己喜欢的段落用笔勾挂下来, 可是这一本书看完勾画下来的那些难道还要再抄一遍么, 所以大部分的勾画都只是留在了书上,等待着下一个看这本书的人。而在Kindle上, 每本书,或者整个Kindle上面做过的标记, 都可以很容易的找出来。
  • 字典, 看英文书的最大困难就是有些单词不知道什么意思, 对于纸质书,你可能要打开你的iphone, 电脑, 文曲星?来查查什么意思, 我不知道这样10次下来你还能不能坚持继续看这本书了, 这种打断对于阅读是不好的, 而Kindle很好的解决了这个问题。 你只需要一直按着你不懂的那个单词, 辞典就会自动跳出来, 这也是我买touch版的原因。 辞典可以是自带的英英牛津辞典,也可以是自己安装的英汉辞典。所以说看英文书前所未有的好感。
  • 推送, 有人不喜欢拿着usb线拷贝文件那样的copy书的方式, 我也不喜欢。 所以kindle有一个功能,解决了这个问题。 在你注册了Amazon帐号以后, 你会得到一个Kindle的邮箱, 有了这个邮箱, 你可以把你想阅读的东西, 无论是什么格式的,发送到这个邮箱。 然后你会发现你的Kindle会自动下载这个文章, Amazon会为你转换格式, 所以也免去了转换格式这个麻烦的过程。 不过你要开wifi或者3g哦。
  • 购物, 如果说你想看原版小说, 而又不想看那些过时的东西, 你可以花一些钱在Amazon上购买Kindle版本的书籍, 其实小说这些书都不贵, 0.99$ 1.99$的比较多, 这个价钱也就是一个红薯的价钱(大一点的红薯)。不过我一般都是下下来Sample(免费的哦)看看, 想买的都比较贵, 不想买的看看Sample就行了。

可惜现在并没有可以买的电子书下载, 那些在LCD上看的不算。 豆瓣读书频道似乎有一些动作,不过还在起步阶段。如果说按美国的这个折扣卖电子书,小说几本都是1折或者2折, 专业书几本是对折, 我还是希望买的, 网上花时间下载所谓的pdf或者是阴影版实在是太奢侈了,浪费时间的说。 

最后推荐一本书:

美国宪政历程-影响美国宪政的25个司法大案 (任东来 白雪峰 陈伟)