📄 glib_hash.c
字号:
/* Copyright (C) 2004,2005,2006 Bull S.A. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */#if HAVE_CONFIG_H#include <config.h>#endif#define _GNU_SOURCE#define __USE_GNU#include <search.h>#include <errno.h>#include <stdlib.h>#include <assert.h>#include <stdio.h>#include <string.h>#include "glib_hash.h"/* size should be at least 2 x number_of_threads * number_of objects for using * ptt_contention without any problems */#define HASH_SIZE 5000#define INTLEN 25inline GHashTable *g_hash_table_new (int a, int b) { struct GHashTable *tab; tab = malloc (sizeof (GHashTable)); if (tab == NULL) return NULL; memset (tab, 0, sizeof (GHashTable)); if (hcreate_r(HASH_SIZE, &tab->table) == 0) return NULL; tab->type = a; return tab;}inline gpointer g_hash_table_lookup (GHashTable *table, gpointer key) { ENTRY item, *ret; char p [INTLEN]; if (g_int_equal == table->type) { snprintf (p, INTLEN, "%d", *(int*)key); item.key = p; } else item.key = key; if (hsearch_r (item, FIND, &ret, &table->table) == 0 && errno == ESRCH) return NULL; if (ret == NULL) return NULL; return ret->data;}inline gpointer* g_hash_table_insert (GHashTable *table,gpointer key, gpointer data) { ENTRY item, *ret; if (g_int_equal == table->type) { char *p = malloc (INTLEN); snprintf (p, INTLEN, "%d", *(int*)key); item.key = p; } else item.key = key; item.data = data; if (hsearch_r (item, ENTER, &ret, &table->table) == 0) { fprintf (stderr, "Use glib implementation !!!\n"); assert (errno != ENOMEM); return NULL; } return ret->data;}typedef struct _ENTRY { unsigned int used; ENTRY entry;} _ENTRY;inline void g_hash_table_foreach (GHashTable *table, void (*func) (gpointer key, gpointer value, gpointer user_data), gpointer userdata) { unsigned int idx; struct hsearch_data *htab = &table->table; for (idx = 0; idx < htab->size; idx++) { if (htab->table[idx].used) { func (htab->table[idx].entry.key, htab->table[idx].entry.data, userdata); } }}inline void g_hash_table_destroy (GHashTable *table) { if (!table) return; if (g_int_equal == table->type) { void free_tab(gpointer key, gpointer value, gpointer user_data) { free(key); } g_hash_table_foreach (table, free_tab, NULL); } hdestroy_r(&table->table); free (table); table = NULL;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -