Permalink
Browse files

Use file API for mmap

  • Loading branch information...
1 parent 63c5cc7 commit 11a95709322971e123061766034883d2b5c3808a @krestenkrab committed Jan 14, 2012
Showing with 40 additions and 15 deletions.
  1. +4 −4 c_src/emmap.cpp
  2. +36 −11 src/emmap.erl
View
@@ -68,10 +68,10 @@ extern "C" {
static ErlNifFunc nif_funcs[] =
{
- {"open", 4, emmap_open},
- {"close", 1, emmap_close},
- {"pread", 3, emmap_pread},
- {"pwrite", 3, emmap_pwrite},
+ {"open_nif", 4, emmap_open},
+ {"close_nif", 1, emmap_close},
+ {"pread_nif", 3, emmap_pread},
+ {"pwrite_nif", 3, emmap_pwrite},
};
ERL_NIF_INIT(emmap, nif_funcs, &on_load, NULL, NULL, NULL);
View
@@ -24,22 +24,36 @@ init() ->
erlang:load_nif(SoName, 0).
-close(_Ref) ->
- ok.
-
-spec open(File::string(),
Offset::pos_integer(),
Length::pos_integer(),
Options::[ read|write|direct|nocache|private|shared ]) ->
{ok, term()} | {error, term()}.
-open(_FileName,_Off,_Len,_Options) ->
- <<>>.
-pread(_File,_Off,_Len) ->
+open(FileName, Off, Len, Options) ->
+ {ok, Mem} = open_nif(FileName, Off, Len, Options),
+ {ok, #file_descriptor{ module=?MODULE, data=Mem }}.
+
+open_nif(_,_,_,_) ->
+ {ok, <<>>}.
+
+close(#file_descriptor{ module=?MODULE, data=Mem }) ->
+ close_nif(Mem).
+
+close_nif(_) ->
+ ok.
+
+pread(#file_descriptor{ module=?MODULE, data=Mem }, Off, Len) ->
+ pread_nif(Mem, Off, Len).
+
+pread_nif(_,_,_) ->
{ok, <<>>}.
-pwrite(_File,_Off,_Data) ->
+pwrite(#file_descriptor{ module=?MODULE, data=Mem }, Off, Data) ->
+ pwrite_nif(Mem, Off, Data).
+
+pwrite_nif(_,_,_) ->
ok.
@@ -51,10 +65,21 @@ simple_test() ->
ok = file:write(File, <<"abcd">>),
ok = file:close(File),
- {ok, MFile} = emmap:open("test.data", 0, 4, [direct]),
- {ok, <<"cd">>} = emmap:pread(MFile, 2, 2),
- {error, eacces} = emmap:pwrite(MFile, 2, <<"xx">>),
- ok = emmap:close(MFile).
+ %% with direct+shared, the contents of a binary may change
+ {ok, MFile} = emmap:open("test.data", 0, 4, [direct, shared]),
+ {ok, Mem} = file:pread(MFile, 2, 2),
+ <<"cd">> = Mem,
+ {error, eacces} = file:pwrite(MFile, 2, <<"xx">>),
+
+ {ok, MFile2} = emmap:open("test.data", 0, 4, [read, write, shared]),
+ ok = file:pwrite(MFile2, 2, <<"xx">>),
+ {ok, <<"xx">>} = file:pread(MFile, 2, 2),
+
+ %% Woot!
+ <<"xx">> = Mem,
+
+ file:close(MFile),
+ file:close(MFile2) .
-endif.

0 comments on commit 11a9570

Please sign in to comment.