<commit_before>#include "ObjectInstanceRegistry.h"

#include "Error.h"

namespace Halide {
namespace Internal {

/* static */
ObjectInstanceRegistry &ObjectInstanceRegistry::get_registry() {
    static ObjectInstanceRegistry *registry = new ObjectInstanceRegistry;
    return *registry;
}

/* static */
void ObjectInstanceRegistry::register_instance(void *this_ptr, size_t size, Kind kind,
                                               void *subject_ptr) {
    ObjectInstanceRegistry &registry = get_registry();
#if __cplusplus > 199711L
    std::lock_guard<std::mutex> lock(registry.mutex);
#endif

    uintptr_t key = (uintptr_t)this_ptr;
    internal_assert(registry.instances.find(key) == registry.instances.end());
    registry.instances[key] = InstanceInfo(size, kind, subject_ptr);
}

/* static */
void ObjectInstanceRegistry::unregister_instance(void *this_ptr) {
    ObjectInstanceRegistry &registry = get_registry();
#if __cplusplus > 199711L
    std::lock_guard<std::mutex> lock(registry.mutex);
#endif

    uintptr_t key = (uintptr_t)this_ptr;
    size_t num_erased = registry.instances.erase(key);
    internal_assert(num_erased == 1) << "num_erased: " << num_erased;
}

/* static */
std::vector<void *> ObjectInstanceRegistry::instances_in_range(void *start, size_t size,
                                                               Kind kind) {
    ObjectInstanceRegistry &registry = get_registry();
#if __cplusplus > 199711L
    std::lock_guard<std::mutex> lock(registry.mutex);
#endif

    std::vector<void *> results;

    std::map<uintptr_t, InstanceInfo>::const_iterator it =
        registry.instances.lower_bound((uintptr_t)start);

    uintptr_t limit_ptr = ((uintptr_t)start) + size;
    while (it != registry.instances.end() && it->first < limit_ptr) {
        if (it->second.kind == kind) {
            results.push_back(it->second.subject_ptr);
        }

        if (it->first > (uintptr_t)start && it->second.size != 0) {
            // Skip over containers that we enclose
            it = registry.instances.lower_bound(it->first + it->second.size);
        } else {
            it++;
        }
    }

    return results;
}

}  // namespace Internal
}  // namespace Halide<commit_msg>Fix race condition for C++ < C++11<commit_after>#include "ObjectInstanceRegistry.h"

#include "Error.h"

namespace Halide {
namespace Internal {

/* static */
ObjectInstanceRegistry &ObjectInstanceRegistry::get_registry() {
    static ObjectInstanceRegistry *registry = new ObjectInstanceRegistry;
    return *registry;
}

/* static */
void ObjectInstanceRegistry::register_instance(void *this_ptr, size_t size, Kind kind,
                                               void *subject_ptr) {
#if __cplusplus > 199711L || _MSC_VER >= 1800
    ObjectInstanceRegistry &registry = get_registry();
    std::lock_guard<std::mutex> lock(registry.mutex);
    uintptr_t key = (uintptr_t)this_ptr;
    internal_assert(registry.instances.find(key) == registry.instances.end());
    registry.instances[key] = InstanceInfo(size, kind, subject_ptr);
#endif
}

/* static */
void ObjectInstanceRegistry::unregister_instance(void *this_ptr) {
#if __cplusplus > 199711L || _MSC_VER >= 1800
    ObjectInstanceRegistry &registry = get_registry();
    std::lock_guard<std::mutex> lock(registry.mutex);
    uintptr_t key = (uintptr_t)this_ptr;
    size_t num_erased = registry.instances.erase(key);
    internal_assert(num_erased == 1) << "num_erased: " << num_erased;
#endif
}

/* static */
std::vector<void *> ObjectInstanceRegistry::instances_in_range(void *start, size_t size,
                                                               Kind kind) {
    std::vector<void *> results;

#if __cplusplus > 199711L || _MSC_VER >= 1800
    ObjectInstanceRegistry &registry = get_registry();
    std::lock_guard<std::mutex> lock(registry.mutex);

    std::map<uintptr_t, InstanceInfo>::const_iterator it =
        registry.instances.lower_bound((uintptr_t)start);

    uintptr_t limit_ptr = ((uintptr_t)start) + size;
    while (it != registry.instances.end() && it->first < limit_ptr) {
        if (it->second.kind == kind) {
            results.push_back(it->second.subject_ptr);
        }

        if (it->first > (uintptr_t)start && it->second.size != 0) {
            // Skip over containers that we enclose
            it = registry.instances.lower_bound(it->first + it->second.size);
        } else {
            it++;
        }
    }
#endif

    return results;
}

}  // namespace Internal
}  // namespace Halide
{'fraction_non_alphanumeric': 0.12602436323366556, 'fraction_numerical': 0.011960132890365448, 'mean_word_length': 3.169898430286242, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 40, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1982655', 'n_tokens_mistral': 1401, 'n_tokens_neox': 1347, 'n_words': 264}