DictionaryWord * dictionary_add_word (const char *word, Phoneme *decomp) { DictionaryWord *dword; char *query_word; g_return_val_if_fail (word != NULL, NULL); g_static_rec_mutex_lock (&dict_mutex); if (dictionary == NULL) { dictionary = g_hash_table_new (g_str_hash, g_str_equal); } query_word = g_ascii_strdown (word, -1); g_strstrip (query_word); dword = g_hash_table_lookup (dictionary, query_word); if (dword == NULL || (decomp != NULL && dword->decomp == NULL)) { gboolean add_word = FALSE; const char *w; if (dword == NULL) { if (dict_chunks == NULL) { dict_chunks = g_mem_chunk_create (DictionaryWord, 1000, G_ALLOC_ONLY); } dword = g_chunk_new0 (DictionaryWord, dict_chunks); dword->word = query_word; /* pre-compute a hash key */ dword->hash = 0xdeadbeef; for (w = word; *w; ++w) dword->hash = 17 * dword->hash + (long)*w; add_word = TRUE; } else { g_free (query_word); } dword->decomp = decomp; if (decomp) rhyme_index_word (dword); if (add_word) g_hash_table_insert (dictionary, dword->word, dword); } g_static_rec_mutex_unlock (&dict_mutex); return dword; }
Token * token_lookup (const char *raw) { Token *tok; g_static_mutex_lock (&token_mutex); if (token_table == NULL) token_table = g_hash_table_new (g_str_hash, g_str_equal); tok = g_hash_table_lookup (token_table, raw); if (tok == NULL) { static GMemChunk *token_chunks = NULL; if (token_chunks == NULL) token_chunks = g_mem_chunk_create (Token, 1000, G_ALLOC_ONLY); tok = g_chunk_new0 (Token, token_chunks); tok->raw = g_strdup (raw); tok->syllables = -1; tok->word_count = -1; if (! strcmp (raw, "*break*")) { tok->is_break = TRUE; tok->syllables = 0; } else if (! strncmp (raw, "*punct*", 7)) { tok->word = g_strdup (raw+8); tok->is_punctuation = TRUE; tok->left_glue = TRUE; /* FIXME */ tok->syllables = 0; tok->word_count = 0; } else if (! strcmp (raw, "*wildcard*")) { tok->word = g_strdup (raw); tok->is_wildcard = TRUE; } else { DictionaryWord *dword; tok->word = g_strdup (raw); dword = dictionary_get_word (tok->word); if (dword != NULL) { tok->dict = dword; tok->syllables = syllable_count_from_decomp (dword->decomp); tok->meter = meter_from_phoneme_decomp (dword->decomp); } else { tok->syllables = syllable_count_approximate (tok->word); } tok->word_count = str_word_count (tok->word); } g_hash_table_insert (token_table, tok->raw, tok); } g_static_mutex_unlock (&token_mutex); return tok; }
static RhymeTreeNode * node_new (PhonemeCode code) { RhymeTreeNode *node; if (rhyme_node_chunks == NULL) { rhyme_node_chunks = g_mem_chunk_create (RhymeTreeNode, 1000, G_ALLOC_ONLY); } node = g_chunk_new0 (RhymeTreeNode, rhyme_node_chunks); node->code = code; return node; }
GHook* g_hook_alloc (GHookList *hook_list) { GHook *hook; g_return_val_if_fail (hook_list != NULL, NULL); g_return_val_if_fail (hook_list->is_setup, NULL); hook = g_chunk_new0 (GHook, hook_list->hook_memchunk); hook->data = NULL; hook->next = NULL; hook->prev = NULL; hook->flags = G_HOOK_FLAG_ACTIVE; hook->ref_count = 0; hook->hook_id = 0; hook->func = NULL; hook->destroy = NULL; return hook; }