<commit_before>//--------------------------------------------------------------------*- C++ -*-
// CLING - the C++ LLVM-based InterpreterG :)
// author:  <NAME> <<EMAIL>>
//
// This file is dual-licensed: you can choose to license it under the University
// of Illinois Open Source License or the GNU Lesser General Public License. See
// LICENSE.TXT for details.
//------------------------------------------------------------------------------

#include "cling/Utils/Validation.h"
#include "llvm/Support/ThreadLocal.h"

#include <assert.h>
#include <errno.h>
#ifdef LLVM_ON_WIN32
# include <Windows.h>
#else
#include <fcntl.h>
# include <unistd.h>
#endif

#define CACHESIZE 8

namespace cling {
  namespace utils {

#ifndef LLVM_ON_WIN32
    struct Cache {
    private:
      const void* lines[CACHESIZE];
      unsigned size, mostRecent;
    public:
      Cache(): lines{0,0,0,0,0,0,0,0}, size(CACHESIZE), mostRecent(0) {}
      bool findInCache(const void* P) {
        for (unsigned index = 0; index < size; index++) {
          if (lines[index] == P)
            return true;
        }
        return false;
      }
      void pushToCache(const void* P) {
        mostRecent = (mostRecent+1)%size;
        lines[mostRecent] = P;
      }
    };

    // Trying to be thread-safe.
    // Each thread creates a new cache when needed.
    static Cache& getCache() {
      static llvm::sys::ThreadLocal<Cache> threadCache;
      if (!threadCache.get()) {
        threadCache.set(new Cache());
      }
      return *threadCache.get();
    }

    static int getNullDevFileDescriptor() {
      struct FileDescriptor {
        int FD;
        const char* file = "/dev/null";
        FileDescriptor() { FD = open(file, O_WRONLY); }
        ~FileDescriptor() {
          close(FD);
        }
      };
      static FileDescriptor nullDev;
      return nullDev.FD;
    }
#endif

    // Checking whether the pointer points to a valid memory location
    bool isAddressValid(const void *P) {
      if (!P || P == (void *) -1)
        return false;

#ifdef LLVM_ON_WIN32
      MEMORY_BASIC_INFORMATION MBI;
      if (!VirtualQuery(P, &MBI, sizeof(MBI)))
        return false;
      if (MBI.State != MEM_COMMIT)
        return false;
      return true;
#else
      // Look-up the address in the cache.
      Cache& currentCache = getCache();
      if (currentCache.findInCache(P))
        return true;
      // There is a POSIX way of finding whether an address
      // can be accessed for reading.
      if (write(getNullDevFileDescriptor(), P, 1/*byte*/) != 1) {
        assert(errno == EFAULT && "unexpected write error at address");
        return false;
      }
      currentCache.pushToCache(P);
      return true;
#endif
    }
  }
}<commit_msg>Refinements: use std::array+find, comments, func names.<commit_after>//--------------------------------------------------------------------*- C++ -*-
// CLING - the C++ LLVM-based InterpreterG :)
// author:  <NAME> <<EMAIL>>
//
// This file is dual-licensed: you can choose to license it under the University
// of Illinois Open Source License or the GNU Lesser General Public License. See
// LICENSE.TXT for details.
//------------------------------------------------------------------------------

#include "cling/Utils/Validation.h"
#include "llvm/Support/ThreadLocal.h"

#include <assert.h>
#include <errno.h>
#ifdef LLVM_ON_WIN32
# include <Windows.h>
#else
#include <array>
#include <algorithm>
#include <fcntl.h>
# include <unistd.h>
#endif

namespace cling {
  namespace utils {

#ifndef LLVM_ON_WIN32
    // A simple round-robin cache: what enters first, leaves first.
    // MRU cache wasn't worth the extra CPU cycles.
    struct Cache {
    private:
      std::array<const void*, 8> lines;
      unsigned mostRecent = 0;
    public:
      bool contains(const void* P) {
        return std::find(lines.begin(), lines.end(), P) != lines.end();
      }
      void push(const void* P) {
        mostRecent = (mostRecent + 1) % lines.size();
        lines[mostRecent] = P;
      }
    };

    // Trying to be thread-safe.
    // Each thread creates a new cache when needed.
    static Cache& getCache() {
      static llvm::sys::ThreadLocal<Cache> threadCache;
      if (!threadCache.get()) {
        // Leak, 1 Cache/thread.
        threadCache.set(new Cache());
      }
      return *threadCache.get();
    }

    static int getNullDevFileDescriptor() {
      struct FileDescriptor {
        int FD;
        const char* file = "/dev/null";
        FileDescriptor() { FD = open(file, O_WRONLY); }
        ~FileDescriptor() {
          close(FD);
        }
      };
      static FileDescriptor nullDev;
      return nullDev.FD;
    }
#endif

    // Checking whether the pointer points to a valid memory location
    bool isAddressValid(const void *P) {
      if (!P || P == (void *) -1)
        return false;

#ifdef LLVM_ON_WIN32
      MEMORY_BASIC_INFORMATION MBI;
      if (!VirtualQuery(P, &MBI, sizeof(MBI)))
        return false;
      if (MBI.State != MEM_COMMIT)
        return false;
      return true;
#else
      // Look-up the address in the cache.
      Cache& currentCache = getCache();
      if (currentCache.contains(P))
        return true;
      // There is a POSIX way of finding whether an address
      // can be accessed for reading.
      if (write(getNullDevFileDescriptor(), P, 1/*byte*/) != 1) {
        assert(errno == EFAULT && "unexpected write error at address");
        return false;
      }
      currentCache.push(P);
      return true;
#endif
    }
  }
}
{'fraction_non_alphanumeric': 0.17073170731707318, 'fraction_numerical': 0.006142728093947606, 'mean_word_length': 2.003798155181769, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '6505474', 'n_tokens_mistral': 1664, 'n_tokens_neox': 1621, 'n_words': 492}