From db97e36e5858887cc069165106004565732f4585 Mon Sep 17 00:00:00 2001 From: Ulf Wohlers Date: Mon, 11 Aug 2003 11:50:30 +0000 Subject: [PATCH] Added reference counting for dos_files Imported-from: https://svn.code.sf.net/p/dosbox/code-0/dosbox/trunk@1181 --- include/dos_system.h | 5 ++++- src/dos/dos_files.cpp | 12 +++++++++--- src/dos/drive_local.cpp | 8 ++++++-- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/include/dos_system.h b/include/dos_system.h index 41624704..68380b59 100644 --- a/include/dos_system.h +++ b/include/dos_system.h @@ -51,7 +51,7 @@ class DOS_DTA; class DOS_File { public: - DOS_File():flags(0) { name=0; }; + DOS_File():flags(0) { name=0; refCtr = 0; }; virtual ~DOS_File(){}; virtual bool Read(Bit8u * data,Bit16u * size)=0; virtual bool Write(Bit8u * data,Bit16u * size)=0; @@ -62,12 +62,15 @@ public: virtual char* GetName(void) { return name; }; virtual bool IsOpen() { return open; }; virtual bool IsName(const char* _name) { if (!name) return false; return strcmp(name,_name)==0; }; + virtual void AddRef() { refCtr++; }; + virtual Bits RemoveRef() { return --refCtr; }; Bit8u type; Bit32u flags; Bit16u time; Bit16u date; Bit16u attr; Bit32u size; + Bits refCtr; bool open; char* name; /* Some Device Specific Stuff */ diff --git a/src/dos/dos_files.cpp b/src/dos/dos_files.cpp index 8c725df9..086a1f88 100644 --- a/src/dos/dos_files.cpp +++ b/src/dos/dos_files.cpp @@ -16,7 +16,7 @@ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ -/* $Id: dos_files.cpp,v 1.42 2003-08-01 16:48:55 qbix79 Exp $ */ +/* $Id: dos_files.cpp,v 1.43 2003-08-11 11:49:58 finsterr Exp $ */ #include #include @@ -315,8 +315,10 @@ bool DOS_CloseFile(Bit16u entry) { { //if close succesfull => delete file/update psp DOS_PSP psp(dos.psp); psp.SetFileHandle(entry,0xff); - delete Files[handle]; - Files[handle]=0; + if (Files[handle]->RemoveRef()<=0) { + delete Files[handle]; + Files[handle]=0; + } } return true; } @@ -345,6 +347,7 @@ bool DOS_CreateFile(char * name,Bit16u attributes,Bit16u * entry) { } bool foundit=Drives[drive]->FileCreate(&Files[handle],fullname,attributes); if (foundit) { + Files[handle]->AddRef(); psp.SetFileHandle(*entry,handle); return true; } else { @@ -387,6 +390,7 @@ bool DOS_OpenFile(char * name,Bit8u flags,Bit16u * entry) { bool exists=false; if (!device) exists=Drives[drive]->FileOpen(&Files[handle],fullname,flags); if (exists || device ) { + Files[handle]->AddRef(); psp.SetFileHandle(*entry,handle); return true; } else { @@ -476,6 +480,7 @@ bool DOS_DuplicateEntry(Bit16u entry,Bit16u * newentry) { DOS_SetError(DOSERR_TOO_MANY_OPEN_FILES); return false; } + Files[handle]->AddRef(); psp.SetFileHandle(*newentry,handle); return true; }; @@ -500,6 +505,7 @@ bool DOS_ForceDuplicateEntry(Bit16u entry,Bit16u newentry) { return false; }; DOS_PSP psp(dos.psp); + Files[orig]->AddRef(); psp.SetFileHandle(newentry,(Bit8u)entry); return true; }; diff --git a/src/dos/drive_local.cpp b/src/dos/drive_local.cpp index 991a9dbc..8c0c119e 100644 --- a/src/dos/drive_local.cpp +++ b/src/dos/drive_local.cpp @@ -372,8 +372,12 @@ bool localFile::Seek(Bit32u * pos,Bit32u type) { bool localFile::Close() { - open=false; - fclose(fhandle); + // only close if one reference left + if (refCtr==1) { + fclose(fhandle); + fhandle = 0; + open = false; + }; return true; }