<commit_before>/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.apache.harmony.luni.platform;

import java.io.IOException;


/**
 * This class enables direct access to OS memory.
 * <p>
 * Methods that take OS addresses define such parameters as a Java
 * <code>long</code>. The <code>long</code> value is interpreted based on
 * the underlying platform pointer size, such that only the lowest significant
 * <code>POINTER_SIZE</code> bytes of the <code>long</code> value are used.
 * In practice this means that methods on 64-bit platforms use the full eight
 * bytes of the address parameter, and on 32-bit platforms the same methods are
 * truncated to use only the low four bytes.
 * </p>
 * <p>
 * Methods that return OS addresses define the return type to be a Java
 * <code>long</code>. If the platform pointer size is less than eight bytes
 * the OS address value is zero-extended to an eight-byte int to correspond to
 * the subsequent interpretation of that jlong as an OS address as defined
 * above.
 * </p>
 */
final class OSMemory implements IMemorySystem {

	/**
	 * Defines the size, in bytes, of a native pointer type for the underlying
	 * platform. This will be 4 (for 32-bit machines) or 8 (for 64-bit
	 * machines).
	 */
	public static final int POINTER_SIZE;

	/**
	 * Defines the natural byte order for this machine.
	 */
	public static final Endianness NATIVE_ORDER;

	private static final OSMemory singleton = new OSMemory();

	static {
		POINTER_SIZE = getPointerSizeImpl();

		if (isLittleEndianImpl()) {
			NATIVE_ORDER = Endianness.LITTLE_ENDIAN;
		} else {
			NATIVE_ORDER = Endianness.BIG_ENDIAN;
		}
	}

	public static OSMemory getOSMemory() {
		return singleton;
	}

	/**
	 * This class is not designed to be publicly instantiated.
	 * 
	 * @see #getOSMemory()
	 */
	private OSMemory() {
		super();
	}

	/**
	 * Answers whether the byte order of this machine is little endian or not..
	 * 
	 * @return <code>false</code> for Big Endian, and
	 *         <code>true</code. for Little Endian.
	 */
	public static native boolean isLittleEndianImpl();

	public boolean isLittleEndian() {
		return isLittleEndianImpl();
	}

	/**
	 * Answers the natural byte order for this machine.<commit_msg>No need to call native method for each check of platform endianness.<commit_after>/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package org.apache.harmony.luni.platform;

import java.io.IOException;


/**
 * This class enables direct access to OS memory.
 * <p>
 * Methods that take OS addresses define such parameters as a Java
 * <code>long</code>. The <code>long</code> value is interpreted based on
 * the underlying platform pointer size, such that only the lowest significant
 * <code>POINTER_SIZE</code> bytes of the <code>long</code> value are used.
 * In practice this means that methods on 64-bit platforms use the full eight
 * bytes of the address parameter, and on 32-bit platforms the same methods are
 * truncated to use only the low four bytes.
 * </p>
 * <p>
 * Methods that return OS addresses define the return type to be a Java
 * <code>long</code>. If the platform pointer size is less than eight bytes
 * the OS address value is zero-extended to an eight-byte int to correspond to
 * the subsequent interpretation of that jlong as an OS address as defined
 * above.
 * </p>
 */
final class OSMemory implements IMemorySystem {

	/**
	 * Defines the size, in bytes, of a native pointer type for the underlying
	 * platform. This will be 4 (for 32-bit machines) or 8 (for 64-bit
	 * machines).
	 */
	public static final int POINTER_SIZE;

	/**
	 * Defines the natural byte order for this machine.
	 */
	public static final Endianness NATIVE_ORDER;

	private static final OSMemory singleton = new OSMemory();

	static {
		POINTER_SIZE = getPointerSizeImpl();

		if (isLittleEndianImpl()) {
			NATIVE_ORDER = Endianness.LITTLE_ENDIAN;
		} else {
			NATIVE_ORDER = Endianness.BIG_ENDIAN;
		}
	}

	public static OSMemory getOSMemory() {
		return singleton;
	}

    /*
     * Native method to determine whether the underlying platform is little
     * endian.
     * 
     * @return <code>true</code> if the platform is little endian or
     * <code>false</code> if it is big endian.
     */
    private static native boolean isLittleEndianImpl();

	/**
	 * This class is not designed to be publicly instantiated.
	 * 
	 * @see #getOSMemory()
	 */
	private OSMemory() {
		super();
	}

    /**
     * Answers whether the byte order of this machine is little endian or not.
     * 
     * @return <code>false</code> for Big Endian, and
     *         <code>true</code> for Little Endian.
     */
	public boolean isLittleEndian() {
		return NATIVE_ORDER == Endianness.LITTLE_ENDIAN;
	}

	/**
	 * Answers the natural byte order for this machine.
{'fraction_non_alphanumeric': 0.08022151898734177, 'fraction_numerical': 0.004430379746835443, 'mean_word_length': 3.4420238931834155, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 42, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5048975', 'n_tokens_mistral': 1840, 'n_tokens_neox': 1732, 'n_words': 793}