@@ -193,6 +193,28 @@ static term nif_jit_stream_mmap_read(Context *ctx, int argc, term argv[])
193193 return term_from_literal_binary (js_obj -> stream_base + offset , len , & ctx -> heap , ctx -> global );
194194}
195195
196+ static term nif_jit_stream_mmap_flush (Context * ctx , int argc , term argv [])
197+ {
198+ UNUSED (argc );
199+
200+ void * js_obj_ptr ;
201+ if (UNLIKELY (!enif_get_resource (erl_nif_env_from_context (ctx ), argv [0 ], jit_stream_mmap_resource_type , & js_obj_ptr ))) {
202+ RAISE_ERROR (BADARG_ATOM );
203+ }
204+ struct JITStreamMMap * js_obj = (struct JITStreamMMap * ) js_obj_ptr ;
205+ if (IS_NULL_PTR (js_obj -> stream_base )) {
206+ RAISE_ERROR (BADARG_ATOM );
207+ }
208+
209+ #if defined(__APPLE__ )
210+ sys_icache_invalidate (js_obj -> stream_base , js_obj -> stream_size );
211+ #elif defined(__GNUC__ )
212+ __builtin___clear_cache (js_obj -> stream_base , js_obj -> stream_base + js_obj -> stream_size );
213+ #endif
214+
215+ return argv [0 ];
216+ }
217+
196218static term nif_jit_stream_module (Context * ctx , int argc , term argv [])
197219{
198220 UNUSED (argc );
@@ -226,6 +248,10 @@ static const struct Nif jit_stream_mmap_read_nif = {
226248 .base .type = NIFFunctionType ,
227249 .nif_ptr = nif_jit_stream_mmap_read
228250};
251+ static const struct Nif jit_stream_mmap_flush_nif = {
252+ .base .type = NIFFunctionType ,
253+ .nif_ptr = nif_jit_stream_mmap_flush
254+ };
229255
230256ModuleNativeEntryPoint jit_stream_entry_point (Context * ctx , term jit_stream )
231257{
@@ -239,18 +265,14 @@ ModuleNativeEntryPoint jit_stream_entry_point(Context *ctx, term jit_stream)
239265 return NULL ;
240266 }
241267
242- #if defined(__APPLE__ )
243- sys_icache_invalidate (js_obj -> stream_base , js_obj -> stream_size );
244- #elif defined(__GNUC__ )
245- __builtin___clear_cache (js_obj -> stream_base , js_obj -> stream_base + js_obj -> stream_size );
246- #endif
247268#if JIT_ARCH_TARGET == JIT_ARCH_ARMV6M
248269 // Set thumb bit for armv6m
249270 ModuleNativeEntryPoint result = (ModuleNativeEntryPoint ) js_obj -> stream_base + 1 ;
250271#else
251272 ModuleNativeEntryPoint result = (ModuleNativeEntryPoint ) js_obj -> stream_base ;
252273#endif
253274
275+ // Prevent module from being unmapped by dtor
254276 js_obj -> stream_base = NULL ;
255277 return result ;
256278}
@@ -291,6 +313,9 @@ const struct Nif *jit_stream_mmap_get_nif(const char *nifname)
291313 if (strcmp ("read/3" , rest ) == 0 ) {
292314 return & jit_stream_mmap_read_nif ;
293315 }
316+ if (strcmp ("flush/1" , rest ) == 0 ) {
317+ return & jit_stream_mmap_flush_nif ;
318+ }
294319 }
295320 return NULL ;
296321}
0 commit comments