ulib dlinklist, string_memmove()

This commit is contained in:
2025-10-05 22:44:59 +02:00
parent a513909189
commit 00b779fb91
4 changed files with 169 additions and 0 deletions

138
ulib/dlinklist.h Normal file
View File

@ -0,0 +1,138 @@
#ifndef ULIB_DLINKLIST_H_
#define ULIB_DLINKLIST_H_
#define DL_APPEND(head, new) \
do { \
if ((new) != NULL) { \
(new)->next = NULL; \
if ((head) != NULL) { \
typeof((head)) __tmp = (head); \
while (__tmp->next != NULL) { \
__tmp = __tmp->next; \
} \
__tmp->next = (new); \
(new)->prev = __tmp; \
} else { \
(new)->prev = NULL; \
(head) = (new); \
} \
} \
} while (0)
#define DL_PREPEND(head, new) \
do { \
if ((new) != NULL) { \
(new)->prev = NULL; \
(new)->next = (head); \
if ((head) != NULL) { \
(head)->prev = (new); \
} \
(head) = (new); \
} \
} while (0)
#define DL_REMOVE(head, ele) \
do { \
if ((ele) != NULL) { \
if ((ele)->prev != NULL) { \
(ele)->prev->next = (ele)->next; \
} else { \
(head) = (ele)->next; \
} \
if ((ele)->next != NULL) { \
(ele)->next->prev = (ele)->prev; \
} \
(ele)->next = NULL; \
(ele)->prev = NULL; \
} \
} while (0)
#define DL_FINDPROP(head, out, propname, propvalue) \
do { \
(out) = NULL; \
typeof((head)) __tmp = (head); \
while (__tmp) { \
if (__tmp->propname == (propvalue)) { \
(out) = __tmp; \
break; \
} \
__tmp = __tmp->next; \
} \
} while (0)
#define DL_FOREACH_SAFE(head, var, tmp) \
for (var = (head), tmp = (var ? var->next : NULL); \
var != NULL; \
var = tmp, tmp = (var ? var->next : NULL))
#define DL_FOREACH_SAFE_IDX(head, var, tmp, idx) \
for ((idx) = 0, var = (head), tmp = (var ? var->next : NULL); \
var != NULL; \
var = tmp, tmp = (var ? var->next : NULL), (idx)++)
#define DL_FOREACH_SAFE_IDX_LIMIT(head, var, tmp, idx, max) \
for ((idx) = 0, var = (head), tmp = (var ? var->next : NULL); \
var != NULL && (idx) < (max); \
var = tmp, tmp = (var ? var->next : NULL), (idx)++)
#define DL_BACK(head, out) \
do { \
(out) = NULL; \
if ((head) != NULL) { \
typeof((head)) __tmp = (head); \
while (__tmp->next != NULL) { \
__tmp = __tmp->next; \
} \
(out) = __tmp; \
} \
} while (0)
#define DL_FRONT(head, out) \
do { \
(out) = NULL; \
if ((head) != NULL) { \
typeof((head)) __tmp = (head); \
while (__tmp->prev != NULL) { \
__tmp = __tmp->prev; \
} \
(out) = __tmp; \
} \
} while (0)
#define DL_INSERT_AFTER(head, pos, new) \
do { \
if ((pos) != NULL && (new) != NULL) { \
(new)->prev = (pos); \
(new)->next = (pos)->next; \
if ((pos)->next != NULL) { \
(pos)->next->prev = (new); \
} \
(pos)->next = (new); \
} else if ((pos) == NULL && (head) == NULL) { \
/* Empty list: make new head */ \
(new)->prev = NULL; \
(new)->next = NULL; \
(head) = (new); \
} \
} while (0)
#define DL_INSERT_BEFORE(head, pos, new) \
do { \
if ((pos) != NULL && (new) != NULL) { \
(new)->next = (pos); \
(new)->prev = (pos)->prev; \
if ((pos)->prev != NULL) { \
(pos)->prev->next = (new); \
} else { \
(head) = (new); \
} \
(pos)->prev = (new); \
} else if ((pos) == NULL && (head) == NULL) { \
/* Empty list: make new head */ \
(new)->prev = NULL; \
(new)->next = NULL; \
(head) = (new); \
} \
} while (0)
#endif // ULIB_DLINKLIST_H_

View File

@ -183,3 +183,32 @@ char *string_combine(char *dest, const char *src) {
dest[i+j] = '\0';
return dest;
}
// https://aticleworld.com/memmove-function-implementation-in-c/
void * string_memmove(void* dest, const void* src, unsigned int n)
{
char *pDest = (char *)dest;
const char *pSrc =( const char*)src;
//allocate memory for tmp array
char *tmp = (char *)umalloc(sizeof(char ) * n);
if(NULL == tmp)
{
return NULL;
}
else
{
unsigned int i = 0;
// copy src to tmp array
for(i =0; i < n ; ++i)
{
*(tmp + i) = *(pSrc + i);
}
//copy tmp to dest
for(i =0 ; i < n ; ++i)
{
*(pDest + i) = *(tmp + i);
}
ufree(tmp); //free allocated memory
}
return dest;
}

View File

@ -18,6 +18,7 @@ char *string_strchr(const char *s, int c);
int string_strncmp(const char * s1, const char * s2, size_t n);
char *string_tokenizealloc(char *s, char *delim);
char *string_combine(char *dest, const char *src);
void * string_memmove(void* dest, const void* src, unsigned int n);
#endif

View File

@ -17,6 +17,7 @@
#include <assert.h>
#include <umalloc/umalloc.h>
#include <fs/path.h>
#include <dlinklist.h>
#include <errors.h>
#include <sysdefs/ioctl.h>