Lines Matching refs:db
39 static void mutex_lock(struct handle_db *db) in mutex_lock() argument
41 if (db->mu) in mutex_lock()
42 pthread_mutex_lock(db->mu); in mutex_lock()
45 static void mutex_unlock(struct handle_db *db) in mutex_unlock() argument
47 if (db->mu) in mutex_unlock()
48 pthread_mutex_unlock(db->mu); in mutex_unlock()
52 void handle_db_set_mutex(struct handle_db *db, pthread_mutex_t *mu) in handle_db_set_mutex() argument
54 db->mu = mu; in handle_db_set_mutex()
57 void handle_db_destroy(struct handle_db *db) in handle_db_destroy() argument
59 if (db) { in handle_db_destroy()
60 mutex_lock(db); in handle_db_destroy()
61 free(db->ptrs); in handle_db_destroy()
62 db->ptrs = NULL; in handle_db_destroy()
63 db->max_ptrs = 0; in handle_db_destroy()
64 mutex_unlock(db); in handle_db_destroy()
68 int handle_get(struct handle_db *db, void *ptr) in handle_get() argument
75 if (!db || !ptr) in handle_get()
78 mutex_lock(db); in handle_get()
81 for (n = 0; n < db->max_ptrs; n++) { in handle_get()
82 if (!db->ptrs[n]) { in handle_get()
83 db->ptrs[n] = ptr; in handle_get()
90 if (db->max_ptrs) in handle_get()
91 new_max_ptrs = db->max_ptrs * 2; in handle_get()
94 p = realloc(db->ptrs, new_max_ptrs * sizeof(void *)); in handle_get()
99 db->ptrs = p; in handle_get()
100 memset(db->ptrs + db->max_ptrs, 0, in handle_get()
101 (new_max_ptrs - db->max_ptrs) * sizeof(void *)); in handle_get()
102 db->max_ptrs = new_max_ptrs; in handle_get()
105 db->ptrs[n] = ptr; in handle_get()
109 mutex_unlock(db); in handle_get()
113 void *handle_put(struct handle_db *db, int handle) in handle_put() argument
117 if (!db || handle < 0) in handle_put()
120 mutex_lock(db); in handle_put()
122 if ((size_t)handle >= db->max_ptrs) { in handle_put()
127 p = db->ptrs[handle]; in handle_put()
128 db->ptrs[handle] = NULL; in handle_put()
131 mutex_unlock(db); in handle_put()
135 void *handle_lookup(struct handle_db *db, int handle) in handle_lookup() argument
139 if (!db || handle < 0) in handle_lookup()
142 mutex_lock(db); in handle_lookup()
144 if ((size_t)handle >= db->max_ptrs) { in handle_lookup()
149 p = db->ptrs[handle]; in handle_lookup()
152 mutex_unlock(db); in handle_lookup()
156 void handle_foreach_put(struct handle_db *db, in handle_foreach_put() argument
162 if (!db || !cb) in handle_foreach_put()
165 mutex_lock(db); in handle_foreach_put()
167 for (n = 0; n < db->max_ptrs; n++) { in handle_foreach_put()
168 if (db->ptrs[n]) { in handle_foreach_put()
169 cb(n, db->ptrs[n], arg); in handle_foreach_put()
170 db->ptrs[n] = NULL; in handle_foreach_put()
174 mutex_unlock(db); in handle_foreach_put()