[easytag] Use the GLib slice allocator where appropriate
- From: David King <davidk src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [easytag] Use the GLib slice allocator where appropriate
- Date: Thu, 18 Dec 2014 18:10:02 +0000 (UTC)
commit 6fcbdd18ef8177df92e09137e4d2d8649bffc92b
Author: David King <amigadave amigadave com>
Date: Thu Dec 18 17:44:38 2014 +0000
Use the GLib slice allocator where appropriate
When allocating structs, use the GLib slice allocator. Simplify some
allocation code to also zero-initialize the struct.
src/cddb_dialog.c | 32 +++++++++++--------
src/et_core.c | 89 +++++++++++++++++++---------------------------------
src/log.c | 4 +-
src/scan_dialog.c | 12 +++---
4 files changed, 60 insertions(+), 77 deletions(-)
---
diff --git a/src/cddb_dialog.c b/src/cddb_dialog.c
index 4c5bd19..1034a2f 100644
--- a/src/cddb_dialog.c
+++ b/src/cddb_dialog.c
@@ -819,6 +819,12 @@ Cddb_Write_Result_To_File (EtCDDBDialog *self,
return 0;
}
+static void
+cddb_track_frame_offset_free (CddbTrackFrameOffset *offset)
+{
+ g_slice_free (CddbTrackFrameOffset, offset);
+}
+
/*
* Look up a specific album in freedb, and save to a CddbAlbum structure
*/
@@ -1070,7 +1076,7 @@ Cddb_Get_Album_Tracks_List (EtCDDBDialog *self, GtkTreeSelection* selection)
{
if ( strtoul(cddb_out+1,NULL,10)>0 )
{
- CddbTrackFrameOffset *cddbtrackframeoffset = g_malloc0(sizeof(CddbTrackFrameOffset));
+ CddbTrackFrameOffset *cddbtrackframeoffset = g_slice_new (CddbTrackFrameOffset);
cddbtrackframeoffset->offset = strtoul(cddb_out+1,NULL,10);
TrackOffsetList = g_list_append(TrackOffsetList,cddbtrackframeoffset);
}else
@@ -1086,7 +1092,7 @@ Cddb_Get_Album_Tracks_List (EtCDDBDialog *self, GtkTreeSelection* selection)
cddbalbum->duration = atoi(strchr(cddb_out,':')+1);
if (TrackOffsetList) // As it must be the last item, do nothing if no previous data
{
- CddbTrackFrameOffset *cddbtrackframeoffset = g_malloc0(sizeof(CddbTrackFrameOffset));
+ CddbTrackFrameOffset *cddbtrackframeoffset = g_slice_new (CddbTrackFrameOffset);
cddbtrackframeoffset->offset = cddbalbum->duration * 75; // It's the last offset
TrackOffsetList = g_list_append(TrackOffsetList,cddbtrackframeoffset);
}
@@ -1150,7 +1156,7 @@ Cddb_Get_Album_Tracks_List (EtCDDBDialog *self, GtkTreeSelection* selection)
{
CddbTrackAlbum *cddbtrackalbum_last = NULL;
- CddbTrackAlbum *cddbtrackalbum = g_malloc0(sizeof(CddbTrackAlbum));
+ CddbTrackAlbum *cddbtrackalbum = g_slice_new0 (CddbTrackAlbum);
cddbtrackalbum->cddbalbum = cddbalbum; // To find the CddbAlbum father quickly
// Here is a fix when TTITLExx doesn't contain an "=", we skip the line
@@ -1179,9 +1185,9 @@ Cddb_Get_Album_Tracks_List (EtCDDBDialog *self, GtkTreeSelection* selection)
cddbtrackalbum_last->track_name = Try_To_Validate_Utf8_String(track_name);
- // Frees useless allocated data previously
+ /* Frees useless allocated data previously. */
g_free(cddbtrackalbum->track_name);
- g_free(cddbtrackalbum);
+ g_slice_free (CddbTrackAlbum, cddbtrackalbum);
}else
{
if (TrackOffsetList && TrackOffsetList->next)
@@ -1241,7 +1247,8 @@ Cddb_Get_Album_Tracks_List (EtCDDBDialog *self, GtkTreeSelection* selection)
show_album_info (self, gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->album_list_view)));
- g_list_free_full (g_list_first (TrackOffsetList), (GDestroyNotify)g_free);
+ g_list_free_full (g_list_first (TrackOffsetList),
+ (GDestroyNotify)cddb_track_frame_offset_free);
return TRUE;
}
@@ -1369,8 +1376,7 @@ Cddb_Free_Album_List (EtCDDBDialog *self)
g_free(cddbalbum->genre);
g_free(cddbalbum->year);
- g_free(cddbalbum);
- cddbalbum = NULL;
+ g_slice_free (CddbAlbum, cddbalbum);
}
}
@@ -1689,7 +1695,7 @@ Cddb_Search_Album_List_From_String_Freedb (EtCDDBDialog *self)
gchar *copy;
CddbAlbum *cddbalbum;
- cddbalbum = g_malloc0(sizeof(CddbAlbum));
+ cddbalbum = g_slice_new0 (CddbAlbum);
// Parameters of the server used
@@ -2039,7 +2045,7 @@ Cddb_Search_Album_List_From_String_Gnudb (EtCDDBDialog *self)
gchar *valid;
CddbAlbum *cddbalbum;
- cddbalbum = g_malloc0(sizeof(CddbAlbum));
+ cddbalbum = g_slice_new0 (CddbAlbum);
// Parameters of the server used
cddbalbum->server_name = g_strdup(cddb_server_name);
@@ -3224,11 +3230,11 @@ Cddb_Free_Track_Album_List (GList *track_list)
for (l = track_list; l != NULL; l = g_list_next (l))
{
CddbTrackAlbum *cddbtrackalbum = l->data;
+
if (cddbtrackalbum)
{
g_free(cddbtrackalbum->track_name);
- g_free(cddbtrackalbum);
- cddbtrackalbum = NULL;
+ g_slice_free (CddbTrackAlbum, cddbtrackalbum);
}
}
@@ -3597,7 +3603,7 @@ et_cddb_dialog_search_from_selection (EtCDDBDialog *self)
gchar* ptr;
CddbAlbum *cddbalbum;
- cddbalbum = g_malloc0(sizeof(CddbAlbum));
+ cddbalbum = g_slice_new0 (CddbAlbum);
// Parameters of the server used
cddbalbum->server_name = g_strdup(cddb_server_name);
diff --git a/src/et_core.c b/src/et_core.c
index 0eb5bbe..e2f60f6 100644
--- a/src/et_core.c
+++ b/src/et_core.c
@@ -140,10 +140,8 @@ static gboolean ET_Free_History_File_List (void);
static gboolean ET_Free_Displayed_File_List (void);
static gboolean ET_Free_Artist_Album_File_List (void);
-static void ET_Initialize_File_Item (ET_File *ETFile);
static void ET_Initialize_File_Tag_Item (File_Tag *FileTag);
static void ET_Initialize_File_Name_Item (File_Name *FileName);
-static void ET_Initialize_File_Info_Item (ET_File_Info *ETFileInfo);
static guint ET_File_Key_New (void);
static guint ET_Undo_Key_New (void);
@@ -257,12 +255,14 @@ gboolean ET_File_Is_Supported (const gchar *filename)
*****************************************************************************/
void ET_Core_Create (void)
{
- // Allocate
+ /* Allocate. */
if (ETCore == NULL)
- ETCore = g_malloc0(sizeof(ET_Core));
+ {
+ ETCore = g_slice_new (ET_Core);
+ }
- // Initialize
- ET_Core_Initialize();
+ /* Initialize. */
+ ET_Core_Initialize ();
}
void ET_Core_Initialize (void)
@@ -303,7 +303,7 @@ ET_Core_Destroy (void)
if (ETCore)
{
ET_Core_Free ();
- g_free (ETCore);
+ g_slice_free (ET_Core, ETCore);
ETCore = NULL;
}
}
@@ -321,8 +321,8 @@ File_Name *ET_File_Name_Item_New (void)
{
File_Name *FileName;
- FileName = g_malloc0(sizeof(File_Name));
- ET_Initialize_File_Name_Item(FileName);
+ FileName = g_slice_new (File_Name);
+ ET_Initialize_File_Name_Item (FileName);
return FileName;
}
@@ -335,8 +335,8 @@ File_Tag *ET_File_Tag_Item_New (void)
{
File_Tag *FileTag;
- FileTag = g_malloc0(sizeof(File_Tag));
- ET_Initialize_File_Tag_Item(FileTag);
+ FileTag = g_slice_new (File_Tag);
+ ET_Initialize_File_Tag_Item (FileTag);
return FileTag;
}
@@ -350,8 +350,7 @@ ET_File_Info_Item_New (void)
{
ET_File_Info *ETFileInfo;
- ETFileInfo = g_malloc0(sizeof(ET_File_Info));
- ET_Initialize_File_Info_Item(ETFileInfo);
+ ETFileInfo = g_slice_new0 (ET_File_Info);
return ETFileInfo;
}
@@ -364,8 +363,7 @@ ET_File *ET_File_Item_New (void)
{
ET_File *ETFile;
- ETFile = g_malloc0(sizeof(ET_File));
- ET_Initialize_File_Item(ETFile);
+ ETFile = g_slice_new0 (ET_File);
return ETFile;
}
@@ -414,37 +412,6 @@ ET_Initialize_File_Tag_Item (File_Tag *FileTag)
}
-static void
-ET_Initialize_File_Info_Item (ET_File_Info *ETFileInfo)
-{
- if (ETFileInfo)
- {
- ETFileInfo->mpc_profile = NULL;
- ETFileInfo->mpc_version = NULL;
- }
-}
-
-
-static void
-ET_Initialize_File_Item (ET_File *ETFile)
-{
- if (ETFile)
- {
- ETFile->IndexKey = 0;
- ETFile->ETFileKey = 0;
- ETFile->ETFileDescription = NULL;
- ETFile->ETFileInfo = NULL;
- ETFile->FileNameCur = NULL;
- ETFile->FileNameNew = NULL;
- ETFile->FileNameList = NULL;
- ETFile->FileNameListBak = NULL;
- ETFile->FileTag = NULL;
- ETFile->FileTagList = NULL;
- ETFile->FileTagListBak = NULL;
- }
-}
-
-
/* Key for each item of ETFileList */
static guint
ET_File_Key_New (void)
@@ -2360,8 +2327,9 @@ ET_Free_File_List_Item (ET_File *ETFile)
{
ET_Free_File_Info_Item (ETFile->ETFileInfo);
}
+
g_free(ETFile->ETFileExtension);
- g_free(ETFile);
+ g_slice_free (ET_File, ETFile);
}
}
@@ -2392,8 +2360,7 @@ ET_Free_File_Name_Item (File_Name *FileName)
g_free(FileName->value);
g_free(FileName->value_utf8);
g_free(FileName->value_ck);
- g_free(FileName);
-
+ g_slice_free (File_Name, FileName);
}
@@ -2460,7 +2427,7 @@ gboolean ET_Free_File_Tag_Item (File_Tag *FileTag)
// Free list of other fields
ET_Free_File_Tag_Item_Other_Field(FileTag);
- g_free(FileTag);
+ g_slice_free (File_Tag, FileTag);
return TRUE;
}
@@ -2476,11 +2443,17 @@ ET_Free_File_Info_Item (ET_File_Info *ETFileInfo)
g_free(ETFileInfo->mpc_profile);
g_free(ETFileInfo->mpc_version);
- g_free(ETFileInfo);
+ g_slice_free (ET_File_Info, ETFileInfo);
return TRUE;
}
+static void
+et_history_file_free (ET_History_File *file)
+{
+ g_slice_free (ET_History_File, file);
+}
+
/*
* History list contains only pointers, so no data to free except the history structure.
*/
@@ -2492,7 +2465,8 @@ ET_Free_History_File_List (void)
ETCore->ETHistoryFileList = g_list_first (ETCore->ETHistoryFileList);
- g_list_free_full (ETCore->ETHistoryFileList, g_free);
+ g_list_free_full (ETCore->ETHistoryFileList,
+ (GDestroyNotify)et_history_file_free);
ETCore->ETHistoryFileList = NULL;
@@ -3094,8 +3068,8 @@ void ET_Save_File_Data_From_UI (ET_File *ETFile)
/*
* Save filename and generate undo for filename
*/
- FileName = g_malloc0(sizeof(File_Name));
- ET_Initialize_File_Name_Item(FileName);
+ FileName = g_slice_new (File_Name);
+ ET_Initialize_File_Name_Item (FileName);
FileName->key = undo_key;
ET_Save_File_Name_From_UI(ETFile,FileName); // Used for all files!
@@ -4013,12 +3987,15 @@ ET_Add_File_To_History_List (ET_File *ETFile)
g_return_val_if_fail (ETFile != NULL, FALSE);
- ETHistoryFile = g_malloc0(sizeof(ET_History_File));
+ ETHistoryFile = g_slice_new0 (ET_History_File);
ETHistoryFile->ETFile = ETFile;
/* The undo list must contains one item before the 'first undo' data */
if (!ETCore->ETHistoryFileList)
- ETCore->ETHistoryFileList =
g_list_append(ETCore->ETHistoryFileList,g_malloc0(sizeof(ET_History_File)));
+ {
+ ETCore->ETHistoryFileList = g_list_append (ETCore->ETHistoryFileList,
+ g_slice_new0 (ET_History_File));
+ }
/* Add the item to the list (cut end of list from the current element) */
ETCore->ETHistoryFileList = g_list_append(ETCore->ETHistoryFileList,ETHistoryFile);
diff --git a/src/log.c b/src/log.c
index 67a3fa8..6eff183 100644
--- a/src/log.c
+++ b/src/log.c
@@ -344,7 +344,7 @@ Log_Print (EtLogAreaKind error_type, const gchar * const format, ...)
}
else
{
- EtLogAreaData *log_data = g_malloc0 (sizeof (EtLogAreaData));
+ EtLogAreaData *log_data = g_slice_new (EtLogAreaData);
log_data->time = Log_Format_Date ();
log_data->error_type = error_type;
log_data->string = g_strdup (string);
@@ -481,7 +481,7 @@ Log_Print_Tmp_List (EtLogArea *self)
g_free (log_data->string);
g_free (log_data->time);
- g_free (log_data);
+ g_slice_free (EtLogAreaData, log_data);
}
g_list_free (priv->log_tmp_list);
diff --git a/src/scan_dialog.c b/src/scan_dialog.c
index ecdb726..15e7d50 100644
--- a/src/scan_dialog.c
+++ b/src/scan_dialog.c
@@ -437,7 +437,7 @@ Scan_Generate_New_Tag_From_Mask (ET_File *ETFile, gchar *mask)
/*
* Allocate a new iten for the fill_tag_list
*/
- mask_item = g_malloc0(sizeof(Scan_Mask_Item));
+ mask_item = g_slice_new0 (Scan_Mask_Item);
// Get the code (used to determine the corresponding target entry)
mask_item->code = tmp[1];
@@ -658,7 +658,7 @@ Scan_Free_File_Fill_Tag_List (GList *list)
if (l->data)
{
g_free (((Scan_Mask_Item *)l->data)->string);
- g_free ((Scan_Mask_Item *)l->data);
+ g_slice_free (Scan_Mask_Item, l->data);
}
}
@@ -805,7 +805,7 @@ et_scan_generate_new_filename_from_mask (const ET_File *ETFile,
// Mask contains some characters after the code ('%b__')
if (strlen(tmp)>2)
{
- mask_item = g_malloc0(sizeof(File_Mask_Item));
+ mask_item = g_slice_new0 (File_Mask_Item);
if (counter)
{
if (strchr(tmp+2,G_DIR_SEPARATOR))
@@ -822,7 +822,7 @@ et_scan_generate_new_filename_from_mask (const ET_File *ETFile,
// Now, parses the code to get the corresponding string (from tag)
source = Scan_Return_File_Tag_Field_From_Mask_Code((File_Tag *)ETFile->FileTag->data,tmp[1]);
- mask_item = g_malloc0(sizeof(File_Mask_Item));
+ mask_item = g_slice_new0 (File_Mask_Item);
if (source && *source && strlen(*source)>0)
{
mask_item->type = FIELD;
@@ -867,7 +867,7 @@ et_scan_generate_new_filename_from_mask (const ET_File *ETFile,
// It may have some characters before the last remaining code ('__%a')
if (mask!=NULL && strlen(mask)>0)
{
- mask_item = g_malloc0(sizeof(File_Mask_Item));
+ mask_item = g_slice_new0 (File_Mask_Item);
mask_item->type = LEADING_SEPARATOR;
mask_item->string = g_strdup(mask);
rename_file_list = g_list_prepend(rename_file_list,mask_item);
@@ -989,7 +989,7 @@ Scan_Free_File_Rename_List (GList *list)
if (l->data)
{
g_free (((File_Mask_Item *)l->data)->string);
- g_free ((File_Mask_Item *)l->data);
+ g_slice_free (File_Mask_Item, l->data);
}
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]