universalservice/include/internal/c89_relo.h

53 lines
1.8 KiB
C

#ifndef UNS_FORMAT_C89_H
#define UNS_FORMAT_C89_H
/* Copyright (C) 2024 Peter McGoron
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this program. If not, see
* <https://www.gnu.org/licenses/>.
*/
#include "uns.h"
/* This is a strictly C89+ header format with relocation pointers and
* lengths.
*
* Forwarding pointers need to be stored in the header because in strict
* C, arithmetic on pointers is undefined for pointers not pointing into
* the same object.
*
* The length of the string is limited to "len" bytes and is stored as
* a `uns_sword`. If the word is negative, then the region is a record,
* and if the number is positive then the region is just bytes.
*/
struct uns_c89_relo_hdr {
void *relo;
uns_sword len;
};
#define uns_c89_relo_get_hdr(p)((struct uns_c89_relo_hdr *)(p) - 1)
size_t uns_c89_relo_get_len(void *p);
size_t uns_c89_relo_get_record_len(void *p);
#define uns_c89_relo_get_relo(p) (uns_c89_relo_get_hdr(p)->relo)
#define uns_c89_relo_set_relo(p, v) (uns_c89_relo_get_relo(p) = (v))
#define uns_c89_relo_is_record(p) (uns_c89_relo_get_hdr(p)->len < 0)
void *uns_c89_relo_init(void *p, size_t len_size, int is_record);
void uns_c89_relo_record_set(void *rec, size_t i, void *v);
void *uns_c89_relo_record_get(void *rec, size_t i);
#endif