Cppgres
Build Postgres extensions in C++
Loading...
Searching...
No Matches
exception_impl.hpp
Go to the documentation of this file.
1
4#pragma once
5
6#include "exception.hpp"
7#include "guard.hpp"
8#include "memory.hpp"
9
10namespace cppgres {
11
12inline pg_exception::pg_exception(::MemoryContext mcxt) : mcxt(mcxt) {
13 ::CurrentMemoryContext = error_cxt =
14 memory_context(std::move(alloc_set_memory_context(top_memory_context())));
15 error = ffi_guard{::CopyErrorData}();
16 ::CurrentMemoryContext = mcxt;
17 ffi_guard{::FlushErrorState}();
18}
19
20inline pg_exception::~pg_exception() { memory_context(error_cxt).delete_context(); }
21
22[[noreturn]] inline void pg_exception::rethrow() {
23 // ReThrowError copies the record into Postgres' error stack (strings into
24 // ErrorContext) and then longjmps, which skips ~pg_exception. Register a
25 // reset callback on ErrorContext so our backing context is freed once the
26 // error has been fully handled (FlushErrorState resets ErrorContext).
27 auto *cb = static_cast<::MemoryContextCallback *>(
28 ffi_guard{::MemoryContextAlloc}(::ErrorContext, sizeof(::MemoryContextCallback)));
29 cb->func = [](void *arg) { ::MemoryContextDelete(static_cast<::MemoryContext>(arg)); };
30 cb->arg = error_cxt;
31 ::MemoryContextRegisterResetCallback(::ErrorContext, cb);
32 ::ReThrowError(error);
33 __builtin_unreachable();
34}
35
36} // namespace cppgres
Definition: guard.hpp:20