33 lines
		
	
	
		
			857 B
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			33 lines
		
	
	
		
			857 B
		
	
	
	
		
			C
		
	
	
	
	
	
| #include "strpool.h"
 | |
| 
 | |
| void init_strpool(strpool_t* pool) {
 | |
|     lalloc_init(&pool->stralloc);
 | |
|     
 | |
|     pool->ht.hash_func = (u32_t(*)(const void*))rt_strhash;
 | |
|     pool->ht.key_cmp = (int(*)(const void*, const void*))rt_strcmp;
 | |
|     init_hashtable(&pool->ht);
 | |
| }
 | |
| 
 | |
| const char* strpool_intern(strpool_t* pool, const char* str) {
 | |
|     void* existing = hashtable_get(&pool->ht, str);
 | |
|     if (existing) {
 | |
|         return existing;
 | |
|     }
 | |
| 
 | |
|     rt_size_t len = rt_strlen(str) + 1;
 | |
|     char* new_str = lalloc_alloc(&pool->stralloc, len);
 | |
|     if (!new_str) {
 | |
|         LOG_ERROR("strpool: Failed to allocate memory for string");
 | |
|         return NULL;
 | |
|     }
 | |
|     rt_memcpy(new_str, str, len);
 | |
| 
 | |
|     hashtable_set(&pool->ht, new_str, new_str);
 | |
|     return new_str;
 | |
| }
 | |
| 
 | |
| void strpool_destroy(strpool_t* pool) {
 | |
|     hashtable_destory(&pool->ht);
 | |
|     lalloc_destroy(&pool->stralloc);
 | |
| }
 |