From fe8d8b3192b347d8ea4b24a7af384861be927c0d Mon Sep 17 00:00:00 2001 From: YanTheKaller Date: Fri, 30 Oct 2026 17:16:26 +0600 Subject: [PATCH] Memory fixes; changes --- source/ubl-settings-resourcequota.c | 162 +++++++++++++++++----------- source/ubl-settings-resourcequota.h | 8 +- 2 files changed, 106 insertions(+), 64 deletions(-) diff --git a/source/ubl-settings-resourcequota.c b/source/ubl-settings-resourcequota.c index 6f863bb..6f8dcf5 100644 --- a/source/ubl-settings-resourcequota.c +++ b/source/ubl-settings-resourcequota.c @@ -64,47 +64,72 @@ void on_quota_configuration_update(GtkWidget *, quota_window *window){ long hrd = (long)gtk_spin_button_get_value(GTK_SPIN_BUTTON(window->HardRestrictionSpin)); long pgl = (long)gtk_spin_button_get_value(GTK_SPIN_BUTTON(window->PagingLimitSpin)); long cpu = (long)gtk_spin_button_get_value(GTK_SPIN_BUTTON(window->CpuLimitSpin)); - char *soft_limit = ""; - char *hard_limit = ""; - char *paging_limit = ""; - char *cpu_limit = ""; + char *soft_limit = NULL; + char *hard_limit = NULL; + char *paging_limit = NULL; + char *cpu_limit = NULL; if (sft) soft_limit = yon_char_unite("MemoryHigh=",yon_char_from_long(sft),get_size_mod(GTK_COMBO_BOX(window->SoftRestrictionCombo)),NULL); if (hrd) hard_limit = yon_char_unite("MemoryMax=",yon_char_from_long(hrd),get_size_mod(GTK_COMBO_BOX(window->HardRestrictionCombo)),NULL); if (pgl) paging_limit = yon_char_unite("MemorySwapMax=",yon_char_from_long(pgl),get_size_mod(GTK_COMBO_BOX(window->PagingLimitCombo)),NULL); if (cpu) cpu_limit = yon_char_unite("CPUQuota=",yon_char_from_long(cpu),"%",NULL); - char *read_limit = ""; - char *read_devices = ""; + char *read_devices = NULL; dictionary *dict; for_dictionaries(dict,window->devices){ device_limits *limits = yon_dictionary_get_data(dict,device_limits*); if (dict->data&&limits->read) - if (!strstr(limits->read," 0 ")) - read_devices = yon_char_unite(read_devices,"IOReadBandwidthMax=",yon_char_divide_search(yon_char_new(dict->key)," ",-1)," ",yon_dictionary_get_data(dict,device_limits*)->read,",",NULL); + if (!strstr(limits->read," 0 ")) { + char *tmp = yon_char_unite(read_devices ? read_devices : "","IOReadBandwidthMax=",yon_char_divide_search(yon_char_new(dict->key)," ",-1)," ",limits->read,",",NULL); + free(read_devices); + read_devices = tmp; + } } - if (read_devices[strlen(read_devices)-1]==',') read_devices[strlen(read_devices)-1]='\0'; - read_limit = yon_char_unite(read_limit,read_devices,NULL); - - - char *write_limit = ""; - char *write_devices = ""; + if (read_devices && strlen(read_devices) > 0 && read_devices[strlen(read_devices)-1]==',') + read_devices[strlen(read_devices)-1]='\0'; + + char *write_devices = NULL; for_dictionaries(dict,window->devices){ device_limits *limits = yon_dictionary_get_data(dict,device_limits*); if (dict->data&&limits->write) - if (!strstr(limits->write," 0 ")) - write_devices = yon_char_unite(write_devices,"IOWriteBandwidthMax=",yon_char_divide_search(yon_char_new(dict->key)," ",-1)," ",yon_dictionary_get_data(dict,device_limits*)->write,",",NULL); + if (!strstr(limits->write," 0 ")) { + char *tmp = yon_char_unite(write_devices ? write_devices : "","IOWriteBandwidthMax=",yon_char_divide_search(yon_char_new(dict->key)," ",-1)," ",limits->write,",",NULL); + free(write_devices); + write_devices = tmp; + } } - if (write_devices[strlen(write_devices)-1]==',') write_devices[strlen(write_devices)-1]='\0'; - write_limit = yon_char_unite(write_limit,write_devices,NULL); - - output_line = yon_char_unite(gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->SoftRestrictionCheck))? yon_char_append(soft_limit,","): "", - gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->HardRestrictionCheck))? yon_char_append(hard_limit,","):"", - gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->PagingLimitCheck))? yon_char_append(paging_limit,","):"", - gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->CpuLimitCheck))? yon_char_append(cpu_limit,","):"", - (read_devices && strcmp(read_devices,"")!=0 ) ? yon_char_append(read_limit,","):"", - (write_devices && strcmp(write_devices,"")!=0 ) ? yon_char_append(write_limit,","):"",NULL); + if (write_devices && strlen(write_devices) > 0 && write_devices[strlen(write_devices)-1]==',') + write_devices[strlen(write_devices)-1]='\0'; + + char *soft_part = NULL; + char *hard_part = NULL; + char *paging_part = NULL; + char *cpu_part = NULL; + char *read_part = NULL; + char *write_part = NULL; + if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->SoftRestrictionCheck)) && soft_limit) soft_part = yon_char_unite(soft_limit,",",NULL); + if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->HardRestrictionCheck)) && hard_limit) hard_part = yon_char_unite(hard_limit,",",NULL); + if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->PagingLimitCheck)) && paging_limit) paging_part = yon_char_unite(paging_limit,",",NULL); + if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(window->CpuLimitCheck)) && cpu_limit) cpu_part = yon_char_unite(cpu_limit,",",NULL); + if (read_devices && strcmp(read_devices,"")!=0) read_part = yon_char_unite(read_devices,",",NULL); + if (write_devices && strcmp(write_devices,"")!=0) write_part = yon_char_unite(write_devices,",",NULL); + + output_line = yon_char_unite(soft_part ? soft_part : "",hard_part ? hard_part : "",paging_part ? paging_part : "",cpu_part ? cpu_part : "",read_part ? read_part : "",write_part ? write_part : "",NULL); if (output_line&&strcmp(output_line,"")!=0&&output_line[strlen(output_line)-1] == ',') output_line[strlen(output_line)-1] = '\0'; - gtk_entry_set_text(GTK_ENTRY(window->ManualInputEntry),output_line); + gtk_entry_set_text(GTK_ENTRY(window->ManualInputEntry),output_line ? output_line : ""); + + free(output_line); + free(soft_limit); + free(hard_limit); + free(paging_limit); + free(cpu_limit); + free(read_devices); + free(write_devices); + free(soft_part); + free(hard_part); + free(paging_part); + free(cpu_part); + free(read_part); + free(write_part); } void on_limit_manual_input(GtkWidget *self, double *, quota_window *window){ @@ -118,12 +143,12 @@ void on_check_maximum(GtkWidget *self, GtkWidget *pair){ if (main_config.avaliable_ram==0){ struct sysinfo inf; sysinfo(&inf); - main_config.avaliable_ram=inf.totalram; - main_config.avaliable_swap=inf.totalswap; + main_config.avaliable_ram = (long)inf.totalram * inf.mem_unit; + main_config.avaliable_swap = (long)inf.totalswap * inf.mem_unit; } double cur_ram = main_config.avaliable_ram; double cur_value = gtk_spin_button_get_value(GTK_SPIN_BUTTON(self)); - for (int i=0;iTargetCombo)); for (int i=0; iTargetCombo),main_config.users[i]); + gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(window->TargetCombo),main_config.users[i],main_config.users[i]); } } if (active == 1) { gtk_combo_box_text_remove_all(GTK_COMBO_BOX_TEXT(window->TargetCombo)); for (int i=0; iTargetCombo),main_config.processes[i]); + gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(window->TargetCombo),main_config.processes[i],main_config.processes[i]); } } if (active == 2) { gtk_combo_box_text_remove_all(GTK_COMBO_BOX_TEXT(window->TargetCombo)); for (int i=0; iTargetCombo),main_config.slices[i]); + gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(window->TargetCombo),main_config.slices[i],main_config.slices[i]); } } } @@ -224,9 +249,10 @@ void yon_get_devices(quota_window *window){ for (int i=0; idevice_name = yon_char_new(pa); limits->read=NULL; @@ -400,7 +426,7 @@ int yon_get_size_request_from_letter(char size){ break; case 'G': return 2; break; - case 'T': return 0; + case 'T': return 3; break; default: return -1; break; @@ -569,10 +595,10 @@ void on_quota_manual_input(GtkWidget *self, quota_window *window){ if (rtn){ for (int i=0; idevices){ - if (strstr(dict->key,name)){ + if (!strcmp(dict->key,name)){ if (!((device_limits*)dict->data)){ dict->data = g_malloc0(sizeof(device_limits)); ((device_limits*)dict->data)->write=NULL; ((device_limits*)dict->data)->read=NULL; } + if (((device_limits*)dict->data)->read) free(((device_limits*)dict->data)->read); ((device_limits*)dict->data)->read = value; + value = NULL; break; } } @@ -666,19 +700,24 @@ void on_quota_manual_input(GtkWidget *self, quota_window *window){ char *name = yon_char_divide_search(value, " ",-1); dictionary *dict; for_dictionaries(dict,window->devices){ - if (strstr(dict->key,name)){ + if (!strcmp(dict->key,name)){ if (!((device_limits*)dict->data)){ dict->data = g_malloc0(sizeof(device_limits)); ((device_limits*)dict->data)->write=NULL; ((device_limits*)dict->data)->read=NULL; } + if (((device_limits*)dict->data)->write) free(((device_limits*)dict->data)->write); ((device_limits*)dict->data)->write = value; + value = NULL; + break; } } - } else return; + } else { free(value); free(string); return; } } + if (!yon_char_is_empty(value)) free(value); } + if (!yon_char_is_empty(string)) free(string); } void on_quota_manual_input_focus (GtkWidget *self, GdkEventFocus *, quota_window *window){ @@ -726,6 +765,7 @@ void on_filters_opened(GtkWidget *, main_window *widgets) { void on_add_open(GtkWidget *, main_window *widgets){ quota_window *window = yon_quota_window_new(); + yon_gtk_window_setup(GTK_WINDOW(window->Window),GTK_WINDOW(widgets->Window),NULL,icon_path,"quota_window"); gtk_window_set_title(GTK_WINDOW(window->Window),TITLE_LABEL); @@ -769,7 +809,7 @@ void on_information(GtkWidget *, main_window *widgets){ } else { gtk_combo_box_set_active(GTK_COMBO_BOX(widgets->TargetTypeCombo),0); } - gtk_combo_box_set_active(GTK_COMBO_BOX(widgets->TargetCombo),yon_gtk_combo_box_text_find(widgets->TargetCombo,name)); + gtk_combo_box_set_active_id(GTK_COMBO_BOX(widgets->TargetCombo),name); g_signal_handlers_unblock_by_func(G_OBJECT(widgets->TargetCombo),on_information_chosen_changed,widgets); char *command = get_information_command((char*)name); main_config.last_info = yon_char_new((char*)name); @@ -967,6 +1007,12 @@ quota_window *yon_quota_window_new(){ g_signal_connect(G_OBJECT(window->ManualInputEntry),"changed",G_CALLBACK(on_manual_edit),window); g_signal_connect(G_OBJECT(window->ManualInputEntry),"focus-out-event",G_CALLBACK(yon_manual_string_finish),window); + struct sysinfo inf; + sysinfo(&inf); + main_config.avaliable_ram=inf.totalram; + main_config.avaliable_swap=inf.totalswap; + main_config.cores = sysconf(_SC_NPROCESSORS_ONLN); + yon_get_devices(window); window->devices=window->devices->first; @@ -988,14 +1034,9 @@ quota_window *yon_quota_window_new(){ yon_char_remove_last_symbol(main_config.slices[i],'\n'); } for (int i=0; iTargetCombo),main_config.users[i]); + gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(window->TargetCombo),main_config.users[i],main_config.users[i]); } - struct sysinfo inf; - sysinfo(&inf); - main_config.avaliable_ram=inf.totalram; - main_config.avaliable_swap=inf.totalswap; - main_config.cores = sysconf(_SC_NPROCESSORS_ONLN); gtk_widget_show(window->Window); return window; @@ -1042,9 +1083,9 @@ void yon_quota_update(quota_window *window){ if (!strcmp(parsed_parameter[0],"MemoryHigh")&&parsed_size>1){ soft_restr = atol(parsed_parameter[1]); int size_mult = yon_get_size_get_from_letter(parsed_parameter[1][strlen(parsed_parameter[1])-1]); - if (size_mult==-1) size_mult=0; - long hard_current_max = main_config.avaliable_ram; - for (int i=0;iSoftRestrictionSpin)),hard_current_max); if (soft_restr) { @@ -1164,6 +1205,7 @@ void on_edit_open(GtkWidget *, main_window *widgets){ GtkTreeIter iter; if (!gtk_tree_selection_get_selected(gtk_tree_view_get_selection(GTK_TREE_VIEW(widgets->MainTree)),&model,&iter)) return; quota_window *window = yon_quota_window_new(); + yon_gtk_window_setup(GTK_WINDOW(window->Window),GTK_WINDOW(widgets->Window),NULL,icon_path,"quota_window"); gtk_widget_set_sensitive(window->BlockUiBox,1); char *target, *type; gtk_tree_model_get(model,&iter,0,&type,1,&target,-1); @@ -1206,9 +1248,9 @@ gboolean on_dispatcher_update(GtkWidget *,main_window *widgets){ void on_processes_update(GtkWidget *,main_window *widgets){ int show_all = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widgets->ShowAllCheck)); int show_core = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(widgets->ShowCoreCheck)); - char *command = get_processes_tree_command; - if (show_all) yon_char_append(command, " -k"); - if (show_core) yon_char_append(command, " -l"); + char *command = yon_char_new(get_processes_tree_command); + if (show_all) command = yon_char_append(command, " -k"); + if (show_core) command = yon_char_append(command, " -l"); yon_terminal_integrated_start_shell(widgets->ProcessesTerminal, command,NULL,NULL); } @@ -1236,7 +1278,7 @@ void on_info_target_type_switched(GtkWidget *self, main_window *widgets){ break; } for (int i=0; iTargetCombo),str[i]); + gtk_combo_box_text_append(GTK_COMBO_BOX_TEXT(widgets->TargetCombo),str[i],str[i]); } } @@ -1363,8 +1405,8 @@ int main(int argc, char *argv[]){ main_config.launch_size=argc; yon_window_check_root_message(widgets); yon_main_window_complete((main_window*)widgets); - char *path = yon_char_unite(yon_ubl_user_get_home_directory(),"/.config/",LocaleName,"/",LocaleName,".conf",NULL); - yon_window_config_load(path); + yon_window_config_load(config_path); + gtk_main(); return 0; } \ No newline at end of file diff --git a/source/ubl-settings-resourcequota.h b/source/ubl-settings-resourcequota.h index b16b6b9..f7d171c 100644 --- a/source/ubl-settings-resourcequota.h +++ b/source/ubl-settings-resourcequota.h @@ -17,15 +17,15 @@ #include #endif #include "ubl-strings.h" -#define config_path yon_char_unite("/home/",yon_ubl_root_user_get(),"/.config/",LocaleName,"/",LocaleName,".conf",NULL) #define _(String) gettext(String) #define glade_path "/com/ublinux/ui/ubl-settings-resourcequota.glade" #define glade_filters_path "/com/ublinux/ui/ubl-settings-resourcequota-filters.glade" #define glade_quota_path "/com/ublinux/ui/ubl-settings-resourcequota-quota.glade" -#define banner_path "/com/ublinux/images/ubl-settings-resourcequota-banner.png" #define CssPath "/com/ublinux/css/ubl-settings-resourcequota.css" +#define config_path yon_char_unite(yon_ubl_user_get_home_directory(),"/.config/",LocaleName,"/",LocaleName,".conf",NULL) +#define icon_path "com.ublinux.ubl-settings-resourcequota" #define LocalePath "/usr/share/locale" #define LocaleName "ubl-settings-resourcequota" @@ -38,7 +38,7 @@ // #define remove_command "ubconfig remove security " // #define remove_global_command "ubconfig --target global --noexecute remove security " // #define remove_local_command "ubconfig --target system remove security " -#define get_devices_command "clear;findmnt -lo source,target,fstype,label,size -t ext4 |grep -vE \"zram|\\]|\\[|SOURCE\"" +#define get_devices_command "findmnt -lo source,target,fstype,label,size -t ext4| tail -n +2| grep -v \"\\[\"" #define get_processes_tree_command "clear; systemd-cgls --no-page" #define get_dispatcher_command "clear; systemd-cgtop -c" #define get_information_command(target) yon_char_unite("systemctl status ",target,".slice --no-pager",NULL) @@ -52,7 +52,7 @@ #define config_get_local_only_parameters "" #define CGROUP_QUOTA(name) yon_char_unite("CGROUP_QUOTA[",name,"]",NULL) -#define CGROUP_QUOTA_comd(name) yon_char_unite("ubconfig -- source global get security CGROUP_QUOTA[",target,"]",NULL) +#define CGROUP_QUOTA_comd(name) yon_char_unite("ubconfig -- source global get security CGROUP_QUOTA[",name,"]",NULL) #define CGROUP_QUOTA_parameter "CGROUP_QUOTA" #define CGROUP_QUOTA_command "ubconfig -- source global get security CGROUP_QUOTA"