diff --git a/daemon/janus.c b/daemon/janus.c index 3aed6ed9f..b1084ce0a 100644 --- a/daemon/janus.c +++ b/daemon/janus.c @@ -29,7 +29,7 @@ struct janus_session { // "login" session }; TYPED_GHASHTABLE(janus_sessions_ht, uint64_t, struct janus_session, int64_hash, int64_eq, NULL, - (void (*)(struct janus_session *)) __obj_put) + (void (*)(struct janus_session *)) obj_put_gen) struct janus_handle { // corresponds to a conference participant diff --git a/daemon/media_player.c b/daemon/media_player.c index b9a377aad..788d0ed1e 100644 --- a/daemon/media_player.c +++ b/daemon/media_player.c @@ -109,12 +109,12 @@ static gboolean media_player_cache_entry_eq(const struct media_player_cache_inde static void __media_player_cache_entry_free(struct media_player_cache_entry *p); TYPED_GHASHTABLE(media_player_cache_ht, struct media_player_cache_index, struct media_player_cache_entry, media_player_cache_entry_hash, media_player_cache_entry_eq, - NULL, (void(*)(struct media_player_cache_entry *)) __obj_put) + NULL, (void(*)(struct media_player_cache_entry *)) obj_put_gen) TYPED_GQUEUE(media_player_cache_entry, struct media_player_cache_entry) static media_player_cache_ht media_player_cache; // keys and values only ever freed at shutdown TYPED_GHASHTABLE(media_player_media_files_ht, str, struct media_player_media_file, str_hash, str_equal, - NULL, (void(*)(struct media_player_media_file *)) __obj_put); + NULL, (void(*)(struct media_player_media_file *)) obj_put_gen); static mutex_t media_player_media_files_lock = MUTEX_STATIC_INIT; static media_player_media_files_ht media_player_media_files; static rwlock_t media_player_media_files_names_lock = RWLOCK_STATIC_INIT; @@ -122,7 +122,7 @@ static str_q media_player_media_files_names = TYPED_GQUEUE_INIT; // lock order: media_player_media_files_names_lock first, media_player_media_files_lock second TYPED_GHASHTABLE(media_player_db_media_ht, void, struct media_player_media_file, g_direct_hash, g_direct_equal, - NULL, (void(*)(struct media_player_media_file *)) __obj_put); + NULL, (void(*)(struct media_player_media_file *)) obj_put_gen); static mutex_t media_player_db_media_lock = MUTEX_STATIC_INIT; static media_player_db_media_ht media_player_db_media; static rwlock_t media_player_db_media_ids_lock = RWLOCK_STATIC_INIT; diff --git a/lib/obj.h b/lib/obj.h index 477fbb912..f6bec39b1 100644 --- a/lib/obj.h +++ b/lib/obj.h @@ -40,8 +40,8 @@ struct obj { char *type; #endif unsigned int ref; + void (*clear_func)(void *); void (*free_func)(void *); - size_t size; }; @@ -54,17 +54,15 @@ struct obj { #define OBJ_MAGIC 0xf1eef1ee -#define obj_alloc(t,f) ({ \ +#define obj_alloc_full(t,f,a,d) ({ \ void (*__ff)(t *) = (f); \ - void *__r = __obj_alloc(sizeof(t), (void (*)(void *)) __ff, #t, __FILE__, __func__, __LINE__); \ + void (*__df)(t *) = (d); \ + void *__r = __obj_alloc(sizeof(t), (void (*)(void *)) __ff, #t, __FILE__, __func__, __LINE__, \ + a, (void (*)(void *)) __df); \ (t *) __r; \ }) -#define obj_alloc0(t,f) ({ \ - void (*__ff)(t *) = (f); \ - void *__r = __obj_alloc0(sizeof(t), (void (*)(void *)) __ff, #t, __FILE__, __func__, __LINE__); \ - (t *) __r; \ - }) -#define obj_alloc0_gen(t,a,b) __obj_alloc0(a,b,t,__FILE__,__func__,__LINE__) + +#define obj_alloc0_gen(t,a,b) __obj_alloc(a,b,t,__FILE__,__func__,__LINE__, g_malloc0, g_free) #define obj_hold(a) __obj_hold(&(a)->obj,__FILE__,__func__,__LINE__) #define obj_get(a) ((__typeof__(a)) (__obj_get(&(a)->obj,__FILE__,__func__,__LINE__))) #define obj_put(a) __obj_put(&(a)->obj,__FILE__,__func__,__LINE__) @@ -72,12 +70,9 @@ struct obj { #define obj_get_o(a) __obj_get(a,__FILE__,__func__,__LINE__) #define obj_put_o(a) __obj_put(a,__FILE__,__func__,__LINE__) -INLINE void __obj_init(struct obj *o, size_t size, void (*free_func)(void *), - const char *type, const char *file, const char *func, unsigned int line); -INLINE void *__obj_alloc(size_t size, void (*free_func)(void *), - const char *type, const char *file, const char *func, unsigned int line); -INLINE void *__obj_alloc0(size_t size, void (*free_func)(void *), - const char *type, const char *file, const char *func, unsigned int line); +INLINE void *__obj_alloc(size_t size, void (*clear_func)(void *), + const char *type, const char *file, const char *func, unsigned int line, + void *(*alloc_func)(size_t), void (*free_func)(void *)); INLINE struct obj *__obj_hold(struct obj *o, const char *file, const char *func, unsigned int line); INLINE void *__obj_get(struct obj *o, @@ -87,17 +82,14 @@ INLINE void __obj_put(struct obj *o, #else -#define obj_alloc(t,f) ({ \ +#define obj_alloc_full(t,f,a,d) ({ \ void (*__ff)(t *) = (f); \ - void *__r = __obj_alloc(sizeof(t), (void (*)(void *)) __ff); \ + void (*__df)(t *) = (d); \ + void *__r = __obj_alloc(sizeof(t), (void (*)(void *)) __ff, a, (void (*)(void *)) __df); \ (t *) __r; \ }) -#define obj_alloc0(t,f) ({ \ - void (*__ff)(t *) = (f); \ - void *__r = __obj_alloc0(sizeof(t), (void (*)(void *)) __ff); \ - (t *) __r; \ - }) -#define obj_alloc0_gen(t,a,b) __obj_alloc0(a,b) + +#define obj_alloc0_gen(t,a,b) __obj_alloc(a,b, g_malloc0, g_free) #define obj_hold(a) __obj_hold(&(a)->obj) #define obj_get(a) ((__typeof__(a)) (__obj_get(&(a)->obj))) #define obj_put(a) __obj_put(&(a)->obj) @@ -105,9 +97,8 @@ INLINE void __obj_put(struct obj *o, #define obj_get_o(a) __obj_get(a) #define obj_put_o(a) __obj_put(a) -INLINE void __obj_init(struct obj *o, size_t size, void (*free_func)(void *)); -INLINE void *__obj_alloc(size_t size, void (*free_func)(void *)); -INLINE void *__obj_alloc0(size_t size, void (*free_func)(void *)); +INLINE void *__obj_alloc(size_t size, void (*clear_func)(void *), + void *(*alloc_func)(size_t), void (*free_func)(void *)); INLINE struct obj *__obj_hold(struct obj *o); INLINE void *__obj_get(struct obj *o); INLINE void __obj_put(struct obj *o); @@ -115,24 +106,36 @@ INLINE void __obj_put(struct obj *o); #endif +#define obj_alloc(t,f) obj_alloc_full(t, f, g_malloc, (void (*)(t *)) g_free) +#define obj_alloc0(t,f) obj_alloc_full(t, f, g_malloc0, (void (*)(t *)) g_free) + #define obj_release_o(op) do { if (op) obj_put_o((struct obj *) op); op = NULL; } while (0) #define obj_release(op) do { if (op) obj_put(op); op = NULL; } while (0) +INLINE void obj_put_gen(struct obj *o) { + obj_put_o(o); +} + #include "log.h" -INLINE void __obj_init(struct obj *o, size_t size, void (*free_func)(void *) +INLINE void *__obj_alloc(size_t size, void (*clear_func)(void *), #if OBJ_DEBUG -, const char *type, const char *file, const char *func, unsigned int line + const char *type, const char *file, const char *func, unsigned int line, #endif -) { + void *(*alloc_func)(size_t), void (*free_func)(void *)) +{ + struct obj *r; + + r = alloc_func(size); + #if OBJ_DEBUG - o->magic = OBJ_MAGIC; - o->type = strdup(type); - write_log(LOG_DEBUG, "obj_allocX(\"%s\") -> %p [%s:%s:%u]", type, o, file, func, line); + r->magic = OBJ_MAGIC; + r->type = strdup(type); + write_log(LOG_DEBUG, "obj_allocX(\"%s\") -> %p [%s:%s:%u]", type, r, file, func, line); #if OBJ_BACKTRACE void *bt[4]; int addrs = backtrace(bt, 4); @@ -144,40 +147,10 @@ INLINE void __obj_init(struct obj *o, size_t size, void (*free_func)(void *) free(syms); #endif #endif - o->ref = 1; - o->free_func = free_func; - o->size = size; -} + r->ref = 1; + r->clear_func = clear_func; + r->free_func = free_func; -INLINE void *__obj_alloc(size_t size, void (*free_func)(void *) -#if OBJ_DEBUG -, const char *type, const char *file, const char *func, unsigned int line -#endif -) { - struct obj *r; - - r = g_malloc(size); - __obj_init(r, size, free_func -#if OBJ_DEBUG - , type, file, func, line -#endif - ); - return r; -} - -INLINE void *__obj_alloc0(size_t size, void (*free_func)(void *) -#if OBJ_DEBUG -, const char *type, const char *file, const char *func, unsigned int line -#endif -) { - struct obj *r; - - r = g_malloc0(size); - __obj_init(r, size, free_func -#if OBJ_DEBUG - , type, file, func, line -#endif - ); return r; } @@ -239,15 +212,14 @@ INLINE void __obj_put(struct obj *o #endif if (atomic_dec(&o->ref) != 1) return; - if (o->free_func) - o->free_func(o); + if (o->clear_func) + o->clear_func(o); #if OBJ_DEBUG o->magic = 0; if (o->type) free(o->type); #endif - if (o->size != -1) - g_free(o); + o->free_func(o); } diff --git a/t/test-transcode.c b/t/test-transcode.c index 156b5b3a8..3dede2f10 100644 --- a/t/test-transcode.c +++ b/t/test-transcode.c @@ -93,6 +93,9 @@ static void __start(const char *file, int line) { ssrc_A = 1234; ssrc_B = 2345; ZERO(call); +#if OBJ_DEBUG + call.obj.magic = OBJ_MAGIC; +#endif obj_hold(&call); call.tags = tags_ht_new(); call.callid = STR("test-call");