(svn r23198) -Codechange: introduce a free that takes const pointers so we don't need to cast to void/non-const before being able to free

replace/41b28d7194a279bdc17475d4fbe2ea6ec885a466
rubidium 13 years ago
parent d61b90ac6f
commit 7fd1e1df81

@ -17,7 +17,7 @@
void AIConfig::ChangeAI(const char *name, int version, bool force_exact_match, bool is_random_ai)
{
free((void *)this->name);
free(this->name);
this->name = (name == NULL) ? NULL : strdup(name);
this->info = (name == NULL) ? NULL : AI::FindInfo(this->name, version, force_exact_match);
this->version = (info == NULL) ? -1 : info->GetVersion();
@ -31,7 +31,7 @@ void AIConfig::ChangeAI(const char *name, int version, bool force_exact_match, b
int start_date = this->GetSetting("start_date");
for (SettingValueList::iterator it = this->settings.begin(); it != this->settings.end(); it++) {
free((void*)(*it).first);
free((*it).first);
}
this->settings.clear();
@ -65,7 +65,7 @@ AIConfig::AIConfig(const AIConfig *config)
AIConfig::~AIConfig()
{
free((void *)this->name);
free(this->name);
this->ResetSettings();
if (this->config_list != NULL) delete this->config_list;
}
@ -148,7 +148,7 @@ void AIConfig::SetSetting(const char *name, int value)
void AIConfig::ResetSettings()
{
for (SettingValueList::iterator it = this->settings.begin(); it != this->settings.end(); it++) {
free((void*)(*it).first);
free((*it).first);
}
this->settings.clear();
}

@ -39,7 +39,7 @@ AIConfigItem _start_date_config = {
AILibrary::~AILibrary()
{
free((void *)this->category);
free(this->category);
}
/* static */ SQInteger AIFileInfo::Constructor(HSQUIRRELVM vm, AIFileInfo *info)
@ -146,8 +146,8 @@ AIInfo::~AIInfo()
{
/* Free all allocated strings */
for (AIConfigItemList::iterator it = this->config_list.begin(); it != this->config_list.end(); it++) {
free((void*)(*it).name);
free((void*)(*it).description);
free((*it).name);
free((*it).description);
if (it->labels != NULL) {
for (LabelMapping::iterator it2 = (*it).labels->Begin(); it2 != (*it).labels->End(); it2++) {
free(it2->second);
@ -156,7 +156,7 @@ AIInfo::~AIInfo()
}
}
this->config_list.clear();
free((void*)this->api_version);
free(this->api_version);
}
bool AIInfo::CanLoadFromVersion(int version) const

@ -68,16 +68,16 @@ void AIScanner::Reset()
{
AIInfoList::iterator it = this->info_list.begin();
for (; it != this->info_list.end(); it++) {
free((void *)(*it).first);
free((*it).first);
delete (*it).second;
}
it = this->info_single_list.begin();
for (; it != this->info_single_list.end(); it++) {
free((void *)(*it).first);
free((*it).first);
}
AILibraryList::iterator lit = this->library_list.begin();
for (; lit != this->library_list.end(); lit++) {
free((void *)(*lit).first);
free((*lit).first);
delete (*lit).second;
}

@ -54,8 +54,8 @@ AIController::AIController() :
AIController::~AIController()
{
for (LoadedLibraryList::iterator iter = this->loaded_library.begin(); iter != this->loaded_library.end(); iter++) {
free((void *)(*iter).second);
free((void *)(*iter).first);
free((*iter).second);
free((*iter).first);
}
this->loaded_library.clear();

@ -70,16 +70,16 @@ struct BaseSet {
/** Free everything we allocated */
~BaseSet()
{
free((void*)this->name);
free(this->name);
for (TranslatedStrings::iterator iter = this->description.Begin(); iter != this->description.End(); iter++) {
free((void*)iter->first);
free((void*)iter->second);
free(iter->first);
free(iter->second);
}
for (uint i = 0; i < NUM_FILES; i++) {
free((void*)this->files[i].filename);
free((void*)this->files[i].missing_warning);
free(this->files[i].filename);
free(this->files[i].missing_warning);
}
delete this->next;

@ -354,7 +354,7 @@ Sprite *Blitter_32bppOptimized::Encode(SpriteLoader::Sprite *sprite, AllocatorPr
lengths[z][1] = (byte *)dst_n_ln - (byte *)dst_n_orig[z];
free(src_orig->data);
free((void *)src_orig);
free(src_orig);
}
uint len = 0; // total length of data

@ -78,7 +78,7 @@ public:
if (this->name == NULL) return;
GetBlitters().erase(this->name);
if (GetBlitters().empty()) delete &GetBlitters();
free((void *)this->name);
free(this->name);
}
/**

@ -30,6 +30,15 @@
#include <set>
#include <stack>
/**
* Version of the standard free that accepts const pointers.
* @param ptr The data to free.
*/
static inline void free(const void *ptr)
{
free(const_cast<void *>(ptr));
}
#ifndef PATH_MAX
/** The maximum length of paths, if we don't know it. */
# define PATH_MAX 260
@ -672,7 +681,7 @@ void ScanFile(const char *filename, const char *ext, bool header, bool verbose)
}
}
if (curfile->second->find(h) == curfile->second->end()) curfile->second->insert(strdup(h));
free((void*)h);
free(h);
}
}
/* FALL THROUGH */
@ -705,7 +714,7 @@ void ScanFile(const char *filename, const char *ext, bool header, bool verbose)
}
StringSet::iterator it = defines.find(lexer.GetString());
if (it != defines.end()) {
free((void*)*it);
free(*it);
defines.erase(it);
}
lexer.Lex();
@ -808,7 +817,7 @@ void ScanFile(const char *filename, const char *ext, bool header, bool verbose)
if (!header) {
for (StringSet::iterator it = defines.begin(); it != defines.end(); it++) {
free((void*)*it);
free(*it);
}
defines.clear();
while (!ignore.empty()) ignore.pop();
@ -940,31 +949,31 @@ int main(int argc, char *argv[])
for (StringMap::iterator it = _files.begin(); it != _files.end(); it++) {
for (StringSet::iterator h = it->second->begin(); h != it->second->end(); h++) {
free((void*)*h);
free(*h);
}
it->second->clear();
delete it->second;
free((void*)it->first);
free(it->first);
}
_files.clear();
for (StringMap::iterator it = _headers.begin(); it != _headers.end(); it++) {
for (StringSet::iterator h = it->second->begin(); h != it->second->end(); h++) {
free((void*)*h);
free(*h);
}
it->second->clear();
delete it->second;
free((void*)it->first);
free(it->first);
}
_headers.clear();
for (StringSet::iterator it = _defines.begin(); it != _defines.end(); it++) {
free((void*)*it);
free(*it);
}
_defines.clear();
for (StringSet::iterator it = _include_dirs.begin(); it != _include_dirs.end(); it++) {
free((void*)*it);
free(*it);
}
_include_dirs.clear();

@ -236,8 +236,8 @@ DriverFactoryBase::~DriverFactoryBase()
const char *longname = (*it).first;
GetDrivers().erase(it);
free((void *)longname);
free(longname);
if (GetDrivers().empty()) delete &GetDrivers();
free((void *)this->name);
free(this->name);
}

@ -1175,7 +1175,7 @@ void DeterminePaths(const char *exe)
/* If we don't have networking, we don't need to make the directory. But
* if it exists we keep it, otherwise remove it from the search paths. */
if (!FileExists(_searchpaths[SP_AUTODOWNLOAD_DIR])) {
free((void*)_searchpaths[SP_AUTODOWNLOAD_DIR]);
free(_searchpaths[SP_AUTODOWNLOAD_DIR]);
_searchpaths[SP_AUTODOWNLOAD_DIR] = NULL;
}
#endif /* ENABLE_NETWORK */

@ -73,7 +73,7 @@ NetworkHTTPSocketHandler::~NetworkHTTPSocketHandler()
if (this->sock != INVALID_SOCKET) closesocket(this->sock);
this->sock = INVALID_SOCKET;
free((void*)this->data);
free(this->data);
}
NetworkRecvStatus NetworkHTTPSocketHandler::CloseConnection(bool error)

@ -105,13 +105,13 @@ public:
/** Free all our allocated data. */
~NetworkHTTPContentConnecter()
{
free((void*)this->url);
free(this->url);
}
virtual void OnFailure()
{
this->callback->OnFailure();
free((void*)this->data);
free(this->data);
}
virtual void OnConnect(SOCKET s)

@ -3071,10 +3071,10 @@ static void CleanIndustryTileTable(IndustrySpec *ind)
if (HasBit(ind->cleanup_flag, CLEAN_TILELAYOUT) && ind->table != NULL) {
for (int j = 0; j < ind->num_table; j++) {
/* remove the individual layouts */
free((void*)ind->table[j]);
free(ind->table[j]);
}
/* remove the layouts pointers */
free((void*)ind->table);
free(ind->table);
ind->table = NULL;
}
}
@ -3322,7 +3322,7 @@ static ChangeInfoResult IndustriesChangeInfo(uint indid, int numinfo, int prop,
}
if (HasBit(indsp->cleanup_flag, CLEAN_RANDOMSOUNDS)) {
free((void*)indsp->random_sounds);
free(indsp->random_sounds);
}
indsp->random_sounds = sounds;
SetBit(indsp->cleanup_flag, CLEAN_RANDOMSOUNDS);
@ -7663,10 +7663,10 @@ static void ResetCustomAirports()
/* We need to remove the tiles layouts */
for (int j = 0; j < as->num_table; j++) {
/* remove the individual layouts */
free((void*)as->table[j]);
free(as->table[j]);
}
free((void*)as->table);
free((void*)as->depot_table);
free(as->table);
free(as->depot_table);
free(as);
}
@ -7703,7 +7703,7 @@ static void ResetCustomIndustries()
/* We need to remove the sounds array */
if (HasBit(ind->cleanup_flag, CLEAN_RANDOMSOUNDS)) {
free((void*)ind->random_sounds);
free(ind->random_sounds);
}
/* We need to remove the tiles layouts */

