Lines Matching +full:cs +full:- +full:1
1 // SPDX-License-Identifier: GPL-2.0
22 static void comm_strs__remove_if_last(struct comm_str *cs);
41 static refcount_t *comm_str__refcnt(struct comm_str *cs) in comm_str__refcnt() argument
43 return &RC_CHK_ACCESS(cs)->refcnt; in comm_str__refcnt()
46 static const char *comm_str__str(const struct comm_str *cs) in comm_str__str() argument
48 return &RC_CHK_ACCESS(cs)->str[0]; in comm_str__str()
51 static struct comm_str *comm_str__get(struct comm_str *cs) in comm_str__get() argument
55 if (RC_CHK_GET(result, cs)) in comm_str__get()
56 refcount_inc_not_zero(comm_str__refcnt(cs)); in comm_str__get()
61 static void comm_str__put(struct comm_str *cs) in comm_str__put() argument
63 if (!cs) in comm_str__put()
66 if (refcount_dec_and_test(comm_str__refcnt(cs))) { in comm_str__put()
67 RC_CHK_FREE(cs); in comm_str__put()
69 if (refcount_read(comm_str__refcnt(cs)) == 1) in comm_str__put()
70 comm_strs__remove_if_last(cs); in comm_str__put()
72 RC_CHK_PUT(cs); in comm_str__put()
79 RC_STRUCT(comm_str) *cs; in comm_str__new()
81 cs = malloc(sizeof(*cs) + strlen(str) + 1); in comm_str__new()
82 if (ADD_RC_CHK(result, cs)) { in comm_str__new()
83 refcount_set(comm_str__refcnt(result), 1); in comm_str__new()
84 strcpy(&cs->str[0], str); in comm_str__new()
97 static void comm_strs__remove_if_last(struct comm_str *cs) in comm_strs__remove_if_last() argument
101 down_write(&comm_strs->lock); in comm_strs__remove_if_last()
106 if (refcount_read(comm_str__refcnt(cs)) == 1) { in comm_strs__remove_if_last()
109 entry = bsearch(comm_str__str(cs), comm_strs->strs, comm_strs->num_strs, in comm_strs__remove_if_last()
112 for (int i = entry - comm_strs->strs; i < comm_strs->num_strs - 1; i++) in comm_strs__remove_if_last()
113 comm_strs->strs[i] = comm_strs->strs[i + 1]; in comm_strs__remove_if_last()
114 comm_strs->num_strs--; in comm_strs__remove_if_last()
116 up_write(&comm_strs->lock); in comm_strs__remove_if_last()
123 result = bsearch(str, comm_strs->strs, comm_strs->num_strs, sizeof(struct comm_str *), in __comm_strs__find()
140 down_read(&comm_strs->lock); in comm_strs__findnew()
142 up_read(&comm_strs->lock); in comm_strs__findnew()
146 down_write(&comm_strs->lock); in comm_strs__findnew()
149 if (comm_strs->num_strs == comm_strs->capacity) { in comm_strs__findnew()
152 tmp = reallocarray(comm_strs->strs, in comm_strs__findnew()
153 comm_strs->capacity + 16, in comm_strs__findnew()
154 sizeof(*comm_strs->strs)); in comm_strs__findnew()
156 up_write(&comm_strs->lock); in comm_strs__findnew()
159 comm_strs->strs = tmp; in comm_strs__findnew()
160 comm_strs->capacity += 16; in comm_strs__findnew()
164 int low = 0, high = comm_strs->num_strs - 1; in comm_strs__findnew()
165 int insert = comm_strs->num_strs; /* Default to inserting at the end. */ in comm_strs__findnew()
168 int mid = low + (high - low) / 2; in comm_strs__findnew()
169 int cmp = strcmp(comm_str__str(comm_strs->strs[mid]), str); in comm_strs__findnew()
172 low = mid + 1; in comm_strs__findnew()
174 high = mid - 1; in comm_strs__findnew()
178 memmove(&comm_strs->strs[insert + 1], &comm_strs->strs[insert], in comm_strs__findnew()
179 (comm_strs->num_strs - insert) * sizeof(struct comm_str *)); in comm_strs__findnew()
180 comm_strs->num_strs++; in comm_strs__findnew()
181 comm_strs->strs[insert] = result; in comm_strs__findnew()
184 up_write(&comm_strs->lock); in comm_strs__findnew()
195 comm->start = timestamp; in comm__new()
196 comm->exec = exec; in comm__new()
198 comm->comm_str = comm_strs__findnew(str); in comm__new()
199 if (!comm->comm_str) { in comm__new()
209 struct comm_str *new, *old = comm->comm_str; in comm__override()
213 return -ENOMEM; in comm__override()
216 comm->comm_str = new; in comm__override()
217 comm->start = timestamp; in comm__override()
219 comm->exec = true; in comm__override()
226 comm_str__put(comm->comm_str); in comm__free()
232 return comm_str__str(comm->comm_str); in comm__str()