1
0
Fork 0

(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

release/1.2
rubidium 2011-11-12 13:00:29 +00:00
parent d61b90ac6f
commit 7fd1e1df81
21 changed files with 78 additions and 60 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -30,6 +30,15 @@
#include <set> #include <set>
#include <stack> #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 #ifndef PATH_MAX
/** The maximum length of paths, if we don't know it. */ /** The maximum length of paths, if we don't know it. */
# define PATH_MAX 260 # 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)); if (curfile->second->find(h) == curfile->second->end()) curfile->second->insert(strdup(h));
free((void*)h); free(h);
} }
} }
/* FALL THROUGH */ /* 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()); StringSet::iterator it = defines.find(lexer.GetString());
if (it != defines.end()) { if (it != defines.end()) {
free((void*)*it); free(*it);
defines.erase(it); defines.erase(it);
} }
lexer.Lex(); lexer.Lex();
@ -808,7 +817,7 @@ void ScanFile(const char *filename, const char *ext, bool header, bool verbose)
if (!header) { if (!header) {
for (StringSet::iterator it = defines.begin(); it != defines.end(); it++) { for (StringSet::iterator it = defines.begin(); it != defines.end(); it++) {
free((void*)*it); free(*it);
} }
defines.clear(); defines.clear();
while (!ignore.empty()) ignore.pop(); 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 (StringMap::iterator it = _files.begin(); it != _files.end(); it++) {
for (StringSet::iterator h = it->second->begin(); h != it->second->end(); h++) { for (StringSet::iterator h = it->second->begin(); h != it->second->end(); h++) {
free((void*)*h); free(*h);
} }
it->second->clear(); it->second->clear();
delete it->second; delete it->second;
free((void*)it->first); free(it->first);
} }
_files.clear(); _files.clear();
for (StringMap::iterator it = _headers.begin(); it != _headers.end(); it++) { for (StringMap::iterator it = _headers.begin(); it != _headers.end(); it++) {
for (StringSet::iterator h = it->second->begin(); h != it->second->end(); h++) { for (StringSet::iterator h = it->second->begin(); h != it->second->end(); h++) {
free((void*)*h); free(*h);
} }
it->second->clear(); it->second->clear();
delete it->second; delete it->second;
free((void*)it->first); free(it->first);
} }
_headers.clear(); _headers.clear();
for (StringSet::iterator it = _defines.begin(); it != _defines.end(); it++) { for (StringSet::iterator it = _defines.begin(); it != _defines.end(); it++) {
free((void*)*it); free(*it);
} }
_defines.clear(); _defines.clear();
for (StringSet::iterator it = _include_dirs.begin(); it != _include_dirs.end(); it++) { for (StringSet::iterator it = _include_dirs.begin(); it != _include_dirs.end(); it++) {
free((void*)*it); free(*it);
} }
_include_dirs.clear(); _include_dirs.clear();

View File

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

View File

@ -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 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 it exists we keep it, otherwise remove it from the search paths. */
if (!FileExists(_searchpaths[SP_AUTODOWNLOAD_DIR])) { if (!FileExists(_searchpaths[SP_AUTODOWNLOAD_DIR])) {
free((void*)_searchpaths[SP_AUTODOWNLOAD_DIR]); free(_searchpaths[SP_AUTODOWNLOAD_DIR]);
_searchpaths[SP_AUTODOWNLOAD_DIR] = NULL; _searchpaths[SP_AUTODOWNLOAD_DIR] = NULL;
} }
#endif /* ENABLE_NETWORK */ #endif /* ENABLE_NETWORK */

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -437,6 +437,15 @@ void NORETURN CDECL error(const char *str, ...) WARN_FORMAT(1, 2);
#define MAX_PATH 260 #define MAX_PATH 260
#endif #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 * The largest value that can be entered in a variable
* @param type the type of the variable * @param type the type of the variable

View File

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