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

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

package sqlite3

import (
	"unsafe"

	"modernc.org/libc"
)

// C documentation
//
//	/*
//	** Set the soft heap-size limit for the library.  An argument of
//	** zero disables the limit.  A negative argument is a no-op used to
//	** obtain the return value.
//	**
//	** The return value is the value of the heap limit just before this
//	** interface was called.
//	**
//	** If the hard heap limit is enabled, then the soft heap limit cannot
//	** be disabled nor raised above the hard heap limit.
//	*/
func Xsqlite3_soft_heap_limit64(tls *libc.TLS, n Tsqlite3_int64) (r Tsqlite3_int64) {
	var excess, nUsed, priorLimit Tsqlite3_int64
	var rc int32
	_, _, _, _ = excess, nUsed, priorLimit, rc
	rc = Xsqlite3_initialize(tls)
	if rc != 0 {
		return int64(-int32(1))
	}
	Xsqlite3_mutex_enter(tls, _mem0.Fmutex)
	priorLimit = _mem0.FalarmThreshold
	if n < 0 {
		Xsqlite3_mutex_leave(tls, _mem0.Fmutex)
		return priorLimit
	}
	if _mem0.FhardLimit > 0 && (n > _mem0.FhardLimit || n == 0) {
		n = _mem0.FhardLimit
	}
	_mem0.FalarmThreshold = n
	nUsed = _sqlite3StatusValue(tls, SQLITE_STATUS_MEMORY_USED)
	libc.AtomicStoreNInt32(uintptr(unsafe.Pointer(&_mem0))+24, libc.BoolInt32(n > 0 && n <= nUsed), libc.Int32FromInt32(__ATOMIC_RELAXED))
	Xsqlite3_mutex_leave(tls, _mem0.Fmutex)
	excess = Xsqlite3_memory_used(tls) - n
	if excess > 0 {
		Xsqlite3_release_memory(tls, int32(excess&libc.Int64FromInt32(0x7fffffff)))
	}
	return priorLimit
}

func _first_valueFinalizeFunc(tls *libc.TLS, pCtx uintptr) {
	var p uintptr
	_ = p
	p = Xsqlite3_aggregate_context(tls, pCtx, int32(16))
	if p != 0 && (*TNthValueCtx)(unsafe.Pointer(p)).FpValue != 0 {
		Xsqlite3_result_value(tls, pCtx, (*TNthValueCtx)(unsafe.Pointer(p)).FpValue)
		Xsqlite3_value_free(tls, (*TNthValueCtx)(unsafe.Pointer(p)).FpValue)
		(*TNthValueCtx)(unsafe.Pointer(p)).FpValue = uintptr(0)
	}
}

func _first_valueStepFunc(tls *libc.TLS, pCtx uintptr, nArg int32, apArg uintptr) {
	var p uintptr
	_ = p
	p = Xsqlite3_aggregate_context(tls, pCtx, int32(16))
	if p != 0 && (*TNthValueCtx)(unsafe.Pointer(p)).FpValue == uintptr(0) {
		(*TNthValueCtx)(unsafe.Pointer(p)).FpValue = Xsqlite3_value_dup(tls, **(**uintptr)(__ccgo_up(apArg)))
		if !((*TNthValueCtx)(unsafe.Pointer(p)).FpValue != 0) {
			Xsqlite3_result_error_nomem(tls, pCtx)
		}
	}
	_ = nArg
	_ = apArg
}

func _fixBoundingBox(tls *libc.TLS, pRtree uintptr, pNode uintptr) (r int32) {
	bp := tls.Alloc(112)
	defer tls.Free(112)
	var nCell, rc int32
	var pParent uintptr
	var _ /* box at bp+8 */ TRtreeCell
	var _ /* cell at bp+56 */ TRtreeCell
	var _ /* ii at bp+0 */ int32
	_, _, _ = nCell, pParent, rc
	pParent = (*TRtreeNode)(unsafe.Pointer(pNode)).FpParent
	rc = SQLITE_OK
	if pParent != 0 {
		nCell = _readInt16(tls, (*TRtreeNode)(unsafe.Pointer(pNode)).FzData+2) /* Bounding box for pNode */
		_nodeGetCell(tls, pRtree, pNode, 0, bp+8)
		**(**int32)(__ccgo_up(bp)) = int32(1)
		for {
			if !(**(**int32)(__ccgo_up(bp)) < nCell) {
				break
			}
			_nodeGetCell(tls, pRtree, pNode, **(**int32)(__ccgo_up(bp)), bp+56)
			_cellUnion(tls, pRtree, bp+8, bp+56)
			goto _1
		_1:
			;
			**(**int32)(__ccgo_up(bp)) = **(**int32)(__ccgo_up(bp)) + 1
		}
		(**(**TRtreeCell)(__ccgo_up(bp + 8))).FiRowid = (*TRtreeNode)(unsafe.Pointer(pNode)).FiNode
		rc = _nodeParentIndex(tls, pRtree, pNode, bp)
		if rc == SQLITE_OK {
			_nodeOverwriteCell(tls, pRtree, pParent, bp+8, **(**int32)(__ccgo_up(bp)))
			rc = _fixBoundingBox(tls, pRtree, pParent)
		}
	}
	return rc
}

// C documentation
//
//	/*
//	** The iterator passed as the first argument has the following fields set
//	** as follows. This function sets up the rest of the iterator so that it
//	** points to the first rowid in the doclist-index.
//	**
//	**   pData:
//	**     pointer to doclist-index record,
//	**
//	** When this function is called pIter->iLeafPgno is the page number the
//	** doclist is associated with (the one featuring the term).
//	*/
func _fts5DlidxIterFirst(tls *libc.TLS, pIter uintptr) (r int32) {
	var i int32
	_ = i
	i = 0
	for {
		if !(i < (*TFts5DlidxIter)(unsafe.Pointer(pIter)).FnLvl) {
			break
		}
		_fts5DlidxLvlNext(tls, pIter+8+uintptr(i)*32)
		goto _1
	_1:
		;
		i = i + 1
	}
	return (*(*TFts5DlidxLvl)(unsafe.Pointer(pIter + 8))).FbEof
}

// C documentation
//
//	/*
//	** Free a doclist-index iterator object allocated by fts5DlidxIterInit().
//	*/
func _fts5DlidxIterFree(tls *libc.TLS, pIter uintptr) {
	var i int32
	_ = i
	if pIter != 0 {
		i = 0
		for {
			if !(i < (*TFts5DlidxIter)(unsafe.Pointer(pIter)).FnLvl) {
				break
			}
			_fts5DataRelease(tls, (*(*TFts5DlidxLvl)(unsafe.Pointer(pIter + 8 + uintptr(i)*32))).FpData)
			goto _1
		_1:
			;
			i = i + 1
		}
		Xsqlite3_free(tls, pIter)
	}
}

func _fts5WriteDlidxClear(tls *libc.TLS, p uintptr, pWriter uintptr, bFlush int32) {
	var i int32
	var pDlidx uintptr
	_, _ = i, pDlidx
	i = 0
	for {
		if !(i < (*TFts5SegWriter)(unsafe.Pointer(pWriter)).FnDlidx) {
			break
		}
		pDlidx = (*TFts5SegWriter)(unsafe.Pointer(pWriter)).FaDlidx + uintptr(i)*32
		if (*TFts5DlidxWriter)(unsafe.Pointer(pDlidx)).Fbuf.Fn == 0 {
			break
		}
		if bFlush != 0 {
			_fts5DataWrite(tls, p, int64((*TFts5SegWriter)(unsafe.Pointer(pWriter)).FiSegid)<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B)+libc.Int32FromInt32(FTS5_DATA_DLI_B))+int64(libc.Int32FromInt32(1))<<(libc.Int32FromInt32(FTS5_DATA_PAGE_B)+libc.Int32FromInt32(FTS5_DATA_HEIGHT_B))+int64(i)<<libc.Int32FromInt32(FTS5_DATA_PAGE_B)+int64((*TFts5DlidxWriter)(unsafe.Pointer(pDlidx)).Fpgno), (*TFts5DlidxWriter)(unsafe.Pointer(pDlidx)).Fbuf.Fp, (*TFts5DlidxWriter)(unsafe.Pointer(pDlidx)).Fbuf.Fn)
		}
		_sqlite3Fts5BufferZero(tls, pDlidx+16)
		(*TFts5DlidxWriter)(unsafe.Pointer(pDlidx)).FbPrevValid = 0
		goto _1
	_1:
		;
		i = i + 1
	}
}

