// Code generated by modernc.org/undup from the per-target sqlite_*.go files; DO NOT EDIT.

//go:build (darwin && amd64) || (darwin && arm64) || (freebsd && 386) || (freebsd && amd64) || (freebsd && arm) || (freebsd && arm64) || (linux && 386) || (linux && amd64) || (linux && arm) || (linux && arm64) || (linux && loong64) || (linux && ppc64le) || (linux && riscv64) || (linux && s390x) || (windows && (amd64 || arm64)) || (windows && 386)

package sqlite3

import (
	"unsafe"

	"modernc.org/libc"
)

const SQLITE_MAX_MMAP_SIZE = 2147418112

// C documentation
//
//	/*
//	** Begin a read transaction on the WAL.
//	**
//	** This routine used to be called "pagerOpenSnapshot()" because it essentially
//	** makes a snapshot of the database at the current point in time and preserves
//	** that snapshot for use by the reader in spite of concurrently changes by
//	** other writers or checkpointers.
//	*/
func _pagerBeginReadTransaction(tls *libc.TLS, pPager uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var rc int32
	var _ /* changed at bp+0 */ int32
	_ = rc                         /* Return code */
	**(**int32)(__ccgo_up(bp)) = 0 /* True if cache must be reset */
	/* sqlite3WalEndReadTransaction() was not called for the previous
	 ** transaction in locking_mode=EXCLUSIVE.  So call it now.  If we
	 ** are in locking_mode=NORMAL and EndRead() was previously called,
	 ** the duplicate call is harmless.
	 */
	_sqlite3WalEndReadTransaction(tls, (*TPager)(unsafe.Pointer(pPager)).FpWal)
	rc = _sqlite3WalBeginReadTransaction(tls, (*TPager)(unsafe.Pointer(pPager)).FpWal, bp)
	if rc != SQLITE_OK || **(**int32)(__ccgo_up(bp)) != 0 {
		_pager_reset(tls, pPager)
		if (*TPager)(unsafe.Pointer(pPager)).FbUseFetch != 0 {
			_sqlite3OsUnfetch(tls, (*TPager)(unsafe.Pointer(pPager)).Ffd, 0, uintptr(0))
		}
	}
	return rc
}

// C documentation
//
//	/*
//	** Invoke SQLITE_FCNTL_MMAP_SIZE based on the current value of szMmap.
//	*/
func _pagerFixMaplimit(tls *libc.TLS, pPager uintptr) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var fd uintptr
	var _ /* sz at bp+0 */ Tsqlite3_int64
	_ = fd
	fd = (*TPager)(unsafe.Pointer(pPager)).Ffd
	if (*Tsqlite3_file)(unsafe.Pointer(fd)).FpMethods != uintptr(0) && (*Tsqlite3_io_methods)(unsafe.Pointer((*Tsqlite3_file)(unsafe.Pointer(fd)).FpMethods)).FiVersion >= int32(3) {
		**(**Tsqlite3_int64)(__ccgo_up(bp)) = (*TPager)(unsafe.Pointer(pPager)).FszMmap
		(*TPager)(unsafe.Pointer(pPager)).FbUseFetch = libc.BoolUint8(**(**Tsqlite3_int64)(__ccgo_up(bp)) > libc.Int64FromInt32(0))
		_setGetterMethod(tls, pPager)
		_sqlite3OsFileControlHint(tls, (*TPager)(unsafe.Pointer(pPager)).Ffd, int32(SQLITE_FCNTL_MMAP_SIZE), bp)
	}
}

// C documentation
//
//	/*
//	** Set the Pager.xGet method for the appropriate routine used to fetch
//	** content from the pager.
//	*/
func _setGetterMethod(tls *libc.TLS, pPager uintptr) {
	if (*TPager)(unsafe.Pointer(pPager)).FerrCode != 0 {
		(*TPager)(unsafe.Pointer(pPager)).FxGet = __ccgo_fp(_getPageError)
	} else {
		if (*TPager)(unsafe.Pointer(pPager)).FbUseFetch != 0 {
			(*TPager)(unsafe.Pointer(pPager)).FxGet = __ccgo_fp(_getPageMMap)
		} else {
			(*TPager)(unsafe.Pointer(pPager)).FxGet = __ccgo_fp(_getPageNormal)
		}
	}
}

