trilogy 2.9.0 → 2.12.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -10,12 +10,24 @@ File.binwrite("trilogy.c",
10
10
  }.join)
11
11
 
12
12
  $objs = %w[trilogy.o cast.o cext.o]
13
- $CFLAGS << " -I #{__dir__}/inc -std=gnu99 -fvisibility=hidden"
13
+ append_cflags([
14
+ # Ref: https://github.com/trilogy-libraries/trilogy/issues/290
15
+ # Fix build on older ruby versions and Xcode 26.4+
16
+ "-Wno-default-const-init-field-unsafe",
14
17
 
15
- dir_config("openssl")
18
+ "-I#{__dir__}/inc",
19
+ "-std=gnu99",
20
+ "-fvisibility=hidden",
21
+ "-DTRILOGY_XALLOCATOR",
22
+ "-g",
23
+ ])
24
+
25
+ dir_config("openssl").any? || pkg_config("openssl")
16
26
 
17
27
  have_library("crypto", "CRYPTO_malloc")
18
28
  have_library("ssl", "SSL_new")
19
- have_func("rb_interned_str", "ruby.h")
29
+ have_func("rb_ractor_local_storage_value_newkey", "ruby.h")
30
+ have_func("rb_enc_interned_str", "ruby.h")
31
+ have_func("rb_io_descriptor", "ruby.h") # Ruby 3.1+
20
32
 
21
33
  create_makefile "trilogy/cext"
@@ -0,0 +1,61 @@
1
+ #ifndef TRILOGY_INTERNAL_ALLOCATOR_H
2
+ #define TRILOGY_INTERNAL_ALLOCATOR_H
3
+
4
+ /* If you build Trilogy with a custom allocator, configure it with
5
+ * "-D TRILOGY_XALLOCATOR" to use your own allocator that defines xmalloc,
6
+ * xrealloc, xcalloc, and xfree.
7
+ *
8
+ * For example, your `trilogy_xallocator.h` file could look like this:
9
+ *
10
+ * ```
11
+ * #ifndef TRILOGY_XALLOCATOR_H
12
+ * #define TRILOGY_XALLOCATOR_H
13
+ * #define xmalloc my_malloc
14
+ * #define xrealloc my_realloc
15
+ * #define xcalloc my_calloc
16
+ * #define xfree my_free
17
+ * #endif
18
+ * ```
19
+ */
20
+ #ifdef TRILOGY_XALLOCATOR
21
+ #include "trilogy_xallocator.h"
22
+ #else
23
+ #ifndef xmalloc
24
+ /* The malloc function that should be used. This can be overridden with
25
+ * the TRILOGY_XALLOCATOR define. */
26
+ #define xmalloc malloc
27
+ #endif
28
+
29
+ #ifndef xrealloc
30
+ /* The realloc function that should be used. This can be overridden with
31
+ * the TRILOGY_XALLOCATOR define. */
32
+ #define xrealloc realloc
33
+ #endif
34
+
35
+ #ifndef xcalloc
36
+ /* The calloc function that should be used. This can be overridden with
37
+ * the TRILOGY_XALLOCATOR define. */
38
+ #define xcalloc calloc
39
+ #endif
40
+
41
+ #ifndef xfree
42
+ /* The free function that should be used. This can be overridden with
43
+ * the TRILOGY_XALLOCATOR define. */
44
+ #define xfree free
45
+ #endif
46
+ #endif
47
+
48
+ #include <string.h>
49
+ static inline char *
50
+ xstrdup(const char *str)
51
+ {
52
+ char *tmp;
53
+ size_t len = strlen(str) + 1;
54
+
55
+ tmp = xmalloc(len);
56
+ memcpy(tmp, str, len);
57
+
58
+ return tmp;
59
+ }
60
+
61
+ #endif
@@ -53,6 +53,19 @@ int trilogy_buffer_expand(trilogy_buffer_t *buffer, size_t needed);
53
53
  */
54
54
  int trilogy_buffer_putc(trilogy_buffer_t *buffer, uint8_t c);
55
55
 
56
+ /* trilogy_buffer_write - Appends multiple bytes to the buffer, resizing the underlying
57
+ * allocation if necessary.
58
+ *
59
+ * buffer - A pointer to a pre-initialized trilogy_buffer_t.
60
+ * ptr - The pointer to the byte array.
61
+ * len - How many bytes to append.
62
+ *
63
+ * Return values:
64
+ * TRILOGY_OK - The character was appended to the buffer
65
+ * TRILOGY_SYSERR - A system error occurred, check errno.
66
+ */
67
+ int trilogy_buffer_write(trilogy_buffer_t *buffer, const uint8_t *ptr, size_t len);
68
+
56
69
  /* trilogy_buffer_free - Free an trilogy_buffer_t's underlying storage. The buffer
57
70
  * must be re-initialized with trilogy_buffer_init if it is to be reused. Any
58
71
  * operations performed on an unintialized or freed buffer are undefined.
@@ -106,6 +106,15 @@ typedef struct {
106
106
  */
107
107
  int trilogy_init(trilogy_conn_t *conn);
108
108
 
