Skip to content
This repository has been archived by the owner on Dec 16, 2019. It is now read-only.

Commit

Permalink
Added (commented out) pool allocator try_to_free() which releases mem…
Browse files Browse the repository at this point in the history
…ory back to the system if possible

  Rarely possible except in artificial tests / closing a mod on save. Unused for now.
Simplified remaining allocator code
Slight optimization to UpdateReferences when used on a single record
Records that are unable to be undeleted (deleted new records, deleted injected records) are fully deleted on load.
Fixed copied records not retaining internal CBash flags
Record cleaning no longer leaves every record loaded into memory
Added IsSkipAllRecords as a mod flag.
  Used with IsTrackNewTypes to more quickly test mergeability
Pool allocators now create records in chunks of 20 rather than 5
cint overhauled
  More condense/efficient code
  Couple minor fixes
  All classes now make use of __slots__ to reduce python memory
  All FormID/MGEFCode/ActorValue fields now return empty classes rather than None
ObTest updated to match cint
  Many errors (use of non-existent vars) found/fixed due to __slots__ use
  • Loading branch information
waruddar committed Sep 26, 2011
1 parent b2f6b7d commit b8724b5
Show file tree
Hide file tree
Showing 194 changed files with 4,093 additions and 6,307 deletions.
Binary file modified CBash.suo
Binary file not shown.
115 changes: 76 additions & 39 deletions CBash/Allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,67 @@ class RecordPoolAllocator
purge_with_destructors();
}

//UINT32 try_to_free()
// {
// //attempts to free any unused buffers. Will mostly fail to free any memory unless a large number of records have been destroyed.
// //this is a relatively slow function. Don't call it unless necessary.
// UINT32 freed_bytes = 0;
// if(freed_position)
// {
// boost::unordered_set<unsigned char *> free_set;
// MakeFreeSet(free_set);
// for(SINT32 p = (SINT32)buffers.size() - 1;p >= 0; --p)
// {
// unsigned char *buffer = buffers[p];
// UINT32 buffer_size = (UINT32)_msize(buffer);
// //in case malloc returned more than the requested amount
// buffer_size -= buffer_size % sizeof(T);

// unsigned char *end_of_buffer = buffer + buffer_size;
// bool buffer_in_use = false;

// for(unsigned char *last_position = buffer;last_position < end_of_buffer; last_position += sizeof(T))
// {
// if(free_set.find(last_position) == free_set.end())
// {
// buffer_in_use = true;
// break;
// }
// }

// if(!buffer_in_use)
// {
// UINT32 max_count = buffer_size / sizeof(T);
// UINT32 removed_count = 0;

// //Remove any freed entries at the head of the list
// while(removed_count < max_count && (freed_position >= buffer && freed_position < end_of_buffer))
// {
// removed_count++;
// freed_position = *(unsigned char **)freed_position;
// };


// for(unsigned char *last_position = freed_position, *next_position = NULL; removed_count < max_count && (last_position != NULL);last_position = next_position)
// {
// next_position = *(unsigned char **)last_position;
// while(next_position >= buffer && next_position < end_of_buffer)
// {
// removed_count++;
// next_position = *(unsigned char **)next_position;
// *(unsigned char **)last_position = next_position;
// };
// }

// freed_bytes += (UINT32)_msize(buffer);
// free(buffer);
// buffers.erase(buffers.begin() + p);
// }
// }
// }
// return freed_bytes;
// }