// C documentation
//
//	/*
//	** Do a memory allocation with statistics and alarms.  Assume the
//	** lock is already held.
//	*/
func _mallocWithAlarm(tls *libc.TLS, n int32, pp uintptr) {
	var nFull int32
	var nUsed Tsqlite3_int64
	var p uintptr
	_, _, _ = nFull, nUsed, p
	/* In Firefox (circa 2017-02-08), xRoundup() is remapped to an internal
	 ** implementation of malloc_good_size(), which must be called in debug
	 ** mode and specifically when the DMD "Dark Matter Detector" is enabled
	 ** or else a crash results.  Hence, do not attempt to optimize out the
	 ** following xRoundup() call. */
	nFull = (*(*func(*libc.TLS, int32) int32)(unsafe.Pointer(&struct{ uintptr }{_sqlite3Config.Fm.FxRoundup})))(tls, n)
	_sqlite3StatusHighwater(tls, int32(SQLITE_STATUS_MALLOC_SIZE), n)
	if _mem0.FalarmThreshold > 0 {
		nUsed = _sqlite3StatusValue(tls, SQLITE_STATUS_MEMORY_USED)
		if nUsed >= _mem0.FalarmThreshold-int64(nFull) {
			libc.AtomicStoreNInt32(uintptr(unsafe.Pointer(&_mem0))+24, libc.Int32FromInt32(1), libc.Int32FromInt32(__ATOMIC_RELAXED))
			_sqlite3MallocAlarm(tls, nFull)
			if _mem0.FhardLimit != 0 {
				nUsed = _sqlite3StatusValue(tls, SQLITE_STATUS_MEMORY_USED)
				if nUsed >= _mem0.FhardLimit-int64(nFull) {
					**(**uintptr)(__ccgo_up(pp)) = uintptr(0)
					return
				}
			}
		} else {
			libc.AtomicStoreNInt32(uintptr(unsafe.Pointer(&_mem0))+24, libc.Int32FromInt32(0), libc.Int32FromInt32(__ATOMIC_RELAXED))
		}
	}
	p = (*(*func(*libc.TLS, int32) uintptr)(unsafe.Pointer(&struct{ uintptr }{_sqlite3Config.Fm.FxMalloc})))(tls, nFull)
	if p == uintptr(0) && _mem0.FalarmThreshold > 0 {
		_sqlite3MallocAlarm(tls, nFull)
		p = (*(*func(*libc.TLS, int32) uintptr)(unsafe.Pointer(&struct{ uintptr }{_sqlite3Config.Fm.FxMalloc})))(tls, nFull)
	}
	if p != 0 {
		nFull = _sqlite3MallocSize(tls, p)
		_sqlite3StatusUp(tls, SQLITE_STATUS_MEMORY_USED, nFull)
		_sqlite3StatusUp(tls, int32(SQLITE_STATUS_MALLOC_COUNT), int32(1))
	}
	**(**uintptr)(__ccgo_up(pp)) = p
}

func _reinsertNodeContent(tls *libc.TLS, pRtree uintptr, pNode uintptr) (r int32) {
	bp := tls.Alloc(64)
	defer tls.Free(64)
	var ii, nCell, rc, rc2 int32
	var _ /* cell at bp+8 */ TRtreeCell
	var _ /* pInsert at bp+0 */ uintptr
	_, _, _, _ = ii, nCell, rc, rc2
	rc = SQLITE_OK
	nCell = _readInt16(tls, (*TRtreeNode)(unsafe.Pointer(pNode)).FzData+2)
	ii = 0
	for {
		if !(rc == SQLITE_OK && ii < nCell) {
			break
		}
		_nodeGetCell(tls, pRtree, pNode, ii, bp+8)
		/* Find a node to store this cell in. pNode->iNode currently contains
		 ** the height of the sub-tree headed by the cell.
		 */
		rc = _ChooseLeaf(tls, pRtree, bp+8, int32((*TRtreeNode)(unsafe.Pointer(pNode)).FiNode), bp)
		if rc == SQLITE_OK {
			rc = _rtreeInsertCell(tls, pRtree, **(**uintptr)(__ccgo_up(bp)), bp+8, int32((*TRtreeNode)(unsafe.Pointer(pNode)).FiNode))
			rc2 = _nodeRelease(tls, pRtree, **(**uintptr)(__ccgo_up(bp)))
			if rc == SQLITE_OK {
				rc = rc2
			}
		}
		goto _1
	_1:
		;
		ii = ii + 1
	}
	return rc
}

