pgrx_pg_sys/
port.rs

1use crate as pg_sys;
2use crate::BLCKSZ;
3use core::mem::offset_of;
4use core::str::FromStr;
5
6/// this comes from `postgres_ext.h`
7pub const InvalidOid: crate::Oid = crate::Oid::INVALID;
8pub const InvalidOffsetNumber: super::OffsetNumber = 0;
9pub const FirstOffsetNumber: super::OffsetNumber = 1;
10pub const MaxOffsetNumber: super::OffsetNumber =
11    (super::BLCKSZ as usize / std::mem::size_of::<super::ItemIdData>()) as super::OffsetNumber;
12pub const InvalidBlockNumber: u32 = 0xFFFF_FFFF as crate::BlockNumber;
13pub const VARHDRSZ: usize = std::mem::size_of::<super::int32>();
14pub const InvalidCommandId: super::CommandId = (!(0 as super::CommandId)) as super::CommandId;
15pub const FirstCommandId: super::CommandId = 0 as super::CommandId;
16pub const InvalidTransactionId: crate::TransactionId = crate::TransactionId::INVALID;
17pub const BootstrapTransactionId: crate::TransactionId = crate::TransactionId::BOOTSTRAP;
18pub const FrozenTransactionId: crate::TransactionId = crate::TransactionId::FROZEN;
19pub const FirstNormalTransactionId: crate::TransactionId = crate::TransactionId::FIRST_NORMAL;
20pub const MaxTransactionId: crate::TransactionId = crate::TransactionId::MAX;
21
22/// Given a valid HeapTuple pointer, return address of the user data
23///
24/// # Safety
25///
26/// This function cannot determine if the `tuple` argument is really a non-null pointer to a [`pg_sys::HeapTuple`].
27#[inline(always)]
28pub unsafe fn GETSTRUCT(tuple: crate::HeapTuple) -> *mut std::os::raw::c_char {
29    // #define GETSTRUCT(TUP) ((char *) ((TUP)->t_data) + (TUP)->t_data->t_hoff)
30
31    // SAFETY:  The caller has asserted `tuple` is a valid HeapTuple and is properly aligned
32    // Additionally, t_data.t_hoff is an a u8, so it'll fit inside a usize
33    (*tuple).t_data.cast::<std::os::raw::c_char>().add((*(*tuple).t_data).t_hoff as _)
34}
35
36//
37// TODO: [`TYPEALIGN`] and [`MAXALIGN`] are also part of PR #948 and when that's all merged,
38//       their uses should be switched to these
39//
40
41#[allow(non_snake_case)]
42#[inline(always)]
43pub const unsafe fn TYPEALIGN(alignval: usize, len: usize) -> usize {
44    // #define TYPEALIGN(ALIGNVAL,LEN)  \
45    // (((uintptr_t) (LEN) + ((ALIGNVAL) - 1)) & ~((uintptr_t) ((ALIGNVAL) - 1)))
46    ((len) + ((alignval) - 1)) & !((alignval) - 1)
47}
48
49#[allow(non_snake_case)]
50#[inline(always)]
51pub const unsafe fn MAXALIGN(len: usize) -> usize {
52    // #define MAXALIGN(LEN) TYPEALIGN(MAXIMUM_ALIGNOF, (LEN))
53    TYPEALIGN(pg_sys::MAXIMUM_ALIGNOF as _, len)
54}
55
56///  Given a currently-allocated chunk of Postgres allocated memory, determine the context
57///  it belongs to.
58///
59/// All chunks allocated by any memory context manager are required to be
60/// preceded by the corresponding MemoryContext stored, without padding, in the
61/// preceding sizeof(void*) bytes.  A currently-allocated chunk must contain a
62/// backpointer to its owning context.  The backpointer is used by pfree() and
63/// repalloc() to find the context to call.
64///
65/// # Safety
66///
67/// The specified `pointer` **must** be one allocated by Postgres (via [`palloc`] and friends).
68///
69///
70/// # Panics
71///
72/// This function will panic if `pointer` is null, if it's not properly aligned, or if the memory
73/// it points to doesn't have a prefix that looks like a memory context pointer
74///
75/// [`palloc`]: crate::palloc
76#[allow(non_snake_case)]
77pub unsafe fn GetMemoryChunkContext(pointer: *mut std::os::raw::c_void) -> pg_sys::MemoryContext {
78    #[cfg(any(feature = "pg13", feature = "pg14", feature = "pg15"))]
79    {
80        // Postgres versions <16 don't export the "GetMemoryChunkContext" function.  It's a "static inline"
81        // function in `memutils.h`, so we port it to Rust right here
82        /*
83         * Try to detect bogus pointers handed to us, poorly though we can.
84         * Presumably, a pointer that isn't MAXALIGNED isn't pointing at an
85         * allocated chunk.
86         */
87        assert!(!pointer.is_null());
88        assert_eq!(pointer, MAXALIGN(pointer as usize) as *mut ::std::os::raw::c_void);
89
90        /*
91         * OK, it's probably safe to look at the context.
92         */
93        // 	context = *(MemoryContext *) (((char *) pointer) - sizeof(void *));
94        let context = unsafe {
95            // SAFETY: the caller has assured us that `pointer` points to palloc'd memory, which
96            // means it'll have this header before it
97            *(pointer
98                .cast::<::std::os::raw::c_char>()
99                .sub(std::mem::size_of::<*mut ::std::os::raw::c_void>())
100                .cast())
101        };
102
103        assert!(MemoryContextIsValid(context));
104
105        context
106    }
107    #[cfg(any(feature = "pg16", feature = "pg17", feature = "pg18"))]
108    {
109        #[pgrx_macros::pg_guard]
110        unsafe extern "C-unwind" {
111            #[link_name = "GetMemoryChunkContext"]
112            pub fn extern_fn(pointer: *mut std::os::raw::c_void) -> pg_sys::MemoryContext;
113        }
114        extern_fn(pointer)
115    }
116}
117
118/// Returns true if memory context is tagged correctly according to Postgres.
119///
120/// # Safety
121///
122/// The caller must only attempt this on a pointer to a Node.
123/// This may clarify if the pointee is correctly-initialized [`pg_sys::MemoryContextData`].
124///
125/// # Implementation Note
126///
127/// If Postgres adds more memory context types in the future, we need to do that here too.
128#[allow(non_snake_case)]
129#[inline(always)]
130pub unsafe fn MemoryContextIsValid(context: crate::MemoryContext) -> bool {
131    // #define MemoryContextIsValid(context) \
132    // 	((context) != NULL && \
133    // 	 (IsA((context), AllocSetContext) || \
134    // 	  IsA((context), SlabContext) || \
135    // 	  IsA((context), GenerationContext)))
136
137    !context.is_null()
138        && unsafe {
139            // SAFETY:  we just determined the pointer isn't null, and
140            // the caller asserts that it is being used on a Node.
141            let tag = (*context.cast::<crate::Node>()).type_;
142            use crate::NodeTag::*;
143            matches!(tag, T_AllocSetContext | T_SlabContext | T_GenerationContext)
144        }
145}
146
147pub const VARHDRSZ_EXTERNAL: usize = offset_of!(super::varattrib_1b_e, va_data);
148pub const VARHDRSZ_SHORT: usize = offset_of!(super::varattrib_1b, va_data);
149
150#[inline]
151pub fn get_pg_major_version_string() -> &'static str {
152    super::PG_MAJORVERSION.to_str().unwrap()
153}
154
155#[inline]
156pub fn get_pg_major_version_num() -> u16 {
157    u16::from_str(super::get_pg_major_version_string()).unwrap()
158}
159
160#[cfg(any(not(target_env = "msvc"), feature = "pg17", feature = "pg18"))]
161#[inline]
162pub fn get_pg_version_string() -> &'static str {
163    super::PG_VERSION_STR.to_str().unwrap()
164}
165
166#[cfg(all(
167    target_env = "msvc",
168    any(feature = "pg13", feature = "pg14", feature = "pg15", feature = "pg16")
169))]
170#[inline]
171pub fn get_pg_version_string() -> &'static str {
172    // bindgen cannot get value of PG_VERSION_STR
173    // PostgreSQL @0@ on @1@-@2@, compiled by @3@-@4@, @5@-bit
174    static PG_VERSION_STR: [u8; 256] = const {
175        let major = super::PG_MAJORVERSION_NUM;
176        let minor = super::PG_MINORVERSION_NUM;
177        #[cfg(target_pointer_width = "32")]
178        let pointer_width = 32_u32;
179        #[cfg(target_pointer_width = "64")]
180        let pointer_width = 64_u32;
181        // a fake value
182        let msc_ver = b"1700";
183        let mut buffer = [0u8; 256];
184        let mut pointer = 0;
185        {
186            let s = b"PostgreSQL ";
187            let mut i = 0;
188            while i < s.len() {
189                buffer[pointer + i] = s[i];
190                i += 1;
191            }
192            pointer += s.len();
193        }
194        {
195            buffer[pointer + 0] = b'0' + (major / 10) as u8;
196            buffer[pointer + 1] = b'0' + (major % 10) as u8;
197            pointer += 2;
198        }
199        {
200            let s = b".";
201            let mut i = 0;
202            while i < s.len() {
203                buffer[pointer + i] = s[i];
204                i += 1;
205            }
206            pointer += s.len();
207        }
208        if minor < 10 {
209            buffer[pointer + 0] = b'0' + (minor % 10) as u8;
210            pointer += 1;
211        } else {
212            buffer[pointer + 0] = b'0' + (minor / 10) as u8;
213            buffer[pointer + 1] = b'0' + (minor % 10) as u8;
214            pointer += 2;
215        }
216        {
217            let s = b", compiled by Visual C++ build ";
218            let mut i = 0;
219            while i < s.len() {
220                buffer[pointer + i] = s[i];
221                i += 1;
222            }
223            pointer += s.len();
224        }
225        {
226            let s = msc_ver;
227            let mut i = 0;
228            while i < s.len() {
229                buffer[pointer + i] = s[i];
230                i += 1;
231            }
232            pointer += s.len();
233        }
234        {
235            let s = b", ";
236            let mut i = 0;
237            while i < s.len() {
238                buffer[pointer + i] = s[i];
239                i += 1;
240            }
241            pointer += s.len();
242        }
243        {
244            buffer[pointer + 0] = b'0' + (pointer_width / 10) as u8;
245            buffer[pointer + 1] = b'0' + (pointer_width % 10) as u8;
246            pointer += 2;
247        }
248        {
249            let s = b"-bit";
250            let mut i = 0;
251            while i < s.len() {
252                buffer[pointer + i] = s[i];
253                i += 1;
254            }
255            pointer += s.len();
256        }
257        buffer[pointer] = 0;
258        buffer
259    };
260    unsafe { std::ffi::CStr::from_ptr(PG_VERSION_STR.as_ptr().cast()).to_str().unwrap() }
261}
262
263#[inline]
264pub fn get_pg_major_minor_version_string() -> &'static str {
265    super::PG_VERSION.to_str().unwrap()
266}
267
268#[inline]
269pub fn TransactionIdIsNormal(xid: super::TransactionId) -> bool {
270    xid >= FirstNormalTransactionId
271}
272
273/// ```c
274///     #define type_is_array(typid)  (get_element_type(typid) != InvalidOid)
275/// ```
276#[inline]
277pub unsafe fn type_is_array(typoid: super::Oid) -> bool {
278    super::get_element_type(typoid) != InvalidOid
279}
280
281/// #define BufferGetPage(buffer) ((Page)BufferGetBlock(buffer))
282#[inline]
283pub unsafe fn BufferGetPage(buffer: crate::Buffer) -> crate::Page {
284    BufferGetBlock(buffer) as crate::Page
285}
286
287/// #define BufferGetBlock(buffer) \
288/// ( \
289///      AssertMacro(BufferIsValid(buffer)), \
290///      BufferIsLocal(buffer) ? \
291///            LocalBufferBlockPointers[-(buffer) - 1] \
292///      : \
293///            (Block) (BufferBlocks + ((Size) ((buffer) - 1)) * BLCKSZ) \
294/// )
295#[inline]
296pub unsafe fn BufferGetBlock(buffer: crate::Buffer) -> crate::Block {
297    if BufferIsLocal(buffer) {
298        *crate::LocalBufferBlockPointers.offset(((-buffer) - 1) as isize)
299    } else {
300        crate::BufferBlocks.add(((buffer as crate::Size) - 1) * crate::BLCKSZ as usize)
301            as crate::Block
302    }
303}
304
305/// #define BufferIsLocal(buffer)      ((buffer) < 0)
306#[inline]
307pub unsafe fn BufferIsLocal(buffer: crate::Buffer) -> bool {
308    buffer < 0
309}
310
311/// Retrieve the "user data" of the specified [`pg_sys::HeapTuple`] as a specific type. Typically this
312/// will be a struct that represents a Postgres system catalog, such as [`FormData_pg_class`].
313///
314/// # Returns
315///
316/// A pointer to the [`pg_sys::HeapTuple`]'s "user data", cast as a mutable pointer to `T`.  If the
317/// specified `htup` pointer is null, the null pointer is returned.
318///
319/// # Safety
320///
321/// This function cannot verify that the specified `htup` points to a valid [`pg_sys::HeapTuple`] nor
322/// that if it does, that its bytes are bitwise compatible with `T`.
323///
324/// [`FormData_pg_class`]: crate::FormData_pg_class
325#[inline]
326pub unsafe fn heap_tuple_get_struct<T>(htup: super::HeapTuple) -> *mut T {
327    if htup.is_null() {
328        std::ptr::null_mut()
329    } else {
330        unsafe {
331            // SAFETY:  The caller has told us `htop` is a valid HeapTuple
332            GETSTRUCT(htup).cast()
333        }
334    }
335}
336
337// All of this weird code is in response to Postgres having a relatively cavalier attitude about types:
338// - https://github.com/postgres/postgres/commit/1c27d16e6e5c1f463bbe1e9ece88dda811235165
339//
340// As a result, we redeclare their functions with the arguments they should have on earlier Postgres
341// and we route people to the old symbols they were using before on later ones.
342#[cfg(any(feature = "pg13", feature = "pg14", feature = "pg15"))]
343#[::pgrx_macros::pg_guard]
344extern "C-unwind" {
345    pub fn planstate_tree_walker(
346        planstate: *mut super::PlanState,
347        walker: ::core::option::Option<
348            unsafe extern "C-unwind" fn(*mut super::PlanState, *mut ::core::ffi::c_void) -> bool,
349        >,
350        context: *mut ::core::ffi::c_void,
351    ) -> bool;
352
353    pub fn query_tree_walker(
354        query: *mut super::Query,
355        walker: ::core::option::Option<
356            unsafe extern "C-unwind" fn(*mut super::Node, *mut ::core::ffi::c_void) -> bool,
357        >,
358        context: *mut ::core::ffi::c_void,
359        flags: ::core::ffi::c_int,
360    ) -> bool;
361
362    pub fn query_or_expression_tree_walker(
363        node: *mut super::Node,
364        walker: ::core::option::Option<
365            unsafe extern "C-unwind" fn(*mut super::Node, *mut ::core::ffi::c_void) -> bool,
366        >,
367        context: *mut ::core::ffi::c_void,
368        flags: ::core::ffi::c_int,
369    ) -> bool;
370
371    pub fn range_table_entry_walker(
372        rte: *mut super::RangeTblEntry,
373        walker: ::core::option::Option<
374            unsafe extern "C-unwind" fn(*mut super::Node, *mut ::core::ffi::c_void) -> bool,
375        >,
376        context: *mut ::core::ffi::c_void,
377        flags: ::core::ffi::c_int,
378    ) -> bool;
379
380    pub fn range_table_walker(
381        rtable: *mut super::List,
382        walker: ::core::option::Option<
383            unsafe extern "C-unwind" fn(*mut super::Node, *mut ::core::ffi::c_void) -> bool,
384        >,
385        context: *mut ::core::ffi::c_void,
386        flags: ::core::ffi::c_int,
387    ) -> bool;
388
389    pub fn expression_tree_walker(
390        node: *mut super::Node,
391        walker: ::core::option::Option<
392            unsafe extern "C-unwind" fn(*mut super::Node, *mut ::core::ffi::c_void) -> bool,
393        >,
394        context: *mut ::core::ffi::c_void,
395    ) -> bool;
396
397    pub fn raw_expression_tree_walker(
398        node: *mut super::Node,
399        walker: ::core::option::Option<
400            unsafe extern "C-unwind" fn(*mut super::Node, *mut ::core::ffi::c_void) -> bool,
401        >,
402        context: *mut ::core::ffi::c_void,
403    ) -> bool;
404}
405
406#[cfg(any(feature = "pg16", feature = "pg17", feature = "pg18"))]
407pub unsafe fn planstate_tree_walker(
408    planstate: *mut super::PlanState,
409    walker: ::core::option::Option<
410        unsafe extern "C-unwind" fn(*mut super::PlanState, *mut ::core::ffi::c_void) -> bool,
411    >,
412    context: *mut ::core::ffi::c_void,
413) -> bool {
414    crate::planstate_tree_walker_impl(planstate, walker, context)
415}
416
417#[cfg(any(feature = "pg16", feature = "pg17", feature = "pg18"))]
418pub unsafe fn query_tree_walker(
419    query: *mut super::Query,
420    walker: ::core::option::Option<
421        unsafe extern "C-unwind" fn(*mut super::Node, *mut ::core::ffi::c_void) -> bool,
422    >,
423    context: *mut ::core::ffi::c_void,
424    flags: ::core::ffi::c_int,
425) -> bool {
426    crate::query_tree_walker_impl(query, walker, context, flags)
427}
428
429#[cfg(any(feature = "pg16", feature = "pg17", feature = "pg18"))]
430pub unsafe fn query_or_expression_tree_walker(
431    node: *mut super::Node,
432    walker: ::core::option::Option<
433        unsafe extern "C-unwind" fn(*mut super::Node, *mut ::core::ffi::c_void) -> bool,
434    >,
435    context: *mut ::core::ffi::c_void,
436    flags: ::core::ffi::c_int,
437) -> bool {
438    crate::query_or_expression_tree_walker_impl(node, walker, context, flags)
439}
440
441#[cfg(any(feature = "pg16", feature = "pg17", feature = "pg18"))]
442pub unsafe fn expression_tree_walker(
443    node: *mut crate::Node,
444    walker: Option<unsafe extern "C-unwind" fn(*mut crate::Node, *mut ::core::ffi::c_void) -> bool>,
445    context: *mut ::core::ffi::c_void,
446) -> bool {
447    crate::expression_tree_walker_impl(node, walker, context)
448}
449
450#[cfg(any(feature = "pg16", feature = "pg17", feature = "pg18"))]
451pub unsafe fn range_table_entry_walker(
452    rte: *mut super::RangeTblEntry,
453    walker: ::core::option::Option<
454        unsafe extern "C-unwind" fn(*mut super::Node, *mut ::core::ffi::c_void) -> bool,
455    >,
456    context: *mut ::core::ffi::c_void,
457    flags: ::core::ffi::c_int,
458) -> bool {
459    crate::range_table_entry_walker_impl(rte, walker, context, flags)
460}
461
462#[cfg(any(feature = "pg16", feature = "pg17", feature = "pg18"))]
463pub unsafe fn range_table_walker(
464    rtable: *mut super::List,
465    walker: ::core::option::Option<
466        unsafe extern "C-unwind" fn(*mut super::Node, *mut ::core::ffi::c_void) -> bool,
467    >,
468    context: *mut ::core::ffi::c_void,
469    flags: ::core::ffi::c_int,
470) -> bool {
471    crate::range_table_walker_impl(rtable, walker, context, flags)
472}
473
474#[cfg(any(feature = "pg16", feature = "pg17", feature = "pg18"))]
475pub unsafe fn raw_expression_tree_walker(
476    node: *mut crate::Node,
477    walker: Option<unsafe extern "C-unwind" fn(*mut crate::Node, *mut ::core::ffi::c_void) -> bool>,
478    context: *mut ::core::ffi::c_void,
479) -> bool {
480    crate::raw_expression_tree_walker_impl(node, walker, context)
481}
482
483#[cfg(feature = "pg18")]
484pub unsafe fn expression_tree_mutator(
485    node: *mut crate::Node,
486    mutator: crate::tree_mutator_callback,
487    context: *mut ::core::ffi::c_void,
488) -> *mut crate::Node {
489    crate::expression_tree_mutator_impl(node, mutator, context)
490}
491
492#[inline(always)]
493pub unsafe fn MemoryContextSwitchTo(context: crate::MemoryContext) -> crate::MemoryContext {
494    let old = crate::CurrentMemoryContext;
495
496    crate::CurrentMemoryContext = context;
497    old
498}
499
500#[allow(non_snake_case)]
501#[inline(always)]
502#[cfg(any(feature = "pg13", feature = "pg14", feature = "pg15"))]
503pub unsafe fn BufferGetPageSize(buffer: pg_sys::Buffer) -> pg_sys::Size {
504    // #define BufferGetPageSize(buffer) \
505    // ( \
506    //     AssertMacro(BufferIsValid(buffer)), \
507    //     (Size)BLCKSZ \
508    // )
509    assert!(BufferIsValid(buffer));
510    pg_sys::BLCKSZ as pg_sys::Size
511}
512
513#[allow(non_snake_case)]
514#[inline(always)]
515#[cfg(any(feature = "pg13", feature = "pg14", feature = "pg15"))]
516pub unsafe fn ItemIdGetOffset(item_id: pg_sys::ItemId) -> u32 {
517    // #define ItemIdGetOffset(itemId) \
518    // ((itemId)->lp_off)
519    (*item_id).lp_off()
520}
521
522#[allow(non_snake_case)]
523#[inline(always)]
524pub const unsafe fn PageIsValid(page: pg_sys::Page) -> bool {
525    // #define PageIsValid(page) PointerIsValid(page)
526    !page.is_null()
527}
528
529#[allow(non_snake_case)]
530#[inline(always)]
531#[cfg(any(feature = "pg13", feature = "pg14", feature = "pg15"))]
532pub unsafe fn PageIsEmpty(page: pg_sys::Page) -> bool {
533    // #define PageIsEmpty(page) \
534    // (((PageHeader) (page))->pd_lower <= SizeOfPageHeaderData)
535    const SizeOfPageHeaderData: pg_sys::Size =
536        core::mem::offset_of!(pg_sys::PageHeaderData, pd_linp);
537    let page_header = page as *mut pg_sys::PageHeaderData;
538    (*page_header).pd_lower <= SizeOfPageHeaderData as u16
539}
540
541#[allow(non_snake_case)]
542#[inline(always)]
543#[cfg(any(feature = "pg13", feature = "pg14", feature = "pg15"))]
544pub unsafe fn PageIsNew(page: pg_sys::Page) -> bool {
545    // #define PageIsNew(page) (((PageHeader) (page))->pd_upper == 0)
546    let page_header = page as *mut pg_sys::PageHeaderData;
547    (*page_header).pd_upper == 0
548}
549
550#[allow(non_snake_case)]
551#[inline(always)]
552#[cfg(any(feature = "pg13", feature = "pg14", feature = "pg15"))]
553pub unsafe fn PageGetItemId(page: pg_sys::Page, offset: pg_sys::OffsetNumber) -> pg_sys::ItemId {
554    // #define PageGetItemId(page, offsetNumber) \
555    // ((ItemId) (&((PageHeader) (page))->pd_linp[(offsetNumber) - 1]))
556    let page_header = page as *mut pg_sys::PageHeaderData;
557    (*page_header).pd_linp.as_mut_ptr().add(offset as usize - 1)
558}
559
560#[allow(non_snake_case)]
561#[inline(always)]
562#[cfg(any(feature = "pg13", feature = "pg14", feature = "pg15"))]
563pub unsafe fn PageGetContents(page: pg_sys::Page) -> *mut ::core::ffi::c_char {
564    // #define PageGetContents(page) \
565    // ((char *) (page) + MAXALIGN(SizeOfPageHeaderData))
566    const SizeOfPageHeaderData: pg_sys::Size =
567        core::mem::offset_of!(pg_sys::PageHeaderData, pd_linp);
568    page.add(pg_sys::MAXALIGN(SizeOfPageHeaderData)) as *mut ::core::ffi::c_char
569}
570
571#[allow(non_snake_case)]
572#[inline(always)]
573#[cfg(any(feature = "pg13", feature = "pg14", feature = "pg15"))]
574pub fn PageSizeIsValid(page_size: usize) -> bool {
575    // #define PageSizeIsValid(pageSize) ((pageSize) == BLCKSZ)
576    page_size == pg_sys::BLCKSZ as usize
577}
578
579#[allow(non_snake_case)]
580#[inline(always)]
581#[cfg(any(feature = "pg13", feature = "pg14", feature = "pg15"))]
582pub unsafe fn PageGetPageSize(page: pg_sys::Page) -> usize {
583    // #define PageGetPageSize(page) \
584    // ((Size) (((PageHeader) (page))->pd_pagesize_version & (uint16) 0xFF00))
585    let page_header = page as *mut pg_sys::PageHeaderData;
586    ((*page_header).pd_pagesize_version & 0xFF00) as usize
587}
588
589#[allow(non_snake_case)]
590#[inline(always)]
591#[cfg(any(feature = "pg13", feature = "pg14", feature = "pg15"))]
592pub unsafe fn PageGetPageLayoutVersion(page: pg_sys::Page) -> ::core::ffi::c_char {
593    // #define PageGetPageLayoutVersion(page) \
594    // (((PageHeader) (page))->pd_pagesize_version & 0x00FF)
595    let page_header = page as *mut pg_sys::PageHeaderData;
596    ((*page_header).pd_pagesize_version & 0x00FF) as ::core::ffi::c_char
597}
598
599#[allow(non_snake_case)]
600#[inline(always)]
601#[cfg(any(feature = "pg13", feature = "pg14", feature = "pg15"))]
602pub unsafe fn PageSetPageSizeAndVersion(page: pg_sys::Page, size: u16, version: u8) {
603    // #define PageSetPageSizeAndVersion(page, size, version) \
604    // ((PageHeader) (page))->pd_pagesize_version = (size) | (version)
605    let page_header = page as *mut pg_sys::PageHeaderData;
606    (*page_header).pd_pagesize_version = size | (version as u16);
607}
608
609#[allow(non_snake_case)]
610#[inline(always)]
611#[cfg(any(feature = "pg13", feature = "pg14", feature = "pg15"))]
612pub unsafe fn PageGetSpecialSize(page: pg_sys::Page) -> u16 {
613    // #define PageGetSpecialSize(page) \
614    // ((uint16) (PageGetPageSize(page) - ((PageHeader)(page))->pd_special))
615    let page_header = page as *mut pg_sys::PageHeaderData;
616    PageGetPageSize(page) as u16 - (*page_header).pd_special
617}
618
619/// line pointer(s) do not count as part of header
620pub const unsafe fn SizeOfPageHeaderData() -> usize {
621    /*
622       #define SizeOfPageHeaderData (offsetof(PageHeaderData, pd_linp))
623    */
624    offset_of!(pg_sys::PageHeaderData, pd_linp)
625}
626
627/// Using assertions, validate that the page special pointer is OK.
628///
629/// This is intended to catch use of the pointer before page initialization.
630/// It is implemented as a function due to the limitations of the MSVC
631/// compiler, which choked on doing all these tests within another macro.  We
632/// return true so that AssertMacro() can be used while still getting the
633/// specifics from the macro failure within this function.
634#[allow(non_snake_case)]
635#[inline(always)]
636pub const unsafe fn PageValidateSpecialPointer(page: pg_sys::Page) -> bool {
637    // static inline bool
638    // PageValidateSpecialPointer(Page page)
639    // {
640    //     Assert(PageIsValid(page));
641    //     Assert(((PageHeader) (page))->pd_special <= BLCKSZ);
642    //     Assert(((PageHeader) (page))->pd_special >= SizeOfPageHeaderData);
643    //
644    //     return true;
645    // }
646    assert!(PageIsValid(page));
647    let page = page as *mut pg_sys::PageHeaderData;
648    assert!((*page).pd_special <= BLCKSZ as _);
649    assert!((*page).pd_special >= SizeOfPageHeaderData() as _);
650    true
651}
652
653#[allow(non_snake_case)]
654#[inline(always)]
655#[cfg(any(feature = "pg13", feature = "pg14", feature = "pg15", feature = "pg18"))]
656pub unsafe fn PageGetSpecialPointer(page: pg_sys::Page) -> *mut ::core::ffi::c_char {
657    /*
658    #define PageGetSpecialPointer(page) \
659    ( \
660        PageValidateSpecialPointer(page), \
661        ((page) + ((PageHeader) (page))->pd_special) \
662    )
663    */
664
665    assert!(PageValidateSpecialPointer(page));
666
667    let page_header = page as *mut pg_sys::PageHeaderData;
668    page.add((*page_header).pd_special as usize) as *mut ::core::ffi::c_char
669}
670
671#[allow(non_snake_case)]
672#[inline(always)]
673#[cfg(any(feature = "pg13", feature = "pg14", feature = "pg15"))]
674pub unsafe fn PageGetItem(page: pg_sys::Page, item_id: pg_sys::ItemId) -> *mut ::core::ffi::c_char {
675    // #define PageGetItem(page, itemId) \
676    // (((char *)(page)) + ItemIdGetOffset(itemId))
677    page.add(ItemIdGetOffset(item_id) as usize)
678}
679
680#[allow(non_snake_case)]
681#[inline(always)]
682#[cfg(any(feature = "pg13", feature = "pg14", feature = "pg15"))]
683pub unsafe fn PageGetMaxOffsetNumber(page: pg_sys::Page) -> pg_sys::OffsetNumber {
684    // #define PageGetMaxOffsetNumber(page) \
685    // (((PageHeader) (page))->pd_lower <= SizeOfPageHeaderData ? 0 : \
686    // ((((PageHeader) (page))->pd_lower - SizeOfPageHeaderData) / sizeof(ItemIdData)))
687    const SizeOfPageHeaderData: pg_sys::Size =
688        core::mem::offset_of!(pg_sys::PageHeaderData, pd_linp);
689    let page_header = page as *mut pg_sys::PageHeaderData;
690    if (*page_header).pd_lower <= SizeOfPageHeaderData as u16 {
691        0
692    } else {
693        ((*page_header).pd_lower - SizeOfPageHeaderData as u16)
694            / std::mem::size_of::<pg_sys::ItemIdData>() as u16
695    }
696}
697
698#[allow(non_snake_case)]
699#[inline(always)]
700#[cfg(any(feature = "pg13", feature = "pg14", feature = "pg15"))]
701pub unsafe fn BufferIsValid(buffer: pg_sys::Buffer) -> bool {
702    // static inline bool
703    // BufferIsValid(Buffer bufnum)
704    // {
705    //     Assert(bufnum <= NBuffers);
706    //     Assert(bufnum >= -NLocBuffer);
707
708    //     return bufnum != InvalidBuffer;
709    // }
710    assert!(buffer <= pg_sys::NBuffers);
711    assert!(buffer >= -pg_sys::NLocBuffer);
712    buffer != pg_sys::InvalidBuffer as pg_sys::Buffer
713}