summary |
shortlog |
log |
commit | commitdiff |
tree
raw |
patch |
inline | side by side (from parent 1:
bafb9f1)
In Position we store a pointer to a StateInfo record
kept outside of the Position object.
When copying a position we copy also that pointer so
after the copy we have two Position objects pointing
to the same StateInfo record. This can be dangerous
so fix by copying also the StateInfo record inside
the new Position object and let the new st pointer
point to it. This completely detach the copied
Position from the original one.
Also rename setStartState() as saveState() and clean up
the API to state more clearly what the function does.
Signed-off-by: Marco Costalba <mcostalba@gmail.com>
/// Position::copy() creates a copy of the input position.
/// Position::copy() creates a copy of the input position.
-void Position::copy(const Position &pos) {
+void Position::copy(const Position& pos) {
memcpy(this, &pos, sizeof(Position));
memcpy(this, &pos, sizeof(Position));
+ saveState(); // detach and copy state info
-/// Position::setStartState() copies the content of the argument
+/// Position::saveState() copies the content of the current state
/// inside startState and makes st point to it. This is needed
/// when the st pointee could become stale, as example because
/// the caller is about to going out of scope.
/// inside startState and makes st point to it. This is needed
/// when the st pointee could become stale, as example because
/// the caller is about to going out of scope.
-void Position::setStartState(const StateInfo& s) {
+void Position::saveState() {
+ st->previous = NULL; // as a safe guard
/// the white and black sides reversed. This is only useful for debugging,
/// especially for finding evaluation symmetry bugs.
/// the white and black sides reversed. This is only useful for debugging,
/// especially for finding evaluation symmetry bugs.
-void Position::flipped_copy(const Position &pos) {
+void Position::flipped_copy(const Position& pos) {
void print(Move m = MOVE_NONE) const;
// Copying
void print(Move m = MOVE_NONE) const;
// Copying
- void copy(const Position &pos);
- void flipped_copy(const Position &pos);
+ void copy(const Position& pos);
+ void flipped_copy(const Position& pos);
// The piece on a given square
Piece piece_on(Square s) const;
// The piece on a given square
Piece piece_on(Square s) const;
bool square_is_weak(Square s, Color c) const;
// Doing and undoing moves
bool square_is_weak(Square s, Color c) const;
// Doing and undoing moves
- void setStartState(const StateInfo& st);
void do_move(Move m, StateInfo& st);
void do_move(Move m, StateInfo& st, Bitboard dcCandidates);
void undo_move(Move m);
void do_move(Move m, StateInfo& st);
void do_move(Move m, StateInfo& st, Bitboard dcCandidates);
void undo_move(Move m);
if (RootPosition.rule_50_counter() == 0)
RootPosition.reset_game_ply();
}
if (RootPosition.rule_50_counter() == 0)
RootPosition.reset_game_ply();
}
- // Our StateInfo st is about going out of scope,
- // so save its content before they disappear.
- RootPosition.setStartState(st);
+ // Our StateInfo st is about going out of scope so copy
+ // its content inside RootPosition before they disappear.
+ RootPosition.saveState();