comparison libfdcore/hooks.c @ 1329:175f2eb883a0

Add function to retrieve pmd structure associated with a request. Returns NULL in case of error.
author Thomas Klausner <tk@giga.or.at>
date Mon, 27 Nov 2017 15:27:31 +0100
parents 7a2ab0087788
children 3cbe458fbfa9
comparison
equal deleted inserted replaced
1328:81af4f5a517a 1329:175f2eb883a0
193 pmdl_free(pmdl); 193 pmdl_free(pmdl);
194 /* We're done */ 194 /* We're done */
195 } 195 }
196 196
197 /* Return the location of the permsgdata area corresponding to this handle, after eventually having created it. Return NULL in case of failure */ 197 /* Return the location of the permsgdata area corresponding to this handle, after eventually having created it. Return NULL in case of failure */
198 static struct fd_hook_permsgdata * get_or_create_pmd(struct fd_msg_pmdl *pmdl, struct fd_hook_hdl * h) 198 static struct fd_hook_permsgdata * get_or_create_pmd(struct fd_msg_pmdl *pmdl, struct fd_hook_data_hdl * h)
199 { 199 {
200 struct fd_hook_permsgdata * ret = NULL; 200 struct fd_hook_permsgdata * ret = NULL;
201 struct fd_list * li; 201 struct fd_list * li;
202 202
203 CHECK_POSIX_DO( pthread_mutex_lock(&pmdl->lock), ); 203 CHECK_POSIX_DO( pthread_mutex_lock(&pmdl->lock), );
207 } 207 }
208 208
209 /* Search in the list for an item with the same handle. The list is ordered by this handle */ 209 /* Search in the list for an item with the same handle. The list is ordered by this handle */
210 for (li=pmdl->sentinel.next; li != &pmdl->sentinel; li = li->next) { 210 for (li=pmdl->sentinel.next; li != &pmdl->sentinel; li = li->next) {
211 struct pmd_list_item * pli = (struct pmd_list_item *) li; 211 struct pmd_list_item * pli = (struct pmd_list_item *) li;
212 if (pli->hdl == h->data_hdl) 212 if (pli->hdl == h)
213 ret = &pli->pmd; 213 ret = &pli->pmd;
214 if (pli->hdl >= h->data_hdl) 214 if (pli->hdl >= h)
215 break; 215 break;
216 } 216 }
217 if (!ret) { 217 if (!ret) {
218 /* we need to create a new one and insert before li */ 218 /* we need to create a new one and insert before li */
219 struct pmd_list_item * pli; 219 struct pmd_list_item * pli;
220 CHECK_MALLOC_DO( pli = malloc(sizeof_pmd(h->data_hdl)), ); 220 CHECK_MALLOC_DO( pli = malloc(sizeof_pmd(h)), );
221 if (pli) { 221 if (pli) {
222 memset(pli, 0, sizeof_pmd(h->data_hdl)); 222 memset(pli, 0, sizeof_pmd(h));
223 fd_list_init(&pli->chain, pli); 223 fd_list_init(&pli->chain, pli);
224 pli->hdl = h->data_hdl; 224 pli->hdl = h;
225 ret = &pli->pmd; 225 ret = &pli->pmd;
226 if (h->data_hdl->pmd_init_cb) { 226 if (h->pmd_init_cb) {
227 (*h->data_hdl->pmd_init_cb)(ret); 227 (*h->pmd_init_cb)(ret);
228 } 228 }
229 fd_list_insert_before(li, &pli->chain); 229 fd_list_insert_before(li, &pli->chain);
230 } 230 }
231 } 231 }
232 232
233 CHECK_POSIX_DO( pthread_mutex_unlock(&pmdl->lock), ); 233 CHECK_POSIX_DO( pthread_mutex_unlock(&pmdl->lock), );
234 return ret; 234 return ret;
235 }
236
237 struct fd_hook_permsgdata * fd_hook_get_pmd(struct fd_hook_data_hdl *data_hdl, struct msg * msg)
238 {
239 return get_or_create_pmd(fd_msg_pmdl_get(msg), data_hdl);
235 } 240 }
236 241
237 struct fd_hook_permsgdata * fd_hook_get_request_pmd(struct fd_hook_data_hdl *data_hdl, struct msg * answer) 242 struct fd_hook_permsgdata * fd_hook_get_request_pmd(struct fd_hook_data_hdl *data_hdl, struct msg * answer)
238 { 243 {
239 struct msg * qry; 244 struct msg * qry;
304 struct fd_hook_hdl * h = (struct fd_hook_hdl *)li->o; 309 struct fd_hook_hdl * h = (struct fd_hook_hdl *)li->o;
305 struct fd_hook_permsgdata * pmd = NULL; 310 struct fd_hook_permsgdata * pmd = NULL;
306 311
307 /* do we need to handle pmd ? */ 312 /* do we need to handle pmd ? */
308 if (h->data_hdl && pmdl) { 313 if (h->data_hdl && pmdl) {
309 pmd = get_or_create_pmd(pmdl, h); 314 pmd = get_or_create_pmd(pmdl, h->data_hdl);
310 } 315 }
311 316
312 /* Now, call this callback */ 317 /* Now, call this callback */
313 (*h->fd_hook_cb)(type, msg, &peer->p_hdr, other, pmd, h->regdata); 318 (*h->fd_hook_cb)(type, msg, &peer->p_hdr, other, pmd, h->regdata);
314 } 319 }
"Welcome to our mercurial repository"