void purge_no_destructors()
{
for(UINT32 p = 0;p < buffers.size(); p++)
Expand Down Expand Up @@ -80,64 +141,40 @@ class RecordPoolAllocator
void reserve(UINT32 elements)
{
//Allocate memory
UINT32 buffer_size = sizeof(T) * elements;
unsigned char *buffer = (unsigned char *)malloc(buffer_size);
unsigned char *buffer = (unsigned char *)malloc(sizeof(T) * elements);
if(buffer == 0)
throw std::bad_alloc();
buffers.push_back(buffer);
//memset(buffer, 0x00, buffer_size);

//Populate the free linked list in reverse so that the first freed_position is at the beginning of the buffer
//unsigned char *end_of_buffer = buffer + buffer_size;
for(unsigned char *last_position = buffer + buffer_size - sizeof(T);last_position >= buffer; last_position -= sizeof(T))
for(unsigned char *last_position = buffer + (sizeof(T) * elements) - sizeof(T);last_position >= buffer; last_position -= sizeof(T))
{
*(unsigned char **)last_position = freed_position;
freed_position = last_position;
}

//Save the buffer so it can be deallocated later
buffers.push_back(buffer);
}

Record *construct(unsigned char *recData, void *Parent, bool IsMod)
{
//See if any memory is free
if(freed_position)
{
unsigned char *next_position = *(unsigned char **)freed_position;
Record * curRecord = new (freed_position) T(recData);
curRecord->SetParent(Parent, IsMod);
freed_position = next_position;
return curRecord;
}
//Otherwise, allocate more memory
else
{
if(freed_position == NULL)
reserve(AllocUnit);
return construct(recData, Parent, IsMod);
}
throw std::bad_alloc();
return NULL;
unsigned char *next_position = *(unsigned char **)freed_position;
Record * curRecord = new (freed_position) T(recData);
curRecord->SetParent(Parent, IsMod);
freed_position = next_position;
return curRecord;
}

Record *construct(Record *SourceRecord, void *Parent, bool IsMod)
{
//See if any memory is free
if(freed_position)
{
unsigned char *next_position = *(unsigned char **)freed_position;
Record * curRecord = new (freed_position) T((T *)SourceRecord);
curRecord->SetParent(Parent, IsMod);
freed_position = next_position;
return curRecord;
}
//Otherwise, allocate more memory
else
{
if(freed_position == NULL)
reserve(AllocUnit);
return construct(SourceRecord, Parent, IsMod);
}
throw std::bad_alloc();
return NULL;
unsigned char *next_position = *(unsigned char **)freed_position;
Record * curRecord = new (freed_position) T((T *)SourceRecord);
curRecord->SetParent(Parent, IsMod);
freed_position = next_position;
return curRecord;
}

void destroy(Record *curRecord)
Expand Down
7 changes: 5 additions & 2 deletions CBash/CBash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1591,8 +1591,11 @@ CPPDLLEXTERN SINT32 UpdateReferences(ModFile *ModID, Record *RecordID, FORMIDARR
swapper.Accept(RecordID);
Changes[ListIndex] = swapper.GetCount();
count += swapper.GetCount();
if(Changes[ListIndex] > 0)
RecordID->GetParentMod()->Parent->changed_records.insert(RecordID);
}
if(count)
{
RecordID->IsChanged(true);
RecordID->GetParentMod()->Parent->changed_records.insert(RecordID);
}
}
else
Expand Down
13 changes: 12 additions & 1 deletion CBash/Collection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -454,7 +454,9 @@ void Collection::UndeleteRecords(std::vector<std::pair<ModFile *, std::vector<Re
else
{
//Deleted injected record?
printer("Load: Warning - Unable to undelete record %08X in \"%s\". Was not able to determine its source record.\n", curRecord->formID, curModFile->FileName);
RecordDeindexer deindexer(curRecord);
curRecord->GetParentMod()->DeleteRecord(curRecord, deindexer);
//printer("Load: Warning - Unable to undelete record %08X in \"%s\". Was not able to determine its source record.\n", curRecord->formID, curModFile->FileName);
}
}
}
Expand Down Expand Up @@ -897,6 +899,11 @@ Record * Collection::CopyRecord(Record *&curRecord, ModFile *&DestModFile, const
if(options.ExistingReturned)
return RecordCopy;

//Copy over the internal flags
RecordCopy->CBash_Flags = curRecord->CBash_Flags;
if(!curRecord->IsChanged())
RecordCopy->IsLoaded(false);

//Give the record a new formID if it isn't an override record
if(!options.SetAsOverride)
RecordCopy->formID = DestRecordFormID ? DestRecordFormID : NextFreeExpandedFormID(DestModFile);
Expand Down Expand Up @@ -1395,6 +1402,10 @@ bool RecordMasterCollector::Accept(Record *&curRecord)
collector.Accept(curRecord->formID);
curRecord->VisitFormIDs(collector);

//If the record was read, but not changed, unload it again
if(reader.GetResult() && !curRecord->IsChanged())
curRecord->Unload();

return stop;
}

