pool.h
changeset 4970 eb2c0fde4b02
parent 3585 43461f26b729
child 4986 76eebd65bc6a
--- a/pool.h	Sat Oct 28 08:48:31 2006 +0000
+++ b/pool.h	Sat Oct 28 10:54:20 2006 +0000
@@ -16,7 +16,7 @@
  *  please try to avoid manual calls!
  */
 struct MemoryPool {
-	const char name[10];        ///< Name of the pool (just for debugging)
+	const char* const name;     ///< Name of the pool (just for debugging)
 
 	const uint max_blocks;      ///< The max amount of blocks this pool can have
 	const uint block_size_bits; ///< The size of each block in bits
@@ -56,4 +56,47 @@
 	return (pool->blocks[index >> pool->block_size_bits] + (index & ((1 << pool->block_size_bits) - 1)) * pool->item_size);
 }
 
+
+#define POOL_ENUM(name, type, block_size_bits, max_blocks) \
+	enum { \
+		name##_POOL_BLOCK_SIZE_BITS = block_size_bits, \
+		name##_POOL_MAX_BLOCKS      = max_blocks \
+	};
+
+
+#define POOL_ACCESSORS(name, type) \
+	static inline type* Get##name(uint index) \
+	{ \
+		assert(index < _##name##_pool.total_items); \
+		return (type*)( \
+			_##name##_pool.blocks[index >> name##_POOL_BLOCK_SIZE_BITS] + \
+			(index & ((1 << name##_POOL_BLOCK_SIZE_BITS) - 1)) * sizeof(type) \
+		); \
+	} \
+\
+	static inline uint Get##name##PoolSize(void) \
+	{ \
+		return _##name##_pool.total_items; \
+	}
+
+
+#define DECLARE_POOL(name, type, block_size_bits, max_blocks) \
+	POOL_ENUM(name, type, block_size_bits, max_blocks) \
+	extern MemoryPool _##name##_pool; \
+	POOL_ACCESSORS(name, type)
+
+
+#define DEFINE_POOL(name, type, new_block_proc, clean_block_proc) \
+	MemoryPool _##name##_pool = { \
+		#name, name##_POOL_MAX_BLOCKS, name##_POOL_BLOCK_SIZE_BITS, sizeof(type), \
+		new_block_proc, clean_block_proc, \
+		0, 0, NULL \
+	};
+
+
+#define STATIC_POOL(name, type, block_size_bits, max_blocks, new_block_proc, clean_block_proc) \
+	POOL_ENUM(name, type, block_size_bits, max_blocks) \
+	static DEFINE_POOL(name, type, new_block_proc, clean_block_proc) \
+	POOL_ACCESSORS(name, type)
+
 #endif /* POOL_H */