// C documentation
//
//	/*
//	** Return true if the heap is currently under memory pressure - in other
//	** words if the amount of heap used is close to the limit set by
//	** sqlite3_soft_heap_limit().
//	*/
func _sqlite3HeapNearlyFull(tls *libc.TLS) (r int32) {
	return libc.AtomicLoadNInt32(uintptr(unsafe.Pointer(&_mem0))+24, libc.Int32FromInt32(__ATOMIC_RELAXED))
}

// C documentation
//
//	/* Allocate memory that is automatically freed when pWInfo is freed.
//	*/
func _sqlite3WhereMalloc(tls *libc.TLS, pWInfo uintptr, nByte Tu64) (r uintptr) {
	var pBlock uintptr
	_ = pBlock
	pBlock = _sqlite3DbMallocRawNN(tls, (*TParse)(unsafe.Pointer((*TWhereInfo)(unsafe.Pointer(pWInfo)).FpParse)).Fdb, nByte+uint64(16))
	if pBlock != 0 {
		(*TWhereMemBlock)(unsafe.Pointer(pBlock)).FpNext = (*TWhereInfo)(unsafe.Pointer(pWInfo)).FpMemToFree
		(*TWhereMemBlock)(unsafe.Pointer(pBlock)).Fsz = nByte
		(*TWhereInfo)(unsafe.Pointer(pWInfo)).FpMemToFree = pBlock
		pBlock += 16
	}
	return pBlock
}

func _sumInverse(tls *libc.TLS, context uintptr, argc int32, argv uintptr) {
	bp := tls.Alloc(16)
	defer tls.Free(16)
	var iVal Ti64
	var p uintptr
	var type1 int32
	var _ /* x at bp+0 */ Ti64
	_, _, _ = iVal, p, type1
	_ = argc
	p = Xsqlite3_aggregate_context(tls, context, int32(40))
	type1 = Xsqlite3_value_numeric_type(tls, **(**uintptr)(__ccgo_up(argv)))
	/* p is always non-NULL because sumStep() will have been called first
	 ** to initialize it */
	if p != 0 && type1 != int32(SQLITE_NULL) {
		(*TSumCtx)(unsafe.Pointer(p)).Fcnt = (*TSumCtx)(unsafe.Pointer(p)).Fcnt - 1
		if !((*TSumCtx)(unsafe.Pointer(p)).Fapprox != 0) {
			**(**Ti64)(__ccgo_up(bp)) = (*TSumCtx)(unsafe.Pointer(p)).FiSum
			if _sqlite3SubInt64(tls, bp, Xsqlite3_value_int64(tls, **(**uintptr)(__ccgo_up(argv)))) == 0 {
				(*TSumCtx)(unsafe.Pointer(p)).FiSum = **(**Ti64)(__ccgo_up(bp))
				return
			}
			(*TSumCtx)(unsafe.Pointer(p)).Fovrfl = uint8(1)
			(*TSumCtx)(unsafe.Pointer(p)).Fapprox = uint8(1)
			_kahanBabuskaNeumaierInit(tls, p, (*TSumCtx)(unsafe.Pointer(p)).FiSum)
		}
		if type1 == int32(SQLITE_INTEGER) {
			iVal = Xsqlite3_value_int64(tls, **(**uintptr)(__ccgo_up(argv)))
			if iVal != int64(-libc.Int32FromInt32(1))-(libc.Int64FromUint32(0xffffffff)|libc.Int64FromInt32(0x7fffffff)<<libc.Int32FromInt32(32)) {
				_kahanBabuskaNeumaierStepInt64(tls, p, -iVal)
			} else {
				_kahanBabuskaNeumaierStepInt64(tls, p, libc.Int64FromUint32(0xffffffff)|libc.Int64FromInt32(0x7fffffff)<<libc.Int32FromInt32(32))
				_kahanBabuskaNeumaierStepInt64(tls, p, int64(1))
			}
		} else {
			_kahanBabuskaNeumaierStep(tls, p, -Xsqlite3_value_double(tls, **(**uintptr)(__ccgo_up(argv))))
		}
	}
}
