LCOV - code coverage report
Current view: top level - libs/walproposer/src - api_bindings.rs (source / functions) Coverage Total Hit
Test: 1d18b743246dcf78c27c0bad0234a4c0da6fde89.info Lines: 96.0 % 399 383
Test Date: 2025-02-14 00:11:37 Functions: 97.3 % 37 36

            Line data    Source code
       1              : //! A C-Rust shim: defines implementation of C walproposer API, assuming wp
       2              : //! callback_data stores Box to some Rust implementation.
       3              : 
       4              : #![allow(dead_code)]
       5              : 
       6              : use std::ffi::CStr;
       7              : use std::ffi::CString;
       8              : 
       9              : use crate::bindings::uint32;
      10              : use crate::bindings::walproposer_api;
      11              : use crate::bindings::NeonWALReadResult;
      12              : use crate::bindings::PGAsyncReadResult;
      13              : use crate::bindings::PGAsyncWriteResult;
      14              : use crate::bindings::Safekeeper;
      15              : use crate::bindings::Size;
      16              : use crate::bindings::StringInfoData;
      17              : use crate::bindings::TimestampTz;
      18              : use crate::bindings::WalProposer;
      19              : use crate::bindings::WalProposerConnStatusType;
      20              : use crate::bindings::WalProposerConnectPollStatusType;
      21              : use crate::bindings::WalProposerExecStatusType;
      22              : use crate::bindings::WalproposerShmemState;
      23              : use crate::bindings::XLogRecPtr;
      24              : use crate::walproposer::ApiImpl;
      25              : use crate::walproposer::StreamingCallback;
      26              : use crate::walproposer::WaitResult;
      27              : 
      28         1382 : extern "C" fn get_shmem_state(wp: *mut WalProposer) -> *mut WalproposerShmemState {
      29         1382 :     unsafe {
      30         1382 :         let callback_data = (*(*wp).config).callback_data;
      31         1382 :         let api = callback_data as *mut Box<dyn ApiImpl>;
      32         1382 :         (*api).get_shmem_state()
      33         1382 :     }
      34         1382 : }
      35              : 
      36          142 : extern "C-unwind" fn start_streaming(wp: *mut WalProposer, startpos: XLogRecPtr) {
      37          142 :     unsafe {
      38          142 :         let callback_data = (*(*wp).config).callback_data;
      39          142 :         let api = callback_data as *mut Box<dyn ApiImpl>;
      40          142 :         let callback = StreamingCallback::new(wp);
      41          142 :         (*api).start_streaming(startpos, &callback);
      42          142 :     }
      43          142 : }
      44              : 
      45          113 : extern "C" fn get_flush_rec_ptr(wp: *mut WalProposer) -> XLogRecPtr {
      46          113 :     unsafe {
      47          113 :         let callback_data = (*(*wp).config).callback_data;
      48          113 :         let api = callback_data as *mut Box<dyn ApiImpl>;
      49          113 :         (*api).get_flush_rec_ptr()
      50          113 :     }
      51          113 : }
      52              : 
      53          837 : extern "C" fn update_donor(wp: *mut WalProposer, donor: *mut Safekeeper, donor_lsn: XLogRecPtr) {
      54          837 :     unsafe {
      55          837 :         let callback_data = (*(*wp).config).callback_data;
      56          837 :         let api = callback_data as *mut Box<dyn ApiImpl>;
      57          837 :         (*api).update_donor(&mut (*donor), donor_lsn)
      58          837 :     }
      59          837 : }
      60              : 
      61       307759 : extern "C" fn get_current_timestamp(wp: *mut WalProposer) -> TimestampTz {
      62       307759 :     unsafe {
      63       307759 :         let callback_data = (*(*wp).config).callback_data;
      64       307759 :         let api = callback_data as *mut Box<dyn ApiImpl>;
      65       307759 :         (*api).get_current_timestamp()
      66       307759 :     }
      67       307759 : }
      68              : 
      69         9888 : extern "C" fn conn_error_message(sk: *mut Safekeeper) -> *mut ::std::os::raw::c_char {
      70         9888 :     unsafe {
      71         9888 :         let callback_data = (*(*(*sk).wp).config).callback_data;
      72         9888 :         let api = callback_data as *mut Box<dyn ApiImpl>;
      73         9888 :         let msg = (*api).conn_error_message(&mut (*sk));
      74         9888 :         let msg = CString::new(msg).unwrap();
      75         9888 :         // TODO: fix leaking error message
      76         9888 :         msg.into_raw()
      77         9888 :     }
      78         9888 : }
      79              : 
      80        32599 : extern "C" fn conn_status(sk: *mut Safekeeper) -> WalProposerConnStatusType {
      81        32599 :     unsafe {
      82        32599 :         let callback_data = (*(*(*sk).wp).config).callback_data;
      83        32599 :         let api = callback_data as *mut Box<dyn ApiImpl>;
      84        32599 :         (*api).conn_status(&mut (*sk))
      85        32599 :     }
      86        32599 : }
      87              : 
      88        32599 : extern "C" fn conn_connect_start(sk: *mut Safekeeper) {
      89        32599 :     unsafe {
      90        32599 :         let callback_data = (*(*(*sk).wp).config).callback_data;
      91        32599 :         let api = callback_data as *mut Box<dyn ApiImpl>;
      92        32599 :         (*api).conn_connect_start(&mut (*sk))
      93        32599 :     }
      94        32599 : }
      95              : 
      96        29335 : extern "C" fn conn_connect_poll(sk: *mut Safekeeper) -> WalProposerConnectPollStatusType {
      97        29335 :     unsafe {
      98        29335 :         let callback_data = (*(*(*sk).wp).config).callback_data;
      99        29335 :         let api = callback_data as *mut Box<dyn ApiImpl>;
     100        29335 :         (*api).conn_connect_poll(&mut (*sk))
     101        29335 :     }
     102        29335 : }
     103              : 
     104        29335 : extern "C" fn conn_send_query(sk: *mut Safekeeper, query: *mut ::std::os::raw::c_char) -> bool {
     105        29335 :     let query = unsafe { CStr::from_ptr(query) };
     106        29335 :     let query = query.to_str().unwrap();
     107        29335 : 
     108        29335 :     unsafe {
     109        29335 :         let callback_data = (*(*(*sk).wp).config).callback_data;
     110        29335 :         let api = callback_data as *mut Box<dyn ApiImpl>;
     111        29335 :         (*api).conn_send_query(&mut (*sk), query)
     112        29335 :     }
     113        29335 : }
     114              : 
     115        29335 : extern "C" fn conn_get_query_result(sk: *mut Safekeeper) -> WalProposerExecStatusType {
     116        29335 :     unsafe {
     117        29335 :         let callback_data = (*(*(*sk).wp).config).callback_data;
     118        29335 :         let api = callback_data as *mut Box<dyn ApiImpl>;
     119        29335 :         (*api).conn_get_query_result(&mut (*sk))
     120        29335 :     }
     121        29335 : }
     122              : 
     123            0 : extern "C" fn conn_flush(sk: *mut Safekeeper) -> ::std::os::raw::c_int {
     124            0 :     unsafe {
     125            0 :         let callback_data = (*(*(*sk).wp).config).callback_data;
     126            0 :         let api = callback_data as *mut Box<dyn ApiImpl>;
     127            0 :         (*api).conn_flush(&mut (*sk))
     128            0 :     }
     129            0 : }
     130              : 
     131        10182 : extern "C" fn conn_finish(sk: *mut Safekeeper) {
     132        10182 :     unsafe {
     133        10182 :         let callback_data = (*(*(*sk).wp).config).callback_data;
     134        10182 :         let api = callback_data as *mut Box<dyn ApiImpl>;
     135        10182 :         (*api).conn_finish(&mut (*sk))
     136        10182 :     }
     137        10182 : }
     138              : 
     139        17131 : extern "C" fn conn_async_read(
     140        17131 :     sk: *mut Safekeeper,
     141        17131 :     buf: *mut *mut ::std::os::raw::c_char,
     142        17131 :     amount: *mut ::std::os::raw::c_int,
     143        17131 : ) -> PGAsyncReadResult {
     144        17131 :     unsafe {
     145        17131 :         let callback_data = (*(*(*sk).wp).config).callback_data;
     146        17131 :         let api = callback_data as *mut Box<dyn ApiImpl>;
     147        17131 : 
     148        17131 :         // This function has guarantee that returned buf will be valid until
     149        17131 :         // the next call. So we can store a Vec in each Safekeeper and reuse
     150        17131 :         // it on the next call.
     151        17131 :         let mut inbuf = take_vec_u8(&mut (*sk).inbuf).unwrap_or_default();
     152        17131 :         inbuf.clear();
     153        17131 : 
     154        17131 :         let result = (*api).conn_async_read(&mut (*sk), &mut inbuf);
     155        17131 : 
     156        17131 :         // Put a Vec back to sk->inbuf and return data ptr.
     157        17131 :         *amount = inbuf.len() as i32;
     158        17131 :         *buf = store_vec_u8(&mut (*sk).inbuf, inbuf);
     159        17131 : 
     160        17131 :         result
     161        17131 :     }
     162        17131 : }
     163              : 
     164         4606 : extern "C" fn conn_async_write(
     165         4606 :     sk: *mut Safekeeper,
     166         4606 :     buf: *const ::std::os::raw::c_void,
     167         4606 :     size: usize,
     168         4606 : ) -> PGAsyncWriteResult {
     169         4606 :     unsafe {
     170         4606 :         let buf = std::slice::from_raw_parts(buf as *const u8, size);
     171         4606 :         let callback_data = (*(*(*sk).wp).config).callback_data;
     172         4606 :         let api = callback_data as *mut Box<dyn ApiImpl>;
     173         4606 :         (*api).conn_async_write(&mut (*sk), buf)
     174         4606 :     }
     175         4606 : }
     176              : 
     177        33129 : extern "C" fn conn_blocking_write(
     178        33129 :     sk: *mut Safekeeper,
     179        33129 :     buf: *const ::std::os::raw::c_void,
     180        33129 :     size: usize,
     181        33129 : ) -> bool {
     182        33129 :     unsafe {
     183        33129 :         let buf = std::slice::from_raw_parts(buf as *const u8, size);
     184        33129 :         let callback_data = (*(*(*sk).wp).config).callback_data;
     185        33129 :         let api = callback_data as *mut Box<dyn ApiImpl>;
     186        33129 :         (*api).conn_blocking_write(&mut (*sk), buf)
     187        33129 :     }
     188        33129 : }
     189              : 
     190          691 : extern "C-unwind" fn recovery_download(wp: *mut WalProposer, sk: *mut Safekeeper) -> bool {
     191          691 :     unsafe {
     192          691 :         let callback_data = (*(*(*sk).wp).config).callback_data;
     193          691 :         let api = callback_data as *mut Box<dyn ApiImpl>;
     194          691 : 
     195          691 :         // currently `recovery_download` is always called right after election
     196          691 :         (*api).after_election(&mut (*wp));
     197          691 : 
     198          691 :         (*api).recovery_download(&mut (*wp), &mut (*sk))
     199          691 :     }
     200          691 : }
     201              : 
     202          891 : extern "C" fn wal_reader_allocate(sk: *mut Safekeeper) {
     203          891 :     unsafe {
     204          891 :         let callback_data = (*(*(*sk).wp).config).callback_data;
     205          891 :         let api = callback_data as *mut Box<dyn ApiImpl>;
     206          891 :         (*api).wal_reader_allocate(&mut (*sk));
     207          891 :     }
     208          891 : }
     209              : 
     210              : #[allow(clippy::unnecessary_cast)]
     211          882 : extern "C" fn wal_read(
     212          882 :     sk: *mut Safekeeper,
     213          882 :     buf: *mut ::std::os::raw::c_char,
     214          882 :     startptr: XLogRecPtr,
     215          882 :     count: Size,
     216          882 :     _errmsg: *mut *mut ::std::os::raw::c_char,
     217          882 : ) -> NeonWALReadResult {
     218          882 :     unsafe {
     219          882 :         let buf = std::slice::from_raw_parts_mut(buf as *mut u8, count);
     220          882 :         let callback_data = (*(*(*sk).wp).config).callback_data;
     221          882 :         let api = callback_data as *mut Box<dyn ApiImpl>;
     222          882 :         // TODO: errmsg is not forwarded
     223          882 :         (*api).wal_read(&mut (*sk), buf, startptr)
     224          882 :     }
     225          882 : }
     226              : 
     227        15814 : extern "C" fn wal_reader_events(sk: *mut Safekeeper) -> uint32 {
     228        15814 :     unsafe {
     229        15814 :         let callback_data = (*(*(*sk).wp).config).callback_data;
     230        15814 :         let api = callback_data as *mut Box<dyn ApiImpl>;
     231        15814 :         (*api).wal_reader_events(&mut (*sk))
     232        15814 :     }
     233        15814 : }
     234              : 
     235         8875 : extern "C" fn init_event_set(wp: *mut WalProposer) {
     236         8875 :     unsafe {
     237         8875 :         let callback_data = (*(*wp).config).callback_data;
     238         8875 :         let api = callback_data as *mut Box<dyn ApiImpl>;
     239         8875 :         (*api).init_event_set(&mut (*wp));
     240         8875 :     }
     241         8875 : }
     242              : 
     243        65143 : extern "C" fn update_event_set(sk: *mut Safekeeper, events: uint32) {
     244        65143 :     unsafe {
     245        65143 :         let callback_data = (*(*(*sk).wp).config).callback_data;
     246        65143 :         let api = callback_data as *mut Box<dyn ApiImpl>;
     247        65143 :         (*api).update_event_set(&mut (*sk), events);
     248        65143 :     }
     249        65143 : }
     250              : 
     251         5718 : extern "C" fn active_state_update_event_set(sk: *mut Safekeeper) {
     252         5718 :     unsafe {
     253         5718 :         let callback_data = (*(*(*sk).wp).config).callback_data;
     254         5718 :         let api = callback_data as *mut Box<dyn ApiImpl>;
     255         5718 :         (*api).active_state_update_event_set(&mut (*sk));
     256         5718 :     }
     257         5718 : }
     258              : 
     259        58670 : extern "C" fn add_safekeeper_event_set(sk: *mut Safekeeper, events: uint32) {
     260        58670 :     unsafe {
     261        58670 :         let callback_data = (*(*(*sk).wp).config).callback_data;
     262        58670 :         let api = callback_data as *mut Box<dyn ApiImpl>;
     263        58670 :         (*api).add_safekeeper_event_set(&mut (*sk), events);
     264        58670 :     }
     265        58670 : }
     266              : 
     267        36253 : extern "C" fn rm_safekeeper_event_set(sk: *mut Safekeeper) {
     268        36253 :     unsafe {
     269        36253 :         let callback_data = (*(*(*sk).wp).config).callback_data;
     270        36253 :         let api = callback_data as *mut Box<dyn ApiImpl>;
     271        36253 :         (*api).rm_safekeeper_event_set(&mut (*sk));
     272        36253 :     }
     273        36253 : }
     274              : 
     275        85646 : extern "C-unwind" fn wait_event_set(
     276        85646 :     wp: *mut WalProposer,
     277        85646 :     timeout: ::std::os::raw::c_long,
     278        85646 :     event_sk: *mut *mut Safekeeper,
     279        85646 :     events: *mut uint32,
     280        85646 : ) -> ::std::os::raw::c_int {
     281        85646 :     unsafe {
     282        85646 :         let callback_data = (*(*wp).config).callback_data;
     283        85646 :         let api = callback_data as *mut Box<dyn ApiImpl>;
     284        85646 :         let result = (*api).wait_event_set(&mut (*wp), timeout);
     285        85646 :         match result {
     286              :             WaitResult::Latch => {
     287         8896 :                 *event_sk = std::ptr::null_mut();
     288         8896 :                 *events = crate::bindings::WL_LATCH_SET;
     289         8896 :                 1
     290              :             }
     291              :             WaitResult::Timeout => {
     292         2769 :                 *event_sk = std::ptr::null_mut();
     293         2769 :                 // WaitEventSetWait returns 0 for timeout.
     294         2769 :                 *events = 0;
     295         2769 :                 0
     296              :             }
     297        73981 :             WaitResult::Network(sk, event_mask) => {
     298        73981 :                 *event_sk = sk;
     299        73981 :                 *events = event_mask;
     300        73981 :                 1
     301              :             }
     302              :         }
     303              :     }
     304        85646 : }
     305              : 
     306         8875 : extern "C" fn strong_random(
     307         8875 :     wp: *mut WalProposer,
     308         8875 :     buf: *mut ::std::os::raw::c_void,
     309         8875 :     len: usize,
     310         8875 : ) -> bool {
     311         8875 :     unsafe {
     312         8875 :         let buf = std::slice::from_raw_parts_mut(buf as *mut u8, len);
     313         8875 :         let callback_data = (*(*wp).config).callback_data;
     314         8875 :         let api = callback_data as *mut Box<dyn ApiImpl>;
     315         8875 :         (*api).strong_random(buf)
     316         8875 :     }
     317         8875 : }
     318              : 
     319          257 : extern "C" fn get_redo_start_lsn(wp: *mut WalProposer) -> XLogRecPtr {
     320          257 :     unsafe {
     321          257 :         let callback_data = (*(*wp).config).callback_data;
     322          257 :         let api = callback_data as *mut Box<dyn ApiImpl>;
     323          257 :         (*api).get_redo_start_lsn()
     324          257 :     }
     325          257 : }
     326              : 
     327          330 : extern "C-unwind" fn finish_sync_safekeepers(wp: *mut WalProposer, lsn: XLogRecPtr) {
     328          330 :     unsafe {
     329          330 :         let callback_data = (*(*wp).config).callback_data;
     330          330 :         let api = callback_data as *mut Box<dyn ApiImpl>;
     331          330 :         (*api).finish_sync_safekeepers(lsn)
     332          330 :     }
     333          330 : }
     334              : 
     335         2160 : extern "C" fn process_safekeeper_feedback(wp: *mut WalProposer, sk: *mut Safekeeper) {
     336         2160 :     unsafe {
     337         2160 :         let callback_data = (*(*wp).config).callback_data;
     338         2160 :         let api = callback_data as *mut Box<dyn ApiImpl>;
     339         2160 :         (*api).process_safekeeper_feedback(&mut (*wp), &mut (*sk));
     340         2160 :     }
     341         2160 : }
     342              : 
     343        88442 : extern "C-unwind" fn log_internal(
     344        88442 :     wp: *mut WalProposer,
     345        88442 :     level: ::std::os::raw::c_int,
     346        88442 :     line: *const ::std::os::raw::c_char,
     347        88442 : ) {
     348        88442 :     unsafe {
     349        88442 :         let callback_data = (*(*wp).config).callback_data;
     350        88442 :         let api = callback_data as *mut Box<dyn ApiImpl>;
     351        88442 :         let line = CStr::from_ptr(line);
     352        88442 :         let line = line.to_str().unwrap();
     353        88442 :         (*api).log_internal(&mut (*wp), Level::from(level as u32), line)
     354        88442 :     }
     355        88442 : }
     356              : 
     357              : #[derive(Debug, PartialEq)]
     358              : pub enum Level {
     359              :     Debug5,
     360              :     Debug4,
     361              :     Debug3,
     362              :     Debug2,
     363              :     Debug1,
     364              :     Log,
     365              :     Info,
     366              :     Notice,
     367              :     Warning,
     368              :     Error,
     369              :     Fatal,
     370              :     Panic,
     371              :     WPEvent,
     372              : }
     373              : 
     374              : impl Level {
     375        88442 :     pub fn from(elevel: u32) -> Level {
     376              :         use crate::bindings::*;
     377              : 
     378        88442 :         match elevel {
     379         3715 :             DEBUG5 => Level::Debug5,
     380            0 :             DEBUG4 => Level::Debug4,
     381            0 :             DEBUG3 => Level::Debug3,
     382         2160 :             DEBUG2 => Level::Debug2,
     383            0 :             DEBUG1 => Level::Debug1,
     384        72261 :             LOG => Level::Log,
     385            0 :             INFO => Level::Info,
     386            0 :             NOTICE => Level::Notice,
     387        10236 :             WARNING => Level::Warning,
     388            0 :             ERROR => Level::Error,
     389           70 :             FATAL => Level::Fatal,
     390            0 :             PANIC => Level::Panic,
     391            0 :             WPEVENT => Level::WPEvent,
     392            0 :             _ => panic!("unknown log level {}", elevel),
     393              :         }
     394        88442 :     }
     395              : }
     396              : 
     397         8875 : pub(crate) fn create_api() -> walproposer_api {
     398         8875 :     walproposer_api {
     399         8875 :         get_shmem_state: Some(get_shmem_state),
     400         8875 :         start_streaming: Some(start_streaming),
     401         8875 :         get_flush_rec_ptr: Some(get_flush_rec_ptr),
     402         8875 :         update_donor: Some(update_donor),
     403         8875 :         get_current_timestamp: Some(get_current_timestamp),
     404         8875 :         conn_error_message: Some(conn_error_message),
     405         8875 :         conn_status: Some(conn_status),
     406         8875 :         conn_connect_start: Some(conn_connect_start),
     407         8875 :         conn_connect_poll: Some(conn_connect_poll),
     408         8875 :         conn_send_query: Some(conn_send_query),
     409         8875 :         conn_get_query_result: Some(conn_get_query_result),
     410         8875 :         conn_flush: Some(conn_flush),
     411         8875 :         conn_finish: Some(conn_finish),
     412         8875 :         conn_async_read: Some(conn_async_read),
     413         8875 :         conn_async_write: Some(conn_async_write),
     414         8875 :         conn_blocking_write: Some(conn_blocking_write),
     415         8875 :         recovery_download: Some(recovery_download),
     416         8875 :         wal_reader_allocate: Some(wal_reader_allocate),
     417         8875 :         wal_read: Some(wal_read),
     418         8875 :         wal_reader_events: Some(wal_reader_events),
     419         8875 :         init_event_set: Some(init_event_set),
     420         8875 :         update_event_set: Some(update_event_set),
     421         8875 :         active_state_update_event_set: Some(active_state_update_event_set),
     422         8875 :         add_safekeeper_event_set: Some(add_safekeeper_event_set),
     423         8875 :         rm_safekeeper_event_set: Some(rm_safekeeper_event_set),
     424         8875 :         wait_event_set: Some(wait_event_set),
     425         8875 :         strong_random: Some(strong_random),
     426         8875 :         get_redo_start_lsn: Some(get_redo_start_lsn),
     427         8875 :         finish_sync_safekeepers: Some(finish_sync_safekeepers),
     428         8875 :         process_safekeeper_feedback: Some(process_safekeeper_feedback),
     429         8875 :         log_internal: Some(log_internal),
     430         8875 :     }
     431         8875 : }
     432              : 
     433         8875 : pub fn empty_shmem() -> crate::bindings::WalproposerShmemState {
     434         8875 :     let empty_feedback = crate::bindings::PageserverFeedback {
     435         8875 :         present: false,
     436         8875 :         currentClusterSize: 0,
     437         8875 :         last_received_lsn: 0,
     438         8875 :         disk_consistent_lsn: 0,
     439         8875 :         remote_consistent_lsn: 0,
     440         8875 :         replytime: 0,
     441         8875 :         shard_number: 0,
     442         8875 :     };
     443         8875 : 
     444         8875 :     crate::bindings::WalproposerShmemState {
     445         8875 :         propEpochStartLsn: crate::bindings::pg_atomic_uint64 { value: 0 },
     446         8875 :         donor_name: [0; 64],
     447         8875 :         donor_conninfo: [0; 1024],
     448         8875 :         donor_lsn: 0,
     449         8875 :         mutex: 0,
     450         8875 :         mineLastElectedTerm: crate::bindings::pg_atomic_uint64 { value: 0 },
     451         8875 :         backpressureThrottlingTime: crate::bindings::pg_atomic_uint64 { value: 0 },
     452         8875 :         currentClusterSize: crate::bindings::pg_atomic_uint64 { value: 0 },
     453         8875 :         shard_ps_feedback: [empty_feedback; 128],
     454         8875 :         num_shards: 0,
     455         8875 :         min_ps_feedback: empty_feedback,
     456         8875 :     }
     457         8875 : }
     458              : 
     459              : impl std::fmt::Display for Level {
     460         1153 :     fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
     461         1153 :         write!(f, "{:?}", self)
     462         1153 :     }
     463              : }
     464              : 
     465              : /// Take ownership of `Vec<u8>` from StringInfoData.
     466              : #[allow(clippy::unnecessary_cast)]
     467        43748 : pub(crate) fn take_vec_u8(pg: &mut StringInfoData) -> Option<Vec<u8>> {
     468        43748 :     if pg.data.is_null() {
     469        26622 :         return None;
     470        17126 :     }
     471        17126 : 
     472        17126 :     let ptr = pg.data as *mut u8;
     473        17126 :     let length = pg.len as usize;
     474        17126 :     let capacity = pg.maxlen as usize;
     475        17126 : 
     476        17126 :     pg.data = std::ptr::null_mut();
     477        17126 :     pg.len = 0;
     478        17126 :     pg.maxlen = 0;
     479        17126 : 
     480        17126 :     unsafe { Some(Vec::from_raw_parts(ptr, length, capacity)) }
     481        43748 : }
     482              : 
     483              : /// Store `Vec<u8>` in StringInfoData.
     484        17131 : fn store_vec_u8(pg: &mut StringInfoData, vec: Vec<u8>) -> *mut ::std::os::raw::c_char {
     485        17131 :     let ptr = vec.as_ptr() as *mut ::std::os::raw::c_char;
     486        17131 :     let length = vec.len();
     487        17131 :     let capacity = vec.capacity();
     488        17131 : 
     489        17131 :     assert!(pg.data.is_null());
     490              : 
     491        17131 :     pg.data = ptr;
     492        17131 :     pg.len = length as i32;
     493        17131 :     pg.maxlen = capacity as i32;
     494        17131 : 
     495        17131 :     std::mem::forget(vec);
     496        17131 : 
     497        17131 :     ptr
     498        17131 : }
        

Generated by: LCOV version 2.1-beta