aboutsummaryrefslogtreecommitdiffstats
path: root/include/uns.h
blob: 93a859d044a53913f880045700ffffa99c55ff0b (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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#ifndef UNIVERSAL_SERVICE_H
#define UNIVERSAL_SERVICE_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 <stddef.h>
#if !defined(UNS_WORD) && __STDC_VERSION__ >= 199101L
# include <stdint.h>
# include <limits.h>
# define UNS_WORD uintptr_t
# define UNS_SIGNED_WORD intptr_t
# define UNS_WORD_MAX UINTPTR_MAX
# define UNS_SWORD_MAX INTPTR_MAX
# define UNS_SWORD_MIN INTPTR_MIN
#elif !defined(UNS_WORD)
# include <limits.h>
# define UNS_WORD unsigned long
# define UNS_WORD_MAX ULONG_MAX
# define UNS_SIGNED_WORD long
# define UNS_SWORD_MAX LONG_MAX
# define UNS_SWORD_MIN LONG_MIN
#endif

typedef UNS_WORD uns_word;
typedef UNS_SIGNED_WORD uns_sword;

/** Doubly linked list used to
  *
  * 1) Store roots of the GC,
  * 2) Store GC pointers used in C and possibly not pointed-to anywhere
  *    else in GC space,
  * 3) Preserve the location of a pointer after it moves.
  *
  * A root that is not a part of any GC MUST have ``prev`` and ``next``
  * set to NULL.
  */
struct uns_ctr {
	struct uns_ctr *prev;
	void *p;
	struct uns_ctr *next;
};

struct uns_gc {
	void *ctx;

	/* The collector only changes the pointer values of "p".
	 *
	 * * The collector never adds or removes roots.
	 * * The collector never rearranges the order of the container structures.
	 *
	 * The roots list can have repeated values.
	 */
	struct uns_ctr *roots;
	size_t next_alloc;
	void (*oom)(struct uns_gc *);
	void (*after_gc)(struct uns_gc *);

	size_t before_collection;
	size_t after_collection;
	long collection_number;

	void (*deinit)(struct uns_gc *);

	/* These functions, and only these functions, may invalidate
	 * pointers by either moving them or freeing them.
	 */
	int (*collect)(struct uns_gc *);
	void *(*alloc)(struct uns_gc *, size_t);
	void *(*alloc_record)(struct uns_gc *, size_t);

	/* These functions MUST NOT cause collections. */

	/* This function is optional. It is only provided if the GC can
	 * calculate the length of the pointer quickly.
	 */
	size_t (*len)(void *);

	void (*record_set_ptr)(void *, size_t, void *);
	void *(*record_get_ptr)(void *, size_t);
};

/** Add a root to the GC. The root must point to valid memory, or NULL. */
void uns_root_add(struct uns_gc *gc, struct uns_ctr *root);

/** Remove a root from the GC. It is OK to remove a root that is not a
  * part of any GC using this function (the call will have no effect).
  */
void uns_root_remove(struct uns_gc *gc, struct uns_ctr *root);

#endif