reqid.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. #include "zebra.h"
  2. #include "hash.h"
  3. #include "nhrpd.h"
  4. static unsigned int nhrp_reqid_key(void *data)
  5. {
  6. struct nhrp_reqid *r = data;
  7. return r->request_id;
  8. }
  9. static int nhrp_reqid_cmp(const void *data, const void *key)
  10. {
  11. const struct nhrp_reqid *a = data, *b = key;
  12. return a->request_id == b->request_id;
  13. }
  14. uint32_t nhrp_reqid_alloc(struct nhrp_reqid_pool *p, struct nhrp_reqid *r, void (*cb)(struct nhrp_reqid *, void *))
  15. {
  16. if (!p->reqid_hash) {
  17. p->reqid_hash = hash_create(nhrp_reqid_key, nhrp_reqid_cmp);
  18. p->next_request_id = 1;
  19. }
  20. if (r->cb != cb) {
  21. r->request_id = p->next_request_id;
  22. if (++p->next_request_id == 0) p->next_request_id = 1;
  23. r->cb = cb;
  24. hash_get(p->reqid_hash, r, hash_alloc_intern);
  25. }
  26. return r->request_id;
  27. }
  28. void nhrp_reqid_free(struct nhrp_reqid_pool *p, struct nhrp_reqid *r)
  29. {
  30. if (r->cb) {
  31. hash_release(p->reqid_hash, r);
  32. r->cb = NULL;
  33. }
  34. }
  35. struct nhrp_reqid *nhrp_reqid_lookup(struct nhrp_reqid_pool *p, uint32_t reqid)
  36. {
  37. struct nhrp_reqid key;
  38. if (!p->reqid_hash) return 0;
  39. key.request_id = reqid;
  40. return hash_lookup(p->reqid_hash, &key);
  41. }