ctdb-tools: Add event daemon config options to config tool
[vlendec/samba-autobuild/.git] / ctdb / common / reqid.c
1 /*
2    ctdb request id handling code
3
4    Copyright (C) Amitay Isaacs  2015
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "replace.h"
21
22 #include <talloc.h>
23
24 #include "lib/util/idtree.h"
25 #include "reqid.h"
26
27 struct reqid_context {
28         struct idr_context *idr;
29         uint32_t lastid;
30 };
31
32 int reqid_init(TALLOC_CTX *mem_ctx, int start_id,
33                struct reqid_context **result)
34 {
35         struct reqid_context *reqid_ctx;
36
37         reqid_ctx = talloc_zero(mem_ctx, struct reqid_context);
38         if (reqid_ctx == NULL) {
39                 return ENOMEM;
40         }
41
42         reqid_ctx->idr = idr_init(reqid_ctx);
43         if (reqid_ctx->idr == NULL) {
44                 talloc_free(reqid_ctx);
45                 return ENOMEM;
46         }
47
48         if (start_id <= 0) {
49                 start_id = 1;
50         }
51         reqid_ctx->lastid = start_id;
52
53         *result = reqid_ctx;
54         return 0;
55 }
56
57 uint32_t reqid_new(struct reqid_context *reqid_ctx, void *private_data)
58 {
59         int id;
60
61         id = idr_get_new_above(reqid_ctx->idr, private_data,
62                                reqid_ctx->lastid+1, INT_MAX);
63         if (id < 0) {
64                 /* reqid wrapped */
65                 id = idr_get_new(reqid_ctx->idr, private_data, INT_MAX);
66         }
67         if (id == -1) {
68                 return REQID_INVALID;
69         }
70
71         reqid_ctx->lastid = id;
72         return id;
73 }
74
75 void *_reqid_find(struct reqid_context *reqid_ctx, uint32_t reqid)
76 {
77         return idr_find(reqid_ctx->idr, reqid);
78 }
79
80 int reqid_remove(struct reqid_context *reqid_ctx, uint32_t reqid)
81 {
82         int ret;
83
84         ret = idr_remove(reqid_ctx->idr, reqid);
85         if (ret < 0) {
86                 return ENOENT;
87         }
88         return 0;
89 }