// Platform notes: tmpfs/ENOSPC, WSL1/ENOLCK, Windows notes
// Textbook section: Volume V, chapter 28, "Platform specifics", §28.2/§28.3.
// What it demonstrates: robust handling of geometry errors on exotic
//   filesystems; the caveat snippets themselves are given in the textbook.
//
// Brief notes (full snippets — in §28.2/§28.3):
//   - tmpfs (e.g. /dev/shm): size is limited by memory; mdbx_env_set_geometry
//     with a large upper may fail with ENOSPC/EDQUOT when the file grows.
//   - WSL1: old versions do not support fcntl(F_SETLK) on some filesystems —
//     MDBX_ENOLCK is possible; workaround — store the DB on ext4 inside WSL.
//   - Windows: 32-bit builds require linking with the LARGEADDRESSAWARE flag
//     to support large address spaces and databases > 2 GB.
//
// Build: g++ -std=c++17 -I<libmdbx-include> -I../common 35-platform-notes.c++ -lmdbx
// Run: ./35-platform-notes
// Expected output:
//   geometry probe: <result>
//   ok: platform error handling shown
// On failure — message to stderr and exit 1.
//
// SPDX-License-Identifier: Apache-2.0

#include <iostream>
#include <string>

#include "common.h++"
#include <mdbx.h++>

int main() {
  try {
    const std::string path = std::string(example::tmpdir()) + "/mdbx-35-platform-notes.mdbx";
    mdbx::env::remove(path);
    mdbx::env_managed env = example::env_open(path);

    // Probing geometry limits must not crash: we request an "absurd"
    // upper bound and handle the result carefully.
    mdbx::env::geometry probe;
#if INTPTR_MAX > 0x7fffFFFFl
    probe.size_upper = intptr_t(8) * mdbx::env::geometry::TB;
#else
    probe.size_upper = mdbx::env::geometry::maximal_value;
#endif
    try {
      env.set_geometry(probe);
      std::cout << "geometry probe: accepted\n";
    } catch (const std::exception &ex) {
      // On tmpfs/WSL1/32-bit Windows this may fail —
      // for the example it matters that the error is caught, not that it crashed the process.
      std::cout << "geometry probe: rejected (" << ex.what() << ")\n";
    }

    std::cout << "ok: platform error handling shown\n";
    return EXIT_SUCCESS;
  } catch (const std::exception &ex) {
    std::cerr << "error: " << ex.what() << "\n";
    return EXIT_FAILURE;
  }
}