aboutsummaryrefslogtreecommitdiffstats
path: root/c89_relo.c
blob: 6150781efb57e4b47a4944c7ab68b1b47203e323 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/* 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 <string.h>
#include <assert.h>
#include "uns.h"
#include "internal/c89_relo.h"

typedef struct uns_c89_relo_hdr Hdr;

size_t uns_c89_relo_get_len(void *p)
{
	Hdr *hdr = uns_c89_relo_get_hdr(p);

	return (size_t)(hdr->len < 0 ? -hdr->len : hdr->len);
}

size_t uns_c89_relo_get_record_len(void *p)
{
	Hdr *hdr = uns_c89_relo_get_hdr(p);

	if (hdr->len > 0)
		return 0;
	return (size_t)(-hdr->len) / sizeof(void *);
}

void *uns_c89_relo_init(void *p, size_t len_size, int is_record)
{
	Hdr *hdr = p;

	assert(len_size < UNS_SWORD_MAX);
	hdr->relo = NULL;
	hdr->len = (uns_sword)len_size;
	if (is_record)
		hdr->len = -hdr->len;

	return hdr + 1;
}

static void check_len(void *rec, size_t i)
{
	assert(i * sizeof(void *) < uns_c89_relo_get_len(rec));
}

void uns_c89_relo_record_set(Uns_ptr rec, size_t i, void *v)
{
	check_len(rec, i);
	memcpy((unsigned char *)rec + i*sizeof(void*), &v, sizeof(v));
}

void *uns_c89_relo_record_get(Uns_ptr rec, size_t i)
{
	void *v;

	check_len(rec, i);
	memcpy(&v, (unsigned char *)rec + i*sizeof(void*), sizeof(v));

	return v;
}