This commit is contained in:
Vitor Santos Costa 2016-06-17 17:12:23 +01:00
parent d1b0f45c28
commit a9839b4d4a

132
os/mem.c
View File

@ -43,6 +43,22 @@ static int MemGetc(int sno) {
} }
return ch; return ch;
} }
/* peek from memory */
int Yap_MemPeekc(int sno) {
StreamDesc *s = &GLOBAL_Stream[sno];
Int ch;
int spos;
spos = s->u.mem_string.pos;
if (spos == s->u.mem_string.max_size) {
return -1;
} else {
ch = s->u.mem_string.buf[spos];
}
return ch;
}
#endif #endif
#if !MAY_WRITE #if !MAY_WRITE
@ -58,34 +74,14 @@ static int MemPutc(int sno, int ch) {
#endif #endif
s->u.mem_string.buf[s->u.mem_string.pos++] = ch; s->u.mem_string.buf[s->u.mem_string.pos++] = ch;
if (s->u.mem_string.pos >= s->u.mem_string.max_size - 8) { if (s->u.mem_string.pos >= s->u.mem_string.max_size - 8) {
int old_src = s->u.mem_string.src, new_src; int new_src;
/* oops, we have reached an overflow */ /* oops, we have reached an overflow */
Int new_max_size = s->u.mem_string.max_size + Yap_page_size; Int new_max_size = s->u.mem_string.max_size + Yap_page_size;
char *newbuf; char *newbuf;
if ((newbuf = (ADDR)realloc(s->u.mem_string.buf,
if (old_src == MEM_BUF_CODE &&
(newbuf = Yap_AllocAtomSpace(new_max_size * sizeof(char))) != NULL) {
new_src = MEM_BUF_CODE;
#if HAVE_MEMMOVE
memmove((void *)newbuf, (void *)s->u.mem_string.buf,
(size_t)((s->u.mem_string.pos) * sizeof(char)));
#else
{
Int n = s->u.mem_string.pos;
char *to = newbuf;
char *from = s->u.mem_string.buf;
while (n-- >= 0) {
*to++ = *from++;
}
}
#endif
Yap_FreeAtomSpace(s->u.mem_string.buf);
#if !HAVE_SYSTEM_MALLOC
} else if ((newbuf = (ADDR)realloc(s->u.mem_string.buf,
new_max_size * sizeof(char))) != NULL) { new_max_size * sizeof(char))) != NULL) {
new_src = MEM_BUF_MALLOC; new_src = MEM_BUF_MALLOC;
#endif
} else { } else {
if (GLOBAL_Stream[sno].u.mem_string.error_handler) { if (GLOBAL_Stream[sno].u.mem_string.error_handler) {
CACHE_REGS CACHE_REGS
@ -98,8 +94,6 @@ static int MemPutc(int sno, int ch) {
} }
return -1; return -1;
} }
if (old_src == MEM_BUF_CODE) {
}
s->u.mem_string.buf = newbuf; s->u.mem_string.buf = newbuf;
s->u.mem_string.max_size = new_max_size; s->u.mem_string.max_size = new_max_size;
s->u.mem_string.src = new_src; s->u.mem_string.src = new_src;
@ -110,6 +104,34 @@ static int MemPutc(int sno, int ch) {
#endif #endif
bool Yap_set_stream_to_buf(StreamDesc *st, const char *buf, size_t nchars) {
FILE *f;
stream_flags_t flags;
#if MAY_READ
// like any file stream.
st->file = f = fmemopen((void *)buf, nchars, "r");
flags = Input_Stream_f | InMemory_Stream_f | Seekable_Stream_f;
#else
st->file = f = NULL;
flags = Input_Stream_f | InMemory_Stream_f;
#endif
Yap_initStream(st-GLOBAL_Stream, f, NULL, TermNil, LOCAL_encoding, flags,
AtomRead);
// like any file stream.
#if !MAY_READ
/* currently these streams are not seekable */
st->status = Input_Stream_f | InMemory_Stream_f;
st->u.mem_string.pos = 0;
st->u.mem_string.buf = (char *)buf;
st->u.mem_string.max_size = nchars;
st->u.mem_string.error_handler = NULL;
// st->u.mem_string.src = src; check new assets coode
#endif
Yap_DefaultStreamOps(st);
return true;
}
int Yap_open_buf_read_stream(const char *buf, size_t nchars, encoding_t *encp, int Yap_open_buf_read_stream(const char *buf, size_t nchars, encoding_t *encp,
memBufSource src) { memBufSource src) {
CACHE_REGS CACHE_REGS
@ -130,7 +152,7 @@ int Yap_open_buf_read_stream(const char *buf, size_t nchars, encoding_t *encp,
encoding = LOCAL_encoding; encoding = LOCAL_encoding;
#if MAY_READ #if MAY_READ
// like any file stream. // like any file stream.
st->file = f = fmemopen((void *)buf, nchars, "r"); f = st->file = fmemopen((void *)buf, nchars, "r");
flags = Input_Stream_f | InMemory_Stream_f | Seekable_Stream_f; flags = Input_Stream_f | InMemory_Stream_f | Seekable_Stream_f;
#else #else
st->file = f = NULL; st->file = f = NULL;
@ -157,44 +179,11 @@ static Int
{ {
Term t, ti; Term t, ti;
int sno; int sno;
Int sl = 0, nchars = 0; const char *buf;
char *buf;
ti = Deref(ARG1); ti = Deref(ARG1);
while (ti != TermNil) { buf = Yap_TextTermToText(ti, NULL, 0, LOCAL_encoding);
if (IsVarTerm(ti)) { sno = Yap_open_buf_read_stream(buf, strlen(buf)+1, &LOCAL_encoding, MEM_BUF_MALLOC);
Yap_Error(INSTANTIATION_ERROR, ti, "open_mem_read_stream");
return (FALSE);
} else if (!IsPairTerm(ti)) {
Yap_Error(TYPE_ERROR_LIST, ti, "open_mem_read_stream");
return (FALSE);
} else {
sl++;
ti = TailOfTerm(ti);
}
}
while ((buf = (char *)Yap_AllocAtomSpace((sl + 1) * sizeof(char))) == NULL) {
if (!Yap_growheap(FALSE, (sl + 1) * sizeof(char), NULL)) {
Yap_Error(RESOURCE_ERROR_HEAP, TermNil, LOCAL_ErrorMessage);
return (FALSE);
}
}
ti = Deref(ARG1);
while (ti != TermNil) {
Term ts = HeadOfTerm(ti);
if (IsVarTerm(ts)) {
Yap_Error(INSTANTIATION_ERROR, ARG1, "open_mem_read_stream");
return (FALSE);
} else if (!IsIntTerm(ts)) {
Yap_Error(TYPE_ERROR_INTEGER, ARG1, "open_mem_read_stream");
return (FALSE);
}
buf[nchars++] = IntOfTerm(ts);
ti = TailOfTerm(ti);
}
buf[nchars] = '\0';
sno = Yap_open_buf_read_stream(buf, nchars, &LOCAL_encoding, MEM_BUF_CODE);
t = Yap_MkStream(sno); t = Yap_MkStream(sno);
return (Yap_unify(ARG2, t)); return (Yap_unify(ARG2, t));
} }
@ -229,10 +218,12 @@ int Yap_open_buf_write_stream(encoding_t enc, memBufSource src) {
} }
#endif #endif
#else #else
char buf[YAP_FILENAME_MAX + 1]; st->nbuf =
st->nbuf = buf; st->u.mem_string.buf =
st->u.mem_string.buf = buf; malloc(PLGETC_BUF_SIZE);
st->u.mem_string.max_size = YAP_FILENAME_MAX; st->u.mem_string.src = 1;
st->u.mem_string.max_size = PLGETC_BUF_SIZE-1;
st->u.mem_string.pos = 0;
#endif #endif
UNLOCK(st->streamlock); UNLOCK(st->streamlock);
return sno; return sno;
@ -275,6 +266,7 @@ char *Yap_MemExportStreamPtr(int sno) {
} }
return NULL; return NULL;
#else #else
GLOBAL_Stream[sno].u.mem_string.buf[GLOBAL_Stream[sno].u.mem_string.pos] = '\0';
return GLOBAL_Stream[sno].u.mem_string.buf; return GLOBAL_Stream[sno].u.mem_string.buf;
#endif #endif
} }
@ -337,16 +329,14 @@ void Yap_MemOps(StreamDesc *st) {
} }
bool Yap_CloseMemoryStream(int sno) { bool Yap_CloseMemoryStream(int sno) {
if (!(GLOBAL_Stream[sno].status & Output_Stream_f)) { if ((GLOBAL_Stream[sno].status & Output_Stream_f)) {
#if MAY_WRITE #if MAY_WRITE
fflush(GLOBAL_Stream[sno].file); fflush(GLOBAL_Stream[sno].file);
fclose(GLOBAL_Stream[sno].file); fclose(GLOBAL_Stream[sno].file);
if (GLOBAL_Stream[sno].status & FreeOnClose_Stream_f) if (GLOBAL_Stream[sno].status & FreeOnClose_Stream_f)
free(GLOBAL_Stream[sno].nbuf); free(GLOBAL_Stream[sno].nbuf);
#else #else
if (GLOBAL_Stream[sno].u.mem_string.src == MEM_BUF_CODE) if (GLOBAL_Stream[sno].u.mem_string.src == MEM_BUF_MALLOC) {
Yap_FreeAtomSpace(GLOBAL_Stream[sno].u.mem_string.buf);
else if (GLOBAL_Stream[sno].u.mem_string.src == MEM_BUF_MALLOC) {
free(GLOBAL_Stream[sno].u.mem_string.buf); free(GLOBAL_Stream[sno].u.mem_string.buf);
} }
#endif #endif
@ -356,9 +346,7 @@ bool Yap_CloseMemoryStream(int sno) {
if (GLOBAL_Stream[sno].status & FreeOnClose_Stream_f) if (GLOBAL_Stream[sno].status & FreeOnClose_Stream_f)
free(GLOBAL_Stream[sno].nbuf); free(GLOBAL_Stream[sno].nbuf);
#else #else
if (GLOBAL_Stream[sno].u.mem_string.src == MEM_BUF_CODE) if (GLOBAL_Stream[sno].u.mem_string.src == MEM_BUF_MALLOC) {
Yap_FreeAtomSpace(GLOBAL_Stream[sno].u.mem_string.buf);
else if (GLOBAL_Stream[sno].u.mem_string.src == MEM_BUF_MALLOC) {
free(GLOBAL_Stream[sno].u.mem_string.buf); free(GLOBAL_Stream[sno].u.mem_string.buf);
} }
#endif #endif