109
+ /* trilogy_init_no_buffer - Same as trilogy_init but doesn't allocate the packet buffer
110
+ *
111
+ * conn - A pre-allocated trilogy_conn_t pointer.
112
+ *
113
+ * Return values:
114
+ * TRILOGY_OK - The trilogy_conn_t pointer was properly initialized
115
+ */
116
+ int trilogy_init_no_buffer(trilogy_conn_t *conn);
117
+
109
118
  /* trilogy_flush_writes - Attempt to flush the internal packet buffer to the
110
119
  * network. This must be used if a `_send` function returns TRILOGY_AGAIN, and
111
120
  * should continue to be called until it returns a value other than
@@ -150,6 +159,8 @@ int trilogy_connect_send(trilogy_conn_t *conn, const trilogy_sockopt_t *opts);
150
159
  */
151
160
  int trilogy_connect_send_socket(trilogy_conn_t *conn, trilogy_sock_t *sock);
152
161
 
162
+ int trilogy_connect_set_fd(trilogy_conn_t *conn, trilogy_sock_t *sock, int fd);
163
+
153
164
  /* trilogy_connect_recv - Read the initial handshake from the server.
154
165
  *
155
166
  * This should be called after trilogy_connect_send returns TRILOGY_OK. Calling
@@ -25,7 +25,8 @@
25
25
  XX(TRILOGY_MAX_PACKET_EXCEEDED, -20) \
26
26
  XX(TRILOGY_UNKNOWN_TYPE, -21) \
27
27
  XX(TRILOGY_TIMEOUT, -22) \
28
- XX(TRILOGY_AUTH_PLUGIN_ERROR, -23)
28
+ XX(TRILOGY_AUTH_PLUGIN_ERROR, -23) \
29
+ XX(TRILOGY_MEM_ERROR, -24)
29
30
 
30
31
  enum {
31
32
  #define XX(name, code) name = code,
@@ -88,6 +88,8 @@ typedef struct trilogy_sock_t {
88
88
 
89
89
  static inline int trilogy_sock_connect(trilogy_sock_t *sock) { return sock->connect_cb(sock); }
90
90
 
91
+ void trilogy_sock_set_fd(trilogy_sock_t *sock, int fd);
92
+
91
93
  static inline ssize_t trilogy_sock_read(trilogy_sock_t *sock, void *buf, size_t n)
92
94
  {
93
95
  return sock->read_cb(sock, buf, n);
@@ -1,6 +1,7 @@
1
1
  #ifndef TRILOGY_H
2
2
  #define TRILOGY_H
3
3
 
4
+ #include "trilogy/allocator.h"
4
5
  #include "trilogy/blocking.h"
5
6
  #include "trilogy/client.h"
6
7
  #include "trilogy/error.h"
@@ -1,6 +1,8 @@
1
1
  #include <stdint.h>
2
2
  #include <stdlib.h>
3
+ #include <string.h>
3
4
 
5
+ #include "trilogy/allocator.h"
4
6
  #include "trilogy/buffer.h"
5
7
  #include "trilogy/error.h"
6
8
 
@@ -8,7 +10,7 @@ int trilogy_buffer_init(trilogy_buffer_t *buffer, size_t initial_capacity)
8
10
  {
9
11
  buffer->len = 0;
10
12
  buffer->cap = initial_capacity;
11
- buffer->buff = malloc(initial_capacity);
13
+ buffer->buff = xmalloc(initial_capacity);
12
14
 
13
15
  if (buffer->buff == NULL) {
14
16
  return TRILOGY_SYSERR;
@@ -23,6 +25,9 @@ int trilogy_buffer_expand(trilogy_buffer_t *buffer, size_t needed)
23
25
  {
24
26
  // expand buffer if necessary
25
27
  if (buffer->len + needed > buffer->cap) {
28
+ if (buffer->buff == NULL)
29
+ return TRILOGY_MEM_ERROR;
30
+
26
31
  size_t new_cap = buffer->cap;
27
32
 
28
33
  while (buffer->len + needed > new_cap) {
@@ -33,7 +38,7 @@ int trilogy_buffer_expand(trilogy_buffer_t *buffer, size_t needed)
33
38
  new_cap *= EXPAND_MULTIPLIER;
34
39
  }
35
40
 
36
- uint8_t *new_buff = realloc(buffer->buff, new_cap);
41
+ uint8_t *new_buff = xrealloc(buffer->buff, new_cap);
37
42
  if (new_buff == NULL)
38
43
  return TRILOGY_SYSERR;
39
44
 
@@ -57,9 +62,24 @@ int trilogy_buffer_putc(trilogy_buffer_t *buffer, uint8_t c)
57
62
  return TRILOGY_OK;
58
63
  }
59
64
 
65
+ int trilogy_buffer_write(trilogy_buffer_t *buffer, const uint8_t *ptr, size_t len)
66
+ {
67
+ int rc = trilogy_buffer_expand(buffer, len);
68
+ if (rc) {
69
+ return rc;
70
+ }
71
+
72
+ memcpy(buffer->buff + buffer->len, ptr, len);
73
+ buffer->len += len;
74
+
75
+ return TRILOGY_OK;
76
+ }
77
+
60
78
  void trilogy_buffer_free(trilogy_buffer_t *buffer)
61
79
  {
62
- free(buffer->buff);
63
- buffer->buff = NULL;
64
- buffer->len = buffer->cap = 0;
80
+ if (buffer->buff) {
81
+ xfree(buffer->buff);
82
+ buffer->buff = NULL;
83
+ buffer->len = buffer->cap = 0;
84
+ }
65
85
  }