Leaks
This commit is contained in:
parent
d1b0f45c28
commit
a9839b4d4a
132
os/mem.c
132
os/mem.c
@ -43,6 +43,22 @@ static int MemGetc(int sno) {
|
||||
}
|
||||
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
|
||||
|
||||
#if !MAY_WRITE
|
||||
@ -58,34 +74,14 @@ static int MemPutc(int sno, int ch) {
|
||||
#endif
|
||||
s->u.mem_string.buf[s->u.mem_string.pos++] = ch;
|
||||
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 */
|
||||
Int new_max_size = s->u.mem_string.max_size + Yap_page_size;
|
||||
char *newbuf;
|
||||
|
||||
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,
|
||||
if ((newbuf = (ADDR)realloc(s->u.mem_string.buf,
|
||||
new_max_size * sizeof(char))) != NULL) {
|
||||
new_src = MEM_BUF_MALLOC;
|
||||
#endif
|
||||
} else {
|
||||
if (GLOBAL_Stream[sno].u.mem_string.error_handler) {
|
||||
CACHE_REGS
|
||||
@ -98,8 +94,6 @@ static int MemPutc(int sno, int ch) {
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
if (old_src == MEM_BUF_CODE) {
|
||||
}
|
||||
s->u.mem_string.buf = newbuf;
|
||||
s->u.mem_string.max_size = new_max_size;
|
||||
s->u.mem_string.src = new_src;
|
||||
@ -110,6 +104,34 @@ static int MemPutc(int sno, int ch) {
|
||||
|
||||
#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,
|
||||
memBufSource src) {
|
||||
CACHE_REGS
|
||||
@ -130,7 +152,7 @@ int Yap_open_buf_read_stream(const char *buf, size_t nchars, encoding_t *encp,
|
||||
encoding = LOCAL_encoding;
|
||||
#if MAY_READ
|
||||
// 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;
|
||||
#else
|
||||
st->file = f = NULL;
|
||||
@ -157,44 +179,11 @@ static Int
|
||||
{
|
||||
Term t, ti;
|
||||
int sno;
|
||||
Int sl = 0, nchars = 0;
|
||||
char *buf;
|
||||
const char *buf;
|
||||
|
||||
ti = Deref(ARG1);
|
||||
while (ti != TermNil) {
|
||||
if (IsVarTerm(ti)) {
|
||||
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);
|
||||
buf = Yap_TextTermToText(ti, NULL, 0, LOCAL_encoding);
|
||||
sno = Yap_open_buf_read_stream(buf, strlen(buf)+1, &LOCAL_encoding, MEM_BUF_MALLOC);
|
||||
t = Yap_MkStream(sno);
|
||||
return (Yap_unify(ARG2, t));
|
||||
}
|
||||
@ -229,10 +218,12 @@ int Yap_open_buf_write_stream(encoding_t enc, memBufSource src) {
|
||||
}
|
||||
#endif
|
||||
#else
|
||||
char buf[YAP_FILENAME_MAX + 1];
|
||||
st->nbuf = buf;
|
||||
st->u.mem_string.buf = buf;
|
||||
st->u.mem_string.max_size = YAP_FILENAME_MAX;
|
||||
st->nbuf =
|
||||
st->u.mem_string.buf =
|
||||
malloc(PLGETC_BUF_SIZE);
|
||||
st->u.mem_string.src = 1;
|
||||
st->u.mem_string.max_size = PLGETC_BUF_SIZE-1;
|
||||
st->u.mem_string.pos = 0;
|
||||
#endif
|
||||
UNLOCK(st->streamlock);
|
||||
return sno;
|
||||
@ -275,6 +266,7 @@ char *Yap_MemExportStreamPtr(int sno) {
|
||||
}
|
||||
return NULL;
|
||||
#else
|
||||
GLOBAL_Stream[sno].u.mem_string.buf[GLOBAL_Stream[sno].u.mem_string.pos] = '\0';
|
||||
return GLOBAL_Stream[sno].u.mem_string.buf;
|
||||
#endif
|
||||
}
|
||||
@ -337,16 +329,14 @@ void Yap_MemOps(StreamDesc *st) {
|
||||
}
|
||||
|
||||
bool Yap_CloseMemoryStream(int sno) {
|
||||
if (!(GLOBAL_Stream[sno].status & Output_Stream_f)) {
|
||||
if ((GLOBAL_Stream[sno].status & Output_Stream_f)) {
|
||||
#if MAY_WRITE
|
||||
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].u.mem_string.src == MEM_BUF_CODE)
|
||||
Yap_FreeAtomSpace(GLOBAL_Stream[sno].u.mem_string.buf);
|
||||
else if (GLOBAL_Stream[sno].u.mem_string.src == MEM_BUF_MALLOC) {
|
||||
if (GLOBAL_Stream[sno].u.mem_string.src == MEM_BUF_MALLOC) {
|
||||
free(GLOBAL_Stream[sno].u.mem_string.buf);
|
||||
}
|
||||
#endif
|
||||
@ -356,9 +346,7 @@ bool Yap_CloseMemoryStream(int sno) {
|
||||
if (GLOBAL_Stream[sno].status & FreeOnClose_Stream_f)
|
||||
free(GLOBAL_Stream[sno].nbuf);
|
||||
#else
|
||||
if (GLOBAL_Stream[sno].u.mem_string.src == MEM_BUF_CODE)
|
||||
Yap_FreeAtomSpace(GLOBAL_Stream[sno].u.mem_string.buf);
|
||||
else if (GLOBAL_Stream[sno].u.mem_string.src == MEM_BUF_MALLOC) {
|
||||
if (GLOBAL_Stream[sno].u.mem_string.src == MEM_BUF_MALLOC) {
|
||||
free(GLOBAL_Stream[sno].u.mem_string.buf);
|
||||
}
|
||||
#endif
|
||||
|
Reference in New Issue
Block a user