memory streams
- work with blobs - recover memory on close - do not call Unix code on win32
This commit is contained in:
parent
703ac35735
commit
697f57ecef
@ -370,7 +370,8 @@ typedef enum stream_f {
|
|||||||
RepError_Prolog_f = 0x400000, /**< handle representation error as Prolog terms */
|
RepError_Prolog_f = 0x400000, /**< handle representation error as Prolog terms */
|
||||||
RepError_Xml_f = 0x800000, /**< handle representation error as XML objects */
|
RepError_Xml_f = 0x800000, /**< handle representation error as XML objects */
|
||||||
DoNotCloseOnAbort_Stream_f= 0x1000000, /**< do not close the stream after an abort event */
|
DoNotCloseOnAbort_Stream_f= 0x1000000, /**< do not close the stream after an abort event */
|
||||||
Readline_Stream_f= 0x2000000 /**< the stream is a readline stream */
|
Readline_Stream_f= 0x2000000, /**< the stream is a readline stream */
|
||||||
|
FreeOnClose_Stream_f= 0x4000000 /**< the stream buffer should be releaed on close */
|
||||||
} estream_f;
|
} estream_f;
|
||||||
|
|
||||||
typedef uint64_t stream_flags_t;
|
typedef uint64_t stream_flags_t;
|
||||||
|
48
os/mem.c
48
os/mem.c
@ -64,6 +64,11 @@ FILE * open_memstream (char **buf, size_t *len);
|
|||||||
#define MAY_WRITE 1
|
#define MAY_WRITE 1
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if _WIN32
|
||||||
|
#undef MAY_WRITE
|
||||||
|
#undef MAY_READ
|
||||||
|
#endif
|
||||||
|
|
||||||
#if !MAY_READ
|
#if !MAY_READ
|
||||||
static int MemGetc( int);
|
static int MemGetc( int);
|
||||||
|
|
||||||
@ -253,13 +258,15 @@ Yap_open_buf_write_stream(char *buf, size_t nchars, encoding_t *encp, memBufSou
|
|||||||
sno = GetFreeStreamD();
|
sno = GetFreeStreamD();
|
||||||
if (sno < 0)
|
if (sno < 0)
|
||||||
return -1;
|
return -1;
|
||||||
|
st = GLOBAL_Stream+sno;
|
||||||
|
st->status = Output_Stream_f | InMemory_Stream_f;
|
||||||
if (!buf) {
|
if (!buf) {
|
||||||
if (!nchars) {
|
if (!nchars) {
|
||||||
nchars = Yap_page_size;
|
nchars = Yap_page_size;
|
||||||
}
|
}
|
||||||
buf = malloc( nchars );
|
buf = malloc( nchars );
|
||||||
|
st->status |= FreeOnClose_Stream_f;
|
||||||
}
|
}
|
||||||
st = GLOBAL_Stream+sno;
|
|
||||||
st->nbuf = buf;
|
st->nbuf = buf;
|
||||||
if(!st->nbuf) {
|
if(!st->nbuf) {
|
||||||
return -1;
|
return -1;
|
||||||
@ -275,12 +282,11 @@ Yap_open_buf_write_stream(char *buf, size_t nchars, encoding_t *encp, memBufSou
|
|||||||
Yap_DefaultStreamOps( st );
|
Yap_DefaultStreamOps( st );
|
||||||
#if MAY_WRITE
|
#if MAY_WRITE
|
||||||
st->file = open_memstream(&st->nbuf, &st->nsize);
|
st->file = open_memstream(&st->nbuf, &st->nsize);
|
||||||
st->status = Output_Stream_f | InMemory_Stream_f|Seekable_Stream_f;
|
st->status |= Seekable_Stream_f;
|
||||||
#else
|
#else
|
||||||
st->u.mem_string.pos = 0;
|
st->u.mem_string.pos = 0;
|
||||||
st->u.mem_string.buf = nbuf;
|
st->u.mem_string.buf = st->nbuf;
|
||||||
st->u.mem_string.max_size = nchars;
|
st->u.mem_string.max_size = nchars;
|
||||||
st->status = Output_Stream_f | InMemory_Stream_f;
|
|
||||||
#endif
|
#endif
|
||||||
Yap_MemOps( st );
|
Yap_MemOps( st );
|
||||||
UNLOCK(st->streamlock);
|
UNLOCK(st->streamlock);
|
||||||
@ -327,8 +333,8 @@ open_mem_write_stream (USES_REGS1) /* $open_mem_write_stream(-Stream) */
|
|||||||
char *
|
char *
|
||||||
Yap_MemExportStreamPtr( int sno )
|
Yap_MemExportStreamPtr( int sno )
|
||||||
{
|
{
|
||||||
char *s;
|
|
||||||
#if MAY_WRITE
|
#if MAY_WRITE
|
||||||
|
char *s;
|
||||||
if (fflush(GLOBAL_Stream[sno].file) == 0)
|
if (fflush(GLOBAL_Stream[sno].file) == 0)
|
||||||
{
|
{
|
||||||
s = GLOBAL_Stream[sno].nbuf;
|
s = GLOBAL_Stream[sno].nbuf;
|
||||||
@ -336,7 +342,7 @@ Yap_MemExportStreamPtr( int sno )
|
|||||||
}
|
}
|
||||||
return NULL;
|
return NULL;
|
||||||
#else
|
#else
|
||||||
return &GLOBAL_Stream[sno].u.mem_string;
|
return GLOBAL_Stream[sno].u.mem_string.buf;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -360,7 +366,6 @@ peek_mem_write_stream ( USES_REGS1 )
|
|||||||
i = GLOBAL_Stream[sno].nsize;
|
i = GLOBAL_Stream[sno].nsize;
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
size_t pos;
|
|
||||||
ptr = GLOBAL_Stream[sno].u.mem_string.buf;
|
ptr = GLOBAL_Stream[sno].u.mem_string.buf;
|
||||||
i = GLOBAL_Stream[sno].u.mem_string.pos;
|
i = GLOBAL_Stream[sno].u.mem_string.pos;
|
||||||
#endif
|
#endif
|
||||||
@ -401,6 +406,35 @@ void
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool Yap_CloseMemoryStream( int sno )
|
||||||
|
{
|
||||||
|
if (!(GLOBAL_Stream[sno].status & Output_Stream_f) ) {
|
||||||
|
#if MAY_WRITE
|
||||||
|
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) {
|
||||||
|
free(GLOBAL_Stream[sno].u.mem_string.buf);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
} else {
|
||||||
|
#if MAY_READ
|
||||||
|
fclose(GLOBAL_Stream[sno].file);
|
||||||
|
Yap_FreeAtomSpace(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) {
|
||||||
|
free(GLOBAL_Stream[sno].u.mem_string.buf);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
Yap_InitMems( void )
|
Yap_InitMems( void )
|
||||||
{
|
{
|
||||||
|
Reference in New Issue
Block a user