@ -140,8 +140,8 @@ struct NewGRFSpriteLayout : ZeroedMemoryAllocator, DrawTileSprites {
virtual ~NewGRFSpriteLayout()
{
free(const_cast<DrawTileSeqStruct*>(this->seq));
free(const_cast<TileLayoutRegisters*>(this->registers));
free(this->seq);
free(this->registers);
}
/**

@ -20,8 +20,8 @@ INSTANTIATE_POOL_METHODS(SpriteGroup)
RealSpriteGroup::~RealSpriteGroup()
{
free((void*)this->loaded);
free((void*)this->loading);
free(this->loaded);
free(this->loading);
}
DeterministicSpriteGroup::~DeterministicSpriteGroup()
@ -32,7 +32,7 @@ DeterministicSpriteGroup::~DeterministicSpriteGroup()
RandomizedSpriteGroup::~RandomizedSpriteGroup()
{
free((void*)this->groups);
free(this->groups);
}
TemporaryStorageArray<int32, 0x110> _temp_store;

@ -806,9 +806,9 @@ int ttd_main(int argc, char *argv[])
/* Reset windowing system, stop drivers, free used memory, ... */
ShutdownGame();
free(const_cast<char *>(BaseGraphics::ini_set));
free(const_cast<char *>(BaseSounds::ini_set));
free(const_cast<char *>(BaseMusic::ini_set));
free(BaseGraphics::ini_set);
free(BaseSounds::ini_set);
free(BaseMusic::ini_set);
free(_ini_musicdriver);
free(_ini_sounddriver);
free(_ini_videodriver);

@ -48,7 +48,7 @@ struct QueryString {
*/
~QueryString()
{
free((void*)this->orig);
free(this->orig);
}
private:

@ -23,13 +23,13 @@ static const int MAX_CREATEINSTANCE_OPS = 100000;
ScriptFileInfo::~ScriptFileInfo()
{
free((void *)this->author);
free((void *)this->name);
free((void *)this->short_name);
free((void *)this->description);
free((void *)this->date);
free((void *)this->instance_name);
free((void *)this->url);
free(this->author);
free(this->name);
free(this->short_name);
free(this->description);
free(this->date);
free(this->instance_name);
free(this->url);
free(this->main_script);
free(this->tar_file);
free(this->SQ_instance);

@ -448,7 +448,7 @@ struct GameOptionsWindow : Window {
if (_game_mode == GM_MENU) {
const char *name = T::GetSet(index)->name;
free(const_cast<char *>(T::ini_set));
free(T::ini_set);
T::ini_set = strdup(name);
T::SetSet(name);

@ -437,6 +437,15 @@ void NORETURN CDECL error(const char *str, ...) WARN_FORMAT(1, 2);
#define MAX_PATH 260
#endif
/**
* Version of the standard free that accepts const pointers.
* @param ptr The data to free.
*/
static FORCEINLINE void free(const void *ptr)
{
free(const_cast<void *>(ptr));
}
/**
* The largest value that can be entered in a variable
* @param type the type of the variable

@ -24,7 +24,7 @@ struct TarListEntry {
* to free filename, which isn't set at that moment... but because it
* initializes the variable with garbage, it's going to segfault. */
TarListEntry() : filename(NULL), dirname(NULL) {}
~TarListEntry() { free((void*)this->filename); free((void*)this->dirname); }
~TarListEntry() { free(this->filename); free(this->dirname); }
};
struct TarFileListEntry {

Loading…
Cancel
Save