Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions changelog.in
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,72 @@ Rank: minor
Post nothing for relax with empty variable arrays. Debug builds aborted with
a failed assertion.

[ENTRY]
Module: kernel
What: bug
Rank: minor
[DESCRIPTION]
Action and CHB information over an empty array, including branching with
ACTION or CHB merits on no variables, requests no heap memory for its values
and posts no recorder propagator. It previously requested zero bytes, which
the C library may answer with a null pointer and thus a MemoryExhausted
exception.

[ENTRY]
Module: kernel
What: bug
Rank: minor
[DESCRIPTION]
Copying or assigning an empty Archive requests no heap memory, and growing
space memory with Space::realloc from an empty block copies nothing. They
previously requested zero bytes or passed a null pointer to memcpy.

[ENTRY]
Module: int
What: bug
Rank: minor
[DESCRIPTION]
Tuple sets of arity zero request no heap memory for tuples and values, also
when they grow or are finalized with a dense representation. They previously
requested zero bytes, which the C library may answer with a null pointer and
thus a MemoryExhausted exception. An IntSet built from an empty initializer
list is the empty set; Debug builds aborted with a failed assertion.

[ENTRY]
Module: flatzinc
What: bug
Rank: minor
[DESCRIPTION]
Integer and set variables declared with an empty set literal, such as var {}
or var set of {}, request no heap memory. The final states of a regular
constraint given as an interval with a maximum below its minimum are treated
as empty; such an interval previously requested zero bytes or a huge size and
could write before the allocated block.

[ENTRY]
Module: example
What: bug
Rank: minor
[DESCRIPTION]
A dictionary file holding no accepted word and no final newline requests no
heap memory in the crossword and word-square examples.

[ENTRY]
Module: support
What: change
Rank: minor
[DESCRIPTION]
Every request for heap memory must ask for more than zero bytes. Code that
asks the heap for zero bytes, through Heap::ralloc, Heap::rrealloc, or the
typed alloc and realloc, now fails an assertion in Debug builds. Reallocating
from zero objects to a positive number stays valid. Heap::rrealloc no longer
returns a null pointer for size zero; free the block with rfree instead.
[MORE]
The C library may answer a request for zero bytes with a null pointer, which
the heap reports as MemoryExhausted, and reallocating to zero bytes is
implementation-defined in C17. Heap::copy also needs non-null pointers, even
for zero objects, because it passes them to memcpy.

