From 601a58027be6497506a44dc469383be5b3e43aaa Mon Sep 17 00:00:00 2001
From: Nikolay Shirokovskiy <nshirokovskiy@tarantool.org>
Date: Thu, 29 Jun 2023 19:05:26 +0300
Subject: [PATCH] misc: get rid of small _xc functions

Small library currently depends on Tarantool core through 'exception.h'.
This is not the way to go. Let's drop this dependency and instead of
moving _xc functions to Tarantool repo we can just stop using them. Our
current policy is to panic on OOM in case of runtime allocation.

Part of #7327

NO_DOC=<OOM behaviour is not documented>
NO_CHANGELOG=<no OOM expectations>
NO_TEST=<no test harness for checking OOM>

(cherry picked from commit 3fccfc8fe64c9a23eab0a0888313d7ac64db2200)
---
 src/box/alter.cc        |  6 ++++--
 src/box/box.cc          |  2 +-
 src/box/iproto.cc       |  6 +++---
 src/box/user.h          |  1 +
 src/lib/core/coio_buf.h |  8 ++++----
 src/trivia/util.h       | 22 +++++++++++++++++-----
 6 files changed, 30 insertions(+), 15 deletions(-)

diff --git a/src/box/alter.cc b/src/box/alter.cc
index 8d8d40a2b8..2cd3ad187d 100644
--- a/src/box/alter.cc
+++ b/src/box/alter.cc
@@ -651,8 +651,10 @@ class AlterSpaceOp {
 
 	void *operator new(size_t size)
 	{
-		return region_aligned_calloc_xc(&in_txn()->region, size,
-						alignof(uint64_t));
+		void *ptr = xregion_aligned_alloc(&in_txn()->region, size,
+						  alignof(uint64_t));
+		memset(ptr, 0, size);
+		return ptr;
 	}
 	void operator delete(void * /* ptr */) {}
 };
diff --git a/src/box/box.cc b/src/box/box.cc
index 62d08215c9..f406369f91 100644
--- a/src/box/box.cc
+++ b/src/box/box.cc
@@ -3538,7 +3538,7 @@ space_truncate(struct space *space)
 	size_t buf_size = 3 * mp_sizeof_array(UINT32_MAX) +
 			  4 * mp_sizeof_uint(UINT64_MAX) + mp_sizeof_str(1);
 	RegionGuard region_guard(&fiber()->gc);
-	char *buf = (char *)region_alloc_xc(&fiber()->gc, buf_size);
+	char *buf = (char *)xregion_alloc(&fiber()->gc, buf_size);
 
 	char *tuple_buf = buf;
 	char *tuple_buf_end = tuple_buf;
diff --git a/src/box/iproto.cc b/src/box/iproto.cc
index e0bb652058..a1419233b9 100644
--- a/src/box/iproto.cc
+++ b/src/box/iproto.cc
@@ -1037,7 +1037,7 @@ iproto_connection_input_buffer(struct iproto_connection *con)
 	 * (in only has unparsed content).
 	 */
 	if (ibuf_used(old_ibuf) == con->parse_size) {
-		ibuf_reserve_xc(old_ibuf, to_read);
+		xibuf_reserve(old_ibuf, to_read);
 		return old_ibuf;
 	}
 
@@ -1055,7 +1055,7 @@ iproto_connection_input_buffer(struct iproto_connection *con)
 		ibuf_create(new_ibuf, cord_slab_cache(), iproto_readahead);
 	}
 
