aboutsummaryrefslogtreecommitdiffstats
path: root/tests/gpgscm/scheme.c
diff options
context:
space:
mode:
authorJustus Winter <[email protected]>2017-03-23 14:21:36 +0000
committerJustus Winter <[email protected]>2017-04-07 11:50:20 +0000
commit8640fa880d7050917f4729f2c0cb506e165ee446 (patch)
tree3fe41a235ac6af9a902555093ff9814194b44995 /tests/gpgscm/scheme.c
parentgpgscm: Make global data constant when possible. (diff)
downloadgnupg-8640fa880d7050917f4729f2c0cb506e165ee446.tar.gz
gnupg-8640fa880d7050917f4729f2c0cb506e165ee446.zip
gpgscm: Allocate small integers in the rodata section.
* tests/gpgscm/Makefile.am (gpgscm_SOURCES): Add new file. * tests/gpgscm/scheme-private.h (struct cell): Move number to the top of the union so that we can initialize it. (struct scheme): Remove 'integer_segment'. * tests/gpgscm/scheme.c (initialize_small_integers): Remove function. (small_integers): New variable. (MAX_SMALL_INTEGER): Compute. (mk_small_integer): Adapt. (mark): Avoid marking objects already marked. This allows us to run the algorithm over objects in the rodata section if they are already marked. (scheme_init_custom_alloc): Remove initialization. (scheme_deinit): Remove deallocation. * tests/gpgscm/small-integers.h: New file. -- Allocate small integers from a fixed pool in the rodata section. This spares us the initialization, and deduplicates integers across different processes. It also makes the integers immutable, increasing memory safety. Signed-off-by: Justus Winter <[email protected]>
Diffstat (limited to 'tests/gpgscm/scheme.c')
-rw-r--r--tests/gpgscm/scheme.c41
1 files changed, 11 insertions, 30 deletions
diff --git a/tests/gpgscm/scheme.c b/tests/gpgscm/scheme.c
index c37b56824..e04394d81 100644
--- a/tests/gpgscm/scheme.c
+++ b/tests/gpgscm/scheme.c
@@ -1312,31 +1312,22 @@ INTERFACE pointer mk_character(scheme *sc, int c) {
/* s_save assumes that all opcodes can be expressed as a small
* integer. */
-#define MAX_SMALL_INTEGER OP_MAXDEFINED
-
-static int
-initialize_small_integers(scheme *sc)
-{
- int i;
- if (_alloc_cellseg(sc, MAX_SMALL_INTEGER, &sc->integer_segment))
- return 1;
-
- for (i = 0; i < MAX_SMALL_INTEGER; i++) {
- pointer x = &sc->integer_segment->cells[i];
- typeflag(x) = T_NUMBER | T_ATOM | MARK;
- ivalue_unchecked(x) = i;
- set_num_integer(x);
- }
+static const struct cell small_integers[] = {
+#define DEFINE_INTEGER(n) { T_NUMBER | T_ATOM | MARK, {{ 1, {n}}}},
+#include "small-integers.h"
+#undef DEFINE_INTEGER
+ {0}
+};
- return 0;
-}
+#define MAX_SMALL_INTEGER (sizeof small_integers / sizeof *small_integers - 1)
static INLINE pointer
mk_small_integer(scheme *sc, long n)
{
#define mk_small_integer_allocates 0
+ (void) sc;
assert(0 <= n && n < MAX_SMALL_INTEGER);
- return &sc->integer_segment->cells[n];
+ return (pointer) &small_integers[n];
}
#else
@@ -1644,7 +1635,8 @@ static void mark(pointer a) {
t = (pointer) 0;
p = a;
-E2: setmark(p);
+E2: if (! is_mark(p))
+ setmark(p);
if(is_vector(p)) {
int i;
for (i = 0; i < vector_length(p); i++) {
@@ -5629,13 +5621,6 @@ int scheme_init_custom_alloc(scheme *sc, func_alloc malloc, func_dealloc free) {
sc->F = &sc->_HASHF;
sc->EOF_OBJ=&sc->_EOF_OBJ;
-#if USE_SMALL_INTEGERS
- if (initialize_small_integers(sc)) {
- sc->no_memory=1;
- return 0;
- }
-#endif
-
sc->free_cell = &sc->_NIL;
sc->fcells = 0;
sc->inhibit_gc = GC_ENABLED;
@@ -5789,10 +5774,6 @@ void scheme_deinit(scheme *sc) {
sc->gc_verbose=0;
gc(sc,sc->NIL,sc->NIL);
-#if USE_SMALL_INTEGERS
- _dealloc_cellseg(sc, sc->integer_segment);
-#endif
-
for (s = sc->cell_segments; s; s = _dealloc_cellseg(sc, s)) {
/* nop */
}