Added reference counting for dos_files
Imported-from: https://svn.code.sf.net/p/dosbox/code-0/dosbox/trunk@1181
This commit is contained in:
parent
6fed40f0a8
commit
db97e36e58
3 changed files with 19 additions and 6 deletions
|
@ -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 */
|
||||
|
|
|
@ -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 <string.h>
|
||||
#include <stdlib.h>
|
||||
|
@ -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;
|
||||
};
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue