/* ScummVM - Graphic Adventure Engine
 *
 * ScummVM is the legal property of its developers, whose names
 * are too numerous to list here. Please refer to the COPYRIGHT
 * file distributed with this source distribution.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * 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 General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 */

#ifndef COMMON_MEMORY_H
#define COMMON_MEMORY_H

#include "common/scummsys.h"

namespace Common {

/**
 * Copies data from the range [first, last) to [dst, dst + (last - first)).
 * It requires the range [dst, dst + (last - first)) to be valid and
 * uninitialized.
 */
/*
 * OpenVMS/__DECCXX PORT CHANGE - DO NOT RESTORE THE UPSTREAM ONE-LINER.
 * =====================================================================
 * Upstream body is:
 *         while (first != last)
 *                 new ((void *)dst++) Type(*first++);
 * HP C++ V7.3-009 on OpenVMS Alpha MISCOMPILES that at /OPTIMIZE=(LEVEL=3,TUNE=HOST)
 * into an INFINITE LOOP.  This is a compiler bug on valid C++, proven from the
 * /MACHINE_CODE listing of the out-of-line instantiation
 * CXX$unntalzdcpy6Cmmnxpkp05rjhp6 (a plain <const int *, int> copy of 4 elements):
 *
 *   0034  XOR  R16, last, R17    ; "first != last" evaluated ONCE, BEFORE the loop
 *   0060  BEQ  R17, L$3          ; ...and that is the ONLY time it is ever tested
 *   0070  L$4:                   ; loop top
 *   0084  LDA  dst, 4(R4)        ; dst advances correctly
 *   0090  JSR  R26, CXXL$__NW__XUIPV   ; operator new
 *   00A0  BEQ  R0, L$4           ; if new returns 0, branch back - a 2nd endless loop
 *   00B0  LDL  R20, (R3)         ; reads *first - R3 IS NEVER INCREMENTED
 *   00B4  STL  R20, (R0)
 *   00BC  BR   L$4               ; UNCONDITIONAL back edge - no exit test at all
 *
 * So the optimizer dropped the "first++" increment and hoisted a NON-invariant exit
 * test out of the loop.  The result copies the first source element forward through
 * memory until it walks into an unmapped page:
 *   %SYSTEM-F-ACCVIO at page-aligned 0xA4000 / 0xA5E000 / 0x300000, appearing as a
 *   READ (reason mask=00, "first" ran away) or a WRITE (mask=04, "dst" ran away).
 * It killed KYRA.EXE during startup, in the FIRST Common::Array push_back the
 * process ever performs (MemoryPool::allocPage, reached from getenv("HOME") ->
 * a >20-char String -> external storage -> String::incRefCount).
 *
 * THE FIX is to take the two increments OUT of the new-expression's operands.  That
 * alone is enough - verified at LEVEL=3 both through a function pointer and at a
 * direct call site, for n = 0, 2 and 4 (n=0 matters: it is the one case the buggy
 * hoisted test got right, so a fix must not regress it).
 *
 * DIAGNOSIS DEAD ENDS - all tested and eliminated, do not repeat them:
 *   - Stale CXX_REPOSITORY: reproduced with a fresh, empty private repository.
 *   - /name=(as_is,shortened) hash collision: 7 distinct instantiation hashes, and
 *     the listing confirms every call site binds to the correct one.
 *   - Array / insert_aux / element-size mismatch: fails on a DIRECT call with
 *     Type=int and compile-time-constant bounds; no container involved.
 *   - Pointer width: /POINTER_SIZE=64 changes the fault ADDRESS but not the runaway.
 *   - Lowering the optimization level: NOT AVAILABLE as a fix here.  /OPTIMIZE=LEVEL=1
 *     crashes this same compiler outright (%GEM-F-ASSERTION in GEM_CX_EXTEND_LIFETIME)
 *     on other files in this tree, so the source must survive LEVEL=3.
 *
 * See also the identical fix applied to copy() and copy_backward() in
 * common/algorithm.h, which had the same one-liner shape.
 */
template<class In, class Type>
Type *uninitialized_copy(In first, In last, Type *dst) {
	while (first != last) {
		new ((void *)dst) Type(*first);
		++dst;
		++first;
	}
	return dst;
}

/**
 * Initializes the memory [first, first + (last - first)) with the value x.
 * It requires the range [first, first + (last - first)) to be valid and
 * uninitialized.
 */
/*template<class Type, class Value>
void uninitialized_fill(Type *first, Type *last, const Value &x) {
	while (first != last)
		new ((void *)first++) Type(x);
}*/

/**
 * Initializes the memory [dst, dst + n) with the value x.
 * It requires the range [dst, dst + n) to be valid and
 * uninitialized.
 */
/*template<class Type, class Value>
void uninitialized_fill_n(Type *dst, size_t n, const Value &x) {
	while (n--)
		new ((void *)dst++) Type(x);
}*/

} // End of namespace Common

#endif
