ABC: A System for Sequential Synthesis and Verification
 
Loading...
Searching...
No Matches
arena.hpp
Go to the documentation of this file.
1#ifndef _arena_hpp_INCLUDED
2#define _arena_hpp_INCLUDED
3
4#include "global.h"
5
7
8namespace CaDiCaL {
9
10// This memory allocation arena provides fixed size pre-allocated memory for
11// the moving garbage collector 'copy_non_garbage_clauses' in 'collect.cpp'
12// to hold clauses which should survive garbage collection.
13
14// The advantage of using a pre-allocated arena is that the allocation order
15// of the clauses can be adapted in such a way that clauses watched by the
16// same literal are allocated consecutively. This improves locality during
17// propagation and thus is more cache friendly. A similar technique is
18// implemented in MiniSAT and Glucose and gives substantial speed-up in
19// propagations per second even though it might even almost double peek
20// memory usage. Note that in MiniSAT this arena is actually required for
21// MiniSAT to be able to use 32 bit clauses references instead of 64 bit
22// pointers. This would restrict the maximum number of clauses and thus is
23// a restriction we do not want to use anymore.
24
25// New learned clauses are allocated in CaDiCaL outside of this arena and
26// moved to the arena during garbage collection. The additional 'to' space
27// required for such a moving garbage collector is only allocated for those
28// clauses surviving garbage collection, which usually needs much less
29// memory than all clauses. The net effect is that in our implementation
30// the moving garbage collector using this arena only needs roughly 50% more
31// memory than allocating the clauses directly. Both implementations can be
32// compared by varying the 'opts.arenatype' option (which also controls the
33// allocation order of clauses during moving them).
34
35// The standard sequence of using the arena is as follows:
36//
37// Arena arena;
38// ...
39// arena.prepare (bytes);
40// q1 = arena.copy (p1, bytes1);
41// ...
42// qn = arena.copy (pn, bytesn);
43// CADICAL_assert (bytes1 + ... + bytesn <= bytes);
44// arena.swap ();
45// ...
46// if (!arena.contains (q)) delete q;
47// ...
48// arena.prepare (bytes);
49// q1 = arena.copy (p1, bytes1);
50// ...
51// qn = arena.copy (pn, bytesn);
52// CADICAL_assert (bytes1 + ... + bytesn <= bytes);
53// arena.swap ();
54// ...
55//
56// One has to be really careful with 'qi' references to arena memory.
57
58struct Internal;
59
60class Arena {
61
62 Internal *internal;
63
64 struct {
65 char *start, *top, *end;
66 } from, to;
67
68public:
69 Arena (Internal *);
70 ~Arena ();
71
72 // Prepare 'to' space to hold that amount of memory. Precondition is that
73 // the 'to' space is empty. The following sequence of 'copy' operations
74 // can use as much memory in sum as pre-allocated here.
75 //
76 void prepare (size_t bytes);
77
78 // Does the memory pointed to by 'p' belong to this arena? More precisely
79 // to the 'from' space, since that is the only one remaining after 'swap'.
80 //
81 bool contains (void *p) const {
82 char *c = (char *) p;
83 return (from.start <= c && c < from.top) ||
84 (to.start <= c && c < to.top);
85 }
86
87 // Allocate that amount of memory in 'to' space. This assumes the 'to'
88 // space has been prepared to hold enough memory with 'prepare'. Then
89 // copy the memory pointed to by 'p' of size 'bytes'. Note that it does
90 // not matter whether 'p' is in 'from' or allocated outside of the arena.
91 //
92 char *copy (const char *p, size_t bytes) {
93 char *res = to.top;
94 to.top += bytes;
95 CADICAL_assert (to.top <= to.end);
96 memcpy (res, p, bytes);
97 return res;
98 }
99
100 // Completely delete 'from' space and then replace 'from' by 'to' (by
101 // pointer swapping). Everything previously allocated (in 'from') and not
102 // explicitly copied to 'to' with 'copy' becomes invalid.
103 //
104 void swap ();
105};
106
107} // namespace CaDiCaL
108
110
111#endif
#define ABC_NAMESPACE_CXX_HEADER_START
#define ABC_NAMESPACE_CXX_HEADER_END
#define CADICAL_assert(ignore)
Definition global.h:14
char * top
Definition arena.hpp:65
char * end
Definition arena.hpp:65
Arena(Internal *)
char * copy(const char *p, size_t bytes)
Definition arena.hpp:92
char * start
Definition arena.hpp:65
bool contains(void *p) const
Definition arena.hpp:81
void prepare(size_t bytes)
Cube * p
Definition exorList.c:222
char * memcpy()