Skip to content

Commit fb47275

Browse files
committed
fix: use c++ memory allocator to match delete calls
Use ::operator new(size) to allocate memory inside C++ instead of using the Rust libc::malloc. This as the Rust call maps to the C malloc function rather than the C++ new keyword. As delete keyword is used to free memory, we also must allocate with new to ensure memory is correctly managed.
1 parent 9a73847 commit fb47275

File tree

4 files changed

+12
-7
lines changed

4 files changed

+12
-7
lines changed

build.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@ fn main() {
198198
.allowlist_function("clipper_clipperoffset_add_paths64")
199199
.allowlist_function("clipper_clipperoffset_execute")
200200
.allowlist_function("clipper_delete_clipperoffset")
201+
// Memory allocation
202+
.allowlist_function("clipper_allocate")
201203
.size_t_is_usize(true);
202204

203205
let bindings = builder.generate().expect("unable to generate bindings");

clipper2c/include/clipper2c.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,7 @@ void clipper_destruct_svgwriter(ClipperSvgWriter *p);
510510
void clipper_destruct_svgreader(ClipperSvgReader *p);
511511

512512
// pointer free + destruction
513+
void* clipper_allocate(size_t size);
513514

514515
void clipper_delete_path64(ClipperPath64 *p);
515516
void clipper_delete_pathd(ClipperPathD *p);
@@ -527,4 +528,4 @@ void clipper_delete_svgreader(ClipperSvgReader *p);
527528

528529
#ifdef __cplusplus
529530
}
530-
#endif
531+
#endif

clipper2c/src/clipper2c.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1003,7 +1003,7 @@ extern "C"
10031003
}
10041004

10051005
// pointer free + destruction
1006-
1006+
void* clipper_allocate(size_t size) { return ::operator new(size); }
10071007
void clipper_delete_path64(ClipperPath64 *p) { delete from_c(p); }
10081008
void clipper_delete_pathd(ClipperPathD *p) { delete from_c(p); }
10091009
void clipper_delete_paths64(ClipperPaths64 *p) { delete from_c(p); }
@@ -1020,4 +1020,4 @@ extern "C"
10201020

10211021
#ifdef __cplusplus
10221022
}
1023-
#endif
1023+
#endif

generated/bindings.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
/* automatically generated by rust-bindgen 0.69.4 */
22

3+
use std::os::raw::c_void;
4+
35
#[repr(C)]
46
#[derive(Debug, Copy, Clone)]
57
pub struct ClipperClipper64 {
@@ -88,10 +90,7 @@ fn bindgen_test_layout_ClipperPointD() {
8890
}
8991
#[repr(C)]
9092
#[derive(Debug, Default, Copy, Clone, Hash, PartialEq, Eq)]
91-
#[cfg_attr(
92-
feature = "serde",
93-
derive(serde::Serialize, serde::Deserialize)
94-
)]
93+
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
9594
pub struct ClipperPoint64 {
9695
pub x: i64,
9796
pub y: i64,
@@ -686,6 +685,9 @@ extern "C" {
686685
extern "C" {
687686
pub fn clipper_clipperoffset_size() -> usize;
688687
}
688+
extern "C" {
689+
pub fn clipper_allocate(size: usize) -> *mut c_void;
690+
}
689691
extern "C" {
690692
pub fn clipper_delete_path64(p: *mut ClipperPath64);
691693
}

0 commit comments

Comments
 (0)