Expand Down
3 changes: 3 additions & 0 deletions CBash/Common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -764,6 +764,7 @@ ModFlags::ModFlags(UINT32 _Flags):
IsFullLoad((_Flags & fIsFullLoad) != 0),
IsNoLoad(!(IsMinLoad || IsFullLoad)),
IsSkipNewRecords((_Flags & fIsSkipNewRecords) != 0),
IsSkipAllRecords((_Flags & fIsSkipAllRecords) != 0),
IsInLoadOrder((_Flags & fIsInLoadOrder) != 0),
IsSaveable(((_Flags & fIsInLoadOrder) != 0) ? ((_Flags & fIsSaveable) != 0) : false),
IsAddMasters(((_Flags & fIsIgnoreAbsentMasters) != 0) ? false : ((_Flags & fIsAddMasters) != 0)),
Expand Down Expand Up @@ -801,6 +802,8 @@ UINT32 ModFlags::GetFlags()
}
if(IsSkipNewRecords)
flags |= fIsSkipNewRecords;
if(IsSkipAllRecords)
flags |= fIsSkipAllRecords;
if(IsInLoadOrder)
flags |= fIsInLoadOrder;
if(IsSaveable)
Expand Down
2 changes: 2 additions & 0 deletions CBash/Common.h
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,7 @@ class ModFlags
fIsFixupPlaceables = 0x00000400,
fIsIgnoreExisting = 0x00000800,
fIsIgnoreAbsentMasters = 0x00001000,
fIsSkipAllRecords = 0x00002000,
};

public:
Expand All @@ -550,6 +551,7 @@ class ModFlags
bool IsFullLoad;
bool IsNoLoad;
bool IsSkipNewRecords;
bool IsSkipAllRecords;
bool IsInLoadOrder;
bool IsSaveable;
bool IsAddMasters;
Expand Down
3 changes: 0 additions & 3 deletions CBash/FalloutNewVegas/Records/ACHRRecord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,7 @@ ACHRRecord::ACHRRecord(ACHRRecord *srcRecord):

recData = srcRecord->recData;
if(!srcRecord->IsChanged())
{
IsLoaded(false);
return;
}

EDID = srcRecord->EDID;
NAME = srcRecord->NAME;
Expand Down
3 changes: 0 additions & 3 deletions CBash/FalloutNewVegas/Records/ACRERecord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,7 @@ ACRERecord::ACRERecord(ACRERecord *srcRecord):

recData = srcRecord->recData;
if(!srcRecord->IsChanged())
{
IsLoaded(false);
return;
}

EDID = srcRecord->EDID;
NAME = srcRecord->NAME;
Expand Down
3 changes: 0 additions & 3 deletions CBash/FalloutNewVegas/Records/ACTIRecord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,7 @@ ACTIRecord::ACTIRecord(ACTIRecord *srcRecord):

recData = srcRecord->recData;
if(!srcRecord->IsChanged())
{
IsLoaded(false);
return;
}

EDID = srcRecord->EDID;
OBND = srcRecord->OBND;
Expand Down
3 changes: 0 additions & 3 deletions CBash/FalloutNewVegas/Records/ADDNRecord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,7 @@ ADDNRecord::ADDNRecord(ADDNRecord *srcRecord):

recData = srcRecord->recData;
if(!srcRecord->IsChanged())
{
IsLoaded(false);
return;
}

EDID = srcRecord->EDID;
OBND = srcRecord->OBND;
Expand Down
3 changes: 0 additions & 3 deletions CBash/FalloutNewVegas/Records/ALCHRecord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,7 @@ ALCHRecord::ALCHRecord(ALCHRecord *srcRecord):

recData = srcRecord->recData;
if(!srcRecord->IsChanged())
{
IsLoaded(false);
return;
}

