GHashTable

glib之g_hash_table详解

glib是一个强大的库。在linux上开发c程序,不用glib真是有点可惜了。

ghashtable是glib中的hash表模块。

glib中已经提供了int,string,pointer三种hash函数,一般的应用已经够用了。

 

g_hash_table_new():创建hash表

g_hash_table_insert():插入项目

g_hash_table_lookup():找项目

g_hash_table_remove():移除项目

g_hash_table_destory():销毁hash表

g_hash_table_foreach():遍历hash表

 

常用的就这几个接口。

使用的时候,感觉没有c++ stl中的map来的方便。

不过在c上面,也算是可以了。

 

使用的时候要小心,我在使用的时候会发生sigsegv的错误,

还是destory hashtable之后,再次调用remove方法引起的。

/* Structs */ typedef struct janus_audiobridge_room { guint64 room_id; /* Unique room ID (when using integers) */ gchar *room_id_str; /* Unique room ID (when using strings) */ gchar *room_name; /* Room description */ gchar *room_secret; /* Secret needed to manipulate (e.g., destroy) this room */ gchar *room_pin; /* Password needed to join this room, if any */ uint32_t room_ssrc; /* SSRC we'll use for packets generated by the mixer */ gboolean is_private; /* Whether this room is 'private' (as in hidden) or not */ uint32_t sampling_rate; /* Sampling rate of the mix (e.g., 16000 for wideband; can be 8, 12, 16, 24 or 48kHz) */ gboolean spatial_audio; /* Whether the mix will use spatial audio, using stereo */ gboolean audiolevel_ext; /* Whether the ssrc-audio-level extension must be negotiated or not for new joins */ gboolean audiolevel_event; /* Whether to emit event to other users about audiolevel */ uint default_expectedloss; /* Percent of packets we expect participants may miss, to help with FEC: can be overridden per-participant */ int32_t default_bitrate; /* Default bitrate to use for all Opus streams when encoding */ int audio_active_packets; /* Amount of packets with audio level for checkup */ int audio_level_average; /* Average audio level */ volatile gint record; /* Whether this room has to be recorded or not */ gchar *record_file; /* Path of the recording file (absolute or relative, depending on record_dir) */ gchar *record_dir; /* Folder to save the recording file to */ gboolean mjrs; /* Whether all participants in the room should be individually recorded to mjr files or not */ gchar *mjrs_dir; /* Folder to save the mjrs file to */ FILE *recording; /* File to record the room into */ gint64 record_lastupdate; /* Time when we last updated the wav header */ volatile gint wav_header_added; /* If wav header is added in recording file */ gint64 rec_start_time; /* Time when recording started for generating file name */ gboolean allow_plainrtp; /* Whether plain RTP participants are allowed*/ gboolean destroy; /* Value to flag the room for destruction */ GHashTable *participants; /* Map of participants */ GHashTable *anncs; /* Map of announcements */ gboolean check_tokens; /* Whether to check tokens when participants join (see below) */ gboolean muted; /* Whether the room is globally muted (except for admins and played files) */ GHashTable *allowed; /* Map of participants (as tokens) allowed to join */ GThread *thread; /* Mixer thread for this room */ volatile gint destroyed; /* Whether this room has been destroyed */ janus_mutex mutex; /* Mutex to lock this room instance */ /* RTP forwarders for this room's mix */ GHashTable *groups; /* Forwarding groups supported in this room, indexed by name */ GHashTable *groups_byid; /* Forwarding groups supported in this room, indexed by numeric ID */ GHashTable *rtp_forwarders; /* RTP forwarders list (as a hashmap) */ OpusEncoder *rtp_encoder; /* Opus encoder instance to use for all RTP forwarders */ janus_mutex rtp_mutex; /* Mutex to lock the RTP forwarders list */ int rtp_udp_sock; /* UDP socket to use to forward RTP packets */ janus_refcount ref; /* Reference counter for this room */ 翻译
最新发布
09-09
// 格式化时间戳为可读字符串 const char *format_timestamp(time_t timestamp) { static char buffer[20]; struct tm *tm_info = localtime(&timestamp); strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", tm_info); return buffer; } // 增强的文件完整性检查 int is_file_complete(const char *filepath) { static GHashTable *active_files = NULL; if (!active_files) { active_files = g_hash_table_new_full(g_str_hash, g_str_equal, free, NULL); } struct stat st; if (stat(filepath, &st) != 0) return 0; // 检查文件是否正在被写入 const char *filename = strrchr(filepath, '/'); filename = filename ? filename + 1 : filepath; // 获取当前文件状态 FileState *state = g_hash_table_lookup(active_files, filename); if (!state) { // 新文件,初始化状态 state = malloc(sizeof(FileState)); state->last_size = st.st_size; state->last_check = time(NULL); g_hash_table_insert(active_files, strdup(filename), state); return 0; } // 检查文件大小变化 if (st.st_size != state->last_size) { // 文件仍在变化 state->last_size = st.st_size; state->last_check = time(NULL); return 0; } // 检查文件是否稳定(2秒内无变化) if (time(NULL) - state->last_check > 2) { // 文件已稳定,可以处理 g_hash_table_remove(active_files, filename); return 1; } return 0; } // 文件状态结构 typedef struct { off_t last_size; // 上次检查的文件大小 time_t last_check; // 上次检查的时间戳 } FileState; 报错:gpointer不能用于初始化filestate
08-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值