Skip to content

Commit

Permalink
Remove remnants of length prefix code in strtab handling
Browse files Browse the repository at this point in the history
The removed code was introduced in commit aa9b705 ("Store strings in
the string table with varint length prefix") and commit 99719da
("Transition from variable-length string size to 2-byte string size").

It involved unnecessary copying of string data and also caused string
comparison to be broken once length prefixes were removed in commit
29e3f42 ("Remove the string length prefix")..

Signed-off-by: Kris Van Hees <kris.van.hees@oracle.com>
Reviewed-by: Eugene Loh <eugene.loh@oracle.com>
  • Loading branch information
kvanhees committed Mar 18, 2022
1 parent a01c3b8 commit 465823c
Showing 1 changed file with 6 additions and 31 deletions.
37 changes: 6 additions & 31 deletions libdtrace/dt_strtab.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,11 +125,7 @@ dt_strtab_compare(dt_strtab_t *sp, dt_strhash_t *hp,
resid = sp->str_bufs[b] + sp->str_bufsz - buf;
n = MIN(resid, len);

if ((rv = buf[0] - str[0]) != 0)
return rv;
if ((rv = buf[1] - str[1]) != 0)
return rv;
if ((rv = strncmp(buf + 2, str + 2, n)) != 0)
if ((rv = strncmp(buf, str, n)) != 0)
return rv;

buf += n;
Expand Down Expand Up @@ -194,21 +190,13 @@ dt_strtab_index(dt_strtab_t *sp, const char *str)
size_t slen;
ssize_t rc;
ulong_t h;
char *s;

if (str == NULL || str[0] == '\0')
return 0; /* The empty string is always at offset 0. */

slen = strlen(str);
s = malloc(slen + 1);
if (s == NULL)
return -1L;

memcpy(s, str, slen + 1);

h = str2hval(str, slen) % sp->str_hashsz;
rc = dt_strtab_xindex(sp, s, slen, h);
free(s);
rc = dt_strtab_xindex(sp, str, slen, h);

return rc;
}
Expand All @@ -220,33 +208,22 @@ dt_strtab_insert(dt_strtab_t *sp, const char *str)
size_t slen;
ssize_t off;
ulong_t h;
char *s;

if (str == NULL || str[0] == '\0')
return 0; /* The empty string is always at offset 0. */

slen = strlen(str);
s = malloc(slen + 1);
if (s == NULL)
return -1L;

memcpy(s, str, slen + 1);

h = str2hval(str, slen) % sp->str_hashsz;
off = dt_strtab_xindex(sp, s, slen, h);
if (off != -1) {
free(s);
off = dt_strtab_xindex(sp, str, slen, h);
if (off != -1)
return off;
}

/*
* Create a new hash bucket, initialize it, and insert it at the front
* of the hash chain for the appropriate bucket.
*/
if ((hp = malloc(sizeof(dt_strhash_t))) == NULL) {
free(s);
if ((hp = malloc(sizeof(dt_strhash_t))) == NULL)
return -1L;
}

hp->str_data = sp->str_ptr;
hp->str_buf = sp->str_nbufs - 1;
Expand All @@ -258,12 +235,10 @@ dt_strtab_insert(dt_strtab_t *sp, const char *str)
* Now copy the string data into our buffer list, and then update
* the global counts of strings and bytes. Return str's byte offset.
*/
if (dt_strtab_copyin(sp, s, slen + 1) == -1) {
free(s);
if (dt_strtab_copyin(sp, str, slen + 1) == -1) {
free(hp);
return -1L;
}
free(s);

sp->str_nstrs++;
sp->str_size += slen + 1;
Expand Down

0 comments on commit 465823c

Please sign in to comment.