g/j1: resolve memory leak around display/input dynamic strings from C++ (#2979)

This commit is contained in:
Tyler Wilding
2023-09-11 16:09:42 -06:00
committed by GitHub
parent 9b9fe7a7af
commit 0a45f50522
4 changed files with 38 additions and 32 deletions
+16 -11
View File
@@ -447,15 +447,16 @@ u64 pc_get_mips2c(u32 name) {
return Mips2C::gLinkedFunctionTable.get(n);
}
u64 pc_get_display_name(u32 id) {
u64 pc_get_display_name(u32 id, u32 str_dest_ptr) {
std::string name = "";
if (Display::GetMainDisplay()) {
name = Display::GetMainDisplay()->get_display_manager()->get_connected_display_name(id);
}
if (name.empty()) {
return s7.offset;
return bool_to_symbol(false);
}
return g_pc_port_funcs.make_string_from_c(str_util::to_upper(name).c_str());
strcpy(Ptr<String>(str_dest_ptr).c()->data(), str_util::to_upper(name).c_str());
return bool_to_symbol(true);
}
u32 pc_get_display_mode() {
@@ -591,21 +592,23 @@ void pc_get_resolution(u32 id, u32 w_ptr, u32 h_ptr) {
}
}
u64 pc_get_controller_name(u32 id) {
u64 pc_get_controller_name(u32 id, u32 str_dest_ptr) {
std::string name = "";
if (Display::GetMainDisplay()) {
name = Display::GetMainDisplay()->get_input_manager()->get_controller_name(id);
}
if (name.empty()) {
return s7.offset;
return bool_to_symbol(false);
}
return g_pc_port_funcs.make_string_from_c(str_util::to_upper(name).c_str());
strcpy(Ptr<String>(str_dest_ptr).c()->data(), str_util::to_upper(name).c_str());
return bool_to_symbol(true);
}
u64 pc_get_current_bind(s32 bind_assignment_info) {
u64 pc_get_current_bind(s32 bind_assignment_info, u32 str_dest_ptr) {
if (!Display::GetMainDisplay()) {
// TODO - return something that lets the runtime use a translatable string if unknown
return g_pc_port_funcs.make_string_from_c(str_util::to_upper("unknown").c_str());
strcpy(Ptr<String>(str_dest_ptr).c()->data(), str_util::to_upper("unknown").c_str());
return bool_to_symbol(true);
}
auto info = bind_assignment_info ? Ptr<BindAssignmentInfo>(bind_assignment_info).c() : NULL;
@@ -619,12 +622,14 @@ u64 pc_get_current_bind(s32 bind_assignment_info) {
auto name = Display::GetMainDisplay()->get_input_manager()->get_current_bind(
port, (InputDeviceType)device_type, for_button, input_idx, analog_min_range);
if (name.empty()) {
return s7.offset;
return bool_to_symbol(false);
}
return g_pc_port_funcs.make_string_from_c(str_util::to_upper(name).c_str());
strcpy(Ptr<String>(str_dest_ptr).c()->data(), str_util::to_upper(name).c_str());
return bool_to_symbol(true);
}
// TODO - return something that lets the runtime use a translatable string if unknown
return g_pc_port_funcs.make_string_from_c(str_util::to_upper("unknown").c_str());
strcpy(Ptr<String>(str_dest_ptr).c()->data(), str_util::to_upper("unknown").c_str());
return bool_to_symbol(true);
}
u64 pc_get_controller_count() {
+3 -3
View File
@@ -333,14 +333,14 @@
;; Input Related Functions
(define-extern pc-get-controller-count (function int))
(define-extern pc-get-controller-name (function int string))
(define-extern pc-get-controller-name (function int string symbol))
(deftype bind-assignment-info (structure)
((port int32)
(device-type int32)
(for-buttons? symbol)
(input-idx int32)
(analog-min-range? symbol)))
(define-extern pc-get-current-bind (function bind-assignment-info string))
(define-extern pc-get-current-bind (function bind-assignment-info string symbol))
(define-extern pc-waiting-for-bind? (function symbol))
(define-extern pc-set-waiting-for-bind! (function int symbol symbol int none))
(define-extern pc-stop-waiting-for-bind! (function none))
@@ -360,7 +360,7 @@
(define-extern pc-get-active-display-size (function (pointer int32) (pointer int32) none))
(define-extern pc-get-active-display-refresh-rate (function int))
(define-extern pc-get-display-count (function int))
(define-extern pc-get-display-name (function int string))
(define-extern pc-get-display-name (function int string symbol))
(define-extern pc-get-os (function symbol))
(define-extern pc-get-window-size (function (pointer int32) (pointer int32) none))
(define-extern pc-get-window-scale (function (pointer float) (pointer float) none))
+3
View File
@@ -800,6 +800,9 @@
;; shared temporary strings.
(define *temp-string* (new 'global 'string 256 (the string #f)))
(define *temp-string2* (new 'global 'string 256 (the string #f)))
(define *pc-cpp-temp-string*
"A conveniant place to retrieve a string from C++"
(new 'global 'string 256 (the-as string #f)))
(defmacro string-format (&rest args)
"Formats into *temp-string* and returns it, for in-place string formating.
+16 -18
View File
@@ -523,10 +523,11 @@
;; this is to avoid changing the value of *temp-options* or reallocating the options when reloading file
(unless *temp-options-alloced*
(define *temp-options* (new 'static 'boxed-array :type game-option :length 200 :allocated-length 200))
(dotimes (i (-> *temp-options* length))
(set! (-> *temp-options* i) (new 'global 'game-option)))
(true! *temp-options-alloced*))
(define *temp-options* (new 'static 'boxed-array :type game-option :length 200 :allocated-length 200))
(dotimes (i (-> *temp-options* length))
(set! (-> *temp-options* i) (new 'global 'game-option))
(set! (-> *temp-options* i name-override) (new 'global 'string 128 (the-as string #f))))
(true! *temp-options-alloced*))
(defmacro add-flava-player-option (text-id flava)
"add a music player w/ flava button to *temp-options* with specified size"
@@ -618,11 +619,11 @@
(defmacro add-monitor-option (text-id display-id)
"add a display button to *temp-options* with specified size"
`(let ((option (-> *temp-options* (length *temp-options*)))
(name (pc-get-display-name ,display-id)))
(name (pc-get-display-name ,display-id *pc-cpp-temp-string*)))
(set! (-> option option-type) (game-option-type monitor))
;; Use the name of the display if it's available
(if name
(set! (-> option name-override) name)
(copy-string<-string (-> option name-override) *pc-cpp-temp-string*)
(set! (-> option name) ,text-id))
(set! (-> option param1) (the float ,display-id))
(set! (-> option scale) #t)
@@ -641,18 +642,15 @@
(defmacro add-controller-option (text-id controller-id)
"add a controller option to *temp-options* with specified size"
`(let ((option (-> *temp-options* (length *temp-options*)))
(name (pc-get-controller-name ,controller-id)))
(name (pc-get-controller-name ,controller-id *pc-cpp-temp-string*)))
(set! (-> option option-type) (game-option-type controller))
;; Use the name of the controller if it's available
(if name
(set! (-> option name-override) name)
(copy-string<-string (-> option name-override) *pc-cpp-temp-string*)
(set! (-> option name) ,text-id))
(set! (-> option param1) (the float ,controller-id))
(set! (-> option scale) #t)
(1+! (-> *temp-options* length))
)
)
(1+! (-> *temp-options* length))))
(defun build-controller-options ()
"Dynamically creates menu options for selecting your controller.
@@ -2344,14 +2342,14 @@
)
)
(((game-option-type binding-assignment))
(let ((bind (pc-get-current-bind (-> options index bind-info))))
(let ((bind (pc-get-current-bind (-> options index bind-info) *pc-cpp-temp-string*)))
(cond
((= bind #f)
(set! option-str (string-format "~S" (lookup-text! *common-text* (text-id input-opts-binds-unset) #f))))
((string= bind "unknown")
((string= *pc-cpp-temp-string* "unknown")
(set! option-str (string-format "~S" (lookup-text! *common-text* (text-id input-opts-binds-unknown) #f))))
(else
(set! option-str (string-format "~S" bind))))))
(set! option-str (string-format "~S" *pc-cpp-temp-string*))))))
(((game-option-type display-mode)
(game-option-type msaa)
(game-option-type lod-bg)
@@ -2466,20 +2464,20 @@
)
(((game-option-type binding-assignment))
;; binding assignment options show their input + the current binding
(let ((bind (pc-get-current-bind (-> options index bind-info))))
(let ((bind (pc-get-current-bind (-> options index bind-info) *pc-cpp-temp-string*)))
(cond
((= bind #f)
(set! option-str (string-format "~S: ~10L~S~0L"
(-> options index name-override)
(lookup-text! *common-text* (text-id input-opts-binds-unset) #f))))
((string= bind "unknown")
((string= *pc-cpp-temp-string* "unknown")
(set! option-str (string-format "~S: ~10L~S~0L"
(-> options index name-override)
(lookup-text! *common-text* (text-id input-opts-binds-unknown) #f))))
(else
(set! option-str (string-format "~S: ~10L~S~0L"
(-> options index name-override)
bind))))))
*pc-cpp-temp-string*))))))
(((game-option-type cheat-toggle))
(cond
((not (logtest? (-> *pc-settings* cheats-known) (ash 1 (the int (-> options index param1)))))