This is an automated email from the ASF dual-hosted git repository.

nickva pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/couchdb-jiffy.git

commit b699032dcd9447ac435f393cc2adf94025052d3a
Author: Nick Vatamaniuc <[email protected]>
AuthorDate: Wed Apr 1 00:12:54 2026 -0400

    Gate the restrict macro with a C version check
    
    For those who are using pre-C99 C compiler, it won't have the `restrict`
    keyword, so define it as an empty macro. If it's undefined the compiler just
    won't do the optimization but it should still work fine.
    
    While monkeying around with macros do some other cleanups:
    
     1) Noticed that some macros were not used at all, some were redefined per
     module when a single definition in jiffy.h would be better
    
     2) `termstack` functions are only used in encode.c, so there is no need to
     compile a separate module for it. Those are just a handful of function so 
move
     them to `encode.c` file. This way if folks don't have link-time 
optimization
     set up, they'll still get pretty much the same kind of optimization from 
the
     inline hints and all the code being in the same module.
---
 c_src/decoder.c   |  6 +---
 c_src/encoder.c   | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++---
 c_src/jiffy.h     | 11 +++++++
 c_src/termstack.c | 87 ----------------------------------------------------
 c_src/termstack.h | 34 --------------------
 5 files changed, 99 insertions(+), 131 deletions(-)

diff --git a/c_src/decoder.c b/c_src/decoder.c
index 93efd90..0b440d9 100644
--- a/c_src/decoder.c
+++ b/c_src/decoder.c
@@ -13,10 +13,6 @@
 #define STACK_SIZE_INC 64
 #define NUM_BUF_LEN 32
 
