Skip navigation

Lost password? | Help

Enthsiasm about what?

Shine in my world

My reverse string code in C

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void reverserWord(char* string, int strlength);

int main(int argc, char* argv[])
{
        char* srcstring = "I will earn bigger money tomorrow";
        char* outstring = NULL;
        char tmp;
        char* cptr;
        int strlength;
        int i,j;
        outstring = malloc(strlen(srcstring)+1);
        if (outstring != NULL) {
                strlength = strlen(srcstring);
                strcpy(outstring, srcstring);
                outstring[strlength] = '\0';
                printf("Input string:%s\n", outstring);
                reverseWord(outstring, strlength);

                for (i = 0, j = 0; outstring[j]!= '\0'; j++) {
                        if ( (outstring[j] == ' ' && outstring[j+1] != ' ') ||
                              outstring[j+1] == '\0' ) {
                                cptr = &outstring[i];
                                strlength = j - i;
                                reverseWord(cptr, strlength);
                                i = j+1;
                        }
                }
                printf("output string %s\n", outstring);


                free(outstring);
        }
        return 0;
}

void reverseWord(char* string, int strlength)
{
        int i;
        char tmp;
        for ( i = 0 ; i < strlength /2 ; i++ ) {
                tmp = string[i];
                string[i] = string[strlength-i-1];
                string[strlength-i-1] = tmp;
        }
}

An article : Doing garbage collection in C

http://www.embedded.com/design/opensource/219400100?_requestid=283157

Next, any unreferenced objects in the linked list will be freed. This is commonly known as the "sweep" phase of a garbage collector.

A short example illustrates how this could be used:

#define TREE_NODE_PTR_COUNT 2
typedef struct TreeNode {
struct TreeNode _gc *left, *right; //pointers at top
int value;
} TreeNode;

TreeNode *top, *node0, *node1, *node2;

int main()
{
node0 = (TreeNode*)CollectMalloc(sizeof(TreeNode), TREE_NODE_PTR_COUNT);
node1 = (TreeNode*)CollectMalloc(sizeof(TreeNode), TREE_NODE_PTR_COUNT);
node2 = (TreeNode*)CollectMalloc(sizeof(TreeNode), TREE_NODE_PTR_COUNT);

CollectRoot(&top); //define a fixed root node
top = node0;
node0->left = node2;

CollectGarbage(); //frees unreferenced node1
top = NULL;
CollectGarbage(); //frees node0 and node2
return 0;
}

Plan from Aug29 2009


Short-term process:
Linux Kernel:
()Read LKA Ch 6, 7, 10
()Updating Linux-USB ML, tracking
()Practicing writing Kernel modules.
C++STL:
()Make a graph class can do graph algorithms
()Do LiarLiar puzzles
Python:
()Do LiarLiar puzzle
Algorithm:
()Device an algorithm that do LiarLiar
()Study Single source shortest path, Dijkstra algorithm
iPhoneAppObjC:
()Continue the study to the next course

Linux kernel reading memo Jul 1

HZ increasing will cause:
. Linux scheduler is called at each timer tick
. More system over head because things have to be switching more frquently
. machine specific timer intr routine should be happening HZ times per second

SoftIRQ:
. execution pending IRQ after each IRQ
. softfirqd is being raised by other proc in order to do softirq

Study on Linux initrd and initramfs

following boot parameters are related:
initrd location in physical ram, related code(spread over arch/arm/kernel/init.c) This is able to be specified by "initrd=" or ATAGs..
static void __init early_initrd(char **p)
{
unsigned long start, size;

start = memparse(*p, p);
if (**p == ',') {
size = memparse((*p) + 1, p);

phys_initrd_start = start;
phys_initrd_size = size;
}
}
__early_param("initrd=", early_initrd);


start_kernel:
    setup_arch:
        paging_init:
            bootmem_init:
                check_initrd(..) ifdef CONFIG_BLK_DEV_INITRD
                bootmem_reserve_initrd(..) ifdef CONFIG_BLK_DEV_INITRD
                    initrd_start = __phys_to_virt(phys_initrd_start);
                    initrd_end = initrd_start + phys_initrd_size;