-	ibuf_reserve_xc(new_ibuf, to_read + con->parse_size);
+	xibuf_reserve(new_ibuf, to_read + con->parse_size);
 	/*
 	 * Discard unparsed data in the old buffer, otherwise it
 	 * won't be recycled when all parsed requests are processed.
@@ -2787,7 +2787,7 @@ tx_process_connect(struct cmsg *m)
 		random_bytes(con->salt, IPROTO_SALT_SIZE);
 		greeting_encode(greeting, tarantool_version_id(), &uuid,
 				con->salt, IPROTO_SALT_SIZE);
-		obuf_dup_xc(out, greeting, IPROTO_GREETING_SIZE);
+		xobuf_dup(out, greeting, IPROTO_GREETING_SIZE);
 		if (! rlist_empty(&session_on_connect)) {
 			if (session_run_on_connect_triggers(con->session) != 0)
 				diag_raise();
diff --git a/src/box/user.h b/src/box/user.h
index 53ad540f57..7001a7b629 100644
--- a/src/box/user.h
+++ b/src/box/user.h
@@ -33,6 +33,7 @@
 #include <stdint.h>
 #include "user_def.h"
 #include "small/region.h"
+#include "diag.h"
 
 #if defined(__cplusplus)
 extern "C" {
diff --git a/src/lib/core/coio_buf.h b/src/lib/core/coio_buf.h
index fabe54cfc0..0d90a483c0 100644
--- a/src/lib/core/coio_buf.h
+++ b/src/lib/core/coio_buf.h
@@ -46,7 +46,7 @@ struct iostream;
 static inline ssize_t
 coio_bread(struct iostream *io, struct ibuf *buf, size_t sz)
 {
-	ibuf_reserve_xc(buf, sz);
+	xibuf_reserve(buf, sz);
 	ssize_t n = coio_read_ahead(io, buf->wpos, sz, ibuf_unused(buf));
 	if (n < 0)
 		diag_raise();
@@ -63,7 +63,7 @@ static inline ssize_t
 coio_bread_timeout(struct iostream *io, struct ibuf *buf, size_t sz,
 		   ev_tstamp timeout)
 {
-	ibuf_reserve_xc(buf, sz);
+	xibuf_reserve(buf, sz);
 	ssize_t n = coio_read_ahead_timeout(io, buf->wpos, sz, ibuf_unused(buf),
 			                    timeout);
 	if (n < 0)
@@ -76,7 +76,7 @@ coio_bread_timeout(struct iostream *io, struct ibuf *buf, size_t sz,
 static inline ssize_t
 coio_breadn(struct iostream *io, struct ibuf *buf, size_t sz)
 {
-	ibuf_reserve_xc(buf, sz);
+	xibuf_reserve(buf, sz);
 	ssize_t n = coio_readn_ahead(io, buf->wpos, sz, ibuf_unused(buf));
 	if (n < 0)
 		diag_raise();
@@ -94,7 +94,7 @@ static inline ssize_t
 coio_breadn_timeout(struct iostream *io, struct ibuf *buf, size_t sz,
 		    ev_tstamp timeout)
 {
-	ibuf_reserve_xc(buf, sz);
+	xibuf_reserve(buf, sz);
 	ssize_t n = coio_readn_ahead_timeout(io, buf->wpos, sz, ibuf_unused(buf),
 			                     timeout);
 	if (n < 0)
diff --git a/src/trivia/util.h b/src/trivia/util.h
index 1654cd60be..cf421173db 100644
--- a/src/trivia/util.h
+++ b/src/trivia/util.h
@@ -106,6 +106,14 @@ strnindex(const char *const *haystack, const char *needle, uint32_t len,
 #define lengthof(array) (sizeof (array) / sizeof ((array)[0]))
 #endif
 
+static inline void
+alloc_failure(const char *filename, int line, size_t size)
+{
+	fprintf(stderr, "Can't allocate %zu bytes at %s:%d",
+		size, filename, line);
+	exit(EXIT_FAILURE);
+}
+
 /**
  * An x* variant of a memory allocation function calls the original function
  * and panics if it fails (i.e. it should never return NULL).
@@ -113,11 +121,8 @@ strnindex(const char *const *haystack, const char *needle, uint32_t len,
 #define xalloc_impl(size, func, args...)					\
 	({									\
 		void *ret = func(args);						\
-		if (unlikely(ret == NULL)) {					\
-			fprintf(stderr, "Can't allocate %zu bytes at %s:%d",	\
-				(size_t)(size), __FILE__, __LINE__);		\
-			exit(EXIT_FAILURE);					\
-		}								\
+		if (unlikely(ret == NULL))					\
+			alloc_failure(__FILE__, __LINE__, (size));		\
 		ret;								\
 	})
 
@@ -148,6 +153,13 @@ strnindex(const char *const *haystack, const char *needle, uint32_t len,
 	(T *)xregion_aligned_alloc((region), sizeof(T) * (count), alignof(T));\
 })
 
+#define xobuf_dup(p, src, size)							\
+	({									\
+		size_t ret = obuf_dup((p), (src), (size));			\
+		if (unlikely(ret != (size_t)(size)))				\
+			alloc_failure(__FILE__, __LINE__, (size));		\
+	})
+
 /** \cond public */
 
 /**
-- 
GitLab