#define PERL_IN_PERLIO_C
#include "perl.h"
+#undef PerlMemShared_calloc
+#define PerlMemShared_calloc(x,y) calloc(x,y)
+#undef PerlMemShared_free
+#define PerlMemShared_free(x) free(x)
+
+
#ifndef PERLIO_LAYERS
int
PerlIO_apply_layers(pTHX_ PerlIO *f, const char *mode, const char *names)
#define PERLIO_TABLE_SIZE 64
PerlIO *
-PerlIO_allocate(void)
+PerlIO_allocate(pTHX)
{
/* Find a free slot in the table, allocating new table as necessary */
- PerlIO **last = &_perlio;
+ PerlIO **last;
PerlIO *f;
+ last = &_perlio;
while ((f = *last))
{
int i;
}
}
}
- Newz('I',f,PERLIO_TABLE_SIZE,PerlIO);
+ f = PerlMemShared_calloc(PERLIO_TABLE_SIZE,sizeof(PerlIO));
if (!f)
- return NULL;
+ {
+ return NULL;
+ }
*last = f;
return f+1;
}
void
-PerlIO_cleantable(PerlIO **tablep)
+PerlIO_cleantable(pTHX_ PerlIO **tablep)
{
PerlIO *table = *tablep;
if (table)
{
int i;
- PerlIO_cleantable((PerlIO **) &(table[0]));
+ PerlIO_cleantable(aTHX_ (PerlIO **) &(table[0]));
for (i=PERLIO_TABLE_SIZE-1; i > 0; i--)
{
PerlIO *f = table+i;
PerlIO_close(f);
}
}
- Safefree(table);
+ PerlMemShared_free(table);
*tablep = NULL;
}
}
AV *PerlIO_layer_av;
void
-PerlIO_cleanup(void)
+PerlIO_cleanup()
{
- PerlIO_cleantable(&_perlio);
+ dTHX;
+ PerlIO_cleantable(aTHX_ &_perlio);
}
void
PerlIO_pop(PerlIO *f)
{
+ dTHX;
PerlIOl *l = *f;
if (l)
{
PerlIO_debug("PerlIO_pop f=%p %s\n",f,l->tab->name);
(*l->tab->Popped)(f);
*f = l->next;
- Safefree(l);
+ PerlMemShared_free(l);
}
}
{
if (!_perlio)
{
- PerlIO_allocate();
+ dTHX;
+ PerlIO_allocate(aTHX);
PerlIO_fdopen(0,"Ir" PERLIO_STDTEXT);
PerlIO_fdopen(1,"Iw" PERLIO_STDTEXT);
PerlIO_fdopen(2,"Iw" PERLIO_STDTEXT);
PerlIO *
PerlIO_push(PerlIO *f,PerlIO_funcs *tab,const char *mode)
{
+ dTHX;
PerlIOl *l = NULL;
- Newc('L',l,tab->size,char,PerlIOl);
+ l = PerlMemShared_calloc(tab->size,sizeof(char));
if (l)
{
Zero(l,tab->size,char);
return (*PerlIOBase(f)->tab->Close)(f);
}
+#undef PerlIO_fdupopen
+PerlIO *
+PerlIO_fdupopen(pTHX_ PerlIO *f)
+{
+ char buf[8];
+ int fd = PerlLIO_dup(PerlIO_fileno(f));
+ PerlIO *new = PerlIO_fdopen(fd,PerlIO_modestr(f,buf));
+ if (new)
+ {
+ Off_t posn = PerlIO_tell(f);
+ PerlIO_seek(new,posn,SEEK_SET);
+ }
+ return new;
+}
#undef PerlIO_close
int
{
char *s = buf;
IV flags = PerlIOBase(f)->flags;
- if (flags & PERLIO_F_CANREAD)
- *s++ = 'r';
- if (flags & PERLIO_F_CANWRITE)
- *s++ = 'w';
- if (flags & PERLIO_F_CRLF)
- *s++ = 't';
- else
+ if (flags & PERLIO_F_APPEND)
+ {
+ *s++ = 'a';
+ if (flags & PERLIO_F_CANREAD)
+ {
+ *s++ = '+';
+ }
+ }
+ else if (flags & PERLIO_F_CANREAD)
+ {
+ *s++ = 'r';
+ if (flags & PERLIO_F_CANWRITE)
+ *s++ = '+';
+ }
+ else if (flags & PERLIO_F_CANWRITE)
+ {
+ *s++ = 'w';
+ if (flags & PERLIO_F_CANREAD)
+ {
+ *s++ = '+';
+ }
+ }
+#if O_TEXT != O_BINARY
+ if (!(flags & PERLIO_F_CRLF))
*s++ = 'b';
+#endif
*s = '\0';
return buf;
}
PerlIO *
PerlIOUnix_fdopen(PerlIO_funcs *self, int fd,const char *mode)
{
+ dTHX;
PerlIO *f = NULL;
if (*mode == 'I')
mode++;
int oflags = PerlIOUnix_oflags(mode);
if (oflags != -1)
{
- PerlIOUnix *s = PerlIOSelf(PerlIO_push(f = PerlIO_allocate(),self,mode),PerlIOUnix);
+ PerlIOUnix *s = PerlIOSelf(PerlIO_push(f = PerlIO_allocate(aTHX),self,mode),PerlIOUnix);
s->fd = fd;
s->oflags = oflags;
PerlIOBase(f)->flags |= PERLIO_F_OPEN;
int fd = PerlLIO_open3(path,oflags,0666);
if (fd >= 0)
{
- PerlIOUnix *s = PerlIOSelf(PerlIO_push(f = PerlIO_allocate(),self,mode),PerlIOUnix);
+ PerlIOUnix *s = PerlIOSelf(PerlIO_push(f = PerlIO_allocate(aTHX),self,mode),PerlIOUnix);
s->fd = fd;
s->oflags = oflags;
PerlIOBase(f)->flags |= PERLIO_F_OPEN;
}
if (stdio)
{
- PerlIOStdio *s = PerlIOSelf(PerlIO_push(f = PerlIO_allocate(),self,mode),PerlIOStdio);
+ PerlIOStdio *s = PerlIOSelf(PerlIO_push(f = PerlIO_allocate(aTHX),self,mode),PerlIOStdio);
s->stdio = stdio;
}
}
PerlIO *
PerlIO_importFILE(FILE *stdio, int fl)
{
+ dTHX;
PerlIO *f = NULL;
if (stdio)
{
- PerlIOStdio *s = PerlIOSelf(PerlIO_push(f = PerlIO_allocate(),&PerlIO_stdio,"r+"),PerlIOStdio);
+ PerlIOStdio *s = PerlIOSelf(PerlIO_push(f = PerlIO_allocate(aTHX),&PerlIO_stdio,"r+"),PerlIOStdio);
s->stdio = stdio;
}
return f;
if (stdio)
{
char tmode[8];
- PerlIOStdio *s = PerlIOSelf(PerlIO_push(f = PerlIO_allocate(), self,
+ PerlIOStdio *s = PerlIOSelf(PerlIO_push(f = PerlIO_allocate(aTHX), self,
(mode = PerlIOStdio_mode(mode,tmode))),
PerlIOStdio);
s->stdio = stdio;
IV
PerlIOBuf_close(PerlIO *f)
{
+ dTHX;
IV code = PerlIOBase_close(f);
PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
if (b->buf && b->buf != (STDCHAR *) &b->oneword)
{
- Safefree(b->buf);
+ PerlMemShared_free(b->buf);
}
b->buf = NULL;
b->ptr = b->end = b->buf;
PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
if (!b->buf)
{
+ dTHX;
if (!b->bufsiz)
b->bufsiz = 4096;
- New('B',b->buf,b->bufsiz,STDCHAR);
+ b->buf = PerlMemShared_calloc(b->bufsiz,sizeof(STDCHAR));
if (!b->buf)
{
b->buf = (STDCHAR *)&b->oneword;
PerlIOBuf *b = PerlIOSelf(f,PerlIOBuf);
if (b->buf && b->buf != (STDCHAR *) &b->oneword)
{
- Safefree(b->buf);
+ dTHX;
+ PerlMemShared_free(b->buf);
b->buf = NULL;
}
PerlIO_pop(f);
FILE *stdio = PerlSIO_tmpfile();
if (stdio)
{
- PerlIOStdio *s = PerlIOSelf(PerlIO_push(f = PerlIO_allocate(),&PerlIO_stdio,"w+"),PerlIOStdio);
+ PerlIOStdio *s = PerlIOSelf(PerlIO_push(f = PerlIO_allocate(aTHX),&PerlIO_stdio,"w+"),PerlIOStdio);
s->stdio = stdio;
}
return f;
class CPerlHost
{
public:
+ /* Constructors */
CPerlHost(void);
CPerlHost(struct IPerlMem** ppMem, struct IPerlMem** ppMemShared,
struct IPerlMem** ppMemParse, struct IPerlEnv** ppEnv,
DWORD m_dwEnvCount;
LPSTR* m_lppEnvList;
+ static long num_hosts;
+public:
+ inline int LastHost(void) { return num_hosts == 1L; };
};
+long CPerlHost::num_hosts = 0L;
+
#define STRUCT2PTR(x, y) (CPerlHost*)(((LPBYTE)x)-offsetof(CPerlHost, y))
return do_aspawn(vreally, vmark, vsp);
}
+int
+PerlProcLastHost(struct IPerlProc* piPerl)
+{
+ dTHXo;
+ CPerlHost *h = (CPerlHost*)w32_internal_host;
+ return h->LastHost();
+}
+
struct IPerlProc perlProc =
{
PerlProcAbort,
PerlProcSpawn,
PerlProcSpawnvp,
PerlProcASpawn,
+ PerlProcLastHost
};
CPerlHost::CPerlHost(void)
{
+ /* Construct a host from scratch */
+ InterlockedIncrement(&num_hosts);
m_pvDir = new VDir();
m_pVMem = new VMem();
m_pVMemShared = new VMem();
struct IPerlDir** ppDir, struct IPerlSock** ppSock,
struct IPerlProc** ppProc)
{
+ InterlockedIncrement(&num_hosts);
m_pvDir = new VDir(0);
m_pVMem = new VMem();
m_pVMemShared = new VMem();
CPerlHost::CPerlHost(CPerlHost& host)
{
+ /* Construct a host from another host */
+ InterlockedIncrement(&num_hosts);
m_pVMem = new VMem();
m_pVMemShared = host.GetMemShared();
m_pVMemParse = host.GetMemParse();
CPerlHost::~CPerlHost(void)
{
// Reset();
+ InterlockedDecrement(&num_hosts);
delete m_pvDir;
m_pVMemParse->Release();
m_pVMemShared->Release();