EDID = srcRecord->EDID;
OBND = srcRecord->OBND;
Expand Down
3 changes: 0 additions & 3 deletions CBash/FalloutNewVegas/Records/ALOCRecord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,7 @@ ALOCRecord::ALOCRecord(ALOCRecord *srcRecord):

recData = srcRecord->recData;
if(!srcRecord->IsChanged())
{
IsLoaded(false);
return;
}

EDID = srcRecord->EDID;
FULL = srcRecord->FULL;
Expand Down
3 changes: 0 additions & 3 deletions CBash/FalloutNewVegas/Records/AMEFRecord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,7 @@ AMEFRecord::AMEFRecord(AMEFRecord *srcRecord):

recData = srcRecord->recData;
if(!srcRecord->IsChanged())
{
IsLoaded(false);
return;
}

EDID = srcRecord->EDID;
FULL = srcRecord->FULL;
Expand Down
3 changes: 0 additions & 3 deletions CBash/FalloutNewVegas/Records/AMMORecord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,7 @@ AMMORecord::AMMORecord(AMMORecord *srcRecord):

recData = srcRecord->recData;
if(!srcRecord->IsChanged())
{
IsLoaded(false);
return;
}

EDID = srcRecord->EDID;
OBND = srcRecord->OBND;
Expand Down
3 changes: 0 additions & 3 deletions CBash/FalloutNewVegas/Records/ANIORecord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,7 @@ ANIORecord::ANIORecord(ANIORecord *srcRecord):

recData = srcRecord->recData;
if(!srcRecord->IsChanged())
{
IsLoaded(false);
return;
}

EDID = srcRecord->EDID;
MODL = srcRecord->MODL;
Expand Down
3 changes: 0 additions & 3 deletions CBash/FalloutNewVegas/Records/ARMARecord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,7 @@ ARMARecord::ARMARecord(ARMARecord *srcRecord):

recData = srcRecord->recData;
if(!srcRecord->IsChanged())
{
IsLoaded(false);
return;
}

EDID = srcRecord->EDID;
OBND = srcRecord->OBND;
Expand Down
3 changes: 0 additions & 3 deletions CBash/FalloutNewVegas/Records/ARMORecord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,7 @@ ARMORecord::ARMORecord(ARMORecord *srcRecord):

recData = srcRecord->recData;
if(!srcRecord->IsChanged())
{
IsLoaded(false);
return;
}

EDID = srcRecord->EDID;
OBND = srcRecord->OBND;
Expand Down
3 changes: 0 additions & 3 deletions CBash/FalloutNewVegas/Records/ASPCRecord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,7 @@ ASPCRecord::ASPCRecord(ASPCRecord *srcRecord):

recData = srcRecord->recData;
if(!srcRecord->IsChanged())
{
IsLoaded(false);
return;
}

EDID = srcRecord->EDID;
OBND = srcRecord->OBND;
Expand Down
3 changes: 0 additions & 3 deletions CBash/FalloutNewVegas/Records/AVIFRecord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,7 @@ AVIFRecord::AVIFRecord(AVIFRecord *srcRecord):

recData = srcRecord->recData;
if(!srcRecord->IsChanged())
{
IsLoaded(false);
return;
}

EDID = srcRecord->EDID;
FULL = srcRecord->FULL;
Expand Down
3 changes: 0 additions & 3 deletions CBash/FalloutNewVegas/Records/BOOKRecord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,7 @@ BOOKRecord::BOOKRecord(BOOKRecord *srcRecord):

recData = srcRecord->recData;
if(!srcRecord->IsChanged())
{
IsLoaded(false);
return;
}

EDID = srcRecord->EDID;
OBND = srcRecord->OBND;
Expand Down
3 changes: 0 additions & 3 deletions CBash/FalloutNewVegas/Records/BPTDRecord.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -387,10 +387,7 @@ BPTDRecord::BPTDRecord(BPTDRecord *srcRecord):

recData = srcRecord->recData;
if(!srcRecord->IsChanged())
{
IsLoaded(false);
return;
}

EDID = srcRecord->EDID;
MODL = srcRecord->MODL;
Expand Down
Loading

0 comments on commit b8724b5

Please sign in to comment.