From b8ea9b7922864be930b2f64edc91dd00e8b4acb6 Mon Sep 17 00:00:00 2001 From: Shuanglei Tao Date: Wed, 27 Dec 2023 15:22:33 +0800 Subject: [PATCH] replace memset with talloc_zero --- CMakeLists.txt | 3 ++- src/menu.c | 9 +++------ src/plugin.c | 11 +++-------- src/types.c | 24 ++++++++---------------- 4 files changed, 16 insertions(+), 31 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index f72c068..b625f3e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,7 @@ cmake_minimum_required(VERSION 3.13) -project(mpv-plugin) +project(mpv-menu-plugin C) +set(CMAKE_C_STANDARD 11) include(FindPkgConfig) pkg_search_module(MPV REQUIRED mpv>=0.37.0) diff --git a/src/menu.c b/src/menu.c index df216e6..e964f75 100644 --- a/src/menu.c +++ b/src/menu.c @@ -125,8 +125,7 @@ static wchar_t *format_title(void *talloc_ctx, bstr name, bstr key) { static int append_menu(HMENU hmenu, UINT fMask, UINT fType, UINT fState, wchar_t *title, HMENU submenu, void *data) { static UINT id = WM_USER + 100; - MENUITEMINFOW mii; - memset(&mii, 0, sizeof(mii)); + MENUITEMINFOW mii = {0}; mii.cbSize = sizeof(mii); mii.fMask = MIIM_ID | fMask; @@ -289,8 +288,7 @@ static bool update_audio_device_menu(plugin_ctx *ctx, dyn_entry *item) { } static void dyn_menu_init(void *talloc_ctx) { - dyn_menus = talloc_ptrtype(talloc_ctx, dyn_menus); - memset(dyn_menus, 0, sizeof(*dyn_menus)); + dyn_menus = talloc_zero(talloc_ctx, dyn_list); } static void dyn_menu_update(plugin_ctx *ctx) { @@ -407,8 +405,7 @@ void show_menu(plugin_ctx *ctx, POINT *pt) { } void handle_menu(plugin_ctx *ctx, int id) { - MENUITEMINFOW mii; - memset(&mii, 0, sizeof(mii)); + MENUITEMINFOW mii = {0}; mii.cbSize = sizeof(mii); mii.fMask = MIIM_DATA; if (!GetMenuItemInfoW(ctx->hmenu, id, FALSE, &mii)) return; diff --git a/src/plugin.c b/src/plugin.c index e1afc7e..f572c77 100644 --- a/src/plugin.c +++ b/src/plugin.c @@ -81,14 +81,9 @@ static void handle_client_message(mpv_event *event) { } static plugin_ctx *create_plugin_ctx() { - plugin_ctx *ctx = talloc_ptrtype(NULL, ctx); - memset(ctx, 0, sizeof(*ctx)); - - ctx->conf = talloc_ptrtype(ctx, ctx->conf); - memset(ctx->conf, 0, sizeof(*ctx->conf)); - ctx->state = talloc_ptrtype(ctx, ctx->state); - memset(ctx->state, 0, sizeof(*ctx->state)); - + plugin_ctx *ctx = talloc_zero(NULL, plugin_ctx); + ctx->conf = talloc_zero(ctx, plugin_config); + ctx->state = talloc_zero(ctx, mp_state); return ctx; } diff --git a/src/types.c b/src/types.c index 7a9346f..3972f7d 100644 --- a/src/types.c +++ b/src/types.c @@ -8,15 +8,13 @@ static void update_track_list(mp_state *state, mpv_node *node) { if (state->track_list != NULL) talloc_free(state->track_list); - state->track_list = talloc_ptrtype(state, state->track_list); - memset(state->track_list, 0, sizeof(*state->track_list)); + state->track_list = talloc_zero(state, mp_track_list); mp_track_list *list = state->track_list; for (int i = 0; i < node->u.list->num; i++) { mpv_node track = node->u.list->values[i]; - mp_track_item item; - memset(&item, 0, sizeof(item)); + mp_track_item item = {0}; for (int j = 0; j < track.u.list->num; j++) { char *key = track.u.list->keys[j]; @@ -50,15 +48,13 @@ static void update_track_list(mp_state *state, mpv_node *node) { static void update_chapter_list(mp_state *state, mpv_node *node) { if (state->chapter_list != NULL) talloc_free(state->chapter_list); - state->chapter_list = talloc_ptrtype(state, state->chapter_list); - memset(state->chapter_list, 0, sizeof(*state->chapter_list)); + state->chapter_list = talloc_zero(state, mp_chapter_list); mp_chapter_list *list = state->chapter_list; for (int i = 0; i < node->u.list->num; i++) { mpv_node chapter = node->u.list->values[i]; - mp_chapter_item item; - memset(&item, 0, sizeof(item)); + mp_chapter_item item = {0}; for (int j = 0; j < chapter.u.list->num; j++) { char *key = chapter.u.list->keys[j]; @@ -80,15 +76,13 @@ static void update_chapter_list(mp_state *state, mpv_node *node) { static void update_edition_list(mp_state *state, mpv_node *node) { if (state->edition_list != NULL) talloc_free(state->edition_list); - state->edition_list = talloc_ptrtype(state, state->edition_list); - memset(state->edition_list, 0, sizeof(*state->edition_list)); + state->edition_list = talloc_zero(state, mp_edition_list); mp_edition_list *list = state->edition_list; for (int i = 0; i < node->u.list->num; i++) { mpv_node edition = node->u.list->values[i]; - mp_edition_item item; - memset(&item, 0, sizeof(item)); + mp_edition_item item = {0}; for (int j = 0; j < edition.u.list->num; j++) { char *key = edition.u.list->keys[j]; @@ -110,15 +104,13 @@ static void update_edition_list(mp_state *state, mpv_node *node) { static void update_audio_device_list(mp_state *state, mpv_node *node) { if (state->audio_device_list != NULL) talloc_free(state->audio_device_list); - state->audio_device_list = talloc_ptrtype(state, state->audio_device_list); - memset(state->audio_device_list, 0, sizeof(*state->audio_device_list)); + state->audio_device_list = talloc_zero(state, mp_audio_device_list); mp_audio_device_list *list = state->audio_device_list; for (int i = 0; i < node->u.list->num; i++) { mpv_node device = node->u.list->values[i]; - mp_audio_device item; - memset(&item, 0, sizeof(item)); + mp_audio_device item = {0}; for (int j = 0; j < device.u.list->num; j++) { char *key = device.u.list->keys[j];