From J Wiki
Jump to navigation Jump to search

using jmf to map arrays in files

keywords: mmap munmap jmf map_jmf_ unmap_jmf_

First, a C program that writes arrays:

#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <j602/system/examples/jfe/jfex.h>

typedef double F;
#define DO(x,y) {I i,i_=x;for(i=0;i<i_;i++)y;}
fw(void*x,I y){assert(1==fwrite(x,y,1,f));}
{ int n=8;
  fo("C");DO(n,{C x='a'+i;fw(&x,sizeof(C));});  fc();
  fo("I");DO(n,{I x=i;    fw(&x,sizeof(I));});  fc();
  fo("F");DO(n,{F x=i/2.0;fw(&x,sizeof(F));});  fc();

and a j program which memory maps the arrays:

require'jmf files dir printf'
JCHAR map_jmf_ 'data';'./C'
'loaded C as %j' printf <data
JINT  map_jmf_ 'datb';'./I'
'loaded I as %j' printf <datb
JFL   map_jmf_ 'datc';'./F'
'loaded F as %j' printf <datc
unmap_jmf_ 'datc'
JCMPX   map_jmf_ 'datd';'./F'
'loaded F as %j' printf <datd

results in:

loaded C as abcdefgh
loaded I as 0 1 2 3 4 5 6 7
loaded F as 0 0.5 1 1.5 2 2.5 3 3.5
loaded F as 0j0.5 1j1.5 2j2.5 3j3.5

see also: