Stringshare

Typedefs | Functions
Stringshare
Eina » Data Types

These functions allow you to store a single copy of a string, and use in multiple places throughout your program. More...

## Typedefs
typedef const char | Eina_Stringshare
| "Eina_Stringshare *" is interchangeable with "const char *" but still a good visual hint for the purpose. More...
typedef const char | Eina_Tmpstr
| Interchangeable with "const char *" but still a good visual hint for the purpose. More...

## Functions
EAPI Eina_Stringshare * | eina_stringshare_add_length (const char *str, unsigned int slen) EINA_WARN_UNUSED_RESULT
| Retrieve an instance of a string with a specific size for use in a program. More...
EAPI Eina_Stringshare * | eina_stringshare_add (const char *str) EINA_WARN_UNUSED_RESULT
| Retrieve an instance of a string for use in a program. More...
EAPI Eina_Stringshare * | eina_stringshare_printf (const char *fmt,...) EINA_WARN_UNUSED_RESULT EINA_PRINTF(1
| Retrieve an instance of a string for use in a program from a format string. More...
EAPI Eina_Stringshare EAPI Eina_Stringshare * | eina_stringshare_vprintf (const char *fmt, va_list args) EINA_WARN_UNUSED_RESULT
| Retrieve an instance of a string for use in a program from a format string. More...
EAPI Eina_Stringshare * | eina_stringshare_nprintf (unsigned int len, const char *fmt,...) EINA_WARN_UNUSED_RESULT EINA_PRINTF(2
| Retrieve an instance of a string for use in a program from a format string with size limitation. More...
EAPI Eina_Stringshare EAPI Eina_Stringshare * | eina_stringshare_ref (Eina_Stringshare *str)
| Increment references of the given shared string. More...
EAPI void | eina_stringshare_del (Eina_Stringshare *str)
| Note that the given string has lost an instance. More...
EAPI int | eina_stringshare_strlen (Eina_Stringshare *str) EINA_PURE EINA_WARN_UNUSED_RESULT
| Note that the given string must be shared. More...
EAPI void | eina_stringshare_dump (void)
| Dump the contents of the share_common. More...
static Eina_Bool | eina_stringshare_replace (Eina_Stringshare **p_str, const char *news) EINA_ARG_NONNULL(1)
static Eina_Bool | eina_stringshare_replace_length (Eina_Stringshare **p_str, const char *news, unsigned int slen) EINA_ARG_NONNULL(1)
EAPI Eina_Tmpstr * | eina_tmpstr_add (const char *str) EINA_WARN_UNUSED_RESULT
| Add a new temporary string based on the input string. More...
EAPI Eina_Tmpstr * | eina_tmpstr_add_length (const char *str, size_t length)
| Add a new temporary string based on the input string and length. More...
EINA_DEPRECATED EAPI size_t | eina_tmpstr_strlen (Eina_Tmpstr *tmpstr)
| Deprecated Return the length of a temporary string including the '\0'. More...
EAPI size_t | eina_tmpstr_len (Eina_Tmpstr *tmpstr)
| Return the length of a temporary string. More...
EAPI void | eina_tmpstr_del (Eina_Tmpstr *tmpstr) EINA_ARG_NONNULL(1)
| Delete the temporary string if it is one, or ignore it if it is not. More...
EAPI Eina_Tmpstr * | eina_tmpstr_manage_new (char *str) EINA_WARN_UNUSED_RESULT
| Add a new temporary string using the passed string. More...
EAPI Eina_Tmpstr * | eina_tmpstr_manage_new_length (char *str, size_t length)
| Add a new temporary string using the passed string. More...

## Detailed Description

These functions allow you to store a single copy of a string, and use in multiple places throughout your program.

This is a method to reduce the number of duplicated strings kept in memory. It's pretty common for the same strings to be dynamically allocated repeatedly between applications and libraries, especially in circumstances where you could have multiple copies of a structure that allocates the string. So rather than duplicating and freeing these strings, you request a read-only pointer to an existing string and only incur the overhead of a hash lookup.

It sounds like micro-optimizing, but profiling has shown this can have a significant impact as you scale the number of copies up. It improves string creation/destruction speed, reduces memory use and decreases memory fragmentation, so a win all-around.

Using eina stringshares usually boils down to:

const char *str = eina_stringshare_add("My string");
...
//Use str
...
eina_stringshare_del(str);

Note
  It's very important to note that string shares are `const`, changing them will result in undefined behavior.
  eina_stringshare_del() doesn't guarantee the string share will be freed, it releases a reference to it, but if other references to it still exist the string share will live until those are released.

The following diagram gives an idea of what happens as you create strings with eina_stringshare_add():

For more information, see this example.

## Typedef Documentation

## ◆ Eina_Stringshare

Eina_Stringshare

"Eina_Stringshare *" is interchangeable with "const char *" but still a good visual hint for the purpose.

Maybe in the far far future we'll even add strict type checking.

Since
  1.2.0

## ◆ Eina_Tmpstr

Eina_Tmpstr

Interchangeable with "const char *" but still a good visual hint for the purpose.

This indicates the string is temporary and should be freed after use.

Since
  1.8.0

## Function Documentation

## ◆ eina_stringshare_add_length()

EAPI Eina_Stringshare* eina_stringshare_add_length | ( | const char * | str,
|  | unsigned int | slen
| ) |  |

Retrieve an instance of a string with a specific size for use in a program.

Parameters

  str | The string to retrieve an instance of.
  slen | The string size (<= strlen(str)).

Returns
  A pointer to an instance of the string on success. `NULL` on failure.

This function retrieves an instance of `str`. If `str` is `NULL`, then `NULL` is returned. If `str` is already stored, it is just returned and its reference counter is increased. Otherwise a duplicated string of `str` is returned.

This function does not check string size, but uses the exact given size. This can be used to share_common part of a larger buffer or substring.

See also
  eina_share_common_add()

References eina_spinlock_release(), and eina_spinlock_take().

Referenced by eet_open(), efreet_icon_deprecated_user_dir_get(), efreet_icon_user_dir_get(), eina_stringshare_add(), eina_stringshare_vprintf(), and evas_textblock_cursor_text_prepend().

## ◆ eina_stringshare_add()

EAPI Eina_Stringshare* eina_stringshare_add | ( | const char * | str | ) |

Retrieve an instance of a string for use in a program.

Parameters

  str | The NULL-terminated string to retrieve an instance of.

Returns
  A pointer to an instance of the string on success. `NULL` on failure.

This function retrieves an instance of `str`. If `str` is `NULL`, then `NULL` is returned. If `str` is already stored, it is just returned and its reference counter is increased. Otherwise a duplicated string of `str` is returned.

The string `str` must be NULL terminated ('\0') and its full length will be used. To use part of the string or non-null terminated, use eina_stringshare_add_length() instead.

See also
  eina_stringshare_add_length()

References eina_stringshare_add_length().

Referenced by ecore_con_socks4_remote_add(), ecore_con_socks5_remote_add(), ecore_drm_device_find(), ecore_evas_aux_hint_add(), ecore_evas_aux_hint_val_set(), edje_color_class_active_iterator_new(), edje_color_class_set(), edje_edit_color_class_add(), edje_edit_color_classes_list_get(), edje_edit_compiler_get(), edje_edit_data_add(), edje_edit_data_list_get(), edje_edit_data_value_get(), edje_edit_external_add(), edje_edit_externals_list_get(), edje_edit_font_path_get(), edje_edit_fonts_list_get(), edje_edit_group_add(), edje_edit_group_alias_add(), edje_edit_group_aliased_get(), edje_edit_group_aliases_get(), edje_edit_group_copy(), edje_edit_group_data_add(), edje_edit_group_data_list_get(), edje_edit_group_data_value_get(), edje_edit_group_name_set(), edje_edit_image_add(), edje_edit_images_list_get(), edje_edit_part_above_get(), edje_edit_part_below_get(), edje_edit_part_copy(), edje_edit_part_item_append(), edje_edit_part_item_source_get(), edje_edit_part_item_source_set(), edje_edit_part_items_list_get(), edje_edit_part_name_set(), edje_edit_part_selected_state_get(), edje_edit_part_selected_state_set(), edje_edit_part_source_get(), edje_edit_parts_list_get(), edje_edit_program_action_set(), edje_edit_program_add(), edje_edit_program_api_description_get(), edje_edit_program_api_name_get(), edje_edit_program_filter_part_get(), edje_edit_program_filter_state_get(), edje_edit_program_sample_name_get(), edje_edit_program_signal_get(), edje_edit_program_source_get(), edje_edit_program_state2_get(), edje_edit_program_state_get(), edje_edit_program_target_del(), edje_edit_program_tone_name_get(), edje_edit_programs_list_get(), edje_edit_script_program_set(), edje_edit_sound_samples_list_get(), edje_edit_sound_samplesource_get(), edje_edit_sound_tone_add(), edje_edit_sound_tones_list_get(), edje_edit_source_generate(), edje_edit_state_add(), edje_edit_state_color_class_get(), edje_edit_state_color_class_set(), edje_edit_state_exist(), edje_edit_state_external_param_set(), edje_edit_state_font_get(), edje_edit_state_image_get(), edje_edit_state_map_light_get(), edje_edit_state_map_perspective_get(), edje_edit_state_map_rotation_center_get(), edje_edit_state_proxy_source_get(), edje_edit_state_text_class_get(), edje_edit_state_text_class_set(), edje_edit_state_text_get(), edje_edit_state_text_repch_get(), edje_edit_state_text_source_get(), edje_edit_state_text_source_set(), edje_edit_state_text_style_get(), edje_edit_state_text_style_set(), edje_edit_state_text_text_source_get(), edje_edit_state_text_text_source_set(), edje_edit_state_tweens_list_get(), edje_edit_style_add(), edje_edit_style_tag_add(), edje_edit_style_tag_value_get(), edje_edit_style_tags_list_get(), edje_edit_styles_list_get(), edje_init(), edje_mmap_collection_list(), edje_mmap_size_class_iterator_new(), edje_size_class_active_iterator_new(), edje_size_class_set(), edje_text_class_active_iterator_new(), edje_text_class_set(), edje_transition_duration_factor_set(), eet_alias_get(), eet_data_write(), eet_dictionary_count(), eet_eina_stream_data_descriptor_class_set(), eet_mmap(), eet_node_hash_new(), eet_node_list_append(), eet_node_struct_append(), eeze_net_new(), eeze_udev_devpath_get_syspath(), eeze_udev_find_by_filter(), eeze_udev_find_by_subsystem_sysname(), eeze_udev_find_by_sysattr(), eeze_udev_find_by_type(), eeze_udev_find_similar_from_syspath(), eeze_udev_syspath_get_devname(), eeze_udev_syspath_get_devpath(), eeze_udev_syspath_get_parent(), eeze_udev_syspath_get_parents(), eeze_udev_syspath_get_property(), eeze_udev_syspath_get_subsystem(), eeze_udev_syspath_get_sysattr(), eeze_udev_syspath_get_sysattr_list(), eeze_udev_walk_get_sysattr(), efreet_desktop_string_list_join(), efreet_desktop_type_add(), efreet_icon_free(), efreet_menu_async_parse(), efreet_menu_file_set(), efreet_menu_unref(), eina_simple_xml_attribute_new(), elua_state_new(), eo_base_legacy_only_event_description_get(), EO_DBG_INFO_LIST_APPEND(), evas_font_path_global_append(), evas_font_path_global_prepend(), evas_object_image_extension_can_load_get(), evas_object_textblock_text_markup_prepend(), evas_textblock_cursor_text_prepend(), evas_textblock_string_escape_get(), and efl::eina::stringshare::stringshare().

## ◆ eina_stringshare_printf()

EAPI Eina_Stringshare* eina_stringshare_printf | ( | const char * | fmt,
|  |  | ...
| ) |  |

Retrieve an instance of a string for use in a program from a format string.

Parameters

  fmt | The NULL-terminated format string to retrieve an instance of.

Returns
  A pointer to an instance of the string on success. `NULL` on failure.

This function retrieves an instance of `fmt`. If `fmt` is `NULL`, then `NULL` is returned. If `fmt` is already stored, it is just returned and its reference counter is increased. Otherwise a duplicated string is returned.

The format string `fmt` must be NULL-terminated ('\0') and its full length will be used. To use part of the format string or non-null terminated, use eina_stringshare_nprintf() instead.

See also
  eina_stringshare_nprintf()

Examples:
  eina_stringshare_01.c.

Referenced by edje_edit_script_program_set().

## ◆ eina_stringshare_vprintf()

EAPI Eina_Stringshare EAPI Eina_Stringshare* eina_stringshare_vprintf | ( | const char * | fmt,
|  | va_list | args
| ) |  |

Retrieve an instance of a string for use in a program from a format string.

Parameters

  fmt | The NULL-terminated format string to retrieve an instance of.
  args | The va_args for `fmt`

Returns
  A pointer to an instance of the string on success. `NULL` on failure.

This function retrieves an instance of `fmt` with `args`. If `fmt` is `NULL`, then `NULL` is returned. If `fmt` with `args` is already stored, it is just returned and its reference counter is increased. Otherwise a duplicated string is returned.

The format string `fmt` must be NULL-terminated ('\0') and its full length will be used. To use part of the format string or non-null terminated, use eina_stringshare_nprintf() instead.

See also
  eina_stringshare_nprintf()

References eina_stringshare_add_length().

## ◆ eina_stringshare_nprintf()

EAPI Eina_Stringshare* eina_stringshare_nprintf | ( | unsigned int | len,
|  | const char * | fmt,
|  |  | ...
| ) |  |

Retrieve an instance of a string for use in a program from a format string with size limitation.

Parameters

  len | The length of the format string to use
  fmt | The format string to retrieve an instance of.

Returns
  A pointer to an instance of the string on success. `NULL` on failure.

This function retrieves an instance of `fmt` limited by `len`. If `fmt` is `NULL` or `len` is < 1, then `NULL` is returned. If the resulting string is already stored, it is returned and its reference counter is increased. Otherwise a duplicated string is returned.

`len` length of the format string will be used. To use the entire format string, use eina_stringshare_printf() instead.

See also
  eina_stringshare_printf()

Examples:
  eina_stringshare_01.c.

## ◆ eina_stringshare_ref()

EAPI Eina_Stringshare EAPI Eina_Stringshare* eina_stringshare_ref | ( | Eina_Stringshare * | str | ) |

Increment references of the given shared string.

Parameters

  str | The shared string.

Returns
  A pointer to an instance of the string on success. `NULL` on failure.

This is similar to eina_share_common_add(), but it's faster since it will avoid lookups if possible, but on the down side it requires the parameter to be shared string. In other words, it must be the return of a previous call to one of the stringshare functions.

There is no unref since this is the work of eina_share_common_del().

References eina_spinlock_release(), and eina_spinlock_take().

Referenced by eo_dbg_info_free(), and ethumb_dup().

## ◆ eina_stringshare_del()

EAPI void eina_stringshare_del | ( | Eina_Stringshare * | str | ) |

Note that the given string has lost an instance.

Parameters

  str | string The given string.

This function decreases the reference counter associated to `str` if it exists. If that counter reaches 0, the memory associated to `str` is freed. If `str` is `NULL`, the function returns immediately.

Note
  If the given pointer is not shared, bad things will happen, likely a segmentation fault.

References eina_spinlock_release(), and eina_spinlock_take().

Referenced by ecore_con_ssl_client_upgrade(), ecore_drm_device_find(), ecore_evas_aux_hint_add(), ecore_evas_aux_hint_del(), ecore_evas_aux_hint_val_set(), edje_available_modules_get(), edje_color_class_active_iterator_new(), edje_color_class_del(), edje_edit_data_add(), edje_edit_group_data_add(), edje_edit_image_add(), edje_edit_image_usage_list_free(), edje_edit_limits_list_free(), edje_edit_part_source_get(), edje_edit_state_external_param_set(), edje_edit_string_free(), edje_edit_string_list_free(), edje_evas_global_perspective_get(), edje_external_type_get(), edje_file_collection_list_free(), edje_mmap_size_class_iterator_new(), edje_shutdown(), edje_size_class_del(), edje_text_class_del(), edje_transition_duration_factor_set(), eet_clearcache(), eet_eina_stream_data_descriptor_class_set(), eet_node_del(), eet_node_list_append(), eet_node_struct_append(), eeze_net_new(), efreet_desktop_free(), efreet_desktop_string_list_join(), eina_simple_xml_attribute_free(), elua_state_free(), elua_state_setup(), eo_base_legacy_only_event_description_get(), eo_dbg_info_free(), evas_device_emulation_source_get(), evas_font_path_global_clear(), evas_object_image_extension_can_load_get(), evas_render_method_list_free(), evas_textblock_cursor_copy(), evas_textblock_cursor_text_prepend(), evas_textblock_string_escape_get(), evas_textblock_style_get(), and efl::eina::stringshare::~stringshare().

## ◆ eina_stringshare_strlen()

EAPI int eina_stringshare_strlen | ( | Eina_Stringshare * | str | ) |

Note that the given string must be shared.

Parameters

  str | the shared string to know the length. It is safe to give `NULL`, in that case `0` is returned.

Returns
  The length of a shared string.

This function is a cheap way to known the length of a shared string.

Note
  If the given pointer is not shared, bad things will happen, likely a segmentation fault. If in doubt, try strlen().

Referenced by evas_textblock_style_get(), and efl::eina::stringshare::size().

## ◆ eina_stringshare_dump()

EAPI void eina_stringshare_dump | ( | void |  | ) |

Dump the contents of the share_common.

This function dumps all strings in the share_common to stdout with a DDD: prefix per line and a memory usage summary.

## ◆ eina_tmpstr_add()

EAPI Eina_Tmpstr* eina_tmpstr_add | ( | const char * | str | ) |

Add a new temporary string based on the input string.

Parameters

  str | This is the input stringthat is copied into the temp string.

Returns
  A pointer to the tmp string that is a standard C string.

When you add a temporary string (tmpstr) it is expected to have a very short lifespan, and at any one time only a few of these are intended to exist. This is not intended for longer term storage of strings. The intended use is the ability to safely pass strings as return values from functions directly into parameters of new functions and then have the string be cleaned up automatically by the caller.

If `str` is NULL, or no memory space exists to store the tmpstr, then NULL will be returned, otherwise a valid string pointer will be returned that you can treat as any other C string (eg strdup(tmpstr) or printf("%s\n", tmpstr) etc.). This string should be considered read-only and immutable, and when youa re done with the string yo should delete it with eina_tmpstr_del().

Example usage:

Eina_Tmpstr *my_homedir(void) {
return eina_tmpstr_add(eina_environment_home_get());
}
void my_rmfile(Eina_Tmpstr *str) {
if (!str) return;
unlink(str);
eina_tmpstr_del(str);
}
my_rmfile(my_homedir());
my_rmfile("/tmp/file");

See also
  eina_tmpstr_del()
  eina_tmpstr_add_length()

Since
  1.8.0

References eina_tmpstr_add_length().

## ◆ eina_tmpstr_add_length()

EAPI Eina_Tmpstr* eina_tmpstr_add_length | ( | const char * | str,
|  | size_t | length
| ) |  |

Add a new temporary string based on the input string and length.

Parameters

  str | This is the input string that is copied into the temp string.
  length | This is the maximum length and the allocated length of the temp string.

Returns
  A pointer to the tmp string that is a standard C string.

When you add a temporary string (tmpstr) it is expected to have a very short lifespan, and at any one time only a few of these are intended to exist. This is not intended for longer term storage of strings. The intended use is the ability to safely pass strings as return values from functions directly into parameters of new functions and then have the string be cleaned up automatically by the caller.

If `str` is NULL, or no memory space exists to store the tmpstr, then NULL will be returned, otherwise a valid string pointer will be returned that you can treat as any other C string (eg strdup(tmpstr) or printf("%s\n", tmpstr) etc.). This string should be considered read-only and immutable, and when you are done with the string you should delete it with eina_tmpstr_del().

Note
  If the length is greater than the actual string, but still '\0' terminated, there won't be any crash and the string will be correct, but eina_tmpstr_len will return an erroneous length. So if you want to have the correct length always call eina_tmpstr_add_length with length == strlen(str).

See also
  eina_tmpstr_del()
  eina_tmpstr_add()

Since
  1.8.0

References EINA_FALSE, eina_lock_release(), and eina_lock_take().

Referenced by eina_file_current_directory_get(), and eina_tmpstr_add().

## ◆ eina_tmpstr_strlen()

EINA_DEPRECATED EAPI size_t eina_tmpstr_strlen | ( | Eina_Tmpstr * | tmpstr | ) |

Deprecated Return the length of a temporary string including the '\0'.

Parameters

  tmpstr | This is any C string pointer, but if it is a tmp string it will return the length faster.

Returns
  The length of the string including the '\0';

Deprecated:

See also
  eina_tmpstr_len()

Since
  1.8.0

References eina_tmpstr_len().

## ◆ eina_tmpstr_len()

EAPI size_t eina_tmpstr_len | ( | Eina_Tmpstr * | tmpstr | ) |

Return the length of a temporary string.

Parameters

  tmpstr | This is any C string pointer, but if it is a tmp string it will return the length faster.

Returns
  The length of the string.

Since
  1.14.0

References eina_lock_release(), and eina_lock_take().

Referenced by eina_file_path_sanitize(), and eina_tmpstr_strlen().

## ◆ eina_tmpstr_del()

EAPI void eina_tmpstr_del | ( | Eina_Tmpstr * | tmpstr | ) |

Delete the temporary string if it is one, or ignore it if it is not.

Parameters

  tmpstr | This is any C string pointer, but if it is a tmp string it is freed.

This will delete the given temporary string `tmpstr` if it is a valid temporary string, or otherwise it will ignore it and do nothing so this can be used safely with non-temporary strings.

See also
  eina_tmpstr_add()

Since
  1.8.0

References eina_lock_release(), and eina_lock_take().

Referenced by eina_file_cleanup(), and eina_file_path_sanitize().

## ◆ eina_tmpstr_manage_new()

EAPI Eina_Tmpstr* eina_tmpstr_manage_new | ( | char * | str | ) |

Add a new temporary string using the passed string.

The passed string is used directly as the buffer. The passed string must be malloced.

Parameters

  str | the input string to manage.

Returns
  A pointer to the tmp string that is a standard C string.

This function creates a new temporary string. On error, `NULL` is returned. To free the resources, use eina_tmpstr_del().

See also
  eina_tmpstr_del()

Since
  1.17.0

References eina_tmpstr_manage_new_length().

## ◆ eina_tmpstr_manage_new_length()

EAPI Eina_Tmpstr* eina_tmpstr_manage_new_length | ( | char * | str,
|  | size_t | length
| ) |  |

Add a new temporary string using the passed string.

The passed string is used directly as the buffer. The passed string must be malloced.

Parameters

  str | the input string to manage.
  length | the length of the string.

Returns
  A pointer to the tmp string that is a standard C string.

This function creates a new temporary string. On error, `NULL` is returned. To free the resources, use eina_tmpstr_del().

See also
  eina_tmpstr_manage_new()
  eina_tmpstr_del()

Since
  1.17.0

References eina_lock_release(), eina_lock_take(), and EINA_TRUE.

Referenced by eina_tmpstr_manage_new().