Example #1
0
SMP_STATUS smp_test_file2memmap(const char *file)
{
    int fd = 0;
    void *start = NULL;
    off_t offset = 0;
    char buf[1024];
    char *new_char = "343345678900987654321****";

    if ((fd = smp_open(file, SMP_FILE_RDWD, SMP_FILE_DEF_MODE)) == SMP_FAILURE) {
        smp_print_error();
        return SMP_FAILURE;
    }

    if ((offset = smp_file_seek_end_offset(fd, 10)) == SMP_FAILURE) {
        smp_print_error();
        return SMP_FAILURE;
    }

    printf("%d\n", (int)smp_write(fd, "~", 1));  //must to do that before mmap

    if ((start = smp_file2memmap(fd, SMP_MAP_PROT_WD, SMP_MAP_SHARED)) == NULL) {
        smp_print_error();
        return SMP_FAILURE;
    }

    printf("the file in mem is : %s\n", (char *) start);

    memcpy(start, new_char, strlen(new_char));

    if (smp_file2memmap_remove(start, fd) == SMP_FAILURE) {
        smp_print_error();
        return SMP_FAILURE;
    }

    smp_close(fd);

    if ((fd = smp_open(file, SMP_FILE_RDWD, SMP_FILE_DEF_MODE)) == SMP_FAILURE) {
        smp_print_error();
        return SMP_FAILURE;
    }

    memset(buf, '\0', 1024);
    smp_read(fd, buf, 1024);
    printf("after mmap, the file was : %s\n", buf);

    return SMP_OK;
}
void loop_step(const char * input) {

   char string[65536];
   char buffer[256];

   // read a line

   if (input != NULL) {
      get_local(string,65536,input);
   } else {
      get_stdin(string,65536);
   }

   // parse

   if (false) {

   } else if (string_start_with(string,"debug ")) {

      if (!Searching) {
         parse_debug(string);
      } else {
         ASSERT(false);
      }

   } else if (string_start_with(string,"go ")) {

      if (!Searching && !Delay) {
         init();
         parse_go(string);
      } else {
         ASSERT(false);
      }

   } else if (string_equal(string,"isready")) {

      if (!Searching && !Delay) {
         init();
      }

      send("readyok"); // no need to wait when searching (fixit SMK)

   } else if (string_equal(string,"ponderhit")) {

      if (Searching) {

         ASSERT(Infinite);

         SearchInput->infinite = false;
         Infinite = false;

      } else if (Delay) {

         send_best_move();
         Delay = false;

      } else {

         ASSERT(false);
      }

   } else if (string_start_with(string,"position ")) {

      if (!Searching && !Delay) {
         init();
         parse_position(string);
      } else {
         ASSERT(false);
      }

   } else if (string_equal(string,"quit")) {

      ASSERT(!Searching); // violated by Arena UI
      ASSERT(!Delay);

      if (Searching && option_get_int("Threads") > 1) {

         // Arena issue

         SearchInfo->stop = true;
         Infinite = false;

         smp_sleep();
      }

      trans_free();
      tb_close();
      egbb_close();
      smp_close();

      exit(EXIT_SUCCESS);

   } else if (string_start_with(string,"setoption ")) {

      if (!Searching && !Delay) {
         parse_setoption(string);
      } else {
         ASSERT(false);
      }

   } else if (string_equal(string,"stop")) {

      if (Searching) {

         SearchInfo->stop = true;
         Infinite = false;

      } else if (Delay) {

         send_best_move();
         Delay = false;
      }

   } else if (string_equal(string,"uci")) {

      ASSERT(!Searching);
      ASSERT(!Delay);

      send("id name Fruit reloaded %s", my_version(buffer));
      send("id author Fabien Letouzey, Ryan Benitez and Daniel Mehrmann");

      option_list();

      send("uciok");

   } else if (string_equal(string,"ucinewgame")) {

      if (!Searching && !Delay && Init) {
         trans_clear();
         pawn_clear();
         material_clear();
      } else {
         ASSERT(false);
      }
   }
}