Implement strndup manually and change types for OS compatibility

This commit is contained in:
XOR 2023-09-30 22:20:00 +02:00
parent 781bb0228b
commit 6c5f5d89a6
2 changed files with 11 additions and 4 deletions

10
shash.c
View file

@ -9,6 +9,14 @@
#include "shash.h"
static char *str_duplicate(char *str, size_t len)
{
char *new_str = malloc(len * sizeof(char));
if(new_str == NULL) return 0;
memcpy(new_str, str, len);
return new_str;
}
static int get_empty_hashtable_slot(shash_hashtable_t *hashtable)
{
assert(hashtable != NULL);
@ -100,7 +108,7 @@ int shash_set(char *key, unsigned int len, void *data, shash_hashtable_t *hashta
shash_table_element_t table_element =
{
.key = strndup(key, len),
.key = str_duplicate(key, len),
.keylen = len,
.data = data};

View file

@ -10,6 +10,7 @@
#ifndef _SHASH_H
#define _SHASH_H
#include <stdint.h>
#include <assert.h>
#include <limits.h>
#include <stdlib.h>
@ -17,8 +18,6 @@
#include <sys/types.h>
#include <time.h>
#include <stdbool.h>
#include <stdint.h>
#define WORD 32
#define TRANSFORM_TABLE_MAX_RAND 4294967296
@ -43,7 +42,7 @@ typedef struct
void *data;
// gets set to 1 if another key, collides with this elemnts location
u_int8_t encountered_collision;
uint8_t encountered_collision;
// On collision, this field stores where in the hash table array the second key (with the same hash) is located
int next_key_location;