[MPlayer-cvslog] r37835 - in trunk: gui/interface.c gui/ui/actions.c input/input.c input/input.h

reimar subversion at mplayerhq.hu
Sun Mar 6 14:00:51 CET 2016


Author: reimar
Date: Sun Mar  6 14:00:51 2016
New Revision: 37835

Log:
input.c: optimize mp_cmd list.

Reduce its size and encode strings directly
to avoid relocations.

Modified:
   trunk/gui/interface.c
   trunk/gui/ui/actions.c
   trunk/input/input.c
   trunk/input/input.h

Modified: trunk/gui/interface.c
==============================================================================
--- trunk/gui/interface.c	Sun Mar  6 14:00:49 2016	(r37834)
+++ trunk/gui/interface.c	Sun Mar  6 14:00:51 2016	(r37835)
@@ -1262,7 +1262,7 @@ void mplayer(int what, float value, void
 
         mp_cmd       = calloc(1, sizeof(*mp_cmd));
         mp_cmd->id   = MP_CMD_PANSCAN;
-        mp_cmd->name = strdup("panscan");
+        ARRAY_STRCPY(mp_cmd->name, "panscan");
         mp_cmd->args[0].v.f = value;
         mp_cmd->args[1].v.i = 1;
         mp_input_queue_cmd(mp_cmd);

Modified: trunk/gui/ui/actions.c
==============================================================================
--- trunk/gui/ui/actions.c	Sun Mar  6 14:00:49 2016	(r37834)
+++ trunk/gui/ui/actions.c	Sun Mar  6 14:00:51 2016	(r37835)
@@ -578,7 +578,7 @@ void uiPause(void)
 
         if (cmd) {
             cmd->id   = MP_CMD_PAUSE;
-            cmd->name = strdup("pause");
+            ARRAY_STRCPY(cmd->name, "pause");
             mp_input_queue_cmd(cmd);
         }
     } else

Modified: trunk/input/input.c
==============================================================================
--- trunk/input/input.c	Sun Mar  6 14:00:49 2016	(r37834)
+++ trunk/input/input.c	Sun Mar  6 14:00:51 2016	(r37835)
@@ -212,7 +212,7 @@ static const mp_cmd_t mp_cmds[] = {
 
   { MP_CMD_GUI, "gui", 1, { {MP_CMD_ARG_STRING, {0}}, {-1,{0}} } },
 
-  { 0, NULL, 0, {} }
+  { 0, "", 0, {} }
 };
 
 /// The names of the keys as used in input.conf
@@ -819,19 +819,19 @@ mp_input_parse_cmd(char* str) {
   if(l == 0)
     return NULL;
 
-  for(i=0; mp_cmds[i].name != NULL; i++) {
+  for(i=0; mp_cmds[i].name[0]; i++) {
     if(strncasecmp(mp_cmds[i].name,str,l) == 0)
       break;
   }
 
-  if(mp_cmds[i].name == NULL)
+  if(!mp_cmds[i].name[0])
     return NULL;
 
   cmd_def = &mp_cmds[i];
 
   cmd = calloc(1, sizeof(mp_cmd_t));
   cmd->id = cmd_def->id;
-  cmd->name = strdup(cmd_def->name);
+  memcpy(cmd->name, cmd_def->name, sizeof(cmd->name));
   if (pausing == -1) {
     switch (cmd->id) {
       case MP_CMD_KEYDOWN_EVENTS:
@@ -1434,8 +1434,6 @@ mp_cmd_free(mp_cmd_t* cmd) {
 //#endif
   if ( !cmd ) return;
 
-  free(cmd->name);
-
   for(i=0; i < MP_CMD_MAX_ARGS && cmd->args[i].type != -1; i++) {
     if(cmd->args[i].type == MP_CMD_ARG_STRING)
       free(cmd->args[i].v.s);
@@ -1453,8 +1451,6 @@ mp_cmd_clone(mp_cmd_t* cmd) {
 
   ret = malloc(sizeof(mp_cmd_t));
   memcpy(ret,cmd,sizeof(mp_cmd_t));
-  if(cmd->name)
-    ret->name = strdup(cmd->name);
   for(i = 0;  i < MP_CMD_MAX_ARGS && cmd->args[i].type != -1; i++) {
     if(cmd->args[i].type == MP_CMD_ARG_STRING && cmd->args[i].v.s != NULL)
       ret->args[i].v.s = strdup(cmd->args[i].v.s);
@@ -1873,7 +1869,7 @@ static int mp_input_print_cmd_list(m_opt
   int i,j;
   const char* type;
 
-  for(i = 0; (cmd = &mp_cmds[i])->name != NULL ; i++) {
+  for(i = 0; (cmd = &mp_cmds[i])->name[0] ; i++) {
     printf("%-20.20s",cmd->name);
     for(j= 0 ; j < MP_CMD_MAX_ARGS && cmd->args[j].type != -1 ; j++) {
       switch(cmd->args[j].type) {

Modified: trunk/input/input.h
==============================================================================
--- trunk/input/input.h	Sun Mar  6 14:00:49 2016	(r37834)
+++ trunk/input/input.h	Sun Mar  6 14:00:51 2016	(r37835)
@@ -174,7 +174,7 @@ typedef enum {
 #define MP_CMD_ARG_VOID 3
 
 #ifndef MP_CMD_MAX_ARGS
-#define MP_CMD_MAX_ARGS 10
+#define MP_CMD_MAX_ARGS 6
 #endif
 
 // Error codes for the drivers
@@ -201,6 +201,20 @@ typedef enum {
 #define MP_MAX_KEY_DOWN 32
 #endif
 
+#if (defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__ >= 460)) || \
+    (defined(__clang__) && (__clang_major__ * 100 + __clang_minor__ >= 306))
+// The type checks are pretty horrible but the only reliable method,
+// checking type of d is char[sizeof(d)] doesn't work in gcc for example
+#define ARRAY_STRCPY(d, s) do { \
+    _Static_assert(sizeof(d) >= sizeof(s), "Destination array too small!"); \
+    _Static_assert(_Generic(&(d), char(*)[]:1, default:0), "Destination not an array!"); \
+    _Static_assert(_Generic(&(s), char(*)[]:1, default:0), "Source not an array!"); \
+    memcpy(d, s, sizeof(s)); \
+} while(0)
+#else
+#define ARRAY_STRCPY(d, s) memcpy(d, s, sizeof(s))
+#endif
+
 typedef union mp_cmd_arg_value {
   int i;
   float f;
@@ -215,7 +229,7 @@ typedef struct mp_cmd_arg {
 
 typedef struct mp_cmd {
   int id;
-  char* name;
+  char name[24];
   int nargs;
   mp_cmd_arg_t args[MP_CMD_MAX_ARGS];
   int pausing;


More information about the MPlayer-cvslog mailing list