Commit c35b9698 authored by Rémi Denis-Courmont's avatar Rémi Denis-Courmont

modules: do not copy strings

Strings are either string literals, or contained in the memory mapped
caches. Either way, there is no needs to allocate memory and copy them.
parent d90c880d
......@@ -67,10 +67,10 @@ struct module_config_t
unsigned b_safe:1; /* Safe in web plugins and playlists */
unsigned b_removed:1; /* Deprecated */
char *psz_type; /* Configuration subtype */
char *psz_name; /* Option name */
char *psz_text; /* Short comment on the configuration option */
char *psz_longtext; /* Long comment on the configuration option */
const char *psz_type; /* Configuration subtype */
const char *psz_name; /* Option name */
const char *psz_text; /* Short comment on the configuration option */
const char *psz_longtext; /* Long comment on the configuration option */
module_value_t value; /* Option value */
module_value_t orig;
......@@ -81,12 +81,12 @@ struct module_config_t
uint16_t list_count; /* Options list size */
union
{
char **psz; /* List of possible values for the option */
const char **psz; /* List of possible values for the option */
int *i;
vlc_string_list_cb psz_cb;
vlc_integer_list_cb i_cb;
} list;
char **list_text; /* Friendly names for list values */
const char **list_text; /* Friendly names for list values */
};
/*****************************************************************************
......
......@@ -304,7 +304,7 @@ audio_output_t *aout_New (vlc_object_t *parent)
if (likely(cfg != NULL))
for (unsigned i = 0; i < cfg->list_count; i++)
{
val.psz_string = cfg->list.psz[i];
val.psz_string = (char *)cfg->list.psz[i];
text.psz_string = vlc_gettext(cfg->list_text[i]);
var_Change (aout, "audio-replay-gain-mode", VLC_VAR_ADDCHOICE,
&val, &text);
......
......@@ -587,11 +587,6 @@ void config_Free (module_config_t *tab, size_t confsize)
{
module_config_t *p_item = &tab[j];
free( p_item->psz_type );
free( p_item->psz_name );
free( p_item->psz_text );
free( p_item->psz_longtext );
if (IsConfigIntegerType (p_item->i_type))
{
if (p_item->list_count)
......@@ -601,17 +596,10 @@ void config_Free (module_config_t *tab, size_t confsize)
if (IsConfigStringType (p_item->i_type))
{
free (p_item->value.psz);
free (p_item->orig.psz);
if (p_item->list_count)
{
for (size_t i = 0; i < p_item->list_count; i++)
free (p_item->list.psz[i]);
free (p_item->list.psz);
}
}
for (size_t i = 0; i < p_item->list_count; i++)
free (p_item->list_text[i]);
free (p_item->list_text);
}
......
......@@ -675,7 +675,7 @@ static void ListModules (vlc_object_t *p_this, bool b_verbose)
if( b_verbose )
{
char *const *pp_shortcuts = p_parser->pp_shortcuts;
const char *const *pp_shortcuts = p_parser->pp_shortcuts;
for( unsigned i = 0; i < p_parser->i_shortcuts; i++ )
if( strcmp( pp_shortcuts[i], objname ) )
printf(color ? CYAN" s %s\n"GRAY : " s %s\n",
......
......@@ -100,7 +100,7 @@ static int vlc_cache_load_bool(bool *out, block_t *in)
return 0;
}
static int vlc_cache_load_string(char **restrict p, block_t *file)
static int vlc_cache_load_string(const char **restrict p, block_t *file)
{
uint16_t size;
......@@ -113,16 +113,13 @@ static int vlc_cache_load_string(char **restrict p, block_t *file)
return 0;
}
char *str = malloc(size);
if (unlikely(str == NULL))
return -1;
const char *str = (char *)file->p_buffer;
if (vlc_cache_load_immediate(str, file, size) || str[size - 1] != '\0')
{
free(str);
if (file->i_buffer < size || str[size - 1] != '\0')
return -1;
}
file->p_buffer += size;
file->i_buffer -= size;
*p = str;
return 0;
}
......@@ -181,11 +178,10 @@ static int CacheLoadConfig(module_config_t *cfg, block_t *file)
if (IsConfigStringType (cfg->i_type))
{
LOAD_STRING (cfg->orig.psz);
if (cfg->orig.psz != NULL)
cfg->value.psz = strdup (cfg->orig.psz);
else
cfg->value.psz = NULL;
const char *psz;
LOAD_STRING(psz);
cfg->orig.psz = (char *)psz;
cfg->value.psz = (psz != NULL) ? strdup (cfg->orig.psz) : NULL;
if (cfg->list_count)
cfg->list.psz = xmalloc (cfg->list_count * sizeof (char *));
......@@ -411,7 +407,6 @@ size_t CacheLoad(vlc_object_t *p_this, const char *dir, module_cache_t **r,
module_cache_t *cache = NULL;
size_t count = 0;
char *path = NULL;
for (;;)
{
......@@ -423,6 +418,7 @@ size_t CacheLoad(vlc_object_t *p_this, const char *dir, module_cache_t **r,
goto error;
}
const char *path;
struct stat st;
/* Load common info */
......@@ -433,8 +429,6 @@ size_t CacheLoad(vlc_object_t *p_this, const char *dir, module_cache_t **r,
LOAD_IMMEDIATE(st.st_size);
CacheAdd (&cache, &count, path, &st, module);
free (path);
path = NULL;
/* TODO: deal with errors */
}
......@@ -444,7 +438,6 @@ size_t CacheLoad(vlc_object_t *p_this, const char *dir, module_cache_t **r,
return count;
error:
free( path );
msg_Warn( p_this, "plugins cache not loaded (corrupted)" );
/* TODO: cleanup */
......
......@@ -35,11 +35,6 @@
#include "config/configuration.h"
#include "libvlc.h"
static char *strdup_null (const char *str)
{
return (str != NULL) ? strdup (str) : NULL;
}
module_t *vlc_module_create (module_t *parent)
{
module_t *module = malloc (sizeof (*module));
......@@ -99,16 +94,8 @@ void vlc_module_destroy (module_t *module)
}
config_Free (module->p_config, module->confsize);
free (module->domain);
free (module->psz_filename);
for (unsigned i = 0; i < module->i_shortcuts; i++)
free (module->pp_shortcuts[i]);
free (module->pp_shortcuts);
free (module->psz_capability);
free (module->psz_help);
free (module->psz_longname);
free (module->psz_shortname);
free (module);
}
......@@ -183,12 +170,12 @@ static int vlc_plugin_setter (void *plugin, void *tgt, int propid, ...)
}
/* Inheritance. Ugly!! */
submodule->pp_shortcuts = xmalloc (sizeof ( *submodule->pp_shortcuts ));
submodule->pp_shortcuts[0] = strdup_null (module->pp_shortcuts[0]);
submodule->pp_shortcuts[0] = module->pp_shortcuts[0];
submodule->i_shortcuts = 1; /* object name */
submodule->psz_shortname = strdup_null (module->psz_shortname);
submodule->psz_longname = strdup_null (module->psz_longname);
submodule->psz_capability = strdup_null (module->psz_capability);
submodule->psz_shortname = module->psz_shortname;
submodule->psz_longname = module->psz_longname;
submodule->psz_capability = module->psz_capability;
break;
}
......@@ -215,8 +202,8 @@ static int vlc_plugin_setter (void *plugin, void *tgt, int propid, ...)
assert(i_shortcuts + index <= MODULE_SHORTCUT_MAX);
const char *const *tab = va_arg (ap, const char *const *);
char **pp = realloc (module->pp_shortcuts,
sizeof (pp[0]) * (index + i_shortcuts));
const char **pp = realloc (module->pp_shortcuts,
sizeof (pp[0]) * (index + i_shortcuts));
if (unlikely(pp == NULL))
{
ret = -1;
......@@ -226,13 +213,12 @@ static int vlc_plugin_setter (void *plugin, void *tgt, int propid, ...)
module->i_shortcuts = index + i_shortcuts;
pp += index;
for (unsigned i = 0; i < i_shortcuts; i++)
pp[i] = strdup (tab[i]);
pp[i] = tab[i];
break;
}
case VLC_MODULE_CAPABILITY:
free (module->psz_capability);
module->psz_capability = strdup (va_arg (ap, char *));
module->psz_capability = va_arg (ap, const char *);
break;
case VLC_MODULE_SCORE:
......@@ -258,36 +244,34 @@ static int vlc_plugin_setter (void *plugin, void *tgt, int propid, ...)
assert (module->i_shortcuts == 0);
module->pp_shortcuts = malloc( sizeof( *module->pp_shortcuts ) );
module->pp_shortcuts[0] = strdup (value);
module->pp_shortcuts[0] = value;
module->i_shortcuts = 1;
assert (module->psz_longname == NULL);
module->psz_longname = strdup (value);
module->psz_longname = value;
break;
}
case VLC_MODULE_SHORTNAME:
assert (module->psz_shortname == NULL || module->parent != NULL);
free (module->psz_shortname);
module->psz_shortname = strdup (va_arg (ap, char *));
module->psz_shortname = va_arg (ap, const char *);
break;
case VLC_MODULE_DESCRIPTION:
// TODO: do not set this in VLC_MODULE_NAME
free (module->psz_longname);
module->psz_longname = strdup (va_arg (ap, char *));
module->psz_longname = va_arg (ap, const char *);
break;
case VLC_MODULE_HELP:
assert (module->parent == NULL);
assert (module->psz_help == NULL);
module->psz_help = strdup (va_arg (ap, char *));
module->psz_help = va_arg (ap, const char *);
break;
case VLC_MODULE_TEXTDOMAIN:
assert (module->parent == NULL);
assert (module->domain == NULL);
module->domain = strdup (va_arg (ap, char *));
module->domain = va_arg (ap, const char *);
break;
case VLC_CONFIG_NAME:
......@@ -295,7 +279,7 @@ static int vlc_plugin_setter (void *plugin, void *tgt, int propid, ...)
const char *name = va_arg (ap, const char *);
assert (name != NULL);
item->psz_name = strdup (name);
item->psz_name = name;
break;
}
......@@ -318,7 +302,7 @@ static int vlc_plugin_setter (void *plugin, void *tgt, int propid, ...)
{
const char *value = va_arg (ap, const char *);
item->value.psz = value ? strdup (value) : NULL;
item->orig.psz = value ? strdup (value) : NULL;
item->orig.psz = (char *)value;
}
break;
}
......@@ -355,11 +339,8 @@ static int vlc_plugin_setter (void *plugin, void *tgt, int propid, ...)
break;
case VLC_CONFIG_CAPABILITY:
{
const char *cap = va_arg (ap, const char *);
item->psz_type = cap ? strdup (cap) : NULL;
item->psz_type = va_arg (ap, const char *);
break;
}
case VLC_CONFIG_SHORTCUT:
item->i_short = va_arg (ap, int);
......@@ -370,14 +351,9 @@ static int vlc_plugin_setter (void *plugin, void *tgt, int propid, ...)
break;
case VLC_CONFIG_DESC:
{
const char *text = va_arg (ap, const char *);
const char *longtext = va_arg (ap, const char *);
item->psz_text = text ? strdup (text) : NULL;
item->psz_longtext = longtext ? strdup (longtext) : NULL;
item->psz_text = va_arg (ap, const char *);
item->psz_longtext = va_arg (ap, const char *);
break;
}
case VLC_CONFIG_LIST:
{
......@@ -400,20 +376,20 @@ static int vlc_plugin_setter (void *plugin, void *tgt, int propid, ...)
if (IsConfigStringType (item->i_type))
{
const char *const *src = va_arg (ap, const char *const *);
char **dst = xmalloc (sizeof (char *) * len);
const char **dst = xmalloc (sizeof (const char *) * len);
for (size_t i = 0; i < len; i++)
dst[i] = src[i] ? strdup (src[i]) : NULL;
memcpy(dst, src, sizeof (const char *) * len);
item->list.psz = dst;
}
else
break;
/* Copy textual descriptions */
/* XXX: item->list_text[len + 1] is probably useless. */
const char *const *text = va_arg (ap, const char *const *);
char **dtext = xmalloc (sizeof (char *) * (len + 1));
for (size_t i = 0; i < len; i++)
dtext[i] = text[i] ? strdup (text[i]) : NULL;
const char **dtext = xmalloc (sizeof (const char *) * (len + 1));
memcpy(dtext, text, sizeof (const char *) * len);
item->list_text = dtext;
item->list_count = len;
break;
......
......@@ -64,16 +64,16 @@ struct module_t
/** Shortcuts to the module */
unsigned i_shortcuts;
char **pp_shortcuts;
const char **pp_shortcuts;
/*
* Variables set by the module to identify itself
*/
char *psz_shortname; /**< Module name */
char *psz_longname; /**< Module descriptive name */
char *psz_help; /**< Long help string for "special" modules */
const char *psz_shortname; /**< Module name */
const char *psz_longname; /**< Module descriptive name */
const char *psz_help; /**< Long help string for "special" modules */
char *psz_capability; /**< Capability */
const char *psz_capability; /**< Capability */
int i_score; /**< Score for the capability */
bool b_loaded; /* Set to true if the dll is loaded */
......@@ -97,7 +97,7 @@ struct module_t
/* Plugin-specific stuff */
module_handle_t handle; /* Unique handle */
char * psz_filename; /* Module filename */
char * domain; /* gettext domain */
const char * domain; /* gettext domain */
};
module_t *vlc_plugin_describe (vlc_plugin_cb);
......
......@@ -202,7 +202,7 @@ void vout_InitInterlacingSupport(vout_thread_t *vout, bool is_interlaced)
if (!DeinterlaceIsModeValid(optm->list.psz[i]))
continue;
val.psz_string = optm->list.psz[i];
val.psz_string = (char *)optm->list.psz[i];
text.psz_string = vlc_gettext(optm->list_text[i]);
var_Change(vout, "deinterlace-mode", VLC_VAR_ADDCHOICE,
&val, &text);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment