mirror of
https://github.com/GOSTSec/sgminer
synced 2025-03-13 06:01:03 +00:00
Provide helper functions calloc_str and realloc_strcat to create and extend arbitrary length arrays based on string length.
This commit is contained in:
parent
cffc21db28
commit
ee8609d9a9
41
util.c
41
util.c
@ -1477,3 +1477,44 @@ void dev_error(struct cgpu_info *dev, enum dev_reason reason)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/* Calloc enough memory to fit string s, rounding up to 4 byte alignment */
|
||||
void *calloc_str(char *s)
|
||||
{
|
||||
size_t len = strlen(s);
|
||||
void *ptr = NULL;
|
||||
|
||||
if (!len)
|
||||
goto out;
|
||||
|
||||
len += 1;
|
||||
if (len % 4)
|
||||
len += 4 - (len % 4);
|
||||
ptr = calloc(len, 1);
|
||||
if (unlikely(!ptr))
|
||||
quit(1, "Failed to calloc ptr in calloc_str");
|
||||
out:
|
||||
return ptr;
|
||||
}
|
||||
|
||||
/* Realloc an existing string to fit an extra string s, appending s to it. */
|
||||
void *realloc_strcat(char *ptr, char *s)
|
||||
{
|
||||
size_t old = strlen(ptr), len = strlen(s);
|
||||
char *ret;
|
||||
|
||||
if (!len)
|
||||
return ptr;
|
||||
|
||||
len += old + 1;
|
||||
if (len % 4)
|
||||
len += 4 - (len % 4);
|
||||
|
||||
ret = malloc(len);
|
||||
if (unlikely(!ret))
|
||||
quit(1, "Failed to malloc in realloc_strcat");
|
||||
|
||||
sprintf(ret, "%s%s", ptr, s);
|
||||
free(ptr);
|
||||
return ret;
|
||||
}
|
||||
|
2
util.h
2
util.h
@ -52,5 +52,7 @@ bool extract_sockaddr(struct pool *pool, char *url);
|
||||
bool auth_stratum(struct pool *pool);
|
||||
bool initiate_stratum(struct pool *pool);
|
||||
void dev_error(struct cgpu_info *dev, enum dev_reason reason);
|
||||
void *calloc_str(char *s);
|
||||
void *realloc_strcat(char *ptr, char *s);
|
||||
|
||||
#endif /* __UTIL_H__ */
|
||||
|
Loading…
x
Reference in New Issue
Block a user