<commit_before>/*
 * opencog/atomspace/TLB.h
 *
 * Copyright (C) 2002-2007 Novamente LLC
 * All Rights Reserved
 *
 * Written by <NAME> <<EMAIL>>
 *            <NAME> <<EMAIL>>
 *            <NAME> <<EMAIL>>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License v3 as
 * published by the Free Software Foundation and including the exceptions
 * at http://opencog.org/wiki/Licenses
 *
 * 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 Affero General Public License
 * along with this program; if not, write to:
 * Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifndef _OPENCOG_TLB_H
#define _OPENCOG_TLB_H

// Use the TLB map only if SQL storage is being used.
// (or if distributed processing is enabled)
#ifdef HAVE_SQL_STORAGE
#define USE_TLB_MAP 1
#endif

#define CHECK_MAP_CONSISTENCY

#ifdef USE_TLB_MAP
#include <map>
#endif

#include <opencog/atomspace/Atom.h>
#include <opencog/atomspace/types.h>
#include <opencog/util/Logger.h>

#define OBFUSCATE (0x55555555UL)

namespace opencog
{

/**
 * Each atom stored on OpenCog will have an immutable ID, which will be used
 * to refer to that atom when a reference to that atom needs to be kept.
 * Each proxy must have a look-up mechanism or table (TLB) to map from
 * this ID to the actual memory address for the atom in the local process
 * address space.
 */
class TLB
{
private:

#ifdef USE_TLB_MAP
    static std::map<Handle, const Atom*> handle_map;
    static std::map<const Atom *, Handle> atom_map;
#endif

    /**
     * Private default constructor for this class to make it abstract.
     */
    TLB() {}

#ifdef USE_TLB_MAP
    static unsigned long uuid;
#endif

public:
    /**
     * Maps a handle to its corresponding atom.
     *
     * @param Handle to be mapped.
     * @return Corresponding atom for the given handle. Returns NULL if handle
     * isn't found.
     */
    static inline Atom* getAtom(const Handle& handle)
    {
#ifdef USE_TLB_MAP
        // if (handle.value() <= NOTYPE) // check for "non-real" atoms
        //     return reinterpret_cast<Atom*>(handle.value());

        std::map<Handle, const Atom*>::iterator it = handle_map.find(handle);
        if (it == handle_map.end()) return NULL;
        else return const_cast<Atom*>(it->second);
#else
        return reinterpret_cast<Atom*>(handle.value() ^ OBFUSCATE);
#endif
    }

    /**<commit_msg>bug fix for getAtom() when the TLB map is disabled.<commit_after>/*
 * opencog/atomspace/TLB.h
 *
 * Copyright (C) 2002-2007 Novamente LLC
 * All Rights Reserved
 *
 * Written by <NAME> <<EMAIL>>
 *            <NAME> <<EMAIL>>
 *            <NAME> <<EMAIL>>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License v3 as
 * published by the Free Software Foundation and including the exceptions
 * at http://opencog.org/wiki/Licenses
 *
 * 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 Affero General Public License
 * along with this program; if not, write to:
 * Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifndef _OPENCOG_TLB_H
#define _OPENCOG_TLB_H

// Use the TLB map only if SQL storage is being used.
// (or if distributed processing is enabled)
#ifdef HAVE_SQL_STORAGE
#define USE_TLB_MAP 1
#endif

#define CHECK_MAP_CONSISTENCY

#ifdef USE_TLB_MAP
#include <map>
#endif

#include <opencog/atomspace/Atom.h>
#include <opencog/atomspace/types.h>
#include <opencog/util/Logger.h>

#define OBFUSCATE (0x55555555UL)

namespace opencog
{

/**
 * Each atom stored on OpenCog will have an immutable ID, which will be used
 * to refer to that atom when a reference to that atom needs to be kept.
 * Each proxy must have a look-up mechanism or table (TLB) to map from
 * this ID to the actual memory address for the atom in the local process
 * address space.
 */
class TLB
{
private:

#ifdef USE_TLB_MAP
    static std::map<Handle, const Atom*> handle_map;
    static std::map<const Atom *, Handle> atom_map;
#endif

    /**
     * Private default constructor for this class to make it abstract.
     */
    TLB() {}

#ifdef USE_TLB_MAP
    static unsigned long uuid;
#endif

public:
    /**
     * Maps a handle to its corresponding atom.
     *
     * @param Handle to be mapped.
     * @return Corresponding atom for the given handle. Returns NULL if handle
     * isn't found.
     */
    static inline Atom* getAtom(const Handle& handle)
    {
#ifdef USE_TLB_MAP
        // if (handle.value() <= NOTYPE) // check for "non-real" atoms
        //     return reinterpret_cast<Atom*>(handle.value());

        std::map<Handle, const Atom*>::iterator it = handle_map.find(handle);
        if (it == handle_map.end()) return NULL;
        else return const_cast<Atom*>(it->second);
#else
        if (Handle::UNDEFINED == handle) return NULL;
        return reinterpret_cast<Atom*>(handle.value() ^ OBFUSCATE);
#endif
    }

    /**
{'fraction_non_alphanumeric': 0.09534127843986999, 'fraction_numerical': 0.010834236186348862, 'mean_word_length': 3.2574942352036893, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '7213205', 'n_tokens_mistral': 1738, 'n_tokens_neox': 1676, 'n_words': 662}