001/*******************************************************************************
002 * Copyright (c) 2024, 2026, Olivier Ayache.  All rights reserved.
003 *
004 * This file is part of AVPKit.
005 *
006 * AVPKit is free software: you can redistribute it and/or modify
007 * it under the terms of the GNU Lesser General Public License as published by
008 * the Free Software Foundation, either version 3 of the License, or
009 * (at your option) any later version.
010 *
011 * AVPKit is distributed in the hope that it will be useful,
012 * but WITHOUT ANY WARRANTY; without even the implied warranty of
013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
014 * GNU Lesser General Public License for more details.
015 *
016 * You should have received a copy of the GNU Lesser General Public License
017 * along with AVPKit.  If not, see <http://www.gnu.org/licenses/>.
018 *******************************************************************************/
019
020package com.avpkit.ferry;
021
022/**
023 * Internal Only.
024 * 
025 * This class is held on to by the {@link RefCounted} classes and nulled when
026 * that object is collected. It has no references to any (non static) members
027 * and so its {@link #finalize()} method will not hold up the collection of any
028 * other object.
029 * <p>
030 * It exists so that we still have a mechanism that always frees native memory;
031 * in most cases the {@link JNIReference} will enqueue correctly with the
032 * {@link JNIMemoryManager}, and then the next call to a Ferry based method will
033 * drain that queue, but sometimes there is no extra call to one of those
034 * methods; in this case we'll drain the queue when this gets finalized.
035 * </p>
036 * <p>
037 * It does a {@link JNIMemoryManager#gc()} which might race with the
038 * {@link JNIMemoryManager#gc()} that a {@link JNIReference} does on allocation
039 * of a new object but that's a safe race.
040 * </p>
041 * 
042 * @author aclarke
043 * 
044 */
045public final class JNINativeFinalizer
046{
047  /**
048   * Internal Only. Creates a new {@link JNINativeFinalizer}. This object must
049   * contain <strong>no references</strong> to any other objects in the system.
050   */
051  public JNINativeFinalizer()
052  {
053  }
054
055  /**
056   * Runs a {@link JNIMemoryManager#gc()} to free up any
057   * {@link RefCounted} objects that are pending release.
058   */
059  protected void finalize()
060  {
061    JNIReference.getMgr().gcInternal();
062  }
063}