// C documentation
//
//	/*
//	** Change the limit on the amount of the database file that may be
//	** memory mapped.
//	*/
func _sqlite3BtreeSetMmapLimit(tls *libc.TLS, p uintptr, szMmap Tsqlite3_int64) (r int32) {
	var pBt uintptr
	_ = pBt
	pBt = (*TBtree)(unsafe.Pointer(p)).FpBt
	_sqlite3BtreeEnter(tls, p)
	_sqlite3PagerSetMmapLimit(tls, (*TBtShared)(unsafe.Pointer(pBt)).FpPager, szMmap)
	_sqlite3BtreeLeave(tls, p)
	return SQLITE_OK
}

// C documentation
//
//	/*
//	** The following singleton contains the global configuration for
//	** the SQLite library.
//	*/
var _sqlite3Config = TSqlite3Config{
	FbCoreMutex:          uint8(1),
	FbFullMutex:          libc.BoolUint8(true),
	FbUseCis:             uint8(SQLITE_ALLOW_COVERING_INDEX_SCAN),
	FbExtraSchemaChecks:  uint8(1),
	FmxStrlen:            int32(0x7ffffffe),
	FszLookaside:         int32(1200),
	FnLookaside:          int32(40),
	FnStmtSpill:          libc.Int32FromInt32(64) * libc.Int32FromInt32(1024),
	FmxMmap:              int64(SQLITE_MAX_MMAP_SIZE),
	FnPage:               int32(SQLITE_DEFAULT_PCACHE_INITSZ),
	FszPma:               uint32(SQLITE_SORTER_PMASZ),
	FmxMemdbSize:         int64(SQLITE_MEMDB_DEFAULT_MAXSIZE),
	FiOnceResetThreshold: int32(0x7ffffffe),
	FszSorterRef:         uint32(SQLITE_DEFAULT_SORTERREF_SIZE),
}

// C documentation
//
//	/* The real implementation of xFetch and xUnfetch */
func _sqlite3OsFetch(tls *libc.TLS, id uintptr, iOff Ti64, iAmt int32, pp uintptr) (r int32) {
	return (*(*func(*libc.TLS, uintptr, Tsqlite3_int64, int32, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_io_methods)(unsafe.Pointer((*Tsqlite3_file)(unsafe.Pointer(id)).FpMethods)).FxFetch})))(tls, id, iOff, iAmt, pp)
}

func _sqlite3OsUnfetch(tls *libc.TLS, id uintptr, iOff Ti64, p uintptr) (r int32) {
	return (*(*func(*libc.TLS, uintptr, Tsqlite3_int64, uintptr) int32)(unsafe.Pointer(&struct{ uintptr }{(*Tsqlite3_io_methods)(unsafe.Pointer((*Tsqlite3_file)(unsafe.Pointer(id)).FpMethods)).FxUnfetch})))(tls, id, iOff, p)
}

// C documentation
//
//	/*
//	** Allocate space for a file-handle and open a temporary file. If successful,
//	** set *ppFd to point to the malloc'd file-handle and return SQLITE_OK.
//	** Otherwise, set *ppFd to 0 and return an SQLite error code.
//	*/
func _vdbeSorterOpenTempFile(tls *libc.TLS, db uintptr, nExtend Ti64, ppFd uintptr) (r int32) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var _ /* max at bp+8 */ Ti64
	var _ /* rc at bp+0 */ int32
	if _sqlite3FaultSim(tls, int32(202)) != 0 {
		return libc.Int32FromInt32(SQLITE_IOERR) | libc.Int32FromInt32(13)<<libc.Int32FromInt32(8)
	}
	**(**int32)(__ccgo_up(bp)) = _sqlite3OsOpenMalloc(tls, (*Tsqlite3)(unsafe.Pointer(db)).FpVfs, uintptr(0), ppFd, libc.Int32FromInt32(SQLITE_OPEN_TEMP_JOURNAL)|libc.Int32FromInt32(SQLITE_OPEN_READWRITE)|libc.Int32FromInt32(SQLITE_OPEN_CREATE)|libc.Int32FromInt32(SQLITE_OPEN_EXCLUSIVE)|libc.Int32FromInt32(SQLITE_OPEN_DELETEONCLOSE), bp)
	if **(**int32)(__ccgo_up(bp)) == SQLITE_OK {
		**(**Ti64)(__ccgo_up(bp + 8)) = int64(SQLITE_MAX_MMAP_SIZE)
		_sqlite3OsFileControlHint(tls, **(**uintptr)(__ccgo_up(ppFd)), int32(SQLITE_FCNTL_MMAP_SIZE), bp+8)
		if nExtend > 0 {
			_vdbeSorterExtendFile(tls, db, **(**uintptr)(__ccgo_up(ppFd)), nExtend)
		}
	}
	return **(**int32)(__ccgo_up(bp))
}

type max_align_t = Tmax_align_t
