Hello!
+ !defined(PG_STACK_USE_ALLOC) &&
There's a typo there, that should be ALLOCA?
+/* Just use palloc. */
+#ifdef PG_STACK_USE_PALLOC
+#define DECLARE_PG_STACK_IMPL(size)
+#define pg_stack_alloc_aligned_impl(size, align) \
+ pg_stack_palloc_aligned((size), (align))
+#define pg_stack_ptr_p() false
+#endif
Isn't a ptr parameter missing from this branch?
+#define pg_stack_strdup_with_len(data, size) \
+ (pg_stack_sanity_checks(1), \
+ pg_stack_let_size = (size), \
+ pg_stack_strdup_with_len_impl( \
+ pg_stack_alloc_aligned_impl(pg_stack_let_size + 1, \
+ alignof(char)),
Both seems like a not realistic edge case, but since the array macros
above have a length check, shouldn't this also guard against +1
overflowing?
+/* Populate and insert a new entry. */
+static inline void *
+pg_stack_debug_link(pg_stack_debug_node **head,
+ pg_stack_debug_node *node,
+ void *ptr,
+ size_t size)
+{
+ node->ptr = ptr;
+ node->size = size;
Again unrealistic edge case, but if the above macros have overflow
checks for typically 64 bit size_t, shouldn't this also have some
safety checks, or at least setting node->size consistently to uint32_t
max if size is bigger?
+#define pg_stack_strdup(cstr) \
+ pg_stack_strdup_with_len((cstr), strlen(cstr))
+#define pg_stack_strndup(cstr, n) \
+ pg_stack_strdup_with_len((cstr), strnlen((cstr), (n)))
+#define pg_stack_text_to_cstring(text) \
+ pg_stack_strdup_with_len(VARDATA_ANY(text), VARSIZE_ANY_EXHDR(text))
+#define pg_stack_text_datum_to_cstring(datum) \
+ pg_stack_text_to_cstring((text *) DatumGetPointer(datum))
I would at least very clearly document that these macros evaluate
their arguments twice. I can't think of any scenario that would
seriously break things with them (or at least not break the stack),
but it could be still easy to forget that these are macros and not
functions.
Or can't they work similarly to DECLARE_PG_STACK_SIZE and store the
pointer in a local variable?