Dispatch table

In computer science, a dispatch table is a data structure that maps a series of known actions to their corresponding code, which can later be retrieved dynamically.

Uses your system voice; voice and rate in settings.

Snapshot

≈ 3 min read

Names in other languages

No names in other languages are held for this entry yet.

Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.

In computer science, a dispatch table is a data structure that maps a series of known actions to their corresponding code, which can later be retrieved dynamically. This structure is commonly implemented either using an associative array to directly map method names to a corresponding function pointer, or a simple array of function pointers with known indices.

Common use cases include the implementation of late binding and virtual polymorphism in object-oriented programming and the implementation of system calls. In operating system kernel design, system calls are commonly implemented using a dispatch table of indices mapping to corresponding privileged system actions.

For example, in Linux, the sys_exit system call has an index of 60, and can be called by passing 60 into the appropriate processor register and triggering a program interrupt. Calls sys_exit with exit code 0 mov rax, 60 mov rdi, 0 syscall Since system calls only require a number to reference the underlying kernel action, they can be called from user space while in reality triggering code in kernel space.

This improves system security by ensuring that privileged code can only be run through the narrow interface that each call provides. Define the table using one anonymous code-ref and one named code-ref my %dispatch = ( "-h" => sub { return "hello\n"; }, "-g" => \&say_goodbye ); sub say_goodbye { return "goodbye\n"; } Fetch the code ref from the table, and invoke it my $sub = $dispatch{$ARGV[0]}; print $sub ? $sub->() : "unknown argument\n"; Running this Perl program as perl greet -h will produce "hello", and running it as perl greet -g will produce "goodbye". // Dispatch table of actions as well as a fallback const actions = { action1() { console.log("action1 called"); }, action2() { console.log("action2 called"); }, action3() { console.log("action3 called"); }, fallback() { console.log("fallback called"); } }; function performAction(key) { const action = Object.hasOwn(actions, key) ? key : "fallback"; return actions[action](); } performAction("action1"); // outputs "action1 called" performAction("action3"); // outputs "action3 called" performAction("action5"); // outputs "fallback called" In Lua, tables can store any value, including functions, making it trivial to implement a simple dispatch table. -- Dispatch table of actions as well as a fallback local actions = { action1 = function () print('action1 called') end, action2 = function () print('action2 called') end, action3 = function () print('action3 called') end, fallback = function () print('fallback called') end } function perform_action(key) if actions[key] ~= nil then return actions[key]() end return actions.fallback() end perform_action('action1') -- outputs "action1 called" perform_action('action3') -- outputs "action3 called" perform_action('action5') -- outputs "fallback called"

Text derived from https://en.wikipedia.org/w/index.php?oldid=1365382304 under CC BY-SA 4.0; modifications have been made.

Sources cited in this revision

9 named source(s) extracted from the cited revision. A source with no author is shown by its publication; nothing is invented.