Mercurial > hg > openttd
changeset 18362:d7607ed4eef6 draft
(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
author | rubidium <rubidium@openttd.org> |
---|---|
date | Sat, 12 Nov 2011 13:00:29 +0000 |
parents | c6ef3088d9a5 |
children | ca42abca1267 |
files | src/ai/ai_config.cpp src/ai/ai_info.cpp src/ai/ai_scanner.cpp src/ai/api/ai_controller.cpp src/base_media_base.h src/blitter/32bpp_optimized.cpp src/blitter/factory.hpp src/depend/depend.cpp src/driver.cpp src/fileio.cpp src/network/core/tcp_http.cpp src/network/core/tcp_http.h src/newgrf.cpp src/newgrf_commons.h src/newgrf_spritegroup.cpp src/openttd.cpp src/querystring_gui.h src/script/script_info.cpp src/settings_gui.cpp src/stdafx.h src/tar_type.h |
diffstat | 21 files changed, 78 insertions(+), 60 deletions(-) [+] |
line wrap: on
line diff
--- a/src/ai/ai_config.cpp +++ b/src/ai/ai_config.cpp @@ -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 @@ 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() { - free((void *)this->name); + free(this->name); this->ResetSettings(); if (this->config_list != NULL) delete this->config_list; } @@ -148,7 +148,7 @@ 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(); }
--- a/src/ai/ai_info.cpp +++ b/src/ai/ai_info.cpp @@ -39,7 +39,7 @@ AILibrary::~AILibrary() { - free((void *)this->category); + free(this->category); } /* static */ SQInteger AIFileInfo::Constructor(HSQUIRRELVM vm, AIFileInfo *info) @@ -146,8 +146,8 @@ { /* 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 @@ } } this->config_list.clear(); - free((void*)this->api_version); + free(this->api_version); } bool AIInfo::CanLoadFromVersion(int version) const
--- a/src/ai/ai_scanner.cpp +++ b/src/ai/ai_scanner.cpp @@ -68,16 +68,16 @@ { 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; }
--- a/src/ai/api/ai_controller.cpp +++ b/src/ai/api/ai_controller.cpp @@ -54,8 +54,8 @@ 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();
--- a/src/base_media_base.h +++ b/src/base_media_base.h @@ -70,16 +70,16 @@ /** 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;
--- a/src/blitter/32bpp_optimized.cpp +++ b/src/blitter/32bpp_optimized.cpp @@ -354,7 +354,7 @@ 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
--- a/src/blitter/factory.hpp +++ b/src/blitter/factory.hpp @@ -78,7 +78,7 @@ if (this->name == NULL) return; GetBlitters().erase(this->name); if (GetBlitters().empty()) delete &GetBlitters(); - free((void *)this->name); + free(this->name); } /**
--- a/src/depend/depend.cpp +++ b/src/depend/depend.cpp @@ -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 @@ } } if (curfile->second->find(h) == curfile->second->end()) curfile->second->insert(strdup(h)); - free((void*)h); + free(h); } } /* FALL THROUGH */ @@ -705,7 +714,7 @@ } 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 @@ 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 @@ 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();
--- a/src/driver.cpp +++ b/src/driver.cpp @@ -236,8 +236,8 @@ 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); }
--- a/src/fileio.cpp +++ b/src/fileio.cpp @@ -1175,7 +1175,7 @@ /* 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 */
--- a/src/network/core/tcp_http.cpp +++ b/src/network/core/tcp_http.cpp @@ -73,7 +73,7 @@ if (this->sock != INVALID_SOCKET) closesocket(this->sock); this->sock = INVALID_SOCKET; - free((void*)this->data); + free(this->data); } NetworkRecvStatus NetworkHTTPSocketHandler::CloseConnection(bool error)
--- a/src/network/core/tcp_http.h +++ b/src/network/core/tcp_http.h @@ -105,13 +105,13 @@ /** 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)
--- a/src/newgrf.cpp +++ b/src/newgrf.cpp @@ -3071,10 +3071,10 @@ 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 @@ } 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 @@ /* 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 @@ /* 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 */
--- a/src/newgrf_commons.h +++ b/src/newgrf_commons.h @@ -140,8 +140,8 @@ virtual ~NewGRFSpriteLayout() { - free(const_cast<DrawTileSeqStruct*>(this->seq)); - free(const_cast<TileLayoutRegisters*>(this->registers)); + free(this->seq); + free(this->registers); } /**
--- a/src/newgrf_spritegroup.cpp +++ b/src/newgrf_spritegroup.cpp @@ -20,8 +20,8 @@ RealSpriteGroup::~RealSpriteGroup() { - free((void*)this->loaded); - free((void*)this->loading); + free(this->loaded); + free(this->loading); } DeterministicSpriteGroup::~DeterministicSpriteGroup() @@ -32,7 +32,7 @@ RandomizedSpriteGroup::~RandomizedSpriteGroup() { - free((void*)this->groups); + free(this->groups); } TemporaryStorageArray<int32, 0x110> _temp_store;
--- a/src/openttd.cpp +++ b/src/openttd.cpp @@ -806,9 +806,9 @@ /* 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);
--- a/src/querystring_gui.h +++ b/src/querystring_gui.h @@ -48,7 +48,7 @@ */ ~QueryString() { - free((void*)this->orig); + free(this->orig); } private:
--- a/src/script/script_info.cpp +++ b/src/script/script_info.cpp @@ -23,13 +23,13 @@ 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);
--- a/src/settings_gui.cpp +++ b/src/settings_gui.cpp @@ -448,7 +448,7 @@ 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);
--- a/src/stdafx.h +++ b/src/stdafx.h @@ -438,6 +438,15 @@ #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 */
--- a/src/tar_type.h +++ b/src/tar_type.h @@ -24,7 +24,7 @@ * 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 {