mirror of
https://github.com/DarkFlippers/unleashed-firmware.git
synced 2025-12-12 04:34:43 +04:00
cli_registry: move from bptree to dict, fix memory leak
This commit is contained in:
@@ -3,16 +3,16 @@
|
|||||||
#include <toolbox/pipe.h>
|
#include <toolbox/pipe.h>
|
||||||
#include <storage/storage.h>
|
#include <storage/storage.h>
|
||||||
|
|
||||||
#define TAG "cli"
|
#define TAG "CliRegistry"
|
||||||
|
|
||||||
struct CliRegistry {
|
struct CliRegistry {
|
||||||
CliCommandTree_t commands;
|
CliCommandDict_t commands;
|
||||||
FuriMutex* mutex;
|
FuriMutex* mutex;
|
||||||
};
|
};
|
||||||
|
|
||||||
CliRegistry* cli_registry_alloc(void) {
|
CliRegistry* cli_registry_alloc(void) {
|
||||||
CliRegistry* registry = malloc(sizeof(CliRegistry));
|
CliRegistry* registry = malloc(sizeof(CliRegistry));
|
||||||
CliCommandTree_init(registry->commands);
|
CliCommandDict_init(registry->commands);
|
||||||
registry->mutex = furi_mutex_alloc(FuriMutexTypeRecursive);
|
registry->mutex = furi_mutex_alloc(FuriMutexTypeRecursive);
|
||||||
return registry;
|
return registry;
|
||||||
}
|
}
|
||||||
@@ -20,7 +20,7 @@ CliRegistry* cli_registry_alloc(void) {
|
|||||||
void cli_registry_free(CliRegistry* registry) {
|
void cli_registry_free(CliRegistry* registry) {
|
||||||
furi_check(furi_mutex_acquire(registry->mutex, FuriWaitForever) == FuriStatusOk);
|
furi_check(furi_mutex_acquire(registry->mutex, FuriWaitForever) == FuriStatusOk);
|
||||||
furi_mutex_free(registry->mutex);
|
furi_mutex_free(registry->mutex);
|
||||||
CliCommandTree_clear(registry->commands);
|
CliCommandDict_clear(registry->commands);
|
||||||
free(registry);
|
free(registry);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -61,7 +61,7 @@ void cli_registry_add_command_ex(
|
|||||||
};
|
};
|
||||||
|
|
||||||
furi_check(furi_mutex_acquire(registry->mutex, FuriWaitForever) == FuriStatusOk);
|
furi_check(furi_mutex_acquire(registry->mutex, FuriWaitForever) == FuriStatusOk);
|
||||||
CliCommandTree_set_at(registry->commands, name_str, command);
|
CliCommandDict_set_at(registry->commands, name_str, command);
|
||||||
furi_check(furi_mutex_release(registry->mutex) == FuriStatusOk);
|
furi_check(furi_mutex_release(registry->mutex) == FuriStatusOk);
|
||||||
|
|
||||||
furi_string_free(name_str);
|
furi_string_free(name_str);
|
||||||
@@ -79,7 +79,7 @@ void cli_registry_delete_command(CliRegistry* registry, const char* name) {
|
|||||||
} while(name_replace != FURI_STRING_FAILURE);
|
} while(name_replace != FURI_STRING_FAILURE);
|
||||||
|
|
||||||
furi_check(furi_mutex_acquire(registry->mutex, FuriWaitForever) == FuriStatusOk);
|
furi_check(furi_mutex_acquire(registry->mutex, FuriWaitForever) == FuriStatusOk);
|
||||||
CliCommandTree_erase(registry->commands, name_str);
|
CliCommandDict_erase(registry->commands, name_str);
|
||||||
furi_check(furi_mutex_release(registry->mutex) == FuriStatusOk);
|
furi_check(furi_mutex_release(registry->mutex) == FuriStatusOk);
|
||||||
|
|
||||||
furi_string_free(name_str);
|
furi_string_free(name_str);
|
||||||
@@ -91,7 +91,7 @@ bool cli_registry_get_command(
|
|||||||
CliRegistryCommand* result) {
|
CliRegistryCommand* result) {
|
||||||
furi_assert(registry);
|
furi_assert(registry);
|
||||||
furi_check(furi_mutex_acquire(registry->mutex, FuriWaitForever) == FuriStatusOk);
|
furi_check(furi_mutex_acquire(registry->mutex, FuriWaitForever) == FuriStatusOk);
|
||||||
CliRegistryCommand* data = CliCommandTree_get(registry->commands, command);
|
CliRegistryCommand* data = CliCommandDict_get(registry->commands, command);
|
||||||
if(data) *result = *data;
|
if(data) *result = *data;
|
||||||
|
|
||||||
furi_check(furi_mutex_release(registry->mutex) == FuriStatusOk);
|
furi_check(furi_mutex_release(registry->mutex) == FuriStatusOk);
|
||||||
@@ -103,16 +103,14 @@ void cli_registry_remove_external_commands(CliRegistry* registry) {
|
|||||||
furi_check(registry);
|
furi_check(registry);
|
||||||
furi_check(furi_mutex_acquire(registry->mutex, FuriWaitForever) == FuriStatusOk);
|
furi_check(furi_mutex_acquire(registry->mutex, FuriWaitForever) == FuriStatusOk);
|
||||||
|
|
||||||
// FIXME FL-3977: memory leak somewhere within this function
|
CliCommandDict_t internal_cmds;
|
||||||
|
CliCommandDict_init(internal_cmds);
|
||||||
CliCommandTree_t internal_cmds;
|
|
||||||
CliCommandTree_init(internal_cmds);
|
|
||||||
for
|
for
|
||||||
M_EACH(item, registry->commands, CliCommandTree_t) {
|
M_EACH(item, registry->commands, CliCommandDict_t) {
|
||||||
if(!(item->value_ptr->flags & CliCommandFlagExternal))
|
if(!(item->value.flags & CliCommandFlagExternal))
|
||||||
CliCommandTree_set_at(internal_cmds, *item->key_ptr, *item->value_ptr);
|
CliCommandDict_set_at(internal_cmds, item->key, item->value);
|
||||||
}
|
}
|
||||||
CliCommandTree_move(registry->commands, internal_cmds);
|
CliCommandDict_move(registry->commands, internal_cmds);
|
||||||
|
|
||||||
furi_check(furi_mutex_release(registry->mutex) == FuriStatusOk);
|
furi_check(furi_mutex_release(registry->mutex) == FuriStatusOk);
|
||||||
}
|
}
|
||||||
@@ -148,7 +146,7 @@ void cli_registry_reload_external_commands(
|
|||||||
.execute_callback = NULL,
|
.execute_callback = NULL,
|
||||||
.flags = CliCommandFlagExternal,
|
.flags = CliCommandFlagExternal,
|
||||||
};
|
};
|
||||||
CliCommandTree_set_at(registry->commands, plugin_name, command);
|
CliCommandDict_set_at(registry->commands, plugin_name, command);
|
||||||
}
|
}
|
||||||
|
|
||||||
furi_string_free(plugin_name);
|
furi_string_free(plugin_name);
|
||||||
@@ -172,7 +170,7 @@ void cli_registry_unlock(CliRegistry* registry) {
|
|||||||
furi_mutex_release(registry->mutex);
|
furi_mutex_release(registry->mutex);
|
||||||
}
|
}
|
||||||
|
|
||||||
CliCommandTree_t* cli_registry_get_commands(CliRegistry* registry) {
|
CliCommandDict_t* cli_registry_get_commands(CliRegistry* registry) {
|
||||||
furi_assert(registry);
|
furi_assert(registry);
|
||||||
return ®istry->commands;
|
return ®istry->commands;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <furi.h>
|
#include <furi.h>
|
||||||
#include <m-bptree.h>
|
#include <m-dict.h>
|
||||||
#include "cli_registry.h"
|
#include "cli_registry.h"
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
@@ -22,19 +22,9 @@ typedef struct {
|
|||||||
size_t stack_depth;
|
size_t stack_depth;
|
||||||
} CliRegistryCommand;
|
} CliRegistryCommand;
|
||||||
|
|
||||||
#define CLI_COMMANDS_TREE_RANK 4
|
DICT_DEF2(CliCommandDict, FuriString*, FURI_STRING_OPLIST, CliRegistryCommand, M_POD_OPLIST);
|
||||||
|
|
||||||
// -V:BPTREE_DEF2:1103
|
#define M_OPL_CliCommandDict_t() DICT_OPLIST(CliCommandDict, FURI_STRING_OPLIST, M_POD_OPLIST)
|
||||||
// -V:BPTREE_DEF2:524
|
|
||||||
BPTREE_DEF2(
|
|
||||||
CliCommandTree,
|
|
||||||
CLI_COMMANDS_TREE_RANK,
|
|
||||||
FuriString*,
|
|
||||||
FURI_STRING_OPLIST,
|
|
||||||
CliRegistryCommand,
|
|
||||||
M_POD_OPLIST);
|
|
||||||
|
|
||||||
#define M_OPL_CliCommandTree_t() BPTREE_OPLIST2(CliCommandTree, FURI_STRING_OPLIST, M_POD_OPLIST)
|
|
||||||
|
|
||||||
bool cli_registry_get_command(
|
bool cli_registry_get_command(
|
||||||
CliRegistry* registry,
|
CliRegistry* registry,
|
||||||
@@ -48,7 +38,7 @@ void cli_registry_unlock(CliRegistry* registry);
|
|||||||
/**
|
/**
|
||||||
* @warning Surround calls to this function with `cli_registry_[un]lock`
|
* @warning Surround calls to this function with `cli_registry_[un]lock`
|
||||||
*/
|
*/
|
||||||
CliCommandTree_t* cli_registry_get_commands(CliRegistry* registry);
|
CliCommandDict_t* cli_registry_get_commands(CliRegistry* registry);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -103,15 +103,15 @@ void cli_command_help(PipeSide* pipe, FuriString* args, void* context) {
|
|||||||
|
|
||||||
printf("Available commands:\r\n" ANSI_FG_GREEN);
|
printf("Available commands:\r\n" ANSI_FG_GREEN);
|
||||||
cli_registry_lock(registry);
|
cli_registry_lock(registry);
|
||||||
CliCommandTree_t* commands = cli_registry_get_commands(registry);
|
CliCommandDict_t* commands = cli_registry_get_commands(registry);
|
||||||
size_t commands_count = CliCommandTree_size(*commands);
|
size_t commands_count = CliCommandDict_size(*commands);
|
||||||
|
|
||||||
CliCommandTree_it_t iterator;
|
CliCommandDict_it_t iterator;
|
||||||
CliCommandTree_it(iterator, *commands);
|
CliCommandDict_it(iterator, *commands);
|
||||||
for(size_t i = 0; i < commands_count; i++) {
|
for(size_t i = 0; i < commands_count; i++) {
|
||||||
const CliCommandTree_itref_t* item = CliCommandTree_cref(iterator);
|
const CliCommandDict_itref_t* item = CliCommandDict_cref(iterator);
|
||||||
printf("%-30s", furi_string_get_cstr(*item->key_ptr));
|
printf("%-30s", furi_string_get_cstr(item->key));
|
||||||
CliCommandTree_next(iterator);
|
CliCommandDict_next(iterator);
|
||||||
|
|
||||||
if(i % columns == columns - 1) printf("\r\n");
|
if(i % columns == columns - 1) printf("\r\n");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -111,10 +111,10 @@ void cli_shell_completions_fill_variants(CliShellCompletions* completions) {
|
|||||||
if(segment.type == CliShellCompletionSegmentTypeCommand) {
|
if(segment.type == CliShellCompletionSegmentTypeCommand) {
|
||||||
CliRegistry* registry = completions->registry;
|
CliRegistry* registry = completions->registry;
|
||||||
cli_registry_lock(registry);
|
cli_registry_lock(registry);
|
||||||
CliCommandTree_t* commands = cli_registry_get_commands(registry);
|
CliCommandDict_t* commands = cli_registry_get_commands(registry);
|
||||||
for
|
for
|
||||||
M_EACH(registered_command, *commands, CliCommandTree_t) {
|
M_EACH(registered_command, *commands, CliCommandDict_t) {
|
||||||
FuriString* command_name = *registered_command->key_ptr;
|
FuriString* command_name = registered_command->key;
|
||||||
if(furi_string_start_with(command_name, input)) {
|
if(furi_string_start_with(command_name, input)) {
|
||||||
CommandCompletions_push_back(completions->variants, command_name);
|
CommandCompletions_push_back(completions->variants, command_name);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user