en:pfw:buffer
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revision | |||
| en:pfw:buffer [2023-09-04 18:11] – gelöscht - Externe Bearbeitung (Unbekanntes Datum) 127.0.0.1 | en:pfw:buffer [2023-09-04 18:11] (current) – ↷ Seite von pfw:buffer nach en:pfw:buffer verschoben uho | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| + | {{pfw: | ||
| + | ====== Buffer data structure ====== | ||
| + | |||
| + | uh 2021-12-11 | ||
| + | |||
| + | ===== Idea ===== | ||
| + | |||
| + | For storing data, applications use memory. Applications need to manage that memory: | ||
| + | |||
| + | * reserve (// | ||
| + | * identifying the appropriate parts of data and the locations in memory where to store that parts. | ||
| + | |||
| + | A **buffer** is a general data structure, that can hold a fixed number of //entries// (also called // | ||
| + | |||
| + | A buffer is located at some (named) // | ||
| + | |||
| + | Ultimately entries must be stored in memory at their particular // | ||
| + | |||
| + | < | ||
| + | entry-address = buffer-base-address + index * entry-size | ||
| + | </ | ||
| + | |||
| + | on their own. | ||
| + | |||
| + | ==== Implementation of Buffers ==== | ||
| + | |||
| + | The most simple implementation just allocates the appropriate number of bytes in memory. | ||
| + | |||
| + | < | ||
| + | (named) | ||
| + | +- buffer-base-address | ||
| + | | ||
| + | | ||
| + | | ||
| + | | ||
| + | ... | x_0 | x_1 | … | x_capacity-1 | ... | ||
| + | | ||
| + | | ||
| + | Index | ||
| + | </ | ||
| + | |||
| + | === Pseudo code of a buffer and the read/write access to its entries === | ||
| + | |||
| + | < | ||
| + | Create a buffer with «capacity» entries of size «entry-size»: | ||
| + | reserve «capacity» * «entry-size» bytes in memory, record its «buffer-base-address» | ||
| + | |||
| + | Function: store-value ( value index buffer-base-address -- ) | ||
| + | Store «value» in the buffer at «index» by storing «value» in memory at | ||
| + | | ||
| + | |||
| + | Function: read-value ( index buffer-base-address -- value ) | ||
| + | Fetch «value» from the buffer at «index» by reading memory at | ||
| + | | ||
| + | </ | ||
| + | |||
| + | |||
| + | ---- | ||
| + | |||
| + | ==== Forth implementations ==== | ||
| + | |||
| + | * **[[https:// | ||
| + | |||
| + | <code forth> | ||
| + | \ buffer in Generic Forth | ||
| + | \ ----------------------------------------------------------------- | ||
| + | |||
| + | 32 CONSTANT capacity | ||
| + | 1 CELL CONSTANT entry-size | ||
| + | |||
| + | CREATE buf capacity entry-size * ALLOT | ||
| + | |||
| + | |||
| + | : store-value ( x index buffer-base-address -- ) SWAP entry-size * + ! ; | ||
| + | |||
| + | : read-value | ||
| + | |||
| + | </ | ||
| + | |||
| + | |||
| + | |||
| + | < | ||
| + | < | ||
| + | < | ||
| + | '' | ||
| + | < | ||
| + | «u» Buffer: «name» | ||
| + | </ | ||
| + | |||
| + | < | ||
| + | < | ||
| + | |||
| + | < | ||
| + | \ For RAM-only systems | ||
| + | : BUFFER: ( u < | ||
| + | CREATE ALLOT ; | ||
| + | |||
| + | \ For RAM-only and for RAM/ROM systems | ||
| + | : BUFFER: ( u < | ||
| + | HERE SWAP ALLOT \ ram { b_0 | ... | b_u } | ||
| + | CREATE , \ rom { 'ram } | ||
| + | DOES> ( -- addr ) @ ; | ||
| + | </ | ||
| + | |||
| + | ===== Contributions ===== | ||
| + | |||
| + | < | ||
| + | |||