git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 01/10] ivec: introduce the C side of ivec

From
Ezekiel Newren via GitGitGadget <gitgitgadget@gmail.com>
Date
Jan 2, 2026, 18:52 UTC
Message-ID
<adf1395d201e916f23accc7644d21aff4f58368b.1767379944.git.gitgitgadget@gmail.com>
In-Reply-To
<pull.2156.git.git.1767379944.gitgitgadget@gmail.com>
From: Ezekiel Newren <ezekielnewren@gmail.com>

Trying to use Rust's Vec in C, or git's ALLOC_GROW() macros (via wrapper functions) in Rust is painful because:

  * C doesn't define its own vector type, and even though Rust does
    have Vec its painful to use on the C side (more on that below).
    However its still not viable to use Rust's Vec type because Git
    needs to be able to compile without Rust. So ivec was created
    expressley to be interoperable between C and Rust without needing
    Rust.
  * C doing vector things the Rust way would require wrapper functions,
    and Rust doing vector things the C way would require wrapper
    functions, so ivec was created to ensure a consistent contract
    between the 2 languages for how to manipulate a vector.
  * Currently, Rust defines its own 'Vec' type that is generic, but its
    memory allocator and struct layout weren't designed for
    interoperability with C (or any language for that matter), meaning
    that the C side cannot push to or expand a 'Vec' without defining
    wrapper functions in Rust that C can call. Without special care,
    the two languages might use different allocators (malloc/free on
    the C side, and possibly something else in Rust), which would make
    it difficult for a function in one language to free elements
    allocated by a call from a function in the other language.
  * Similarly, git defines ALLOC_GROW() and related macros in
    git-compat-util.h. While we could add functions allowing Rust to
    invoke something similar to those macros, passing three variables
    (pointer, length, allocated_size) instead of a single variable
    (vector) across the language boundary requires more cognitive
    overhead for readers to keep track of and makes it easier to make
    mistakes. Further, for low-level components that we want to
    eventually convert to pure Rust, such triplets would feel very out
    of place.

To address these issue, introduce a new type, ivec -- short for interoperable vector. (We refer to it as 'ivec' generally, though on the Rust side the struct is called IVec to match Rust style.) This new type is specifically designed for FFI purposes, so that both languages handle the vector in the same way, though it could be used on either side independently. This type is designed such that it can easily be replaced by a Rust 'Vec' once interoperability is no longer a concern.

One particular item to note is that Git's macros to handle vec operations infer the amount that a vec needs to grow from the size of a pointer, but that makes it somewhat specific to the macros used in C. To avoid defining every ivec function as a macro I opted to also include an element_size field that allows concrete functions like push() to know how much to grow the memory. This element_size also helps in verifying that the ivec is correct when passing from C to Rust.

Signed-off-by: Ezekiel Newren <ezekielnewren@gmail.com>
---
 Makefile      |   1 +
 compat/ivec.c | 113 ++++++++++++++++++++++++++++++++++++++++++++++++++
 compat/ivec.h |  52 +++++++++++++++++++++++
 meson.build   |   1 +
 4 files changed, 167 insertions(+)
 create mode 100644 compat/ivec.c
 create mode 100644 compat/ivec.h
diff --git a/Makefile b/Makefile
index 89d8d73ec0..f923b307d6 100644
--- a/Makefile
+++ b/Makefile
@@ -1107,6 +1107,7 @@ LIB_OBJS += commit-reach.o
 LIB_OBJS += commit.o
 LIB_OBJS += common-exit.o
 LIB_OBJS += common-init.o
+LIB_OBJS += compat/ivec.o
 LIB_OBJS += compat/nonblock.o
 LIB_OBJS += compat/obstack.o
 LIB_OBJS += compat/open.o
