Skip to content

Commit

Permalink
Change MRULIST from dynamic allocation to static allocation.
Browse files Browse the repository at this point in the history
  • Loading branch information
zufuliu committed Dec 9, 2023
1 parent 2bdc014 commit 4474739
Show file tree
Hide file tree
Showing 9 changed files with 79 additions and 103 deletions.
10 changes: 6 additions & 4 deletions metapath/src/Dialogs.c
Original file line number Diff line number Diff line change
Expand Up @@ -1811,8 +1811,10 @@ bool CopyMoveDlg(HWND hwnd, UINT *wFunc) {
}

FILEOPDLGDATA fod;
fod.pmru = MRU_Create(MRU_KEY_COPY_MOVE_HISTORY, MRUFlags_FilePath);
MRULIST mru;
fod.pmru = &mru;
fod.wFunc = *wFunc;
MRU_Init(&mru, MRU_KEY_COPY_MOVE_HISTORY, MRUFlags_FilePath);
lstrcpy(fod.szSource, PathFindFileName(dli.szFileName));

const INT_PTR result = ThemedDialogBoxParam(g_hInstance, MAKEINTRESOURCE(IDD_COPYMOVE), hwnd, CopyMoveDlgProc, (LPARAM)&fod);
Expand All @@ -1832,8 +1834,8 @@ bool CopyMoveDlg(HWND hwnd, UINT *wFunc) {
}

// Save item
MRU_Add(fod.pmru, fod.szDestination);
MRU_Save(fod.pmru);
MRU_Add(&mru, fod.szDestination);
MRU_Save(&mru);
ExpandEnvironmentStringsEx(fod.szDestination, COUNTOF(fod.szDestination));

// Double null terminated strings are essential!!!
Expand Down Expand Up @@ -1869,7 +1871,7 @@ bool CopyMoveDlg(HWND hwnd, UINT *wFunc) {
*wFunc = fod.wFunc; // save state for next call
}

MRU_Destroy(fod.pmru);
MRU_Empty(&mru, false);
return result == IDOK;
}

Expand Down
19 changes: 4 additions & 15 deletions metapath/src/Helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -1757,7 +1757,7 @@ void History_Init(PHISTORY ph) {
ph->iCurItem = -1;
}

