ubgpsuite/lonetix/include/df/bgp/bytebuf.h

63 lines
1.7 KiB
C
Executable File

// SPDX-License-Identifier: LGPL-3.0-or-later
/**
* \file bgp/bytebuf.h
*
* Allocator optimized for trivial BGP workflows.
*
* \copyright The DoubleFourteen Code Forge (C) All Rights Reserved
* \author Lorenzo Cogotti
*/
#ifndef DF_BGP_MEMBUF_H_
#define DF_BGP_MEMBUF_H_
#include "mem.h"
/// Memory alignment for `Bgpbytebuf` allocations.
#define BGP_MEMBUF_ALIGN 4
// NOTE: Need at least 4 bytes alignment for TABLE_DUMPV2 peer lookup tables!
/**
* \brief Basic fixed-size packed single-threaded byte buffer.
*
* Nearly zero-overhead byte pool optimized for typical BGP message
* allocations. Returns chunks from an internal fixed-size byte buffer
*/
typedef struct {
size_t size; ///< Buffer block size in bytes
size_t pos; ///< Current position inside block
ALIGNED(BGP_MEMBUF_ALIGN, Uint8 base[FLEX_ARRAY]); ///< Block buffer
} Bgpbytebuf;
/**
* \brief Create `Bgpbytebuf` with statically sized fixed buffer
* (as opposed to a flexible array buffer).
*
* This is useful when the buffer should be placed in statically
* allocated variable, e.g.
* ```c
* static BGP_FIXBYTEBUF(4096) bgp_msgBuf = { 4096 };
* ```
*
* May also be used for a `typedef`:
* ```c
* typedef BGP_FIXBYTEBUF(1024) Bgpsmallbuf;
* ```
*
* Variables generated by this macro may be used
* as `allocp` of any API expecting a `MemOps` interface.
*/
#define BGP_FIXBYTEBUF(bufsiz) \
struct { \
size_t size; \
size_t pos; \
ALIGNED(BGP_MEMBUF_ALIGN, Uint8 base[bufsiz]); \
}
/// `MemOps` operating over `Bgpbytebuf`, use pointer to `Bgpbytebuf` as `allocp`.
extern const MemOps *const Mem_BgpBufOps;
#endif