简体   繁体   English

什么是调度表? 如何在 C 中实现它?

[英]What is a dispatch table? How can I implement it in C?

Let me start by saying that I know how function pointers work.首先让我说我知道 function 指针是如何工作的。 If you would like to explain them in more detail please go ahead, however what I ask from you is how can I implement them in a dispatch table using C.如果您想更详细地解释它们,请提前 go,但是我问你的是如何使用 C 在调度表中实现它们。

I have searched for what a dispatch table is but wasn't really able to comprehend anything more than a vague understanding about how it might possibly work.我已经搜索了调度表是什么,但除了对它可能如何工作的模糊理解之外,我真的无法理解任何东西。

Please be kind enough to share it's practical uses and how I can create my own dispatch table in C.请善意分享它的实际用途以及如何在 C 中创建自己的调度表。 Help is very much appreciated.非常感谢您的帮助。

Dispatch tables can be implemented in several ways.调度表可以通过多种方式实现。 One is with a table of pointers to functions:一个是指向函数的指针表:

int Add     (int a, int b) { return a + b; }
int Subtract(int a, int b) { return a - b; }
int Multiply(int a, int b) { return a * b; }
int Divide  (int a, int b) { return a / b; }


int DoFunction(int Select, int a, int b)
{
    /*  Declare a type to point to a function with parameters (int a, int b)
        and returning an int.
    */
    typedef int (*MyFunctionPointer)(int a, int b);

    //  Build a dispatch table with pointers to functions.
    MyFunctionPointer Table[] =
        {
            Add,
            Subtract,
            Multiply,
            Divide,
        };

    //  Dispatch to the requested function.
    return Table[Select](a, b);
}


#include <stdio.h>


int main(void)
{
    //  Demonstrate calls using dispatch table.
    printf("7 + 3 = %d.\n", DoFunction(0, 7, 3));
    printf("7 - 3 = %d.\n", DoFunction(1, 7, 3));
    printf("7 * 3 = %d.\n", DoFunction(2, 7, 3));
    printf("7 / 3 = %d.\n", DoFunction(3, 7, 3));
}

One might also jump into a table of branch instructions.也可以跳转到一个分支指令表。 This is more common in assembly than in higher level languages.这在汇编中比在高级语言中更常见。

Essentially, a dispatch table is some method of transferring program control to a location selected via an index, rather than by individual selections such as with if or switch statements.本质上,调度表是一种将程序控制转移到通过索引选择的位置的方法,而不是通过单独的选择,例如ifswitch语句。 In some situations, it is easier or cleaner to compute an index to select a function than to write some convoluted selection statements.在某些情况下,计算 select 和 function 的索引比编写一些复杂的选择语句更容易或更简洁。

(This example shows homogeneous functions—they all have the same parameter type list and return type. If functions are not homogeneous, it may be trickier to use a dispatch table in C.) (这个例子展示了同构函数——它们都具有相同的参数类型列表和返回类型。如果函数不是同构的,使用 C 中的调度表可能会比较棘手。)

Although dispatch tables are not frequently encountered in much source code (nor are they uncommon), they can be used for a variety of things, such as:尽管调度表在很多源代码中并不常见(它们也并不罕见),但它们可以用于多种用途,例如:

  • On some processors, interrupt service routines are handled through a dispatch table: There are fixed locations in memory where addresses of routines are stored, forming a table of addresses.在某些处理器上,中断服务程序是通过一个调度表来处理的:在 memory 中有固定的位置存储例程的地址,形成一个地址表。 When an interrupt occurs, the hardware looks up the address and transfer control to it.当发生中断时,硬件会查找地址并将控制权转移给它。

  • In code that should be high-performance on a variety of hardware, we might prepare several functions that each use a different algorithm designed for particular hardware.在应该在各种硬件上具有高性能的代码中,我们可能会准备几个函数,每个函数都使用为特定硬件设计的不同算法。 When the program starts, it could test what hardware it is executing on (such as specific processor model) and record an index into the table.当程序启动时,它可以测试它正在执行的硬件(例如特定的处理器型号)并将索引记录到表中。 That index would indicate which routine to execute.该索引将指示要执行的例程。 Then calls to the function can use the index table for quick dispatching, without needing to test-and-branch for each call.然后对 function 的调用可以使用索引表进行快速调度,而无需为每个调用进行测试和分支。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM