Debian Codegraph @

runtime/prop.c

raw
1 /* prop.c - rsyslog's prop object
2 *
3 * This object is meant to support message properties that are stored
4 * seperately from the message. The main intent is to support properties
5 * that are "constant" during a period of time, so that many messages may
6 * contain a reference to the same property. It is important, though, that
7 * properties are destroyed when they are no longer needed.
8 *
9 * Please note that this is a performance-critical part of the software and
10 * as such we may use some methods in here which do not look elegant, but
11 * which are fast...
12 *
13 * Module begun 2009-06-17 by Rainer Gerhards
14 *
15 * Copyright 2009-2016 Adiscon GmbH.
16 *
17 * This file is part of the rsyslog runtime library.
18 *
19 * Licensed under the Apache License, Version 2.0 (the "License");
20 * you may not use this file except in compliance with the License.
21 * You may obtain a copy of the License at
22 *
23 * http://www.apache.org/licenses/LICENSE-2.0
24 * -or-
25 * see COPYING.ASL20 in the source distribution
26 *
27 * Unless required by applicable law or agreed to in writing, software
28 * distributed under the License is distributed on an "AS IS" BASIS,
29 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
30 * See the License for the specific language governing permissions and
31 * limitations under the License.
32 */
33
34 #include "config.h"
35 #include <stdlib.h>
36 #include <assert.h>
37 #include <string.h>
38
39 #include "rsyslog.h"
40 #include "obj.h"
41 #include "obj-types.h"
42 #include "unicode-helper.h"
43 #include "atomic.h"
44 #include "prop.h"
45
46 /* static data */
47 DEFobjStaticHelpers;
48
49 // extern uchar *propGetSzStr(prop_t *pThis); /* expand inline function here */
50
51 /* Standard-Constructor
52 */
53 BEGINobjConstruct(prop) /* be sure to specify the object type also in END macro! */
54 pThis->iRefCount = 1;
55 INIT_ATOMIC_HELPER_MUT(pThis->mutRefCount);
56 ENDobjConstruct(prop)
57
58
59 /* destructor for the prop object */
60 BEGINobjDestruct(prop) /* be sure to specify the object type also in END and CODESTART macros! */
61 int currRefCount;
62 CODESTARTobjDestruct(prop);
63 currRefCount = ATOMIC_DEC_AND_FETCH(&pThis->iRefCount, &pThis->mutRefCount);
64 if (currRefCount == 0) {
65 /* (only) in this case we need to actually destruct the object */
66 if (pThis->len >= CONF_PROP_BUFSIZE) free(pThis->szVal.psz);
67 DESTROY_ATOMIC_HELPER_MUT(pThis->mutRefCount);
68 } else {
69 pThis = NULL; /* tell framework NOT to destructing the object! */
70 }
71 ENDobjDestruct(prop)
72
73 /* set string, we make our own private copy! This MUST only be called BEFORE
74 * ConstructFinalize()!
75 */
76 static rsRetVal SetString(prop_t *pThis, const uchar *psz, const int len) {
77 DEFiRet;
78 ISOBJ_TYPE_assert(pThis, prop);
79 if (pThis->len >= CONF_PROP_BUFSIZE) free(pThis->szVal.psz);
80 pThis->len = len;
81 if (len < CONF_PROP_BUFSIZE) {
82 memcpy(pThis->szVal.sz, psz, len + 1);
83 } else {
84 if (pThis->szVal.psz != NULL) {
85 free(pThis->szVal.psz);
86 }
87 CHKmalloc(pThis->szVal.psz = malloc(len + 1));
88 memcpy(pThis->szVal.psz, psz, len + 1);
89 }
90
91 finalize_it:
92 RETiRet;
93 }
94
95
96 /* get string length */
97 static int GetStringLen(prop_t *pThis) {
98 return pThis->len;
99 }
100
101
102 /* get string */
103 static rsRetVal GetString(prop_t *pThis, uchar **ppsz, int *plen) {
104 ISOBJ_TYPE_assert(pThis, prop);
105 if (pThis->len < CONF_PROP_BUFSIZE) {
106 *ppsz = pThis->szVal.sz;
107 } else {
108 *ppsz = pThis->szVal.psz;
109 }
110 *plen = pThis->len;
111 return RS_RET_OK;
112 }
113
114
115 /* ConstructionFinalizer
116 * rgerhards, 2008-01-09
117 */
118 static rsRetVal propConstructFinalize(prop_t __attribute__((unused)) * pThis) {
119 DEFiRet;
120 ISOBJ_TYPE_assert(pThis, prop);
121 RETiRet;
122 }
123
124
125 /* add a new reference. It is VERY IMPORTANT to call this function whenever
126 * the property is handed over to some entitiy that later call Destruct() on it.
127 */
128 static rsRetVal AddRef(prop_t *pThis) {
129 if (pThis == NULL) {
130 DBGPRINTF(
131 "prop/AddRef is passed a NULL ptr - ignoring it "
132 "- further problems may occur\n");
133 FINALIZE;
134 }
135 ATOMIC_INC(&pThis->iRefCount, &pThis->mutRefCount);
136 finalize_it:
137 return RS_RET_OK;
138 }
139
140
141 /* this is a "do it all in one shot" function that creates a new property,
142 * assigns the provided string to it and finalizes the property. Among the
143 * convenience, it is also (very, very) slightly faster.
144 * rgerhards, 2009-07-01
145 */
146 static rsRetVal CreateStringProp(prop_t **ppThis, const uchar *psz, const int len) {
147 prop_t *pThis = NULL;
148 DEFiRet;
149
150 CHKiRet(propConstruct(&pThis));
151 CHKiRet(SetString(pThis, psz, len));
152 CHKiRet(propConstructFinalize(pThis));
153 *ppThis = pThis;
154 finalize_it:
155 if (iRet != RS_RET_OK) {
156 if (pThis != NULL) propDestruct(&pThis);
157 }
158
159 RETiRet;
160 }
161
162 /* another one-stop function, quite useful: it takes a property pointer and
163 * a string. If the string is already contained in the property, nothing happens.
164 * If the string is different (or the pointer NULL), the current property
165 * is destructed and a new one created. This can be used to get a specific
166 * name in those cases where there is a good chance that the property
167 * immediatly previously processed already contained the value we need - in
168 * which case we save us all the creation overhead by just reusing the already
169 * existing property).
170 * rgerhards, 2009-07-01
171 */
172 static rsRetVal CreateOrReuseStringProp(prop_t **ppThis, const uchar *psz, const int len) {
173 uchar *pszPrev;
174 int lenPrev;
175 DEFiRet;
176 assert(ppThis != NULL);
177
178 if (*ppThis == NULL) {
179 /* we need to create a property */
180 CHKiRet(CreateStringProp(ppThis, psz, len));
181 } else {
182 /* already exists, check if we can re-use it */
183 GetString(*ppThis, &pszPrev, &lenPrev);
184 if (len != lenPrev || ustrcmp(psz, pszPrev)) {
185 /* different, need to discard old & create new one */
186 propDestruct(ppThis);
187 CHKiRet(CreateStringProp(ppThis, psz, len));
188 } /* else we can re-use the existing one! */
189 }
190
191 finalize_it:
192 RETiRet;
193 }
194
195
196 /* debugprint for the prop object */
197 BEGINobjDebugPrint(prop) /* be sure to specify the object type also in END and CODESTART macros! */
198 CODESTARTobjDebugPrint(prop);
199 dbgprintf("prop object %p - no further debug info implemented\n", pThis);
200 ENDobjDebugPrint(prop)
201
202
203 /* queryInterface function
204 * rgerhards, 2008-02-21
205 */
206 BEGINobjQueryInterface(prop)
207 CODESTARTobjQueryInterface(prop);
208 if (pIf->ifVersion != propCURR_IF_VERSION) { /* check for current version, increment on each change */
209 ABORT_FINALIZE(RS_RET_INTERFACE_NOT_SUPPORTED);
210 }
211
212 /* ok, we have the right interface, so let's fill it
213 * Please note that we may also do some backwards-compatibility
214 * work here (if we can support an older interface version - that,
215 * of course, also affects the "if" above).
216 */
217 pIf->Construct = propConstruct;
218 pIf->ConstructFinalize = propConstructFinalize;
219 pIf->Destruct = propDestruct;
220 pIf->DebugPrint = propDebugPrint;
221 pIf->SetString = SetString;
222 pIf->GetString = GetString;
223 pIf->GetStringLen = GetStringLen;
224 pIf->AddRef = AddRef;
225 pIf->CreateStringProp = CreateStringProp;
226 pIf->CreateOrReuseStringProp = CreateOrReuseStringProp;
227
228 finalize_it:
229 ENDobjQueryInterface(prop)
230
231
232 /* Exit the prop class.
233 * rgerhards, 2009-04-06
234 */
235 BEGINObjClassExit(prop, OBJ_IS_CORE_MODULE) /* class, version */
236 ENDObjClassExit(prop)
237
238
239 /* Initialize the prop class. Must be called as the very first method
240 * before anything else is called inside this class.
241 * rgerhards, 2008-02-19
242 */
243 BEGINObjClassInit(prop, 1, OBJ_IS_CORE_MODULE) /* class, version */
244 /* request objects we use */
245
246 /* set our own handlers */
247 OBJSetMethodHandler(objMethod_DEBUGPRINT, propDebugPrint);
248 OBJSetMethodHandler(objMethod_CONSTRUCTION_FINALIZER, propConstructFinalize);
249 ENDObjClassInit(prop)
250
251 /* vi:set ai:
252 */