예제 #1
0
void *mk_scon(char *s)
{
    unsigned nbytes = strlen(s);
    unsigned header = RML_STRINGHDR(nbytes);
    unsigned nwords = RML_HDRSLOTS(header) + 1;
    struct rml_string *p = alloc_words(nwords);
    p->header = header;
    memcpy(p->data, s, nbytes+1);	/* including terminating '\0' */
    return RML_TAGPTR(p);
}
예제 #2
0
void *mk_scon(char *s) {
  rml_uint_t nbytes = strlen(s);
  rml_uint_t header= RML_STRINGHDR(nbytes);
  rml_uint_t nwords= RML_HDRSLOTS(header) + 1;
  if (!rml_string_cache_index) /* no string in the cache */
  {
    struct rml_string *p = alloc_words(nwords);
    p->header = header;
    memcpy(p->data, s, nbytes+1); /* including terminating '\0' */
    if (rml_string_cache_index < RML_STRING_CACHE_MAX &&
        nbytes < RML_SHARED_STRING_MAX) /* add to sharing only if less than RML_SHARED_STRING_MAX */
      rml_string_cache[rml_string_cache_index++] = p;
    return RML_TAGPTR(p);
  }
  /* else, try to find if we already have the same string in the heap */
  {
    unsigned int i;
    struct rml_string *p;
    for (i = 0; i < rml_string_cache_index; i++)
    {
      p = rml_string_cache[i];
      if (strcmp(p->data,s) == 0)
      {
        rml_total_shared_strings++;
        rml_total_shared_strings_words += nwords;
        return RML_TAGPTR(p);
      }
    }
    /* no string found in cache */
    {
      struct rml_string *p = alloc_words(nwords);
      p->header = header;
      memcpy(p->data, s, nbytes+1); /* including terminating '\0' */
      if (rml_string_cache_index < RML_STRING_CACHE_MAX &&
          nbytes < RML_SHARED_STRING_MAX) /* add to sharing only if less than RML_SHARED_STRING_MAX */
        rml_string_cache[rml_string_cache_index++] = p;
      return RML_TAGPTR(p);
    }
  }
}