Add ARRAY convenience macros
Because dynamically allocated arrays in C are a pain.
This commit is contained in:
		
							
								
								
									
										17
									
								
								liberty.c
									
									
									
									
									
								
							
							
						
						
									
										17
									
								
								liberty.c
									
									
									
									
									
								
							@@ -301,6 +301,23 @@ xstrndup (const char *s, size_t n)
 | 
			
		||||
	return copy;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// --- Simple array support ----------------------------------------------------
 | 
			
		||||
 | 
			
		||||
// The most basic helper macros to make working with arrays not suck
 | 
			
		||||
 | 
			
		||||
#define ARRAY(type, name) type *name; size_t name ## _len, name ## _size;
 | 
			
		||||
#define ARRAY_INIT_SIZED(a, n)                                                 \
 | 
			
		||||
	BLOCK_START                                                                \
 | 
			
		||||
		(a) = xcalloc (sizeof *(a), (a ## _size) = (n));                       \
 | 
			
		||||
		(a ## _len) = 0;                                                       \
 | 
			
		||||
	BLOCK_END
 | 
			
		||||
#define ARRAY_INIT(a) ARRAY_INIT_SIZED (a, 16)
 | 
			
		||||
#define ARRAY_RESERVE(a, n)                                                    \
 | 
			
		||||
	BLOCK_START                                                                \
 | 
			
		||||
		while ((a ## _size) - (a ## _len) < n)                                 \
 | 
			
		||||
			(a) = xreallocarray ((a), sizeof *(a), (a ## _size) <<= 1);        \
 | 
			
		||||
	BLOCK_END
 | 
			
		||||
 | 
			
		||||
// --- Double-linked list helpers ----------------------------------------------
 | 
			
		||||
 | 
			
		||||
#define LIST_HEADER(type)                                                      \
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user