<commit_before>/* f/loom.c
**
** This file is in the public domain.
*/
#include "all.h"
#include <sys/uio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sigsegv.h>
#include <termios.h>
#include <uv.h>
#include "v/vere.h"

/* _loom_stop(): signal handler to stop computation.
*/
static void
_loom_stop(c3_i sig)
{
}

static c3_i
_loom_sigsegv_handler(void* adr_v, c3_i ser_i) 
{
  if ( ser_i ) {
    c3_w*    bas_w = (c3_w*)(void *)(U2_OS_LoomBase);
    c3_w     off_w = (((c3_c*) adr_v) - (c3_c*)U2_OS_LoomBase) >> 2;
    c3_w     pag_w = off_w >> LoomPageWords;
    c3_w     win_w;
    u2_cheg* ceg_u;

    if ( pag_w > LoomAllPages ) {
      fprintf(stderr, "address %p out of loom!\r\n", adr_v);
      return 0;
    }
    c3_assert((u2_page_neat == LoomChem[pag_w].lif_e) ||
              (u2_page_none == LoomChem[pag_w].lif_e) );

    LoomChem[pag_w].lif_e = u2_page_tref;
    LoomChem[pag_w].mug_e = 0;

    for ( ceg_u = &LoomSegmentA; ceg_u; ceg_u = ceg_u->nex_u ) {
      if ( (pag_w >= ceg_u->bot_w) && 
           (win_w=(pag_w - ceg_u->bot_w)) < ceg_u->len_w )
      {
        if ( win_w >= ceg_u->pgs_w ) {
          ceg_u->pgs_w = win_w + 1;
        }
        break;
      }
    }
    if ( 0 == ceg_u ) {
      fprintf(stderr, "page %d is not in a segment!\n", pag_w);
      return 1;
    }

    if ( -1 == mprotect((void *)(bas_w + (pag_w << LoomPageWords)),
                        (1 << (LoomPageWords + 2)),
                        (PROT_READ | PROT_WRITE)) ) 
    {
      perror("mprotect");
      exit(1);
    }
    return 1;
  }
  return 0;
}

/* _loom_read(): full blocking read.
*/
static u2_bean
_loom_read(c3_i fid_i, void* buf_w, c3_w len_w)
{
  return ((4 * len_w) == read(fid_i, buf_w, (4 * len_w))) ? u2_yes : u2_no;
}

/* _loom_write(): full blocking write.
*/
static u2_bean
_loom_write(c3_i fid_i, void* buf_w, c3_w len_w)
{
  if ((4 * len_w) != write(fid_i, buf_w, (4 * len_w))) {
    fprintf(stderr, "couldn't write %d\n", fid_i);
    perror("huh?");
    exit(1);
  }
  else {
    fsync(fid_i);   //  for what it's worth
    return u2_yes;
  }
//  return ((4 * len_w) == write(fid_i, buf_w, (4 * len_w))) ? u2_yes : u2_no;
}

/* u2_loom_clip():
**
**   Clip top and bottom.
*/
void
u2_loom_clip(c3_w bot_w, c3_w top_w)
{
}<commit_msg>loom.c -- drop unnecessary fsync in _loom_write that causes lag on some systems.<commit_after>/* f/loom.c
**
** This file is in the public domain.
*/
#include "all.h"
#include <sys/uio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sigsegv.h>
#include <termios.h>
#include <uv.h>
#include "v/vere.h"

/* _loom_stop(): signal handler to stop computation.
*/
static void
_loom_stop(c3_i sig)
{
}

static c3_i
_loom_sigsegv_handler(void* adr_v, c3_i ser_i) 
{
  if ( ser_i ) {
    c3_w*    bas_w = (c3_w*)(void *)(U2_OS_LoomBase);
    c3_w     off_w = (((c3_c*) adr_v) - (c3_c*)U2_OS_LoomBase) >> 2;
    c3_w     pag_w = off_w >> LoomPageWords;
    c3_w     win_w;
    u2_cheg* ceg_u;

    if ( pag_w > LoomAllPages ) {
      fprintf(stderr, "address %p out of loom!\r\n", adr_v);
      return 0;
    }
    c3_assert((u2_page_neat == LoomChem[pag_w].lif_e) ||
              (u2_page_none == LoomChem[pag_w].lif_e) );

    LoomChem[pag_w].lif_e = u2_page_tref;
    LoomChem[pag_w].mug_e = 0;

    for ( ceg_u = &LoomSegmentA; ceg_u; ceg_u = ceg_u->nex_u ) {
      if ( (pag_w >= ceg_u->bot_w) && 
           (win_w=(pag_w - ceg_u->bot_w)) < ceg_u->len_w )
      {
        if ( win_w >= ceg_u->pgs_w ) {
          ceg_u->pgs_w = win_w + 1;
        }
        break;
      }
    }
    if ( 0 == ceg_u ) {
      fprintf(stderr, "page %d is not in a segment!\n", pag_w);
      return 1;
    }

    if ( -1 == mprotect((void *)(bas_w + (pag_w << LoomPageWords)),
                        (1 << (LoomPageWords + 2)),
                        (PROT_READ | PROT_WRITE)) ) 
    {
      perror("mprotect");
      exit(1);
    }
    return 1;
  }
  return 0;
}

/* _loom_read(): full blocking read.
*/
static u2_bean
_loom_read(c3_i fid_i, void* buf_w, c3_w len_w)
{
  return ((4 * len_w) == read(fid_i, buf_w, (4 * len_w))) ? u2_yes : u2_no;
}

/* _loom_write(): full blocking write.
*/
static u2_bean
_loom_write(c3_i fid_i, void* buf_w, c3_w len_w)
{
  if ((4 * len_w) != write(fid_i, buf_w, (4 * len_w))) {
    fprintf(stderr, "couldn't write %d\n", fid_i);
    perror("huh?");
    exit(1);
  }
  else {
    return u2_yes;
  }
//  return ((4 * len_w) == write(fid_i, buf_w, (4 * len_w))) ? u2_yes : u2_no;
}

/* u2_loom_clip():
**
**   Clip top and bottom.
*/
void
u2_loom_clip(c3_w bot_w, c3_w top_w)
{
}
{'fraction_non_alphanumeric': 0.16706757052263285, 'fraction_numerical': 0.022304832713754646, 'mean_word_length': 2.4916030534351146, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 41, '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': '6968846', 'n_tokens_mistral': 2162, 'n_tokens_neox': 2113, 'n_words': 416}