root=/dev/ram0 rw: specifies the rootfs device

(tell the difference between initramfs and initrd)
populate_rootfs is a initcall execute by kernel_init
static int __init kernel_init(void * unused)
{
    :
    do_basic_setup();
    :
    prepare_namespace();
}
static void __init do_basic_setup(void)
{
    rcu_init_sched(); /* needed by module_init stage. */
    init_workqueues();
    usermodehelper_init();
    driver_init();
    init_irq_proc();
    do_initcalls();
        /* the do_initcalls will eventually call a function:
           populate_rootfs(..) if CONFIG_BLK_DEV_RAM is defined, 
           then probably the init rootfs is a initrd, or a initramfs 
           if CONFIG_BLK_DEV_RAM is not defined
           then it only accepts initramfs
         */
}
void __init prepare_namespace(void)
{
    mount_block_root(root_device_name, root_mountflags);
    :
    if (initrd_load())
         go out
    :
    mount_root();
}



rdinit=ramdisk_execute_command, ..
init=/bin/init or /bin/bash or /bin/ash

when not specified, no effect..
related kernel code snippet(init_post from main.c):
if (execute_command) {
run_init_process(execute_command);
printk(KERN_WARNING "Failed to execute %s.  Attempting "
"defaults...\n", execute_command);
}
run_init_process("/sbin/init");
run_init_process("/etc/init");
run_init_process("/bin/init");
run_init_process("/bin/sh");
:

Objective-C beginner..for fluent c/c++ programmer

1. Objective-C doesn't have value types, so there is nothing similar to C++'s: Fraction frac; frac.print();. You always deal with objects as pointers in Objective-C. for example:
MyClass *myObj = [[MyClass alloc] init];
....// do something
[myObj release];


2. Dynamic types in Objective-C
-(BOOL) isKindOfClass: classObjis object a descendent or member of classObj
-(BOOL) isMemberOfClass: classObjis object a member of classObj
-(BOOL) respondsToSelector: selectordoes the object have a method named specifiec by the selector
+(BOOL) instancesRespondToSelector: selectordoes an object created by this class have the ability to respond to the specified selector
-(id) performSelector: selectorinvoke the specified selector on the object

3. Every object inherited from NSObject has a class method that returns a class object. This is very similar to Java's getClass() method. This class object is used in the methods above.


Video over USB, UWB, IP network

An article from Embedded.com says there are three major methods transferring videos between PC and video display device at home. They are Wireless USB, UWB, wireless TCP/IP.
USB and UWB are more applicable to PC and PC peripherals and the 3rd one is more complicated and costly.

For a wired architecture as a baseline system, we have north bridge and south bridge:
North bridge:
High bandwidth memory chip, GPU
South bridge:
Eithernet, SATA, USB, PCI-E

For videos it is imperative to use GPU to accelerate the graphic processing.

For wireless video solutions:
Recently product of UWB has integrated support of USB, Video, Audio.

The UWB solution make the most cost effective, most viable solutions because it doesn't have to modify to much of the current architecture. Reduced the system cost and doesn't have the burden for other part of the system.

For USB wireless solutions:
Since the CPU has to spend more cycle on USB software, there is much more costly than UWB solutions. The USB latency and overhead will also significantly reduce the bandwith for video transferring on the air. Thus not as good as UWB.

For video over IP solutions:
Power burden, software management, complex software stacks. All increase the cost of this solution.

Component requirement comparison:

So developing a system that is optimized with the number of components is much obviously displayed.

SLAB allocation prelim...

Since memory allocation is limited to page only. The SLAB allocator can manage the even more fine-grained mem allocation further to any kind of object size. The key struct is a struct called: kmem_cache. It is used to manage one kind of allocation object or general buffers only. Each kmem_cache has 3 lists that manage the slabs of each list. The three slab lists differs from the degree of content they fill. 1)contain full slabs, 2)contain partially free slabs, 3)all free slabs. These three lists are managed in a struct called: kmem_list3.
The objects are appeared all over the 3 lists. The objects that appear all over the 3 lists are the same kind of structures.
The design of the free list and full and partial list is in consideration of the contiguous of memory.
Each slab contains a management area, and a object buffer array. We can easily:
1. Use the object's vir pointer to find the slab by page
2. Use the slab and its buffer size and its index to indicate an object.

