Loading...
Searching...
No Matches
tree_edit.h
Go to the documentation of this file.
#ifndef LY_TREE_EDIT_H_
#define LY_TREE_EDIT_H_
#include <stdlib.h>
#ifndef LOGMEM
#define LOGMEM(CTX)
#endif
#ifdef __cplusplus
extern "C" {
#endif
void *ly_realloc(void *ptr, size_t size);
#define LY_ARRAY_NEW(CTX, ARRAY, EACTION) \
{ \
char *p__; \
if (ARRAY) { \
++(*((LY_ARRAY_COUNT_TYPE*)(ARRAY) - 1)); \
p__ = (char *)realloc(((LY_ARRAY_COUNT_TYPE*)(ARRAY) - 1), \
sizeof(LY_ARRAY_COUNT_TYPE) + (*((LY_ARRAY_COUNT_TYPE*)(ARRAY) - 1) * sizeof *(ARRAY))); \
if (!p__) { \
--(*((LY_ARRAY_COUNT_TYPE*)(ARRAY) - 1)); \
LOGMEM(CTX); \
EACTION; \
} \
} else { \
p__ = (char *)malloc(sizeof(LY_ARRAY_COUNT_TYPE) + sizeof *(ARRAY)); \
if (!p__) { \
LOGMEM(CTX); \
EACTION; \
} \
*((LY_ARRAY_COUNT_TYPE*)(p__)) = 1; \
} \
p__ = (char *)((LY_ARRAY_COUNT_TYPE*)(p__) + 1); \
memcpy(&(ARRAY), &p__, sizeof p__); \
}
#define LY_ARRAY_NEW_RET(CTX, ARRAY, NEW_ITEM, RETVAL) \
LY_ARRAY_NEW(CTX, ARRAY, return RETVAL); \
(NEW_ITEM) = &(ARRAY)[*((LY_ARRAY_COUNT_TYPE*)(ARRAY) - 1) - 1]; \
memset(NEW_ITEM, 0, sizeof *(NEW_ITEM))
#define LY_ARRAY_NEW_GOTO(CTX, ARRAY, NEW_ITEM, RET, GOTO) \
LY_ARRAY_NEW(CTX, ARRAY, RET = LY_EMEM; goto GOTO); \
(NEW_ITEM) = &(ARRAY)[*((LY_ARRAY_COUNT_TYPE*)(ARRAY) - 1) - 1]; \
memset(NEW_ITEM, 0, sizeof *(NEW_ITEM))
#define LY_ARRAY_CREATE(CTX, ARRAY, SIZE, EACTION) \
{ \
char *p__; \
if (ARRAY) { \
p__ = (char *)realloc(((LY_ARRAY_COUNT_TYPE*)(ARRAY) - 1), \
sizeof(LY_ARRAY_COUNT_TYPE) + ((*((LY_ARRAY_COUNT_TYPE*)(ARRAY) - 1) + (SIZE)) * sizeof *(ARRAY))); \
if (!p__) { \
LOGMEM(CTX); \
EACTION; \
} \
} else { \
p__ = (char *)calloc(1, sizeof(LY_ARRAY_COUNT_TYPE) + (SIZE) * sizeof *(ARRAY)); \
if (!p__) { \
LOGMEM(CTX); \
EACTION; \
} \
} \
p__ = (char *)((LY_ARRAY_COUNT_TYPE*)(p__) + 1); \
memcpy(&(ARRAY), &p__, sizeof p__); \
if ((ARRAY) && (SIZE > 0)) { \
memset(&(ARRAY)[*((LY_ARRAY_COUNT_TYPE*)(p__) - 1)], 0, (SIZE) * sizeof *(ARRAY)); \
} \
}
#define LY_ARRAY_CREATE_RET(CTX, ARRAY, SIZE, RETVAL) \
LY_ARRAY_CREATE(CTX, ARRAY, SIZE, return RETVAL)
#define LY_ARRAY_CREATE_GOTO(CTX, ARRAY, SIZE, RET, GOTO) \
LY_ARRAY_CREATE(CTX, ARRAY, SIZE, RET = LY_EMEM; goto GOTO)
#define LY_ARRAY_INCREMENT(ARRAY) \
++(*((LY_ARRAY_COUNT_TYPE*)(ARRAY) - 1))
#define LY_ARRAY_DECREMENT(ARRAY) \
--(*((LY_ARRAY_COUNT_TYPE*)(ARRAY) - 1))
#define LY_ARRAY_DECREMENT_FREE(ARRAY) \
--(*((LY_ARRAY_COUNT_TYPE*)(ARRAY) - 1)); \
if (!LY_ARRAY_COUNT(ARRAY)) { \
LY_ARRAY_FREE(ARRAY); \
(ARRAY) = NULL; \
}
#define LY_ARRAY_FREE(ARRAY) \
if (ARRAY){free((LY_ARRAY_COUNT_TYPE*)(ARRAY) - 1);}
#define LY_ARRAY_REMOVE_VALUE(ARRAY, VALUE) \
{ \
LY_ARRAY_COUNT_TYPE index__; \
LY_ARRAY_FOR(ARRAY, index__) { \
if ((ARRAY)[index__] == VALUE) { \
if (index__ != LY_ARRAY_COUNT(ARRAY) - 1) { \
memmove(&((ARRAY)[index__]), &((ARRAY)[LY_ARRAY_COUNT(ARRAY) - 1]), sizeof *(ARRAY)); \
} \
LY_ARRAY_DECREMENT(ARRAY); \
break; \
} \
} \
}
#define LY_LIST_INSERT(LIST, NEW_ITEM, LINKER)\
if (!(*LIST)) { \
memcpy(LIST, &(NEW_ITEM), sizeof NEW_ITEM); \
} else { \
size_t offset__ = (char *)&(*LIST)->LINKER - (char *)(*LIST); \
char **iter__ = (char **)((size_t)(*LIST) + offset__); \
while (*iter__) { \
iter__ = (char **)((size_t)(*iter__) + offset__); \
} \
memcpy(iter__, &(NEW_ITEM), sizeof NEW_ITEM); \
}
#define LY_LIST_NEW(CTX, LIST, NEW_ITEM, LINKER, EACTION) \
{ \
char *p__ = (char *)calloc(1, sizeof *NEW_ITEM); \
if (!p__) { \
LOGMEM(CTX); \
EACTION; \
} \
memcpy(&(NEW_ITEM), &p__, sizeof p__); \
LY_LIST_INSERT(LIST, NEW_ITEM, LINKER); \
}
#define LY_LIST_NEW_RET(CTX, LIST, NEW_ITEM, LINKER, RETVAL) \
LY_LIST_NEW(CTX, LIST, NEW_ITEM, LINKER, return RETVAL)
#define LY_LIST_NEW_GOTO(CTX, LIST, NEW_ITEM, LINKER, RET, LABEL) \
LY_LIST_NEW(CTX, LIST, NEW_ITEM, LINKER, RET = LY_EMEM; goto LABEL)
#ifdef __cplusplus
}
#endif
#endif /* LY_TREE_EDIT_H_ */
ly_realloc
void * ly_realloc(void *ptr, size_t size)
Wrapper for realloc() call. The only difference is that if it fails to allocate the requested memory,...