Skip to content

Commit 31d290b

Browse files
committed
Combine memory allocation for exclude_pattern items, simplify loops
1 parent 5836a6d commit 31d290b

File tree

3 files changed

+20
-30
lines changed

3 files changed

+20
-30
lines changed

client/castApp/src/caster.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,6 @@ void casterInit(caster_t *self)
120120
errlogPrintf("Error: casterInit failed to create shutdown socket: %d\n", SOCKERRNO);
121121
}
122122

123-
static void nodeFree(void *node) {
124-
string_list_t *temp = (string_list_t *)node;
125-
free(temp->item_str);
126-
free(temp);
127-
}
128-
129123
void casterShutdown(caster_t *self)
130124
{
131125
int i;
@@ -150,7 +144,7 @@ void casterShutdown(caster_t *self)
150144
epicsMutexUnlock(self->lock);
151145

152146
epicsMutexMustLock(self->lock);
153-
ellFree2(&self->exclude_patterns, &nodeFree);
147+
ellFree(&self->exclude_patterns);
154148
epicsMutexUnlock(self->lock);
155149

156150
epicsEventDestroy(self->shutdownEvent);

client/castApp/src/castinit.c

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#include <epicsExit.h>
88
#include <epicsMutex.h>
99
#include <epicsAssert.h>
10+
#include <cantProceed.h>
11+
#include <dbDefs.h>
1012
#include <iocsh.h>
1113
#include <errlog.h>
1214

@@ -202,6 +204,8 @@ static void addReccasterEnvVarsCallFunc(const iocshArgBuf *args)
202204

203205
void addReccasterExcludePattern(caster_t* self, int argc, char **argv) {
204206
size_t i;
207+
int dup;
208+
ELLNODE *cur;
205209
argv++; argc--; /* skip function arg */
206210
if (argc < 1) {
207211
errlogSevPrintf(errlogMinor, "At least one argument expected for addReccasterExcludePattern\n");
@@ -221,38 +225,31 @@ void addReccasterExcludePattern(caster_t* self, int argc, char **argv) {
221225
}
222226

223227
for (i = 0; i < argc; i++) {
228+
const size_t arg_len = strlen(argv[i]) + 1;
224229
if (argv[i][0] == '\0') {
225230
errlogSevPrintf(errlogMinor, "Arg is empty for addReccasterExcludePattern\n");
226231
continue;
227232
}
228233
/* check duplicates */
229-
int dup = 0;
230-
ELLNODE *cur = ellFirst(&self->exclude_patterns);
231-
while (cur != NULL) {
232-
string_list_t *temp = (string_list_t *)cur;
233-
if (strcmp(argv[i], temp->item_str) == 0) {
234+
dup = 0;
235+
for(cur = ellFirst(&self->exclude_patterns); cur; cur = ellNext(cur)) {
236+
const string_list_t *ppattern = CONTAINER(cur, string_list_t, node);
237+
if (strcmp(argv[i], ppattern->item_str) == 0) {
234238
dup = 1;
235239
break;
236240
}
237-
cur = ellNext(cur);
238241
}
239242
if (dup) {
240243
errlogSevPrintf(errlogMinor, "Duplicate pattern %s in addReccasterExcludePattern\n", argv[i]);
241244
continue;
242245
}
243-
string_list_t *new_list = malloc(sizeof(string_list_t));
244-
if (new_list == NULL) {
245-
errlogSevPrintf(errlogMajor, "Error in addReccasterExcludePattern - malloc error for creating linked list node");
246-
break;
247-
}
248-
new_list->item_str = strdup(argv[i]);
249-
if (new_list->item_str == NULL) {
250-
errlogSevPrintf(errlogMajor, "Error in addReccasterExcludePattern - strdup error for copying %s to new->item_str from addReccasterExcludePattern\n", argv[i]);
251-
free(new_list); /* frees if strdup fails */
252-
break;
253-
}
254-
ellAdd(&self->exclude_patterns, &new_list->node);
246+
string_list_t *new_node = mallocMustSucceed(sizeof(string_list_t) + arg_len, "addReccasterExcludePattern");
247+
new_node->item_str = (char *)(new_node + 1);
248+
memcpy(new_node->item_str, argv[i], arg_len);
249+
250+
ellAdd(&self->exclude_patterns, &new_node->node);
255251
}
252+
256253
epicsMutexUnlock(self->lock);
257254
}
258255

client/castApp/src/dbcb.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <epicsVersion.h>
33
#include <epicsString.h>
44
#include <envDefs.h>
5+
#include <dbDefs.h>
56

67
#include <dbStaticLib.h>
78
#include <dbAccess.h>
@@ -94,12 +95,10 @@ static int pushRecord(caster_t *caster, DBENTRY *pent)
9495
if(dbIsAlias(pent))
9596
return 0;
9697

97-
cur = ellFirst(&caster->exclude_patterns);
98-
while (cur != NULL) {
99-
string_list_t *temp = (string_list_t *)cur;
100-
if(epicsStrGlobMatch(prec->name, temp->item_str))
98+
for(cur = ellFirst(&caster->exclude_patterns); cur; cur = ellNext(cur)) {
99+
const string_list_t *ppattern = CONTAINER(cur, string_list_t, node);
100+
if(epicsStrGlobMatch(prec->name, ppattern->item_str))
101101
return 0;
102-
cur = ellNext(cur);
103102
}
104103

105104
rid = casterSendRecord(caster, prec->rdes->name, prec->name);

0 commit comments

Comments
 (0)