| #ifndef CRAYON_TRIE_NODE_H |
| #define CRAYON_TRIE_NODE_H |
|
|
| #include <stdint.h> |
| #include <stdlib.h> |
| #include <string.h> |
|
|
| |
| #if defined(_MSC_VER) |
| #define ALIGN_64 __declspec(align(64)) |
| #include <malloc.h> |
| static __inline void* aligned_alloc_64(size_t size) { |
| return _aligned_malloc(size, 64); |
| } |
| static __inline void aligned_free_64(void* ptr) { |
| _aligned_free(ptr); |
| } |
| #else |
| #define ALIGN_64 __attribute__((aligned(64))) |
| static inline void* aligned_alloc_64(size_t size) { |
| void* ptr = NULL; |
| if (posix_memalign(&ptr, 64, size) != 0) return NULL; |
| return ptr; |
| } |
| static inline void aligned_free_64(void* ptr) { |
| free(ptr); |
| } |
| #endif |
|
|
| |
| struct TrieNode; |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| typedef struct ALIGN_64 TrieNode { |
| int32_t token_id; |
| uint16_t child_count; |
| uint16_t flags; |
| uint64_t child_bitmap; |
| |
| struct TrieNode* children; |
| uint8_t* child_chars; |
|
|
| |
| uint8_t padding[32]; |
| |
| } TrieNode; |
|
|
| |
| #if defined(_MSC_VER) |
| static_assert(sizeof(TrieNode) == 64, "TrieNode MUST be exactly 64 bytes"); |
| #else |
| _Static_assert(sizeof(TrieNode) == 64, "TrieNode MUST be exactly 64 bytes"); |
| #endif |
|
|
| |
| |
| |
| |
| |
| |
| static inline TrieNode* alloc_trie_node_array(size_t count) { |
| if (count == 0) return NULL; |
| size_t size = count * sizeof(TrieNode); |
| TrieNode* arr = (TrieNode*)aligned_alloc_64(size); |
| if (arr) { |
| memset(arr, 0, size); |
| } |
| return arr; |
| } |
|
|
| |
| |
| |
| static inline TrieNode* alloc_trie_node(void) { |
| TrieNode* node = (TrieNode*)aligned_alloc_64(sizeof(TrieNode)); |
| if (node) { |
| memset(node, 0, sizeof(TrieNode)); |
| node->token_id = -1; |
| } |
| return node; |
| } |
|
|
| |
| |
| |
| static inline void free_trie_node_array(TrieNode* arr) { |
| if (arr) { |
| aligned_free_64(arr); |
| } |
| } |
|
|
| #endif |