[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

scm_take_locale_stringn using realloc

From: Kevin Ryde
Subject: scm_take_locale_stringn using realloc
Date: Sat, 11 Jun 2005 08:12:25 +1000
User-agent: Gnus/5.110004 (No Gnus v0.4) Emacs/21.3 (gnu/linux)

In scm_take_locale_stringn, how about using realloc to add a null
terminator to the block being acquired?  That should usually be better
than the current code doing malloc+copy+free.

Something like the following,

scm_take_locale_stringn (char *str, size_t len)
  SCM buf, res;

  if (len == (size_t)-1)
    len = strlen (str);
      /* Ensure STR is null terminated.  A realloc for 1 extra byte should
         often be satisfied from the alignment padding after the block, with
         no actual data movement.  */
      str = scm_realloc (str, len+1);
      str[len] = '\0';

  buf = scm_double_cell (STRINGBUF_TAG, (scm_t_bits) str,
                         (scm_t_bits) len, (scm_t_bits) 0);
  res = scm_double_cell (STRING_TAG,
                         SCM_UNPACK (buf),
                         (scm_t_bits) 0, (scm_t_bits) len);
  scm_gc_register_collectable_memory (str, len+1, "string");
  return res;

scm_take_locale_string (char *str)
  return scm_take_locale_stringn (str, -1);

reply via email to

[Prev in Thread] Current Thread [Next in Thread]