1/* @ajax/mesh-abi-fields: Region layout checks: (offset, bytes) fields, overlap and extent.
2 *
3 * Part of alpha-c-mesh, a C port of alpha-rust-mesh (README.md says from where). */
4#ifndef MESH_ABI_FIELDS_H
5#define MESH_ABI_FIELDS_H
67#include <stdbool.h>
8#include <stddef.h>
9#include <stdint.h>
1011/* One field of a region: its offset and size in bytes. */
12typedef struct {
13 size_t off;
14 size_t bytes;
15} mesh_field;
1617/* No two (offset, bytes) ranges overlap. */
18static inline bool mesh_disjoint(const mesh_field *f, size_t n) {
19 for (size_t i = 0; i < n; i++)
20 for (size_t j = i + 1; j < n; j++)
21 if (f[i].off < f[j].off + f[j].bytes && f[j].off < f[i].off + f[i].bytes) return false;
22 return true;
23}
2425/* The end of a region's last-ending field. */
26static inline size_t mesh_fields_end(const mesh_field *f, size_t n) {
27 size_t e = 0;
28 for (size_t i = 0; i < n; i++)
29 if (f[i].off + f[i].bytes > e) e = f[i].off + f[i].bytes;
30 return e;
31}
3233#endif