2010年12月6日 星期一

function pointer 的用法

//local 宣告function pointer
int (*netcmd)(uint16,int(*)(cbuf_t*,uint16,void*,uint16),void*,uint16,void*,uint16*,uint32,int*);

//指定成不同的function pointer 根據傳入的參數
netcmd = (block) ? netcmdBlockSend : netcmdNonblockSend;

//可以直接call不需dereference 但是(*netcmd)才是比較一般的作法 參考這個網址
err_code=netcmd
    EVT_ARP_ADD, netexeArpAdd, &add, sizeof(cmdArpAdd_t),
    NULL, NULL, 5000, &result);

 

一些介紹function pointer的link:

pointer的加減

每次都忘記, 人真的老了

char * p ; p++時, p=p+1
int *
p ; p++時, p=p+4
memh_t *
p ; p++時, p = p + sizeof(memh_t)  

所以當

void* blk;
mp = (memh_t *)blk-1;
則mp 會指向 blk 往前推 memh_t 的位置

2010年11月25日 星期四

[好code分享] 利用掛function pointer 對同一堆data structure做不同的操作

int ecmpIprtNextHopIterate(
    void    (*exefunc)(ecmpRtNhEntry_t* ,ecmpNhEntry_t *,void*),
    void * data
){
    ecmpRtNhEntry_t* ecmpRtNhEntry_p = NULL;
    ecmpNhEntry_t* ecmpNhEntry_p = NULL;
    int32 bits,hashVal;
    for ( bits = IPRT_ECMP_NETMASK-1; bits >= 0; bits--)
    {
        for(hashVal = HASHMOD-1; hashVal >= 0; hashVal--){
            TAILQ_FOREACH(ecmpRtNhEntry_p, &ecmpRtNhTable[bits][hashVal],link) 
            {
                TAILQ_FOREACH(ecmpNhEntry_p, &(ecmpRtNhEntry_p->nhList),link){
                    if(exefunc){
                        exefunc(ecmpRtNhEntry_p, ecmpNhEntry_p,data);
                    }
                }
            }
        }
    }
    return ZYNOS_E_NONE;
}

要一直對相同的一堆data structure做不同的處理時用的, 感覺還不錯

印很多變數 檢查null的方式

if (str && buf && hw_addr){
    IPRT_ECMP_LOG(ECMP_DB_INFO,
        "update nexthop =%s hw addr = %02x:%02x:%02x:%02x:%02x:%02x    flags = %s caller = 0x%x\n",
        buf, MACADDR_LIST(hw_addr), str, addr);
}

我喜歡這個 很乾淨

2010年11月2日 星期二

passwd

帳號: 公司mail

密碼: real name

2010年11月1日 星期一

Sample.c

/*
* $Id: $
*/
/****************************************************************************/
/*
*    Copyright <Company logo>
*/
/****************************************************************************/
/*
* $Log: $
*/
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include "global.h"


#ifdef SAMPLE_SUPPORT

#if 1 /* macro */
/*************************************************************
*
* macro
*
*************************************************************/
#endif /* macro */

#if 1 /* prototype */
/**************************************************************
*
* prototype
*
**************************************************************/
#endif /* prototype */

#if 1 /* global variable */
/**************************************************************
*
* global variable
*
**************************************************************/
#endif /* global variable */

#if 1 /* local variable */
/**************************************************************
*
* local variable
*
**************************************************************/
#endif  /* local variable */

#if 1 /* local function */
/**************************************************************
*
* local function
*
**************************************************************/
#endif  /* local function */

#if 1 /* global function */
/**************************************************************
*
* global function
*
**************************************************************/
#endif /* global function */

#endif /* SAMPLE_SUPPORT */

Sample.h

/*
* $Id:  $
*/

/****************************************************************************/
/*
*    Copyright <Company logo>
*/
/****************************************************************************/

/*
* $Log:  $
*/

#ifndef _SAMPLE_H_
#define _SAMPLE_H_

#if 1  /* define */
/**************************************************************
*
* define
*
**************************************************************/
#endif /* define */

#if 1  /* enum */
/**************************************************************
*
* enum
*
**************************************************************/
#endif /* enum */

#if 1  /* type */
/**************************************************************
*
* type
*
**************************************************************/
#endif /* type */

#if 1  /* prototype */
/**************************************************************
*
* prototype
*
**************************************************************/
#endif /* prototype */

#endif /* !_SAMPLE_H_ */