-#if WINDOWS || WIN32
-#define snprintf  _snprintf
-#endif
-
 enum {
     st_value=0,
     st_object,
@@ -273,7 +269,7 @@ dec_string(Decoder* d, ERL_NIF_TERM* value)
             }
         } else if(d->p[d->i] < 0x80) {
             // Scan ahead plain ASCII as an optimization
-            const unsigned char* restrict p = d->p;
+            const unsigned char* JIFFY_RESTRICT p = d->p;
             size_t idx = d->i + 1;
             const size_t len = d->len;
             while(idx < len
diff --git a/c_src/encoder.c b/c_src/encoder.c
index 29ff860..0cf1b08 100644
--- a/c_src/encoder.c
+++ b/c_src/encoder.c
@@ -6,12 +6,13 @@
 #include <string.h>
 
 #include "jiffy.h"
-#include "termstack.h"
 
 #define BIN_INC_SIZE 2048
 
 #define MIN(X, Y) ((X) < (Y) ? (X) : (Y))
 
+#define SMALL_TERMSTACK_SIZE 16
+
 #define MAYBE_PRETTY(e)             \
 do {                                \
     if(e->pretty) {                 \
@@ -20,10 +21,13 @@ do {                                \
     }                               \
 } while(0)
 
-#if WINDOWS || WIN32
-#define inline __inline
-#define snprintf  _snprintf
-#endif
+typedef struct {
+    ERL_NIF_TERM* elements;
+    size_t size;
+    size_t top;
+
+    ERL_NIF_TERM __default_elements[SMALL_TERMSTACK_SIZE];
+} TermStack;
 
 typedef struct {
     ErlNifEnv*      env;
@@ -66,6 +70,84 @@ static char* shifts[NUM_SHIFTS] = {
 };
 
 
+static inline ERL_NIF_TERM
+termstack_save(ErlNifEnv* env, TermStack* stack)
+{
+    return enif_make_tuple_from_array(env, stack->elements, stack->top);
+}
+
+static inline int
+termstack_restore(ErlNifEnv* env, ERL_NIF_TERM from, TermStack* stack)
+{
+    const ERL_NIF_TERM* elements;
+    int arity;
+
+    if(enif_get_tuple(env, from, &arity, &elements)) {
+        stack->top = arity;
+
+        if(arity <= SMALL_TERMSTACK_SIZE) {
+            stack->elements = &stack->__default_elements[0];
+            stack->size = SMALL_TERMSTACK_SIZE;
+        } else {
+            stack->size = arity * 2;
+            stack->elements = enif_alloc(stack->size * sizeof(ERL_NIF_TERM));
+
+            if(!stack->elements) {
+                return 0;
+            }
+        }
+
+        memcpy(stack->elements, elements, arity * sizeof(ERL_NIF_TERM));
+        return 1;
+    }
+
+    return 0;
+}
+
+static inline void
+termstack_destroy(TermStack* stack)
+{
+    if(stack->elements != &stack->__default_elements[0]) {
+        enif_free(stack->elements);
+    }
+}
+
+static inline void
+termstack_push(TermStack* stack, ERL_NIF_TERM term)
+{
+
+    if(stack->top == stack->size) {
+        size_t new_size = stack->size * 2;
+        size_t num_bytes = new_size * sizeof(ERL_NIF_TERM);
+
+        if (stack->elements == &stack->__default_elements[0]) {
+            ERL_NIF_TERM* elems = enif_alloc(num_bytes);
+            memcpy(elems, stack->elements, num_bytes);
+            stack->elements = elems;
+        } else {
+            stack->elements = enif_realloc(stack->elements, num_bytes);
+        }
+
+        stack->size = new_size;
+    }
+
+    assert(stack->top < stack->size);
+    stack->elements[stack->top++] = term;
+}
+
+static inline ERL_NIF_TERM
+termstack_pop(TermStack* stack)
+{
+    assert(stack->top > 0 && stack->top <= stack->size);
+    return stack->elements[--stack->top];
+}
+
+static inline int
+termstack_is_empty(TermStack* stack)
+{
+    return stack->top == 0;
+}
+
 static Encoder*
 enc_new(ErlNifEnv* env)
 {
diff --git a/c_src/jiffy.h b/c_src/jiffy.h
index af755da..a0bcc28 100644
--- a/c_src/jiffy.h
+++ b/c_src/jiffy.h
@@ -9,6 +9,17 @@
 #define DEFAULT_BYTES_PER_REDUCTION 20
 #define DEFAULT_ERLANG_REDUCTION_COUNT 2000
 
+// Check for C99
+#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
+  #define JIFFY_RESTRICT restrict
+#else
+  #define JIFFY_RESTRICT
+#endif
+
+#if WINDOWS || WIN32
+  #define inline __inline
+#endif
+
 typedef struct {
     ERL_NIF_TERM    atom_ok;
     ERL_NIF_TERM    atom_error;
diff --git a/c_src/termstack.c b/c_src/termstack.c
deleted file mode 100644
index a558b0a..0000000
--- a/c_src/termstack.c
+++ /dev/null
@@ -1,87 +0,0 @@
-// This file is part of Jiffy released under the MIT license.
-// See the LICENSE file for more information.
-
-#include <assert.h>
-#include <stdio.h>
-#include <string.h>
-
-#include "jiffy.h"
-#include "termstack.h"
-
-ERL_NIF_TERM
-termstack_save(ErlNifEnv* env, TermStack* stack)
-{
-    return enif_make_tuple_from_array(env, stack->elements, stack->top);
-}
-
-int
-termstack_restore(ErlNifEnv* env, ERL_NIF_TERM from, TermStack* stack)
-{
-    const ERL_NIF_TERM* elements;
-    int arity;
-
-    if(enif_get_tuple(env, from, &arity, &elements)) {
-        stack->top = arity;
-
-        if(arity <= SMALL_TERMSTACK_SIZE) {
-            stack->elements = &stack->__default_elements[0];
-            stack->size = SMALL_TERMSTACK_SIZE;
-        } else {
-            stack->size = arity * 2;
-            stack->elements = enif_alloc(stack->size * sizeof(ERL_NIF_TERM));
-
-            if(!stack->elements) {
-                return 0;
-            }
-        }
-
-        memcpy(stack->elements, elements, arity * sizeof(ERL_NIF_TERM));
-        return 1;
-    }
-
-    return 0;
-}
-
-void
-termstack_destroy(TermStack* stack)
-{
-    if(stack->elements != &stack->__default_elements[0]) {
-        enif_free(stack->elements);
-    }
-}
-
-inline void
-termstack_push(TermStack* stack, ERL_NIF_TERM term)
-{
-
-    if(stack->top == stack->size) {
-        size_t new_size = stack->size * 2;
-        size_t num_bytes = new_size * sizeof(ERL_NIF_TERM);
-
-        if (stack->elements == &stack->__default_elements[0]) {
-            ERL_NIF_TERM* elems = enif_alloc(num_bytes);
-            memcpy(elems, stack->elements, num_bytes);
-            stack->elements = elems;
-        } else {
-            stack->elements = enif_realloc(stack->elements, num_bytes);
-        }
-
-        stack->size = new_size;
-    }
-
-    assert(stack->top < stack->size);
-    stack->elements[stack->top++] = term;
-}
-
-inline ERL_NIF_TERM
-termstack_pop(TermStack* stack)
-{
-    assert(stack->top > 0 && stack->top <= stack->size);
-    return stack->elements[--stack->top];
-}
-
-inline int
-termstack_is_empty(TermStack* stack)
-{
-    return stack->top == 0;
-}
diff --git a/c_src/termstack.h b/c_src/termstack.h
deleted file mode 100644
index d1f8ee4..0000000
--- a/c_src/termstack.h
+++ /dev/null
@@ -1,34 +0,0 @@
-// This file is part of Jiffy released under the MIT license.
-// See the LICENSE file for more information.
-
-#ifndef TERMSTACK_H
-#define TERMSTACK_H
-
-#include "erl_nif.h"
-
-#ifdef _WIN32
-#define JIFFY_EXPORT __declspec(dllexport)
-#else
-#define JIFFY_EXPORT
-#endif
-
-#define SMALL_TERMSTACK_SIZE 16
-
-typedef struct {
-    ERL_NIF_TERM* elements;
-    size_t size;
-    size_t top;
-
-    ERL_NIF_TERM __default_elements[SMALL_TERMSTACK_SIZE];
-} TermStack;
-
-
-ERL_NIF_TERM termstack_save(ErlNifEnv* env, TermStack* stack);
-int termstack_restore(ErlNifEnv* env, ERL_NIF_TERM from, TermStack* stack);
-void termstack_destroy(TermStack* stack);
-
-JIFFY_EXPORT void termstack_push(TermStack* stack, ERL_NIF_TERM term);
-JIFFY_EXPORT ERL_NIF_TERM termstack_pop(TermStack* stack);
-JIFFY_EXPORT int termstack_is_empty(TermStack* stack);
-
-#endif

Reply via email to