Debian Codegraph @

src/bus/reply.c

raw
1 /*
2 * Reply Registry
3 */
4
5 #include <c-list.h>
6 #include <c-rbtree.h>
7 #include <c-stdaux.h>
8 #include <stdlib.h>
9 #include "bus/reply.h"
10 #include "util/error.h"
11 #include "util/user.h"
12
13 typedef struct ReplySlotKey ReplySlotKey;
14
15 struct ReplySlotKey {
16 uint64_t id;
17 uint32_t serial;
18 };
19
20 static int reply_slot_compare(CRBTree *tree, void *k, CRBNode *rb) {
21 ReplySlot *slot = c_container_of(rb, ReplySlot, registry_node);
22 ReplySlotKey *key = k;
23
24 if (key->id < slot->id)
25 return -1;
26 if (key->id > slot->id)
27 return 1;
28
29 if (key->serial < slot->serial)
30 return -1;
31 if (key->serial > slot->serial)
32 return 1;
33
34 return 0;
35 }
36
37 int reply_slot_new(ReplySlot **replyp, ReplyRegistry *registry, ReplyOwner *owner, User *user, User *actor, uint64_t id, uint32_t serial) {
38 ReplySlot *reply;
39 CRBNode **slot, *parent;
40 ReplySlotKey key = {
41 .id = id,
42 .serial = serial,
43 };
44 int r;
45
46 slot = c_rbtree_find_slot(&registry->reply_tree, reply_slot_compare, &key, &parent);
47 if (!slot)
48 return REPLY_E_EXISTS;
49
50 reply = calloc(1, sizeof(*reply));
51 if (!reply)
52 return error_origin(-ENOMEM);
53
54 reply->registry = registry;
55 reply->owner = owner;
56 reply->charge = (UserCharge)USER_CHARGE_INIT;
57 reply->registry_node = (CRBNode)C_RBNODE_INIT(reply->registry_node);
58 reply->owner_link = (CList)C_LIST_INIT(reply->owner_link);
59 reply->id = id;
60 reply->serial = serial;
61
62 r = user_charge(user, &reply->charge, actor, USER_SLOT_OBJECTS, 1);
63 if (r)
64 return (r == USER_E_QUOTA) ? REPLY_E_QUOTA : error_fold(r);
65
66 c_rbtree_add(&registry->reply_tree, parent, slot, &reply->registry_node);
67 c_list_link_tail(&owner->reply_list, &reply->owner_link);
68
69 *replyp = reply;
70
71 return 0;
72 }
73
74 ReplySlot *reply_slot_free(ReplySlot *slot) {
75 if (!slot)
76 return NULL;
77
78 user_charge_deinit(&slot->charge);
79 c_list_unlink(&slot->owner_link);
80 c_rbnode_unlink(&slot->registry_node);
81
82 free(slot);
83
84 return NULL;
85 }
86
87 ReplySlot *reply_slot_get_by_id(ReplyRegistry *registry, uint64_t id, uint32_t serial) {
88 ReplySlotKey key = {
89 .id = id,
90 .serial = serial,
91 };
92
93 return c_rbtree_find_entry(&registry->reply_tree, reply_slot_compare, &key, ReplySlot, registry_node);
94 }
95
96 void reply_registry_init(ReplyRegistry *registry) {
97 *registry = (ReplyRegistry)REPLY_REGISTRY_INIT;
98 }
99
100 void reply_registry_deinit(ReplyRegistry *registry) {
101 c_assert(c_rbtree_is_empty(&registry->reply_tree));
102 }
103
104 void reply_owner_init(ReplyOwner *owner) {
105 *owner = (ReplyOwner)REPLY_OWNER_INIT(*owner);
106 }
107
108 void reply_owner_deinit(ReplyOwner *owner) {
109 c_assert(c_list_is_empty(&owner->reply_list));
110 }
111
112 void reply_owner_get_stats(ReplyOwner *owner, unsigned int *n_objectsp) {
113 ReplySlot *reply;
114 unsigned int n_objects = 0;
115
116 c_list_for_each_entry(reply, &owner->reply_list, owner_link)
117 n_objects += reply->charge.charge;
118
119 *n_objectsp = n_objects;
120 }