@@ -63,6 +63,36 @@ static MemorySegment findOrThrow(String symbol) {
63
63
.orElseThrow (() -> new UnsatisfiedLinkError ("unresolved symbol: %s" .formatted (symbol )));
64
64
}
65
65
66
+ // ==== ------------------------------------------------------------------------------------------------------------
67
+ // free
68
+ /**
69
+ * Descriptor for the free C runtime function.
70
+ */
71
+ public static final FunctionDescriptor free$descriptor = FunctionDescriptor .ofVoid (
72
+ ValueLayout .ADDRESS
73
+ );
74
+
75
+ /**
76
+ * Address of the free C runtime function.
77
+ */
78
+ public static final MemorySegment free$addr = findOrThrow ("free" );
79
+
80
+ /**
81
+ * Handle for the free C runtime function.
82
+ */
83
+ public static final MethodHandle free$handle = Linker .nativeLinker ().downcallHandle (free$addr , free$descriptor );
84
+
85
+ /**
86
+ * free the given pointer
87
+ */
88
+ public static void cFree (MemorySegment pointer ) {
89
+ try {
90
+ free$handle .invokeExact (pointer );
91
+ } catch (Throwable ex$ ) {
92
+ throw new AssertionError ("should not reach here" , ex$ );
93
+ }
94
+ }
95
+
66
96
// ==== ------------------------------------------------------------------------------------------------------------
67
97
// swift_retainCount
68
98
@@ -383,7 +413,9 @@ public static String nameOfSwiftType(MemorySegment typeMetadata, boolean qualifi
383
413
try (Arena arena = Arena .ofConfined ()) {
384
414
MemorySegment charsAndLength = (MemorySegment )swift_getTypeName$handle .invokeExact ((SegmentAllocator )arena , typeMetadata , qualified );
385
415
MemorySegment utf8Chars = charsAndLength .get (SWIFT_POINTER , 0 );
386
- return utf8Chars .getString (0 );
416
+ String typeName = utf8Chars .getString (0 );
417
+ cFree (utf8Chars );
418
+ return typeName ;
387
419
}
388
420
} catch (Throwable ex$ ) {
389
421
throw new AssertionError ("should not reach here" , ex$ );
0 commit comments