project_files/frontlib/util/list.h
changeset 10017 de822cd3df3a
parent 7316 f7b49b2c5d84
equal deleted inserted replaced
10015:4feced261c68 10017:de822cd3df3a
    36  * The function takes a pointer to the array variable and a pointer to the size variable
    36  * The function takes a pointer to the array variable and a pointer to the size variable
    37  * because both can be changed by this operation (realloc / increment).
    37  * because both can be changed by this operation (realloc / increment).
    38  * The function returns 0 on success and leaves the array unchanged on error.
    38  * The function returns 0 on success and leaves the array unchanged on error.
    39  */
    39  */
    40 #define GENERATE_STATIC_LIST_INSERT(fname, type) \
    40 #define GENERATE_STATIC_LIST_INSERT(fname, type) \
    41 	static int fname(type **listptr, int *listSizePtr, type element, int pos) { \
    41     static int fname(type **listptr, int *listSizePtr, type element, int pos) { \
    42 		int result = -1; \
    42         int result = -1; \
    43 		if(!log_badargs_if4(listptr==NULL, listSizePtr==NULL, pos < 0, pos > *listSizePtr)) { \
    43         if(!log_badargs_if4(listptr==NULL, listSizePtr==NULL, pos < 0, pos > *listSizePtr)) { \
    44 			type *newList = flib_realloc(*listptr, ((*listSizePtr)+1)*sizeof(type)); \
    44             type *newList = flib_realloc(*listptr, ((*listSizePtr)+1)*sizeof(type)); \
    45 			if(newList) { \
    45             if(newList) { \
    46 				memmove(newList + (pos+1), newList + pos, ((*listSizePtr)-pos)*sizeof(type)); \
    46                 memmove(newList + (pos+1), newList + pos, ((*listSizePtr)-pos)*sizeof(type)); \
    47 				newList[pos] = element; \
    47                 newList[pos] = element; \
    48 				(*listSizePtr)++; \
    48                 (*listSizePtr)++; \
    49 				*listptr = newList; \
    49                 *listptr = newList; \
    50 				result = 0; \
    50                 result = 0; \
    51 			} \
    51             } \
    52 		} \
    52         } \
    53 		return result; \
    53         return result; \
    54 	}
    54     }
    55 
    55 
    56 /**
    56 /**
    57  * Generate a static function that deletes a value from a heap array of the given type,
    57  * Generate a static function that deletes a value from a heap array of the given type,
    58  * using realloc and memmove to decrease the capacity and shift existing values.
    58  * using realloc and memmove to decrease the capacity and shift existing values.
    59  * The function takes a pointer to the array variable and a pointer to the size variable
    59  * The function takes a pointer to the array variable and a pointer to the size variable
    60  * because both can be changed by this operation (realloc / decrement).
    60  * because both can be changed by this operation (realloc / decrement).
    61  * The function returns 0 on success and leaves the array unchanged on error.
    61  * The function returns 0 on success and leaves the array unchanged on error.
    62  */
    62  */
    63 #define GENERATE_STATIC_LIST_DELETE(fname, type) \
    63 #define GENERATE_STATIC_LIST_DELETE(fname, type) \
    64 	static int fname(type **listPtr, int *listSizePtr, int pos) { \
    64     static int fname(type **listPtr, int *listSizePtr, int pos) { \
    65 		int result = -1; \
    65         int result = -1; \
    66 		if(!log_badargs_if4(listPtr==NULL, listSizePtr==NULL, pos < 0, pos >= *listSizePtr)) { \
    66         if(!log_badargs_if4(listPtr==NULL, listSizePtr==NULL, pos < 0, pos >= *listSizePtr)) { \
    67 			memmove((*listPtr) + pos, (*listPtr) + (pos+1), ((*listSizePtr)-(pos+1))*sizeof(type)); \
    67             memmove((*listPtr) + pos, (*listPtr) + (pos+1), ((*listSizePtr)-(pos+1))*sizeof(type)); \
    68 			(*listSizePtr)--; \
    68             (*listSizePtr)--; \
    69 			\
    69             \
    70 			size_t newCharSize = (*listSizePtr)*sizeof(type); \
    70             size_t newCharSize = (*listSizePtr)*sizeof(type); \
    71 			type *newList = flib_realloc((*listPtr), newCharSize); \
    71             type *newList = flib_realloc((*listPtr), newCharSize); \
    72 			if(newList || newCharSize==0) { \
    72             if(newList || newCharSize==0) { \
    73 				(*listPtr) = newList; \
    73                 (*listPtr) = newList; \
    74 			} /* If the realloc fails, just keep using the old buffer...*/ \
    74             } /* If the realloc fails, just keep using the old buffer...*/ \
    75 			result = 0; \
    75             result = 0; \
    76 		} \
    76         } \
    77 		return result; \
    77         return result; \
    78 	}
    78     }
    79 
    79 
    80 #endif /* LIST_H_ */
    80 #endif /* LIST_H_ */