void History_Uninit(PHISTORY ph) {
void History_Empty(PHISTORY ph) {
for (int i = 0; i < HISTORY_ITEMS; i++) {
if (ph->psz[i]) {
LocalFree(ph->psz[i]);
Expand Down Expand Up @@ -1858,21 +1858,12 @@ void History_UpdateToolbar(LCPHISTORY ph, HWND hwnd, int cmdBack, int cmdForward
//
// MRU functions
//
LPMRULIST MRU_Create(LPCWSTR pszRegKey, int iFlags) {
LPMRULIST pmru = (LPMRULIST)NP2HeapAlloc(sizeof(MRULIST));
void MRU_Init(LPMRULIST pmru, LPCWSTR pszRegKey, int iFlags) {
pmru->iSize = 0;
pmru->iFlags = iFlags;
pmru->szRegKey = pszRegKey;
memset(pmru->pszItems, 0, sizeof(pmru->pszItems));
MRU_Load(pmru);
return pmru;
}

void MRU_Destroy(LPMRULIST pmru) {
for (int i = 0; i < pmru->iSize; i++) {
LocalFree(pmru->pszItems[i]);
}

memset(pmru, 0, sizeof(MRULIST));
NP2HeapFree(pmru);
}

static inline bool MRU_Equal(int flags, LPCWSTR psz1, LPCWSTR psz2) {
Expand Down Expand Up @@ -1946,9 +1937,7 @@ void MRU_Load(LPMRULIST pmru) {
const int cchIniSection = (int)(NP2HeapSize(pIniSectionBuf) / sizeof(WCHAR));
IniSection * const pIniSection = &section;

//MRU_Empty(pmru, false);
IniSectionInit(pIniSection, MRU_MAXITEMS);

LoadIniSection(pmru->szRegKey, pIniSectionBuf, cchIniSection);
IniSectionParseArray(pIniSection, pIniSectionBuf);
const UINT count = pIniSection->count;
Expand Down
5 changes: 2 additions & 3 deletions metapath/src/Helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -580,7 +580,7 @@ typedef struct HISTORY {
typedef const HISTORY *LCPHISTORY;

void History_Init(PHISTORY ph);
void History_Uninit(PHISTORY ph);
void History_Empty(PHISTORY ph);
bool History_Add(PHISTORY ph, LPCWSTR pszNew);
bool History_Forward(PHISTORY ph, LPWSTR pszItem, int cItem);
bool History_Back(PHISTORY ph, LPWSTR pszItem, int cItem);
Expand Down Expand Up @@ -608,8 +608,7 @@ typedef struct MRULIST {

typedef const MRULIST *LPCMRULIST;

LPMRULIST MRU_Create(LPCWSTR pszRegKey, int iFlags);
void MRU_Destroy(LPMRULIST pmru);
void MRU_Init(LPMRULIST pmru, LPCWSTR pszRegKey, int iFlags);
void MRU_Add(LPMRULIST pmru, LPCWSTR pszNew);
void MRU_Delete(LPMRULIST pmru, int iIndex);
void MRU_Empty(LPMRULIST pmru, bool save);
Expand Down
2 changes: 1 addition & 1 deletion metapath/src/metapath.c
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ LRESULT CALLBACK MainWndProc(HWND hwnd, UINT umsg, WPARAM wParam, LPARAM lParam)
DirList_Destroy(hwndDirList);
DragAcceptFiles(hwnd, FALSE);

History_Uninit(&mHistory);
History_Empty(&mHistory);

SaveSettings(false);

Expand Down
16 changes: 8 additions & 8 deletions src/Dialogs.c
Original file line number Diff line number Diff line change
Expand Up @@ -980,7 +980,7 @@ bool AddToFavDlg(HWND hwnd, LPCWSTR lpszName, LPCWSTR lpszTarget) {
// FileMRUDlgProc()
//
//
extern LPMRULIST pFileMRU;
extern MRULIST mruFile;
extern bool bSaveRecentFiles;
extern int cxFileMRUDlg;
extern int cyFileMRUDlg;
Expand Down Expand Up @@ -1208,11 +1208,11 @@ static INT_PTR CALLBACK FileMRUDlgProc(HWND hwnd, UINT umsg, WPARAM wParam, LPAR
}
} else if (pnmhdr->idFrom == IDC_EMPTY_MRU) {
if ((pnmhdr->code == NM_CLICK || pnmhdr->code == NM_RETURN)) {
MRU_Empty(pFileMRU, false);
MRU_Empty(&mruFile, false);
if (StrNotEmpty(szCurFile)) {
MRU_Add(pFileMRU, szCurFile);
MRU_Add(&mruFile, szCurFile);
}
MRU_Save(pFileMRU);
MRU_Save(&mruFile);
SendWMCommand(hwnd, IDC_FILEMRU_UPDATE_VIEW);
}
}
Expand All @@ -1237,8 +1237,8 @@ static INT_PTR CALLBACK FileMRUDlgProc(HWND hwnd, UINT umsg, WPARAM wParam, LPAR
SHGFI_USEFILEATTRIBUTES | SHGFI_SMALLICON | SHGFI_SYSICONINDEX);
lvi.iImage = shfi.iIcon;

for (int i = 0; i < pFileMRU->iSize; i++) {
LPWSTR path = pFileMRU->pszItems[i];
for (int i = 0; i < mruFile.iSize; i++) {
LPWSTR path = mruFile.pszItems[i];
lvi.iItem = i;
lvi.pszText = path;
ListView_InsertItem(hwndLV, &lvi);
Expand Down Expand Up @@ -1274,8 +1274,8 @@ static INT_PTR CALLBACK FileMRUDlgProc(HWND hwnd, UINT umsg, WPARAM wParam, LPAR
if (!PathIsFile(tch)) {
// Ask...
if (IDYES == MsgBoxWarn(MB_YESNO, IDS_ERR_MRUDLG)) {
MRU_DeleteFileFromStore(pFileMRU, tch);
MRU_Delete(pFileMRU, lvi.iItem);
MRU_DeleteFileFromStore(&mruFile, tch);
MRU_Delete(&mruFile, lvi.iItem);

// must use recreate the list, index might change...
//ListView_DeleteItem(hwndLV, lvi.iItem);
Expand Down
18 changes: 9 additions & 9 deletions src/Edit.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ extern int iSrcEncoding;
extern int iWeakSrcEncoding;
extern int iCurrentEncoding;

extern LPMRULIST mruFind;
extern LPMRULIST mruReplace;
extern MRULIST mruFind;
extern MRULIST mruReplace;

static DStringW wchPrefixSelection;
static DStringW wchAppendSelection;
Expand Down Expand Up @@ -4776,7 +4776,7 @@ static INT_PTR CALLBACK EditFindReplaceDlgProc(HWND hwnd, UINT umsg, WPARAM wPar
AddBackslashComboBoxSetup(hwnd, IDC_FINDTEXT);

// Load MRUs
MRU_AddToCombobox(mruFind, hwndFind);
MRU_AddToCombobox(&mruFind, hwndFind);

LPEDITFINDREPLACE lpefr = (LPEDITFINDREPLACE)lParam;
// don't copy selection after toggle find & replace on this window.
Expand All @@ -4794,7 +4794,7 @@ static INT_PTR CALLBACK EditFindReplaceDlgProc(HWND hwnd, UINT umsg, WPARAM wPar
HWND hwndRepl = GetDlgItem(hwnd, IDC_REPLACETEXT);
if (hwndRepl) {
AddBackslashComboBoxSetup(hwnd, IDC_REPLACETEXT);
MRU_AddToCombobox(mruReplace, hwndRepl);
MRU_AddToCombobox(&mruReplace, hwndRepl);
ComboBox_LimitText(hwndRepl, NP2_FIND_REPLACE_LIMIT);
ComboBox_SetExtendedUI(hwndRepl, TRUE);
SetDlgItemTextA2W(CP_UTF8, hwnd, IDC_REPLACETEXT, lpefr->szReplaceUTF8);
Expand Down Expand Up @@ -5044,13 +5044,13 @@ static INT_PTR CALLBACK EditFindReplaceDlgProc(HWND hwnd, UINT umsg, WPARAM wPar
if (StrNotEmptyA(lpefr->szFind)) {
if (GetDlgItemTextA2W(CP_UTF8, hwnd, IDC_FINDTEXT, lpefr->szFindUTF8, COUNTOF(lpefr->szFindUTF8))) {
ComboBox_GetText(hwndFind, tch, COUNTOF(tch));
MRU_AddMultiline(mruFind, tch);
MRU_AddMultiline(&mruFind, tch);
}
}
if (StrNotEmptyA(lpefr->szReplace)) {
if (GetDlgItemTextA2W(CP_UTF8, hwnd, IDC_REPLACETEXT, lpefr->szReplaceUTF8, COUNTOF(lpefr->szReplaceUTF8))) {
ComboBox_GetText(hwndRepl, tch, COUNTOF(tch));
MRU_AddMultiline(mruReplace, tch);
MRU_AddMultiline(&mruReplace, tch);
}
} else {
strcpy(lpefr->szReplaceUTF8, "");
Expand All @@ -5070,8 +5070,8 @@ static INT_PTR CALLBACK EditFindReplaceDlgProc(HWND hwnd, UINT umsg, WPARAM wPar
// Reload MRUs
ComboBox_ResetContent(hwndFind);
ComboBox_ResetContent(hwndRepl);
MRU_AddToCombobox(mruFind, hwndFind);
MRU_AddToCombobox(mruReplace, hwndRepl);
MRU_AddToCombobox(&mruFind, hwndFind);
MRU_AddToCombobox(&mruReplace, hwndRepl);

SetDlgItemTextA2W(CP_UTF8, hwnd, IDC_FINDTEXT, lpefr->szFindUTF8);
SetDlgItemTextA2W(CP_UTF8, hwnd, IDC_REPLACETEXT, lpefr->szReplaceUTF8);
Expand Down Expand Up @@ -5217,7 +5217,7 @@ static INT_PTR CALLBACK EditFindReplaceDlgProc(HWND hwnd, UINT umsg, WPARAM wPar
HWND hwndFind = GetDlgItem(hwnd, (pnmhdr->idFrom == IDC_CLEAR_FIND) ? IDC_FINDTEXT : IDC_REPLACETEXT);
ComboBox_GetText(hwndFind, tch, COUNTOF(tch));
ComboBox_ResetContent(hwndFind);
MRU_Empty((pnmhdr->idFrom == IDC_CLEAR_FIND) ? mruFind : mruReplace, true);
MRU_Empty((pnmhdr->idFrom == IDC_CLEAR_FIND) ? &mruFind : &mruReplace, true);
ComboBox_SetText(hwndFind, tch);
}
break;
Expand Down
49 changes: 20 additions & 29 deletions src/Helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -2344,21 +2344,12 @@ void ComboBox_AddStringA2W(UINT uCP, HWND hwnd, LPCSTR lpString) {
//
// MRU functions
//
LPMRULIST MRU_Create(LPCWSTR pszRegKey, int iFlags) {
LPMRULIST pmru = (LPMRULIST)NP2HeapAlloc(sizeof(MRULIST));
void MRU_Init(LPMRULIST pmru, LPCWSTR pszRegKey, int iFlags) {
pmru->iSize = 0;
pmru->iFlags = iFlags;
pmru->szRegKey = pszRegKey;
memset(pmru->pszItems, 0, sizeof(pmru->pszItems));
MRU_Load(pmru);
return pmru;
}

void MRU_Destroy(LPMRULIST pmru) {
for (int i = 0; i < pmru->iSize; i++) {
LocalFree(pmru->pszItems[i]);
}

memset(pmru, 0, sizeof(MRULIST));
NP2HeapFree(pmru);
}

static inline bool MRU_Equal(int flags, LPCWSTR psz1, LPCWSTR psz2) {
Expand Down Expand Up @@ -2420,27 +2411,28 @@ void MRU_Delete(LPMRULIST pmru, int iIndex) {
}

void MRU_DeleteFileFromStore(LPCMRULIST pmru, LPCWSTR pszFile) {
LPMRULIST pmruStore = MRU_Create(pmru->szRegKey, pmru->iFlags);
MRULIST mruStore;
MRU_Init(&mruStore, pmru->szRegKey, pmru->iFlags);
int deleted = 0;

for (int index = 0; index < pmruStore->iSize; ) {
LPCWSTR path = pmruStore->pszItems[index];
for (int index = 0; index < mruStore.iSize; ) {
LPCWSTR path = mruStore.pszItems[index];
if (PathEqual(path, pszFile)) {
deleted += 1;
LocalFree(pmruStore->pszItems[index]);
pmruStore->pszItems[index] = NULL;
for (int i = index; i < pmruStore->iSize - 1; i++) {
pmruStore->pszItems[i] = pmruStore->pszItems[i + 1];
pmruStore->pszItems[i + 1] = NULL;
LocalFree(mruStore.pszItems[index]);
mruStore.pszItems[index] = NULL;
for (int i = index; i < mruStore.iSize - 1; i++) {
mruStore.pszItems[i] = mruStore.pszItems[i + 1];
mruStore.pszItems[i + 1] = NULL;
}
} else {
index++;
}
}

pmruStore->iSize -= deleted;
MRU_Save(pmruStore);
MRU_Destroy(pmruStore);
mruStore.iSize -= deleted;
MRU_Save(&mruStore);
MRU_Empty(&mruStore, false);
}

void MRU_Empty(LPMRULIST pmru, bool save) {
Expand All @@ -2465,9 +2457,7 @@ void MRU_Load(LPMRULIST pmru) {
IniSection * const pIniSection = &section;
const int iFlags = pmru->iFlags;

//MRU_Empty(pmru, false);
IniSectionInit(pIniSection, MRU_MAXITEMS);

LoadIniSection(pmru->szRegKey, pIniSectionBuf, cchIniSection);
IniSectionParseArray(pIniSection, pIniSectionBuf, iFlags & MRUFlags_QuoteValue);
const UINT count = pIniSection->count;
Expand Down Expand Up @@ -2535,14 +2525,15 @@ void MRU_MergeSave(LPMRULIST pmru, bool keep) {
if (pmru->iSize <= 0) {
return;
}
LPMRULIST pmruBase = MRU_Create(pmru->szRegKey, pmru->iFlags);

MRULIST mruBase;
MRU_Init(&mruBase, pmru->szRegKey, pmru->iFlags);
for (int i = pmru->iSize - 1; i >= 0; i--) {
MRU_Add(pmruBase, pmru->pszItems[i]);
MRU_Add(&mruBase, pmru->pszItems[i]);
}

MRU_Save(pmruBase);
MRU_Destroy(pmruBase);
MRU_Save(&mruBase);
MRU_Empty(&mruBase, false);
}

void MRU_AddToCombobox(LPCMRULIST pmru, HWND hwnd) {
Expand Down
19 changes: 9 additions & 10 deletions src/Helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -890,16 +890,15 @@ typedef struct MRULIST {

typedef const MRULIST * LPCMRULIST;

LPMRULIST MRU_Create(LPCWSTR pszRegKey, int iFlags);
void MRU_Destroy(LPMRULIST pmru);
void MRU_Add(LPMRULIST pmru, LPCWSTR pszNew);
void MRU_AddMultiline(LPMRULIST pmru, LPCWSTR pszNew);
void MRU_Delete(LPMRULIST pmru, int iIndex);
void MRU_DeleteFileFromStore(LPCMRULIST pmru, LPCWSTR pszFile);
void MRU_Empty(LPMRULIST pmru, bool save);
void MRU_Load(LPMRULIST pmru);
void MRU_Save(LPCMRULIST pmru);
void MRU_MergeSave(LPMRULIST pmru, bool keep);
void MRU_Init(LPMRULIST pmru, LPCWSTR pszRegKey, int iFlags);
void MRU_Add(LPMRULIST pmru, LPCWSTR pszNew);
void MRU_AddMultiline(LPMRULIST pmru, LPCWSTR pszNew);
void MRU_Delete(LPMRULIST pmru, int iIndex);
void MRU_DeleteFileFromStore(LPCMRULIST pmru, LPCWSTR pszFile);
void MRU_Empty(LPMRULIST pmru, bool save);
void MRU_Load(LPMRULIST pmru);
void MRU_Save(LPCMRULIST pmru);
void MRU_MergeSave(LPMRULIST pmru, bool keep);
void MRU_AddToCombobox(LPCMRULIST pmru, HWND hwnd);

//==== Themed Dialogs =========================================================
Expand Down
Loading

0 comments on commit 4474739

Please sign in to comment.