srvsvc: Avoid a dependency on share_mode_entry->id
[Samba.git] / ctdb / common / reqid.c
blob0e651cfa530ed99138c621cac4aeda4af7064f11
1 /*
2 ctdb request id handling code
4 Copyright (C) Amitay Isaacs 2015
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.
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.
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/>.
20 #include "replace.h"
22 #include <talloc.h>
24 #include "lib/util/idtree.h"
25 #include "reqid.h"
27 struct reqid_context {
28 struct idr_context *idr;
29 uint32_t lastid;
32 int reqid_init(TALLOC_CTX *mem_ctx, int start_id,
33 struct reqid_context **result)
35 struct reqid_context *reqid_ctx;
37 reqid_ctx = talloc_zero(mem_ctx, struct reqid_context);
38 if (reqid_ctx == NULL) {
39 return ENOMEM;
42 reqid_ctx->idr = idr_init(reqid_ctx);
43 if (reqid_ctx->idr == NULL) {
44 talloc_free(reqid_ctx);
45 return ENOMEM;
48 if (start_id <= 0) {
49 start_id = 1;
51 reqid_ctx->lastid = start_id;
53 *result = reqid_ctx;
54 return 0;
57 uint32_t reqid_new(struct reqid_context *reqid_ctx, void *private_data)
59 int id;
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);
67 if (id == -1) {
68 return REQID_INVALID;
71 reqid_ctx->lastid = id;
72 return id;
75 void *_reqid_find(struct reqid_context *reqid_ctx, uint32_t reqid)
77 return idr_find(reqid_ctx->idr, reqid);
80 int reqid_remove(struct reqid_context *reqid_ctx, uint32_t reqid)
82 int ret;
84 ret = idr_remove(reqid_ctx->idr, reqid);
85 if (ret < 0) {
86 return ENOENT;
88 return 0;