Fork me on GitHub

3/10/2013

[C++] Reference to Pointer

Recently, I met the usage of reference of the pointer as the function parameter. I don't know what the purpose of it, and google the answer for it.

The 1st ref. well explains the difference between "pass by pointer" and "pass by reference of the pointer". The key is the default parameter passing of C/C++ is "pass by value", evne the pointer.

The 2nd ref. shows the situation which the reference of pointer come to solve elegantly

Ref

  1. C++: Reference to Pointer

  2. why pointer to pointer is needed to allocate memory in function

11/23/2012

[Android] Pack 3rd-party shared library .jar/.so into apk by Android.mk

When building android application package by Eclipse, the shared library placed in libs/armeabi* will be packed into apk automatically.
If the building process is from android source tree, the Android.mk needs to be written. So, how to pack 3rd-party shared library .jar/.so into apk by Android.mk?
For .jar
# the name is just a name, it will map to real .jar later
LOCAL_STATIC_JAVA_LIBRARIES := refJar1 \
                               refJar2

# here maps to actual .jar location
LOCAL_PREBUILT_STATIC_JAVA_LIBRARIES := refJar1 :libs/oooo.jar\
                                        refJar2 :libs/xxxx.jar
For .so
# Put .so to out/target/product/***/obj/lib
$(shell cp $(wildcard $(LOCAL_PATH)/libs/armeabi/*.so) $(TARGET_OUT_INTERMEDIATE_LIBRARIES)) 

LOCAL_JNI_SHARED_LIBRARIES := libs/libxxxx

References

  1. 在apk裡打包進.so文件的方法: http://blog.csdn.net/androidboy365/article/details/6772890
  2. Android build system分析: http://blog.csdn.net/ccskyer/article/details/6122963
  3. android 內置app編譯方法及Android.mk中的系統變量說明: http://bbs.ancode.org/forum.php?mod=viewthread&tid=86
  4. Android NDK開發指南---Android.mk文件: http://hualang.iteye.com/blog/1140414
  5. Android Application, Android Libraries and Jar Libraries: http://devmaze.wordpress.com/2011/05/22/android-application-android-libraries-and-jar-libraries/

11/06/2012

[OpenGL] RedBook example environment setup on Mac

This post records how to setup the OpenGL programming guide source example environment on Mac.

  • Install XCode OpenGL and GLUT come with the OS and Xcode installations. To verify, check for
     /System/Library/Frameworks/{OpenGL,GLUT}.framework
    
  • In your OpenGL source files, include the following line

      #include <OpenGL/gl.h>
      #include <OpenGL/glu.h>
      #include <GLUT/glut.h>
    
  • To make a GLUT application on the command line, use the following linker options:

     -framework OpenGL -framework GLUT
    

For Ubuntu, you can refer this: Setting up an OpenGL development environment in Ubuntu Linux

--EOF--

[SimpleCV] SimpleCV environment setup on Mac

This post records the process to setup the SimepleCV environment on MacOSX 10.8.2. The source code of SimpleCV is hosted on the Github. Below steps mostly refers to this project.
  • Install Xcode (remember to install Command-Line Tools)
  • Install Homebrew
    $ ruby -e "$(curl -fsSkL raw.github.com/mxcl/homebrew/go)"  
    
    // Add the Homebrew directory to your system path  
    _export PATH=/usr/local/bin:$PATH_ to .bash_profile
    
  • Install Python
    $ brew install python --framework --universal 
    
    // Add /usr/local/share/python to your system path   
    export PATH=/usr/local/share/python:$PATH  
    
    // change Lion's symlink to point at your new Python install  
    $ cd /System/Library/Frameworks/Python.framework/Versions  
    $ sudo rm Current  
    $ ln -s /usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/Current  
    
    // To verify that your installation went as planned, type which python at the command line. You should see /usr/local/bin/python in response
  • Use Homebrew to install git, opencv
    $ brew install git  
    $ brew install opencv
    
  • Install the dependent libraries
    $ brew install sdl sdl_image sdl_mixer sdl_ttf smpeg portmidi  
    $ ARCHFLAGS="-arch i386 -arch x86_64" brew install PIL
    
  • Make symbolic links
    $ sudo ln -s /usr/local/lib/python2.7/site-packages/cv.so /Library/Python/2.7/site-packages/cv.so
    
    $ sudo ln -s /usr/local/lib/python2.7/site-packages/PIL /Library/Python/2.7/site-packages/PIL
    
    $ sudo ln -s /usr/local/lib/python2.7/site-packages/cv2.so /Library/Python/2.7/site-packages/cv2.so
    
    $ sudo ln -s /usr/local/lib/python2.7/site-packages/cv.py /Library/Python/2.7/site-packages/cv.py
    
  • Install pip (pip installs packages)
    $ sudo easy_install pip
    
  • Install NumPy, SciPy, IPython
    $ pip install numpy
    
    $ brew install gfortran
    $ pip install -e git+https://github.com/scipy/scipy#egg=scipy-dev  
    
    $ pip install ipython
    
  • Intall Mercurial
    $ brew install mercurial
    
  • Install PyGame
    $ sudo pip install hg+http://bitbucket.org/pygame/pygame
    
  • Install SimpleCV
    $ pip install https://github.com/ingenuitas/SimpleCV/zipball/master 
    
  • Install readline
    $ sudo easy_install readline
    
  • Start the SimpleCV shell
    $ simplecv
    

--EOF--

8/11/2012

[Linux] tweak process priority

在 debug vsync 的過程中,發現 CPU scheduling 對於 vsync accuracy 有影響,因此需要針對 vsync 所在的 process 修改其 process priority.
過程中發現利用 pthread 的 pthread_attr_t 並沒有辦法更動 priority,以下整理了網路上關於 process priority 的資料,目前猜測原因應該是:
Setting a priority under the default scheduling policy (SCHED_OTHER) isn't valid. The default Linux scheduling policy is SCHED_OTHER, which have no priority choice but a nice level to tweak inside the policy
由上面節錄的敘述中可以發現兩個陌生的東西:"scheduling policy" 和 "nice value". 來認識他們:
All Linux threads have one of the following scheduling policies:
  1. 'Normal' scheduling policies: ranging from 100 (highest priority) to 139 (lowest priority)
    SCHED_OTHER   the standard round-robin time-sharing policy;
    SCHED_BATCH   for "batch" style execution of processes; and
    SCHED_IDLE    for running very low priority background jobs.
    
  2. Real-time scheduling policies: ranging from 1 (lowest priority) to 99 (higest priority)
    SCHED_FIFO    a first-in, first-out policy
    SCHED_RR      a round-robin policy
    
Real-time scheduling 有什麼特性呢,簡單理解應該就是擁有比 Normal scheduling 更不可被搶佔的特性:
SCHED_FIFO can't be preempted (context switched to another process) unless another process of higher priority shows up in the execution queue.

SCHED_RR can be preempted by a time quantum (delay given to a process to execute).
一般的 process 沒有特別指定 default 應該都是 SCHED_OTHER 所以沒辦法透過 pthread_attr_t 設定其 priority,不過還有另外一個方法就是使用其 nice value.
Conventional process's static priority = (120 + Nice value)
因為 Normal scheduling policies 的 priority range 是從 100 ~ 139,那麼從上面可以推得,nice value 的 range 是 -20(highest priority) ~ 19(lowest priority).
至於設定 nice value 的方式似乎不只一種,經過試驗,setpriority 這個 function 是有效的。

Ref

  1. How to increase thread priority in pthreads?
  2. how to increase the priority of a child pthread relative to the parent thread
  3. Understanding Linux CPU scheduling priority
-- EOF --