The kmem_cache also has a array_cache for each cpu. That is to say for each CPU, the kmem_cache can make an effort on it can return an object just freed in the PerCPU-cache as soon as possible. So if someone free an object and try to allocate it very quickly, the same object is from the cache.

Python and its built-in function obj.__str__(....)

When we use some Object as a argument for print function, then python will automatically invoke its __str__ function. So what we saw in so many derived classes that over write the __str__ function is to modify the print-out of its own object.
Here is a classic one that print out a BST:
    def __str__(self):
        if self.root is None: return '<empty tree>'
        def recurse(node):
            if node is None: return [], 0, 0
            label = str(node.key)
            left_lines, left_pos, left_width = recurse(node.left)
            right_lines, right_pos, right_width = recurse(node.right)
            middle = max(right_pos + left_width - left_pos + 1, len(label), 2)
            pos = left_pos + middle // 2
            width = left_pos + middle + right_width - right_pos
            while len(left_lines) < len(right_lines):
                left_lines.append(' ' * left_width)
            while len(right_lines) < len(left_lines):
                right_lines.append(' ' * right_width)
            if (middle - len(label)) % 2 == 1 and node.parent is not None and \
               node is node.parent.left and len(label) < middle:
                label += '.'
            label = label.center(middle, '.')
            if label[0] == '.': label = ' ' + label[1:]
            if label[-1] == '.': label = label[:-1] + ' '
            lines = [' ' * left_pos + label + ' ' * (right_width - right_pos),
                     ' ' * left_pos + '/' + ' ' * (middle-2) +
                     '\\' + ' ' * (right_width - right_pos)] + \
              [left_line + ' ' * (width - left_width - right_width) +
               right_line
               for left_line, right_line in zip(left_lines, right_lines)]
            return lines, pos, width
        return '\n'.join(recurse(self.root) [0])

another git blog

Setting web and cgi
First, because Ubuntu has another package name git, so we have to change the soft link name of git to point to our git-scm binary. That is in /etc/alternatives for example:
sudo ln -s git /usr/bin/git-scm

Second, since I am planning to publish the project, I want to use gitweb, so I have to take care of few things: apache2 support, cgi, perl support, and build options for project root(default: /pub/git), git binary directory and your cgi directory. In git example:
make GITWEB_PROJECTROOT="/home/local/scm" \
     GITWEB_CSS="/gitweb/gitweb.css" \
     GITWEB_LOGO="/gitweb/git-logo.png" \
     GITWEB_FAVICON="/gitweb/git-favicon.png" \
     bindir=/usr/local/bin \
     gitweb/gitweb.cgi

Setting each project
Then we came to each project, to make it public. The detailed step is at public repo. I am listing few step below, interested people are suggest to visit the git official webpage.
1. to use git protocol and git_daemon as git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6.git
git clone --bare ~/proj proj.git
touch proj.git/git-daemon-export-ok
mv proj.git /home/you/public_html/proj.git
cd proj.git
git --bare update-server-info
mv hooks/post-update.sample hooks/post-update

Finally, broadcast your project like:
git clone git://yourserver.com/~you/proj.git

then you are all set

2. use the http protocol:
mv proj.git /pub/git
cd proj.git
git --bare update-server-info
mv hooks/post-update.sample hooks/post-update

then you can use http to access the project like:
 git clone http://yourserver.com/~you/proj.git 


back log:
I haven't been able to build a web server yet, so its really unclear in the webserver setting above. I will keep this updated once I have built the server.


log2:
I have the script gitweb.cgi under cgi-bin directory works, but somehow it could not load the css file. In fact the gitweb.css is in the same directory of gitweb.cgi. Maybe it is my apache2 server setting, maybe it is my fault during build and install. I don't plan to solve this issue immediately since I have a lot other works to do. I will update this while I find out what's going on.
January 2010
M T W T F S S
December 2009February 2010
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31