From 876e42d629c6d9ca2fc21e87417fa3595118181b Mon Sep 17 00:00:00 2001 From: Vitor Santos Costa Date: Fri, 1 Dec 2017 10:42:10 +0000 Subject: [PATCH] memory --- C/c_interface.c | 9 ++--- C/text.c | 2 +- C/text.c.new | 2 +- C/tracer.c | 2 +- C/write.c | 2 +- CXX/yapi.cpp | 2 +- CXX/yapt.hh | 2 +- H/Yapproto.h | 2 +- os/assets.c | 2 +- os/files.c | 4 +-- os/fmem.c | 8 ++--- os/iopreds.c | 3 +- os/streams.c | 3 +- os/sysbits.c | 93 +++++++++++++++++++++++++++++-------------------- os/writeterm.c | 4 +-- os/yapio.h | 37 ++++++++++++++++++-- 16 files changed, 111 insertions(+), 66 deletions(-) diff --git a/C/c_interface.c b/C/c_interface.c index 99e366232..f0d9c324d 100755 --- a/C/c_interface.c +++ b/C/c_interface.c @@ -2144,7 +2144,6 @@ X_API int YAP_InitConsult(int mode, const char *fname, char *full, GLOBAL_Stream[sno].name = Yap_LookupAtom(fl); GLOBAL_Stream[sno].user_name = MkAtomTerm(Yap_LookupAtom(fname)); GLOBAL_Stream[sno].encoding = LOCAL_encoding; - pop_text_stack(lvl); RECOVER_MACHINE_REGS(); UNLOCK(GLOBAL_Stream[sno].streamlock); return sno; @@ -2263,7 +2262,7 @@ X_API int YAP_WriteDynamicBuffer(YAP_Term t, char *buf, size_t sze, char *b; BACKUP_MACHINE_REGS(); - b = Yap_TermToString(t, enc, flags); + b = Yap_TermToBuffer(t, enc, flags); strncpy(buf, b, sze); buf[sze] = 0; RECOVER_MACHINE_REGS(); @@ -2431,9 +2430,8 @@ X_API YAP_file_type_t YAP_Init(YAP_init_args *yap_init) { if (!LOCAL_TextBuffer) LOCAL_TextBuffer = Yap_InitTextAllocator(); - int lvl = push_text_stack(); - yroot = Malloc(YAP_FILENAME_MAX + 1); - boot_file = Malloc(YAP_FILENAME_MAX + 1); + yroot = malloc(YAP_FILENAME_MAX + 1); + boot_file = malloc(YAP_FILENAME_MAX + 1); /* ignore repeated calls to YAP_Init */ Yap_embedded = yap_init->Embedded; Yap_page_size = Yap_InitPageSize(); /* init memory page size, required by @@ -2674,7 +2672,6 @@ X_API YAP_file_type_t YAP_Init(YAP_init_args *yap_init) { start_modules(); YAP_initialized = true; - pop_text_stack(lvl); return YAP_BOOT_PL; } diff --git a/C/text.c b/C/text.c index 71ea38488..413551764 100644 --- a/C/text.c +++ b/C/text.c @@ -487,7 +487,7 @@ unsigned char *Yap_readText(seq_tv_t *inp USES_REGS) { #endif if (inp->type & YAP_STRING_TERM) { // Yap_DebugPlWriteln(inp->val.t); - char *s = (char *)Yap_TermToString(inp->val.t, ENC_ISO_UTF8, 0); + char *s = (char *) Yap_TermToBuffer(inp->val.t, ENC_ISO_UTF8, 0); return inp->val.uc = (unsigned char *)s; } if (inp->type & YAP_STRING_CHARS) { diff --git a/C/text.c.new b/C/text.c.new index 9569d2ca4..7c1d67f41 100644 --- a/C/text.c.new +++ b/C/text.c.new @@ -501,7 +501,7 @@ unsigned char *Yap_readText(seq_tv_t *inp, size_t *lengp) { #endif if (inp->type & YAP_STRING_TERM) { // Yap_DebugPlWriteln(inp->val.t); - char *s = (char *)Yap_TermToString(inp->val.t, lengp, ENC_ISO_UTF8, 0); + char *s = (char *) Yap_TermToBuffer(inp->val.t, lengp, ENC_ISO_UTF8, 0); return inp->val.uc = (unsigned char *)s; } if (inp->type & YAP_STRING_CHARS) { diff --git a/C/tracer.c b/C/tracer.c index ab926acc3..d060c5067 100644 --- a/C/tracer.c +++ b/C/tracer.c @@ -87,7 +87,7 @@ static char *send_tracer_message(char *start, char *name, arity_t arity, continue; } } - const char *sn = Yap_TermToString(args[i], LOCAL_encoding, + const char *sn = Yap_TermToBuffer(args[i], LOCAL_encoding, Quote_illegal_f | Handle_vars_f); size_t sz; if (sn == NULL) { diff --git a/C/write.c b/C/write.c index 20ed60bbc..bcf6613a5 100644 --- a/C/write.c +++ b/C/write.c @@ -1255,7 +1255,7 @@ static void wrputref(CODEADDR ref, int Quote_illegal, Yap_CloseSlots(sls); } - char *Yap_TermToString(Term t, encoding_t enc, int flags) { + char *Yap_TermToBuffer(Term t, encoding_t enc, int flags) { CACHE_REGS int sno = Yap_open_buf_write_stream(enc, flags); const char *sf; diff --git a/CXX/yapi.cpp b/CXX/yapi.cpp index 9a98aa4c4..f7fc1e8fb 100644 --- a/CXX/yapi.cpp +++ b/CXX/yapi.cpp @@ -12,7 +12,7 @@ extern "C" { #include "YapInterface.h" #include "blobs.h" -X_API char *Yap_TermToString(Term t, encoding_t encodingp, +X_API char *Yap_TermToBuffer(Term t, encoding_t encodingp, int flags); X_API void YAP_UserCPredicate(const char *, YAP_UserCPred, arity_t arity); diff --git a/CXX/yapt.hh b/CXX/yapt.hh index f3ce6f5b7..c88519dc8 100644 --- a/CXX/yapt.hh +++ b/CXX/yapt.hh @@ -241,7 +241,7 @@ public: char *os; BACKUP_MACHINE_REGS(); - if (!(os = Yap_TermToString(Yap_GetFromSlot(t), enc, Handle_vars_f))) { + if (!(os = Yap_TermToBuffer(Yap_GetFromSlot(t), enc, Handle_vars_f))) { RECOVER_MACHINE_REGS(); return 0; } diff --git a/H/Yapproto.h b/H/Yapproto.h index 183a4e27c..a9a2aaa07 100755 --- a/H/Yapproto.h +++ b/H/Yapproto.h @@ -416,7 +416,7 @@ extern AAssetManager *Yap_assetManager; extern void *Yap_openAssetFile(const char *path); extern bool Yap_isAsset(const char *path); #endif -extern const char *Yap_getcwd(const char *, size_t); +extern const char *Yap_getcwd( char *, size_t); extern void Yap_cputime_interval(Int *, Int *); extern void Yap_systime_interval(Int *, Int *); extern void Yap_InitSysbits(int wid); diff --git a/os/assets.c b/os/assets.c index 8c0f17694..2d8639b73 100644 --- a/os/assets.c +++ b/os/assets.c @@ -178,7 +178,7 @@ static bool set_cwd(VFS_t *me, const char *dirName) { strcpy(virtual_cwd, dirName); __android_log_print(ANDROID_LOG_INFO, "YAPDroid", "chdir %s", virtual_cwd); - Yap_do_low_level_trace = true; + Yap_do_low_level_trace=1; return true; } diff --git a/os/files.c b/os/files.c index b579d832d..0c6841551 100644 --- a/os/files.c +++ b/os/files.c @@ -460,7 +460,7 @@ static Int exists_directory(USES_REGS1) { #if HAVE_STAT struct SYSTEM_STAT ss; - file_name = RepAtom(AtomOfTerm(tname))->StrOfAE; + file_name = Yap_VF(RepAtom(AtomOfTerm(tname))->StrOfAE); if (SYSTEM_STAT(file_name, &ss) != 0) { /* ignore errors while checking a file */ return false; @@ -483,7 +483,7 @@ static Int is_absolute_file_name(USES_REGS1) { /* file_base_name(Stream,N) */ int l = push_text_stack(); const char *buf = Yap_TextTermToText(t PASS_REGS); if (buf) { - rc = Yap_IsAbsolutePath(buf); + rc = Yap_IsAbsolutePath(buf, true); } else { at = AtomOfTerm(t); #if _WIN32 diff --git a/os/fmem.c b/os/fmem.c index a07d94812..66dfb6b24 100644 --- a/os/fmem.c +++ b/os/fmem.c @@ -119,7 +119,7 @@ bool Yap_set_stream_to_buf(StreamDesc *st, const char *buf, FILE *f; // like any file stream. - st->file = f = fmemopen(buf, nchars, "r"); + st->file = f = fmemopen((char *)buf, nchars, "r"); st->status = Input_Stream_f | Seekable_Stream_f | InMemory_Stream_f; st->vfs = NULL; st->encoding = LOCAL_encoding; @@ -292,17 +292,15 @@ void Yap_MemOps(StreamDesc *st) { st->stream_getc = PlGetc; } -static int sssno; bool Yap_CloseMemoryStream(int sno) { - sssno++; - // if (sssno > 1720) Yap_do_low_level_trace=1; - if ((GLOBAL_Stream[sno].status & Output_Stream_f)) { + if ((GLOBAL_Stream[sno].status & Output_Stream_f) && GLOBAL_Stream[sno].file) { fflush(GLOBAL_Stream[sno].file); fclose(GLOBAL_Stream[sno].file); if (GLOBAL_Stream[sno].status & FreeOnClose_Stream_f) free(GLOBAL_Stream[sno].nbuf); } else { + if (GLOBAL_Stream[sno].file) fclose(GLOBAL_Stream[sno].file); if (GLOBAL_Stream[sno].status & FreeOnClose_Stream_f) free(GLOBAL_Stream[sno].nbuf); diff --git a/os/iopreds.c b/os/iopreds.c index e6e4498cf..e8cae80cf 100644 --- a/os/iopreds.c +++ b/os/iopreds.c @@ -1183,7 +1183,6 @@ do_open(Term file_name, Term t2, const char *fname; char fbuf[FILENAME_MAX]; stream_flags_t flags; - FILE *fd; const char *s_encoding; encoding_t encoding; Term tenc; @@ -1515,7 +1514,6 @@ int Yap_OpenStream(const char *fname, const char *io_mode) { struct vfs *vfsp; FILE *fd; int flags; - SMALLUNSGN s; sno = GetFreeStreamD(); if (sno < 0) { @@ -1526,6 +1524,7 @@ int Yap_OpenStream(const char *fname, const char *io_mode) { st = GLOBAL_Stream + sno; // read, write, append st->file = NULL; + fname = Yap_VF(fname); if ((vfsp = vfs_owner(fname)) != NULL ) { if (!vfsp->open(vfsp, sno, fname, io_mode)) { UNLOCK(st->streamlock); diff --git a/os/streams.c b/os/streams.c index f343884af..681c9b5df 100644 --- a/os/streams.c +++ b/os/streams.c @@ -959,7 +959,8 @@ static void CloseStream(int sno) { CACHE_REGS fflush(NULL); - if (!(GLOBAL_Stream[sno].status & + if (GLOBAL_Stream[sno].file && + !(GLOBAL_Stream[sno].status & (Null_Stream_f | Socket_Stream_f | InMemory_Stream_f | Pipe_Stream_f))) fclose(GLOBAL_Stream[sno].file); #if HAVE_SOCKET diff --git a/os/sysbits.c b/os/sysbits.c index fae36c733..8250ad79c 100644 --- a/os/sysbits.c +++ b/os/sysbits.c @@ -95,6 +95,7 @@ static bool is_directory(const char *FileName) { bool Yap_Exists(const char *f) { VFS_t *vfs; + f = Yap_VFAlloc(f); if ((vfs = vfs_owner(f))) { return vfs->exists(vfs,f); } @@ -106,8 +107,9 @@ bool Yap_Exists(const char *f) { } return false; #elif HAVE_ACCESS - if (access(f, F_OK) == 0) - return true; + if (access(f, F_OK) == 0) { + return true; + } if (errno == EINVAL) { Yap_Error(SYSTEM_ERROR_INTERNAL, TermNil, "bad flags to access"); } @@ -139,10 +141,13 @@ int Yap_dir_separator(int ch) { return dir_separator(ch); } char *libdir = NULL; #endif -bool Yap_IsAbsolutePath(const char *p0) { +bool Yap_IsAbsolutePath(const char *p0, bool expand) { // verify first if expansion is needed: ~/ or $HOME/ - const char *p = expandVars(p0, LOCAL_FileNameBuf); + const char *p = p0; bool nrc; + if (expand) { + p = expandVars(p0, LOCAL_FileNameBuf); + } #if _WIN32 || __MINGW32__ nrc = !PathIsRelative(p); #else @@ -258,7 +263,7 @@ static const char *PlExpandVars(const char *source, const char *root, pop_text_stack(lvl); return NULL; } - if (root && !Yap_IsAbsolutePath(source)) { + if (root && !Yap_IsAbsolutePath(source, false)) { strncpy(result, root, YAP_FILENAME_MAX); if (root[strlen(root) - 1] != '/') strncat(result, "/", YAP_FILENAME_MAX); @@ -364,14 +369,15 @@ char virtual_cwd[YAP_FILENAME_MAX + 1]; return rc; } + static const char *myrealpath(const char *path, char *out) { - if (!out) - out = LOCAL_FileNameBuf; -#if _WIN32 + int lvl = push_text_stack(); + #if _WIN32 DWORD retval = 0; // notice that the file does not need to exist retval = GetFullPathName(path, YAP_FILENAME_MAX, out, NULL); + pop_text_stack(lvl); if (retval == 0) { Yap_WinError("Generating a full path name for a file"); return NULL; @@ -382,12 +388,13 @@ static const char *myrealpath(const char *path, char *out) { char *rc = realpath(path, NULL); if (rc) { + pop_text_stack(lvl); return rc; } // rc = NULL; if (errno == ENOENT || errno == EACCES) { - char base[YAP_FILENAME_MAX + 1]; - strncpy(base, path, YAP_FILENAME_MAX - 1); + char *base= Malloc(YAP_FILENAME_MAX + 1); + strncpy(base, path, YAP_FILENAME_MAX ); rc = realpath(dirname(base), out); if (rc) { @@ -411,6 +418,7 @@ static const char *myrealpath(const char *path, char *out) { } #endif strcat(rc, b); + rc = pop_output_text_stack(lvl, rc); return rc; } } @@ -418,6 +426,7 @@ static const char *myrealpath(const char *path, char *out) { #endif out = malloc(strlen(path) + 1); strcpy(out, path); + pop_text_stack(lvl); return out; } @@ -923,7 +932,7 @@ static Int make_directory(USES_REGS1) { #if defined(__MINGW32__) || _MSC_VER if (_mkdir(fd) == -1) { #else - if (mkdir(fd, 0777) == -1) { + if (mkdir(Yap_VFAlloc(fd), 0777) == -1) { #endif /* return an error number */ return false; // errno? @@ -932,7 +941,7 @@ static Int make_directory(USES_REGS1) { } static Int p_rmdir(USES_REGS1) { - const char *fd = AtomName(AtomOfTerm(ARG1)); + const char *fd = Yap_VFAlloc(AtomName(AtomOfTerm(ARG1))); #if defined(__MINGW32__) || _MSC_VER if (_rmdir(fd) == -1) { #else @@ -1130,7 +1139,7 @@ static int volume_header(char *file) { int Yap_volume_header(char *file) { return volume_header(file); } -const char *Yap_getcwd(const char *cwd, size_t cwdlen) { +const char *Yap_getcwd(char *cwd, size_t cwdlen) { if (virtual_cwd[0]) { if (!cwd) { cwd = malloc(cwdlen+1); @@ -1189,7 +1198,7 @@ const char *Yap_findFile(const char *isource, const char *idef, YAP_file_type_t ftype, bool expand_root, bool in_lib) { char *save_buffer = NULL; - char *root = iroot, *source = isource; + char *root, *source; int rc = FAIL_RESTORE; int try = 0; bool abspath = false; @@ -1216,10 +1225,10 @@ const char *Yap_findFile(const char *isource, const char *idef, strcpy(source, idef); } if (source[0]) { - abspath = Yap_IsAbsolutePath(source); + abspath = Yap_IsAbsolutePath(source, expand_root); } if (!abspath && !root[0] && ftype == YAP_BOOT_PL) { - root = YAP_PL_SRCDIR; + strcpy(root, YAP_PL_SRCDIR); } break; case 1: // library directory is given in command line @@ -1246,8 +1255,7 @@ const char *Yap_findFile(const char *isource, const char *idef, if (ftype == YAP_SAVED_STATE || ftype == YAP_OBJ) { eroot = getenv("YAPLIBDIR"); } else if (ftype == YAP_BOOT_PL) { - eroot = getenv("YAPSHAREDIR" - "/pl"); + eroot = getenv("YAPSHAREDIR"); if (eroot == NULL) { continue; } else { @@ -1306,24 +1314,27 @@ const char *Yap_findFile(const char *isource, const char *idef, const char *pt = Yap_FindExecutable(); if (pt) { - if (ftype == YAP_BOOT_PL) { + if (ftype == YAP_BOOT_PL) { #if __ANDROID__ - strcpy(root, "../../../files/Yap/pl"); + strcpy(root, "../../../files/Yap/pl"); #else - root = "../../share/Yap/pl"; + root = "../../share/Yap/pl"; #endif - } else { - strcpy(root, (ftype == YAP_SAVED_STATE || ftype == YAP_OBJ - ? "../../lib/Yap" - : "../../share/Yap")); - } - if (strcmp(root, iroot)==0) { - done = true; - continue; - } - if (!save_buffer) - save_buffer = Malloc(YAP_FILENAME_MAX+1); - if (Yap_findFile(source, NULL, root, save_buffer, access, ftype, + } else { + strcpy(root, (ftype == YAP_SAVED_STATE || ftype == YAP_OBJ + ? "../../lib/Yap" + : "../../share/Yap")); + } + if (strcmp(root, iroot) == 0) { + done = true; + continue; + } + if (!save_buffer) { + save_buffer = Malloc(YAP_FILENAME_MAX + 1); + + save_buffer[0] = 0; + } + if (Yap_findFile(source, NULL, root, save_buffer, access, ftype, expand_root, in_lib)) strcpy(root, save_buffer); else @@ -1361,9 +1372,13 @@ const char *Yap_findFile(const char *isource, const char *idef, if (!access || Yap_Exists(work)) { work = pop_output_text_stack(lvl,work); return work; // done - } else if (abspath) - return NULL; + } else if (abspath) { + pop_text_stack(lvl); + return NULL; + } } + pop_text_stack(lvl); + return NULL; } @@ -1452,7 +1467,7 @@ static Int p_sh(USES_REGS1) { /* sh */ shell = (char *)getenv("SHELL"); if (shell == NULL) shell = "/bin/sh"; - if (system(shell) < 0) { + if (system(Yap_VFAlloc(shell)) < 0) { #if HAVE_STRERROR Yap_Error(SYSTEM_ERROR_OPERATING_SYSTEM, TermNil, "%s in sh/0", strerror(errno)); @@ -1658,10 +1673,12 @@ static Int p_mv(USES_REGS1) { /* rename(+OldName,+NewName) */ } else if (!IsAtomTerm(t2)) { Yap_Error(TYPE_ERROR_ATOM, t2, "second argument to rename/2 not atom"); } else { - oldname = (RepAtom(AtomOfTerm(t1)))->StrOfAE; - newname = (RepAtom(AtomOfTerm(t2)))->StrOfAE; + oldname = Yap_VFAlloc((RepAtom(AtomOfTerm(t1)))->StrOfAE); + newname = Yap_VFAlloc((RepAtom(AtomOfTerm(t2)))->StrOfAE); if ((r = link(oldname, newname)) == 0 && (r = unlink(oldname)) != 0) unlink(newname); + free(oldname); + free(newname); if (r != 0) { #if HAVE_STRERROR Yap_Error(SYSTEM_ERROR_OPERATING_SYSTEM, t2, "%s in rename(%s,%s)", diff --git a/os/writeterm.c b/os/writeterm.c index bc8697446..f61cd85bb 100644 --- a/os/writeterm.c +++ b/os/writeterm.c @@ -678,7 +678,7 @@ static Int term_to_string(USES_REGS1) { Term t2 = Deref(ARG2), rc = false, t1 = Deref(ARG1); const char *s; if (IsVarTerm(t2)) { - s = Yap_TermToString(ARG1,LOCAL_encoding, + s = Yap_TermToBuffer(ARG1, LOCAL_encoding, Quote_illegal_f | Handle_vars_f); if (!s || !MkStringTerm(s)) { Yap_Error(RESOURCE_ERROR_HEAP, t1, @@ -699,7 +699,7 @@ static Int term_to_atom(USES_REGS1) { Term t2 = Deref(ARG2), ctl, rc = false; Atom at; if (IsVarTerm(t2)) { - const char *s = Yap_TermToString(Deref(ARG1), LOCAL_encoding, + const char *s = Yap_TermToBuffer(Deref(ARG1), LOCAL_encoding, Quote_illegal_f | Handle_vars_f); if (!s || !(at = Yap_UTF8ToAtom((const unsigned char *)s))) { Yap_Error(RESOURCE_ERROR_HEAP, t2, diff --git a/os/yapio.h b/os/yapio.h index 5665b8302..8b23eea13 100644 --- a/os/yapio.h +++ b/os/yapio.h @@ -88,7 +88,7 @@ extern int Yap_GetCharForSIGINT(void); extern Int Yap_StreamToFileNo(Term); extern int Yap_OpenStream(const char*, const char*); extern int Yap_FileStream(FILE*, char *, Term, int); -extern char *Yap_TermToString(Term t, encoding_t encoding, int flags); +extern char *Yap_TermToBuffer(Term t, encoding_t encoding, int flags); extern char *Yap_HandleToString(yhandle_t l, size_t sz, size_t *length, encoding_t *encoding, int flags); extern int Yap_GetFreeStreamD(void); @@ -103,7 +103,7 @@ extern int Yap_growheap_in_parser(tr_fr_ptr *, TokEntry **, VarEntry **); extern int Yap_growstack_in_parser(tr_fr_ptr *, TokEntry **, VarEntry **); extern int Yap_growtrail_in_parser(tr_fr_ptr *, TokEntry **, VarEntry **); -extern bool Yap_IsAbsolutePath(const char *p); +extern bool Yap_IsAbsolutePath(const char *p, bool); extern Atom Yap_TemporaryFile(const char *prefix, int *fd); extern const char *Yap_AbsoluteFile(const char *spec, char *obuf, bool expand); @@ -156,6 +156,37 @@ INLINE_ONLY inline EXTERN Term MkCharTerm(Int c) { return MkAtomTerm(Yap_ULookupAtom(cs)); } + + +INLINE_ONLY inline EXTERN char *Yap_VF(const char *path){ + char out[YAP_FILENAME_MAX+1], *p = (char *)path; + extern char virtual_cwd[]; + + if ( virtual_cwd[0] == 0 || Yap_IsAbsolutePath(path, false)) { + return p; + } + strcpy(out, virtual_cwd); + strcat(out, "/" ); + strcat(out, p); + strcpy(p, out); + return p; +} + + +INLINE_ONLY inline EXTERN char *Yap_VFAlloc(const char *path){ + char *out; + extern char virtual_cwd[]; + + out = (char *)malloc(YAP_FILENAME_MAX+1); + if ( virtual_cwd[0] == 0 || !Yap_IsAbsolutePath(path, false)) { + return (char *)path; + } + strcpy(out, virtual_cwd); + strcat(out, "/" ); + strcat(out, path); + return out; +} + /// UT when yap started extern uint64_t Yap_StartOfWTimes; @@ -165,4 +196,6 @@ extern bool Yap_HandleSIGINT(void); extern bool Yap_set_stream_to_buf(StreamDesc *st, const char *bufi, size_t nchars USES_REGS); + + #endif