diff --git a/compat/ivec.c b/compat/ivec.c
new file mode 100644
index 0000000000..0a777e78dc
--- /dev/null
+++ b/compat/ivec.c
@@ -0,0 +1,113 @@
+#include "ivec.h"
+
+struct IVec_c_void {
+	void *ptr;
+	size_t length;
+	size_t capacity;
+	size_t element_size;
+};
+
+static void _set_capacity(void *self_, size_t new_capacity)
+{
+	struct IVec_c_void *self = self_;
+
+	if (new_capacity == self->capacity) {
+		return;
+	}
+	if (new_capacity == 0) {
+		free(self->ptr);
+		self->ptr = NULL;
+	} else {
+		self->ptr = realloc(self->ptr, new_capacity * self->element_size);
+	}
+	self->capacity = new_capacity;
+}
+
+
+void ivec_init(void *self_, size_t element_size)
+{
+	struct IVec_c_void *self = self_;
+
+	self->ptr = NULL;
+	self->length = 0;
+	self->capacity = 0;
+	self->element_size = element_size;
+}
+
+void ivec_zero(void *self_, size_t capacity)
+{
+	struct IVec_c_void *self = self_;
+
+	self->ptr = calloc(capacity, self->element_size);
+	self->length = capacity;
+	self->capacity = capacity;
+	// DO NOT MODIFY element_size!!!
+}
+
+void ivec_reserve_exact(void *self_, size_t additional)
+{
+	struct IVec_c_void *self = self_;
+
+	_set_capacity(self, self->capacity + additional);
+}
+
+void ivec_reserve(void *self_, size_t additional)
+{
+	struct IVec_c_void *self = self_;
+
+	size_t growby = 128;
+	if (self->capacity > growby)
+		growby = self->capacity;
+	if (additional > growby)
+		growby = additional;
+
+	_set_capacity(self, self->capacity + growby);
+}
+
+void ivec_shrink_to_fit(void *self_)
+{
+	struct IVec_c_void *self = self_;
+
+	_set_capacity(self, self->length);
+}
+
+void ivec_push(void *self_, const void *value)
+{
+	struct IVec_c_void *self = self_;
+	void *dst = NULL;
+
+	if (self->length == self->capacity)
+		ivec_reserve(self, 1);
+
+	dst = (uint8_t*)self->ptr + self->length * self->element_size;
+	memcpy(dst, value, self->element_size);
+	self->length++;
+}
+
+void ivec_free(void *self_)
+{
+	struct IVec_c_void *self = self_;
+
+	free(self->ptr);
+	self->ptr = NULL;
+	self->length = 0;
+	self->capacity = 0;
+	// DO NOT MODIFY element_size!!!
+}
+
+void ivec_move(void *src_, void *dst_)
+{
+	struct IVec_c_void *src = src_;
+	struct IVec_c_void *dst = dst_;
+
+	ivec_free(dst);
+	dst->ptr = src->ptr;
+	dst->length = src->length;
+	dst->capacity = src->capacity;
+	// DO NOT MODIFY element_size!!!
+
+	src->ptr = NULL;
+	src->length = 0;
+	src->capacity = 0;
+	// DO NOT MODIFY element_size!!!
+}
diff --git a/compat/ivec.h b/compat/ivec.h
new file mode 100644
index 0000000000..654a05c506
--- /dev/null
+++ b/compat/ivec.h
@@ -0,0 +1,52 @@
+#ifndef IVEC_H
+#define IVEC_H
+
+#include <git-compat-util.h>
+
+#define IVEC_INIT(variable) ivec_init(&(variable), sizeof(*(variable).ptr))
+
+#ifndef CBINDGEN
+#define DEFINE_IVEC_TYPE(type, suffix) \
+struct IVec_##suffix { \
+	type* ptr; \
+	size_t length; \
+	size_t capacity; \
+	size_t element_size; \
+}
+
+DEFINE_IVEC_TYPE(bool, bool);
+
+DEFINE_IVEC_TYPE(uint8_t, u8);
+DEFINE_IVEC_TYPE(uint16_t, u16);
+DEFINE_IVEC_TYPE(uint32_t, u32);
+DEFINE_IVEC_TYPE(uint64_t, u64);
+
+DEFINE_IVEC_TYPE(int8_t, i8);
+DEFINE_IVEC_TYPE(int16_t, i16);
+DEFINE_IVEC_TYPE(int32_t, i32);
+DEFINE_IVEC_TYPE(int64_t, i64);
+
+DEFINE_IVEC_TYPE(float, f32);
+DEFINE_IVEC_TYPE(double, f64);
+
+DEFINE_IVEC_TYPE(size_t, usize);
+DEFINE_IVEC_TYPE(ssize_t, isize);
+#endif
+
+void ivec_init(void *self_, size_t element_size);
+
+void ivec_zero(void *self_, size_t capacity);
+
+void ivec_reserve_exact(void *self_, size_t additional);
+
+void ivec_reserve(void *self_, size_t additional);
+
+void ivec_shrink_to_fit(void *self_);
+
+void ivec_push(void *self_, const void *value);
+
+void ivec_free(void *self_);
+
+void ivec_move(void *src, void *dst);
+
+#endif /* IVEC_H */
diff --git a/meson.build b/meson.build
index dd52efd1c8..42ac0c8c42 100644
--- a/meson.build
+++ b/meson.build
@@ -302,6 +302,7 @@ libgit_sources = [
   'commit.c',
   'common-exit.c',
   'common-init.c',
+  'compat/ivec.c',
   'compat/nonblock.c',
   'compat/obstack.c',
   'compat/open.c',
-- 
gitgitgadget
Previous: Ezekiel Newren via GitGitGadgetNext: Junio C Hamano
Message 2 of 124 in “Xdiff cleanup part 3”
  1. 00/10 Xdiff cleanup part 3Ezekiel Newren via GitGitGadget, Jan 2, 2026
  2. 01/10 ivec: introduce the C side of ivecEzekiel Newren via GitGitGadget, Jan 2, 2026
  3. Junio C HamanoJan 4, 2026
  4. Ezekiel NewrenJan 17, 2026
  5. Phillip WoodJan 8, 2026
  6. Ezekiel NewrenJan 15, 2026
  7. Phillip WoodJan 16, 2026
  8. René ScharfeJan 16, 2026
  9. Phillip WoodJan 17, 2026
  10. Ezekiel NewrenJan 17, 2026
  11. René ScharfeJan 18, 2026
  12. Ezekiel NewrenJan 17, 2026
  13. Ezekiel NewrenJan 17, 2026
  14. Phillip WoodJan 17, 2026
  15. Jeff KingJan 19, 2026
  16. Ezekiel NewrenJan 19, 2026
  17. Jeff KingJan 19, 2026
  18. D. Ben KnobleJan 20, 2026
  19. Ezekiel NewrenJan 21, 2026
  20. Jeff KingJan 21, 2026
  21. Junio C HamanoJan 21, 2026
  22. Ezekiel NewrenJan 21, 2026
  23. Phillip WoodJan 20, 2026
  24. Phillip WoodJan 20, 2026
  25. Ezekiel NewrenJan 21, 2026
  26. Phillip WoodJan 28, 2026
  27. René ScharfeJan 16, 2026
  28. Ezekiel NewrenJan 17, 2026
  29. René ScharfeJan 18, 2026
  30. 02/10 xdiff: make classic diff explicit by creating xdl_do_classic_diff()Ezekiel Newren via GitGitGadget, Jan 2, 2026
  31. Phillip WoodJan 20, 2026
  32. Ezekiel NewrenJan 21, 2026
  33. 03/10 xdiff: don't waste time guessing the number of linesEzekiel Newren via GitGitGadget, Jan 2, 2026
  34. Phillip WoodJan 20, 2026
  35. Ezekiel NewrenJan 21, 2026
  36. Phillip WoodJan 22, 2026
  37. 04/10 xdiff: let patience and histogram benefit from xdl_trim_ends()Ezekiel Newren via GitGitGadget, Jan 2, 2026
  38. Phillip WoodJan 20, 2026
  39. Phillip WoodJan 21, 2026
  40. 05/10 xdiff: use xdfenv_t in xdl_trim_ends() and xdl_cleanup_records()Ezekiel Newren via GitGitGadget, Jan 2, 2026
  41. Phillip WoodJan 20, 2026
  42. 06/10 xdiff: cleanup xdl_trim_ends()Ezekiel Newren via GitGitGadget, Jan 2, 2026
  43. Phillip WoodJan 20, 2026
  44. 07/10 xdiff: replace xdfile_t.dstart with xdfenv_t.delta_startEzekiel Newren via GitGitGadget, Jan 2, 2026
  45. Phillip WoodJan 20, 2026
  46. Phillip WoodJan 28, 2026
  47. 08/10 xdiff: replace xdfile_t.dend with xdfenv_t.delta_endEzekiel Newren via GitGitGadget, Jan 2, 2026
  48. 09/10 xdiff: remove dependence on xdlclassifier from xdl_cleanup_records()Ezekiel Newren via GitGitGadget, Jan 2, 2026
  49. René ScharfeJan 16, 2026
  50. Ezekiel NewrenJan 17, 2026
  51. René ScharfeJan 18, 2026
  52. Phillip WoodJan 21, 2026
  53. 10/10 xdiff: move xdl_cleanup_records() from xprepare.c to xdiffi.cEzekiel Newren via GitGitGadget, Jan 2, 2026
  54. Phillip WoodJan 21, 2026
  55. Phillip WoodJan 28, 2026
  56. Junio C HamanoJan 4, 2026
  57. Yee Cheng ChinJan 4, 2026
  58. Phillip WoodJan 28, 2026
  59. Junio C HamanoMar 6, 2026
  60. Ezekiel NewrenMar 9, 2026
  61. Junio C HamanoMar 9, 2026
  62. 0/5 Xdiff cleanup part 3Ezekiel Newren via GitGitGadget, Mar 25, 2026
  63. 1/5 xdiff/xdl_cleanup_records: delete local recs pointerEzekiel Newren via GitGitGadget, Mar 25, 2026
  64. 2/5 xdiff/xdl_cleanup_records: make limits more clearEzekiel Newren via GitGitGadget, Mar 25, 2026
  65. 3/5 xdiff/xdl_cleanup_records: make setting action easier to followEzekiel Newren via GitGitGadget, Mar 25, 2026
  66. 4/5 xdiff/xdl_cleanup_records: simplify INVESTIGATE handling for clarityEzekiel Newren via GitGitGadget, Mar 25, 2026
  67. 5/5 xdiff/xdl_cleanup_records: use unambiguous typesEzekiel Newren via GitGitGadget, Mar 25, 2026
  68. Junio C HamanoMar 25, 2026
  69. SZEDER GáborMar 26, 2026
  70. 0/6 Xdiff cleanup part 3Ezekiel Newren via GitGitGadget, Mar 27, 2026
  71. 1/6 xdiff/xdl_cleanup_records: delete local recs pointerEzekiel Newren via GitGitGadget, Mar 27, 2026
  72. 2/6 xdiff: use unambiguous types in xdl_bogo_sqrt()Ezekiel Newren via GitGitGadget, Mar 27, 2026
  73. 3/6 xdiff/xdl_cleanup_records: use unambiguous typesEzekiel Newren via GitGitGadget, Mar 27, 2026
  74. 4/6 xdiff/xdl_cleanup_records: make limits more clearEzekiel Newren via GitGitGadget, Mar 27, 2026
  75. Junio C HamanoMar 27, 2026
  76. Junio C HamanoMar 27, 2026
  77. Ezekiel NewrenMar 30, 2026
  78. Junio C HamanoMar 30, 2026
  79. Ezekiel NewrenMar 31, 2026
  80. 5/6 xdiff/xdl_cleanup_records: make setting action easier to followEzekiel Newren via GitGitGadget, Mar 27, 2026
  81. 6/6 xdiff/xdl_cleanup_records: simplify INVESTIGATE handling for clarityEzekiel Newren via GitGitGadget, Mar 27, 2026
  82. 0/6 Xdiff cleanup part 3Ezekiel Newren via GitGitGadget, Mar 30, 2026
  83. 1/6 xdiff/xdl_cleanup_records: delete local recs pointerEzekiel Newren via GitGitGadget, Mar 30, 2026
  84. Ezekiel NewrenMar 30, 2026
  85. Junio C HamanoMar 30, 2026
  86. 2/6 xdiff: use unambiguous types in xdl_bogo_sqrt()Ezekiel Newren via GitGitGadget, Mar 30, 2026
  87. Junio C HamanoMar 30, 2026
  88. 3/6 xdiff/xdl_cleanup_records: use unambiguous typesEzekiel Newren via GitGitGadget, Mar 30, 2026
  89. 4/6 xdiff/xdl_cleanup_records: make limits more clearEzekiel Newren via GitGitGadget, Mar 30, 2026
  90. Phillip WoodMar 31, 2026
  91. Junio C HamanoMar 31, 2026
  92. Ezekiel NewrenApr 14, 2026
  93. Junio C HamanoApr 14, 2026
  94. Phillip WoodApr 15, 2026
  95. 5/6 xdiff/xdl_cleanup_records: make setting action easier to followEzekiel Newren via GitGitGadget, Mar 30, 2026
  96. Junio C HamanoMar 30, 2026
  97. Phillip WoodMar 31, 2026
  98. 6/6 xdiff/xdl_cleanup_records: simplify INVESTIGATE handling for clarityEzekiel Newren via GitGitGadget, Mar 30, 2026
  99. Phillip WoodMar 31, 2026
  100. Phillip WoodApr 1, 2026
  101. Junio C HamanoMar 30, 2026
  102. Phillip WoodMar 31, 2026
  103. 0/6 Xdiff cleanup part 3Ezekiel Newren via GitGitGadget, Apr 8, 2026
  104. 1/6 xdiff/xdl_cleanup_records: delete local recs pointerEzekiel Newren via GitGitGadget, Apr 8, 2026
  105. 2/6 xdiff: use unambiguous types in xdl_bogo_sqrt()Ezekiel Newren via GitGitGadget, Apr 8, 2026
  106. 3/6 xdiff/xdl_cleanup_records: use unambiguous typesEzekiel Newren via GitGitGadget, Apr 8, 2026
  107. 4/6 xdiff/xdl_cleanup_records: make limits more clearEzekiel Newren via GitGitGadget, Apr 8, 2026
  108. Phillip WoodApr 14, 2026
  109. 5/6 xdiff/xdl_cleanup_records: make setting action easier to followEzekiel Newren via GitGitGadget, Apr 8, 2026
  110. 6/6 xdiff/xdl_cleanup_records: put braces around the else clauseEzekiel Newren via GitGitGadget, Apr 8, 2026
  111. Junio C HamanoApr 8, 2026
  112. Phillip WoodApr 9, 2026
  113. Phillip WoodApr 14, 2026
  114. Junio C HamanoApr 14, 2026
  115. 0/6 Xdiff cleanup part 3Ezekiel Newren via GitGitGadget, Apr 29, 2026
  116. 1/6 xdiff/xdl_cleanup_records: delete local recs pointerEzekiel Newren via GitGitGadget, Apr 29, 2026
  117. 2/6 xdiff: use unambiguous types in xdl_bogo_sqrt()Ezekiel Newren via GitGitGadget, Apr 29, 2026
  118. 3/6 xdiff/xdl_cleanup_records: use unambiguous typesEzekiel Newren via GitGitGadget, Apr 29, 2026
  119. 4/6 xdiff/xdl_cleanup_records: make limits more clearEzekiel Newren via GitGitGadget, Apr 29, 2026
  120. 5/6 xdiff/xdl_cleanup_records: make setting action easier to followEzekiel Newren via GitGitGadget, Apr 29, 2026
  121. 6/6 xdiff/xdl_cleanup_records: make execution of action easier to followEzekiel Newren via GitGitGadget, Apr 29, 2026
  122. Phillip WoodApr 30, 2026
  123. Ezekiel NewrenApr 30, 2026
  124. Junio C HamanoMay 4, 2026

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.