[ENTRY]
Module: other
What: performance
Expand Down
1 change: 1 addition & 0 deletions cmake/GecodeSources.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,7 @@ set(GECODE_TEST_SOURCES
test/flatzinc/cutstock.cpp
test/flatzinc/empty_domain_1.cpp
test/flatzinc/empty_domain_2.cpp
test/flatzinc/empty_set_literal.cpp
test/flatzinc/eq20.cpp
test/flatzinc/factory_planning_instance.cpp
test/flatzinc/golomb.cpp
Expand Down
7 changes: 5 additions & 2 deletions examples/scowl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7820,7 +7820,9 @@ Dictionary::init(const char* fn) {
f.close();
}

chunk = static_cast<char*>(Gecode::heap.ralloc(sz));
// A file with no accepted word and no final newline needs no bytes
if (sz > 0)
chunk = static_cast<char*>(Gecode::heap.ralloc(sz));

{
// Initialize start information in chunk
Expand Down Expand Up @@ -7900,7 +7902,8 @@ operator<<(std::basic_ostream<Char, Traits>& os, const Dictionary& d) {
}

inline Dictionary::~Dictionary(void) {
Gecode::heap.rfree(chunk);
if (chunk != nullptr)
Gecode::heap.rfree(chunk);
}

// STATISTICS: example-ignore
Expand Down
19 changes: 3 additions & 16 deletions gecode/flatzinc/flatzinc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -365,12 +365,7 @@ namespace Gecode { namespace FlatZinc {
if (sl->interval) {
return IntSet(sl->min, sl->max);
} else {
int* newdom = heap.alloc<int>(static_cast<unsigned long int>(sl->s.size()));
for (int i = sl->s.size(); i--;)
newdom[i] = sl->s[i];
IntSet ret(newdom, sl->s.size());
heap.free(newdom, static_cast<unsigned long int>(sl->s.size()));
return ret;
return IntSet(sl->s.data(), static_cast<int>(sl->s.size()));
}
}
return IntSet(Int::Limits::min, Int::Limits::max);
Expand Down Expand Up @@ -970,11 +965,7 @@ namespace Gecode { namespace FlatZinc {
IntSet d(vsv->min, vsv->max);
sv[set_var_count++] = SetVar(*this, d, d);
} else {
int* is = heap.alloc<int>(static_cast<unsigned long int>(vsv->s.size()));
for (int i = vsv->s.size(); i--;)
is[i] = vsv->s[i];
IntSet d(is, vsv->s.size());
heap.free(is, static_cast<unsigned long int>(vsv->s.size()));
IntSet d(vsv->s.data(), static_cast<int>(vsv->s.size()));
sv[set_var_count++] = SetVar(*this, d, d);
}
} else if (vs->upper_bound()) {
Expand All @@ -983,11 +974,7 @@ namespace Gecode { namespace FlatZinc {
IntSet d(vsv->min, vsv->max);
sv[set_var_count++] = SetVar(*this, IntSet::empty, d);
} else {
int* is = heap.alloc<int>(static_cast<unsigned long int>(vsv->s.size()));
for (int i = vsv->s.size(); i--;)
is[i] = vsv->s[i];
IntSet d(is, vsv->s.size());
heap.free(is, static_cast<unsigned long int>(vsv->s.size()));
IntSet d(vsv->s.data(), static_cast<int>(vsv->s.size()));
sv[set_var_count++] = SetVar(*this, IntSet::empty, d);
}
} else {
Expand Down
11 changes: 6 additions & 5 deletions gecode/flatzinc/registry.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1270,10 +1270,11 @@ namespace Gecode { namespace FlatZinc {
// Final states
int* f;
if (finals->interval) {
f = static_cast<int*>(heap.ralloc(sizeof(int) * (finals->max - finals->min + 2)));
for (int i = finals->min; i <= finals->max; i++)
f[i - finals->min] = i;
f[finals->max - finals->min + 1] = -1;
const int n_finals = (finals->min <= finals->max) ? finals->max - finals->min + 1 : 0;
f = static_cast<int*>(heap.ralloc(sizeof(int) * (n_finals + 1)));
for (int i = 0; i < n_finals; i++)
f[i] = finals->min + i;
f[n_finals] = -1;
} else {
f = static_cast<int*>(heap.ralloc(sizeof(int) * (finals->s.size() + 1)));
for (int j = finals->s.size(); j--;)
Expand All @@ -1282,7 +1283,7 @@ namespace Gecode { namespace FlatZinc {
}

DFA dfa(q0, t, f);
free(f);
heap.rfree(f);
unshare(s, iv);
extensional(s, iv, s.get_shared_dfa(dfa), s.annotation_to_int_prop_level(ann));
}
Expand Down
2 changes: 1 addition & 1 deletion gecode/int/extensional/dfa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ namespace Gecode {
}

// Temporary structure for transitions
Transition* trans = region.alloc<Transition>(n_trans);
Transition* trans = (n_trans > 0) ? region.alloc<Transition>(n_trans) : nullptr;
if (compact) {
for (int i = 0; i < n_trans; i++) {
trans[i] = t_spec[i];
Expand Down
51 changes: 33 additions & 18 deletions gecode/int/extensional/tuple-set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,20 +96,31 @@ namespace Gecode { namespace Int { namespace Extensional {

namespace Gecode {

namespace {

/// Free the heap buffer \a p unless it was never allocated
void
free_buffer(void* p) {
if (p != nullptr)
heap.rfree(p);
}

} // namespace

/*
* Tuple set data
*
*/
void
TupleSet::Data::clear_support(void) {
heap.rfree(range);
heap.rfree(range_base);
heap.rfree(support);
heap.rfree(sparse_offsets);
heap.rfree(sparse_tuples);
heap.rfree(sparse_tv);
heap.rfree(compressed_offsets);
heap.rfree(compressed_words);
free_buffer(range);
free_buffer(range_base);
free_buffer(support);
free_buffer(sparse_offsets);
free_buffer(sparse_tuples);
free_buffer(sparse_tv);
free_buffer(compressed_offsets);
free_buffer(compressed_words);
range = nullptr;
range_base = nullptr;
support = nullptr;
Expand Down Expand Up @@ -215,7 +226,7 @@ namespace Gecode {
// Initialization
if (n_tuples == 0) {
const State selected = empty_state(epk);
heap.rfree(td);
free_buffer(td);
td = nullptr;
guard.commit(selected);
return;
Expand Down Expand Up @@ -256,7 +267,7 @@ namespace Gecode {
for (int a = 0; a < arity; a++)
if (!Int::Limits::valid(tuples[t][a]))
throw Int::OutOfLimits("TupleSet::finalize()");
int* new_td = heap.alloc<int>(n_tcells);
int* new_td = (n_tcells > 0UL) ? heap.alloc<int>(n_tcells) : nullptr;
for (int t = 0; t < n_tuples; t++) {
for (int a = 0; a < arity; a++) {
new_td[static_cast<unsigned long>(t) * static_cast<unsigned long>(arity) +
Expand All @@ -265,7 +276,7 @@ namespace Gecode {
}
tuples[t] = new_td + static_cast<unsigned long>(t) * static_cast<unsigned long>(arity);
}
heap.rfree(td);
free_buffer(td);
td = new_td;
}

Expand Down Expand Up @@ -349,7 +360,7 @@ namespace Gecode {
* Pass 2: allocate memory and fill data structures
*/
// Allocate memory for ranges
Range* range_cursor = range = heap.alloc<Range>(n_ranges);
Range* range_cursor = range = (n_ranges > 0U) ? heap.alloc<Range>(n_ranges) : nullptr;
// Allocate and initialize memory for support data.
support = nullptr;
sparse_n_vals = 0U;
Expand All @@ -364,11 +375,14 @@ namespace Gecode {
if (build_dense) {
assert((n_words == 0U) || (n_vals <= std::numeric_limits<unsigned int>::max() / n_words));
n_support_entries_u32 = n_words * n_vals;
support_cursor = support = heap.alloc<BitSetData>(n_support_entries_u32);
support_cursor = support =
(n_support_entries_u32 > 0U) ? heap.alloc<BitSetData>(n_support_entries_u32) : nullptr;
for (unsigned int i = 0; i < n_support_entries_u32; i++)
support_cursor[i].init();
}
for (int a = 0; a < arity; a++) {
// Every position has at least one range and thus a tuple cell
assert(n_ranges > 0U);
// Set range pointer
vd[a].r = range_cursor;
vd[a].base = nullptr;
Expand Down Expand Up @@ -552,14 +566,15 @@ namespace Gecode {
TupleSet::Data::resize(void) {
assert(n_free == 0);
int n = static_cast<int>(1 + n_tuples * 1.5);
td = heap.realloc<int>(td, n_tuples * arity, n * arity);
if (arity > 0)
td = heap.realloc<int>(td, n_tuples * arity, n * arity);
n_free = n - n_tuples;
}

TupleSet::Data::~Data(void) {
clear_support();
heap.rfree(td);
heap.rfree(vd);
free_buffer(td);
free_buffer(vd);
}

/*
Expand Down Expand Up @@ -633,8 +648,8 @@ namespace Gecode {
assert(layers[0].states[0].tuples != nullptr);

// Allocate temporary memory for edges and supports
Edge* edges = r.alloc<Edge>(dfa.max_degree());
Support* supports = r.alloc<Support>(dfa.n_symbols());
Edge* edges = (dfa.max_degree() > 0U) ? r.alloc<Edge>(dfa.max_degree()) : nullptr;
Support* supports = (dfa.n_symbols() > 0U) ? r.alloc<Support>(dfa.n_symbols()) : nullptr;

// Forward pass: accumulate
for (int i = 0; i < a; i++) {
Expand Down
7 changes: 4 additions & 3 deletions gecode/int/extensional/tuple-set.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,10 @@ namespace Gecode {
TupleSet::Data::Data(int a)
: arity(a), n_words(0U), // To be initialized in finalize
n_tuples(0), n_free(n_initial_free), min(Int::Limits::max), max(Int::Limits::min), key(0),
td(heap.alloc<int>(n_initial_free * a)), vd(heap.alloc<ValueData>(a)), range(nullptr), range_base(nullptr),
support(nullptr), state(TS_BUILDING), sparse_n_vals(0U), sparse_offsets(nullptr), sparse_tuples(nullptr),
sparse_tv(nullptr), compressed_offsets(nullptr), compressed_words(nullptr), compressed_n_entries(0U) {}
td((a > 0) ? heap.alloc<int>(n_initial_free * a) : nullptr), vd((a > 0) ? heap.alloc<ValueData>(a) : nullptr),
range(nullptr), range_base(nullptr), support(nullptr), state(TS_BUILDING), sparse_n_vals(0U),
sparse_offsets(nullptr), sparse_tuples(nullptr), sparse_tv(nullptr), compressed_offsets(nullptr),
compressed_words(nullptr), compressed_n_entries(0U) {}

forceinline bool
TupleSet::Data::finalized(void) const {
Expand Down
6 changes: 4 additions & 2 deletions gecode/int/int-set.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,8 @@ namespace Gecode {

IntSet::IntSet(std::initializer_list<int> r) {
int n = static_cast<int>(r.size());
assert(n > 0);
if (n == 0)
return;
Region reg;
Range* dr = reg.alloc<Range>(n);
int j = 0;
Expand All @@ -212,7 +213,8 @@ namespace Gecode {

IntSet::IntSet(std::initializer_list<std::pair<int, int>> r) {
int n = static_cast<int>(r.size());
assert(n > 0);
if (n == 0)
return;
Region reg;
Range* dr = reg.alloc<Range>(n);
int j = 0;
Expand Down
20 changes: 14 additions & 6 deletions gecode/kernel/archive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,31 @@ namespace Gecode {
_size = m;
}

Archive::Archive(const Archive& e) : _size(e._n), _n(e._n), _pos(e._pos) {
_a = heap.alloc<unsigned int>(_n);
heap.copy<unsigned int>(_a, e._a, _n);
Archive::Archive(const Archive& e) : _size(e._n), _n(e._n), _a(nullptr), _pos(e._pos) {
if (_n > 0) {
_a = heap.alloc<unsigned int>(_n);
heap.copy<unsigned int>(_a, e._a, _n);
}
}

Archive&
Archive::operator=(const Archive& e) {
if (this != &e) {
_a = heap.realloc<unsigned int>(_a, _size, e._n);
heap.copy<unsigned int>(_a, e._a, e._n);
if (e._n > 0) {
_a = heap.realloc<unsigned int>(_a, _size, e._n);
heap.copy<unsigned int>(_a, e._a, e._n);
} else if (_a != nullptr) {
heap.free<unsigned int>(_a, _size);
_a = nullptr;
}
_size = _n = e._n;
}
return *this;
}

Archive::~Archive(void) {
heap.free<unsigned int>(_a, _size);
if (_a != nullptr)
heap.free<unsigned int>(_a, _size);
}

} // namespace Gecode
Expand Down
3 changes: 2 additions & 1 deletion gecode/kernel/branch/action.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ namespace Gecode {
Support::Mutex Action::Storage::m;

Action::Storage::~Storage(void) {
heap.free<double>(a, n);
if (n > 0)
heap.free<double>(a, n);
}

const Action Action::def;
Expand Down
Loading
Loading