Skip to content

Commit

Permalink
(rhmap) use custom strdup
Browse files Browse the repository at this point in the history
  • Loading branch information
LibretroAdmin committed Jul 5, 2024
1 parent eea9394 commit 7c60545
Showing 1 changed file with 18 additions and 9 deletions.
27 changes: 18 additions & 9 deletions libretro-common/include/array/rhmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@
#include <string.h> /* for memcpy, memset */
#include <stddef.h> /* for ptrdiff_t, size_t */
#include <stdint.h> /* for uint32_t */
#include <string.h> /* for strlen */

#define RHMAP_LEN(b) ((b) ? RHMAP__HDR(b)->len : 0)
#define RHMAP_MAX(b) ((b) ? RHMAP__HDR(b)->maxlen : 0)
Expand Down Expand Up @@ -229,6 +228,23 @@ RHMAP__UNUSED static void* rhmap__grow(void* old_ptr, size_t elem_size, size_t r
return new_vals;
}

/* This is just a custom version of strdup so we don't have an inherent
* dependency on strdup for this file. It is functionally equivalent to
* a system-provided strdup */
static char *rhmap_strdup(const char *s)
{
char *out;
int count = 0;
while (s[count])
++count;
++count;
out = malloc(sizeof(char) * count);
out[--count] = 0;
while (--count >= 0)
out[count] = s[count];
return out;
}

RHMAP__UNUSED static ptrdiff_t rhmap__idx(struct rhmap__hdr* hdr, uint32_t key, const char * str, int add, size_t del)
{
uint32_t i;
Expand Down Expand Up @@ -263,17 +279,10 @@ RHMAP__UNUSED static ptrdiff_t rhmap__idx(struct rhmap__hdr* hdr, uint32_t key,
{
if (add)
{
int l;
char *t;

hdr->len++;
hdr->keys[i] = key;
l = strlen(str);
t = malloc(l + 1);
memcpy(t, str, l);
t[l] = '\0';
if (str)
hdr->key_strs[i] = t;
hdr->key_strs[i] = rhmap_strdup(str);
return (ptrdiff_t)i;
}
return (ptrdiff_t)-1;
Expand Down

0 comments on commit 7c60545

Please sign in to comment.