0

私は次のコードを持っています:

struct cache_t *                /* pointer to cache created */
cache_create(char *name,        /* name of the cache */
             int nsets,         /* total number of sets in cache */
             int bsize,         /* block (line) size of cache */
             int balloc,        /* allocate data space for blocks? */
             int usize,         /* size of user data to alloc w/blks */
             int assoc,         /* associativity of cache */
             enum cache_policy policy,  /* replacement policy w/in sets */
             /* block access function, see description w/in struct cache def */
             unsigned int (*blk_access_fn) (enum mem_cmd cmd,
                                            md_addr_t baddr, int bsize,
                                            struct cache_blk_t * blk,
                                            tick_t now,
                                            int context_id),
             unsigned int hit_latency)
{                               /* latency in cycles for a hit */
    struct cache_t *cp;
    struct cache_blk_t *blk;
    int i, j, bindex;
----
----
---

  cp->blk_access_fn = blk_access_fn;

----
---

context_idとbaddrを出力したい。どうすればいいですか? 型キャストとすべてを試しましたが、エラーが発生し続けます。シンボル「context_id」は現在のコンテキストでは無効です。助けてください。

4

2 に答える 2

4

機能を誤解していると思いますcache_createcontext_idまたはbaddrパラメーターはまったくありません。パラメータとして持っているのblk_access_fnは、関数ポインタです。おそらく呼び出されるその関数はcache_create、これらの2つの変数をパラメーターとして持ちます

これをよりよく視覚化する方法は次のようになります。

typedef unsigned int (*blk_access_fn_ptr)(enum mem_cmd cmd, md_addr_t baddr, int bsize, struct cache_blk_t *blk, tick_t now, int context_id);

struct cache_t *            /* pointer to cache created */
cache_create(char *name,        /* name of the cache */
     int nsets,         /* total number of sets in cache */
     int bsize,         /* block (line) size of cache */
     int balloc,        /* allocate data space for blocks? */
     int usize,         /* size of user data to alloc w/blks */
     int assoc,         /* associativity of cache */
     enum cache_policy policy,  /* replacement policy w/in sets */
     /* block access function, see description w/in struct cache def */
     blk_access_fn_ptr blk_access_fn,
     unsigned int hit_latency)  /* latency in cycles for a hit */
{
    ...
}

このコードは、投稿したコードと機能が同じです。ご覧のとおり、cache_create探しているパラメーターがまったくありません。適切なプロトタイプをblk_access_fnパラメーターとして関数に渡す必要があります。

于 2012-12-18T21:01:19.557 に答える
0

私がこれを正しく読んでいる場合、context_id を渡しているのではなく、context_id の引数を取る fxn ポインターを取得しています。blk_access_fn 内ではアクセスできますが、cache_create 内ではアクセスできません。

于 2012-12-18T21:03:57.207 に答える