Hi,
I have been trying to run some code against "/var/log/system.log" on my iMac,
which is currently 6.4M which is not *that* big. Having first exhausted the 32K
default global stack I re-ran it with:
$ GLOBALSZ=128000 gprolog
and this predicate works:
file('/var/log/system.log',X).
but this one doesn't, resulting in a segfault after a few seconds:
file_get_contents('/var/log/system.log',X).
| ?- file_get_contents('/var/log/system.log',X).
Segmentation fault: 11
bash-3.2$
I have no doubt it is to do with the way my code is structured so I wondered
what improvements I could make. I am currently trying to lex and parse a file
but first I wanted to get comfortable with ways of loading files as part of my
learning.
Here is my code, comments removed for brevity… the borked predicate first...
file_get_contents(Filename, Contents) :-
open(Filename, read, F),
fgc_read(F, [], Data),
close(F),
reverse(Data, Contents).
fgc_read(F, Acc, Acc) :- at_end_of_stream(F), !.
fgc_read(F, Acc, Contents) :-
get_char(F, Chr),
fgc_read(F, [Chr | Acc], Contents).
And the code for the predicate that manages to complete with the increased
memory available...
file(Filename, Lines) :-
file_get_contents(Filename, F),
file_eol(F, [], Lines).
file_eol([], Acc, Lines) :- reverse(Acc, Lines).
file_eol(Contents, Acc, Lines) :-
until_eol(Contents, Rest, Extracted),
file_eol(Rest, [Extracted | Acc], Lines).
until_eol([], [], []). % nothing to extract
until_eol(['\n'|T], T, []). % on a boundary, extracted nothing.
until_eol([C|T],Acc,[C|Data]) :- until_eol(T,Acc,Data).
skip_eol(X,Y) :- until_eol(X,Y,_).
Any suggestions for efficiency improvements are more than welcome as are style
tips. In the meantime I shall continue to work with GNU Prolog as it embodies
the free spirit I felt when I learned to program some thirty-six years ago when
I learned BASIC cassette loaded into an 8K computer (anybody remember
cassettes?)
I will also try to run it under strace
Thanks,
Sean.
_______________________________________________
Users-prolog mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/users-prolog