| 1 |
/* linkedlist.c |
| 2 |
* This file set implements a generic linked list object. It can be used |
| 3 |
* wherever a linke list is required. |
| 4 |
* |
| 5 |
* NOTE: we do not currently provide a constructor and destructor for the |
| 6 |
* object itself as we assume it will always be part of another strucuture. |
| 7 |
* Having a pointer to it, I think, does not really make sense but costs |
| 8 |
* performance. Consequently, there is is llInit() and llDestroy() and they |
| 9 |
* do what a constructor and destructur do, except for creating the |
| 10 |
* linkedList_t structure itself. |
| 11 |
* |
| 12 |
* File begun on 2007-07-31 by RGerhards |
| 13 |
* |
| 14 |
* Copyright (C) 2007-2012 Adiscon GmbH. |
| 15 |
* |
| 16 |
* This file is part of the rsyslog runtime library. |
| 17 |
* |
| 18 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 19 |
* you may not use this file except in compliance with the License. |
| 20 |
* You may obtain a copy of the License at |
| 21 |
* |
| 22 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 23 |
* -or- |
| 24 |
* see COPYING.ASL20 in the source distribution |
| 25 |
* |
| 26 |
* Unless required by applicable law or agreed to in writing, software |
| 27 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 28 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 29 |
* See the License for the specific language governing permissions and |
| 30 |
* limitations under the License. |
| 31 |
*/ |
| 32 |
#include "config.h" |
| 33 |
|
| 34 |
#include <stdio.h> |
| 35 |
#include <stdlib.h> |
| 36 |
#include <assert.h> |
| 37 |
|
| 38 |
#include "rsyslog.h" |
| 39 |
#include "linkedlist.h" |
| 40 |
|
| 41 |
|
| 42 |
/* Initialize an existing linkedList_t structure |
| 43 |
* pKey destructor may be zero to take care of non-keyed lists. |
| 44 |
*/ |
| 45 |
rsRetVal llInit(linkedList_t *pThis, |
| 46 |
rsRetVal (*pEltDestructor)(void *), |
| 47 |
rsRetVal (*pKeyDestructor)(void *), |
| 48 |
int (*pCmpOp)(void *, void *)) { |
| 49 |
assert(pThis != NULL); |
| 50 |
assert(pEltDestructor != NULL); |
| 51 |
|
| 52 |
pThis->pEltDestruct = pEltDestructor; |
| 53 |
pThis->pKeyDestruct = pKeyDestructor; |
| 54 |
pThis->cmpOp = pCmpOp; |
| 55 |
pThis->pKey = NULL; |
| 56 |
pThis->iNumElts = 0; |
| 57 |
pThis->pRoot = NULL; |
| 58 |
pThis->pLast = NULL; |
| 59 |
|
| 60 |
return RS_RET_OK; |
| 61 |
}; |
| 62 |
|
| 63 |
|
| 64 |
/* llDestroyEltData - destroys a list element |
| 65 |
* It is a separate function as the |
| 66 |
* functionality is needed in multiple code-pathes. |
| 67 |
*/ |
| 68 |
static rsRetVal llDestroyElt(linkedList_t *pList, llElt_t *pElt) { |
| 69 |
DEFiRet; |
| 70 |
|
| 71 |
assert(pList != NULL); |
| 72 |
assert(pElt != NULL); |
| 73 |
|
| 74 |
/* we ignore errors during destruction, as we need to try |
| 75 |
* free the element in any case. |
| 76 |
*/ |
| 77 |
if (pElt->pData != NULL) pList->pEltDestruct(pElt->pData); |
| 78 |
if (pElt->pKey != NULL) pList->pKeyDestruct(pElt->pKey); |
| 79 |
free(pElt); |
| 80 |
pList->iNumElts--; /* one less */ |
| 81 |
|
| 82 |
RETiRet; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
/* llDestroy - destroys a COMPLETE linkedList |
| 87 |
*/ |
| 88 |
rsRetVal llDestroy(linkedList_t *pThis) { |
| 89 |
DEFiRet; |
| 90 |
llElt_t *pElt; |
| 91 |
|
| 92 |
assert(pThis != NULL); |
| 93 |
|
| 94 |
pElt = pThis->pRoot; |
| 95 |
while (pElt != NULL) { |
| 96 |
/* keep the list structure in a consistent state as |
| 97 |
* the destructor bellow may reference it again |
| 98 |
*/ |
| 99 |
pThis->pRoot = pElt->pNext; |
| 100 |
if (pElt->pNext == NULL) pThis->pLast = NULL; |
| 101 |
|
| 102 |
/* we ignore errors during destruction, as we need to try |
| 103 |
* finish the linked list in any case. |
| 104 |
*/ |
| 105 |
llDestroyElt(pThis, pElt); |
| 106 |
pElt = pThis->pRoot; |
| 107 |
} |
| 108 |
|
| 109 |
RETiRet; |
| 110 |
} |
| 111 |
|
| 112 |
/* llDestroyRootElt - destroy the root element but otherwise |
| 113 |
* keeps this list intact. -- rgerhards, 2007-08-03 |
| 114 |
*/ |
| 115 |
rsRetVal llDestroyRootElt(linkedList_t *pThis) { |
| 116 |
DEFiRet; |
| 117 |
llElt_t *pPrev; |
| 118 |
|
| 119 |
if (pThis->pRoot == NULL) { |
| 120 |
ABORT_FINALIZE(RS_RET_EMPTY_LIST); |
| 121 |
} |
| 122 |
|
| 123 |
pPrev = pThis->pRoot; |
| 124 |
if (pPrev->pNext == NULL) { |
| 125 |
/* it was the only list element */ |
| 126 |
pThis->pLast = NULL; |
| 127 |
pThis->pRoot = NULL; |
| 128 |
} else { |
| 129 |
/* there are other list elements */ |
| 130 |
pThis->pRoot = pPrev->pNext; |
| 131 |
} |
| 132 |
|
| 133 |
CHKiRet(llDestroyElt(pThis, pPrev)); |
| 134 |
|
| 135 |
finalize_it: |
| 136 |
RETiRet; |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
/* get next user data element of a linked list. The caller must also |
| 141 |
* provide a "cookie" to the function. On initial call, it must be |
| 142 |
* NULL. Other than that, the caller is not allowed to to modify the |
| 143 |
* cookie. In the current implementation, the cookie is an actual |
| 144 |
* pointer to the current list element, but this is nothing that the |
| 145 |
* caller should rely on. |
| 146 |
*/ |
| 147 |
rsRetVal llGetNextElt(linkedList_t *pThis, linkedListCookie_t *ppElt, void **ppUsr) { |
| 148 |
llElt_t *pElt; |
| 149 |
DEFiRet; |
| 150 |
|
| 151 |
assert(pThis != NULL); |
| 152 |
assert(ppElt != NULL); |
| 153 |
assert(ppUsr != NULL); |
| 154 |
|
| 155 |
pElt = *ppElt; |
| 156 |
|
| 157 |
pElt = (pElt == NULL) ? pThis->pRoot : pElt->pNext; |
| 158 |
|
| 159 |
if (pElt == NULL) { |
| 160 |
iRet = RS_RET_END_OF_LINKEDLIST; |
| 161 |
} else { |
| 162 |
*ppUsr = pElt->pData; |
| 163 |
} |
| 164 |
|
| 165 |
*ppElt = pElt; |
| 166 |
|
| 167 |
RETiRet; |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
/* return the key of an Elt |
| 172 |
* rgerhards, 2007-09-11: note that ppDatea is actually a void**, |
| 173 |
* but I need to make it a void* to avoid lots of compiler warnings. |
| 174 |
* It will be converted later down in the code. |
| 175 |
*/ |
| 176 |
rsRetVal llGetKey(llElt_t *pThis, void *ppData) { |
| 177 |
assert(pThis != NULL); |
| 178 |
assert(ppData != NULL); |
| 179 |
|
| 180 |
*(void **)ppData = pThis->pKey; |
| 181 |
|
| 182 |
return RS_RET_OK; |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
/* construct a new llElt_t |
| 187 |
*/ |
| 188 |
static rsRetVal llEltConstruct(llElt_t **ppThis, void *pKey, void *pData) { |
| 189 |
DEFiRet; |
| 190 |
llElt_t *pThis; |
| 191 |
|
| 192 |
assert(ppThis != NULL); |
| 193 |
|
| 194 |
if ((pThis = (llElt_t *)calloc(1, sizeof(llElt_t))) == NULL) { |
| 195 |
ABORT_FINALIZE(RS_RET_OUT_OF_MEMORY); |
| 196 |
} |
| 197 |
|
| 198 |
pThis->pKey = pKey; |
| 199 |
pThis->pData = pData; |
| 200 |
|
| 201 |
finalize_it: |
| 202 |
*ppThis = pThis; |
| 203 |
RETiRet; |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
/* append a user element to the end of the linked list. This includes setting a key. If no |
| 208 |
* key is desired, simply pass in a NULL pointer for it. |
| 209 |
*/ |
| 210 |
rsRetVal llAppend(linkedList_t *pThis, void *pKey, void *pData) { |
| 211 |
llElt_t *pElt; |
| 212 |
DEFiRet; |
| 213 |
|
| 214 |
CHKiRet(llEltConstruct(&pElt, pKey, pData)); |
| 215 |
|
| 216 |
pThis->iNumElts++; /* one more */ |
| 217 |
if (pThis->pLast == NULL) { |
| 218 |
pThis->pRoot = pElt; |
| 219 |
} else { |
| 220 |
pThis->pLast->pNext = pElt; |
| 221 |
} |
| 222 |
pThis->pLast = pElt; |
| 223 |
|
| 224 |
finalize_it: |
| 225 |
RETiRet; |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
/* unlink a requested element. As we have singly-linked lists, the |
| 230 |
* caller also needs to pass in the previous element (or NULL, if it is the |
| 231 |
* root element). |
| 232 |
* rgerhards, 2007-11-21 |
| 233 |
*/ |
| 234 |
static rsRetVal llUnlinkElt(linkedList_t *pThis, llElt_t *pElt, llElt_t *pEltPrev) { |
| 235 |
assert(pElt != NULL); |
| 236 |
|
| 237 |
if (pEltPrev == NULL) { /* root element? */ |
| 238 |
pThis->pRoot = pElt->pNext; |
| 239 |
} else { /* regular element */ |
| 240 |
pEltPrev->pNext = pElt->pNext; |
| 241 |
} |
| 242 |
|
| 243 |
if (pElt == pThis->pLast) pThis->pLast = pEltPrev; |
| 244 |
|
| 245 |
return RS_RET_OK; |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
/* unlinks and immediately deletes an element. Previous element must |
| 250 |
* be given (or zero if the root element is to be deleted). |
| 251 |
* rgerhards, 2007-11-21 |
| 252 |
*/ |
| 253 |
static rsRetVal llUnlinkAndDelteElt(linkedList_t *pThis, llElt_t *pElt, llElt_t *pEltPrev) { |
| 254 |
DEFiRet; |
| 255 |
|
| 256 |
assert(pElt != NULL); |
| 257 |
|
| 258 |
CHKiRet(llUnlinkElt(pThis, pElt, pEltPrev)); |
| 259 |
CHKiRet(llDestroyElt(pThis, pElt)); |
| 260 |
|
| 261 |
finalize_it: |
| 262 |
RETiRet; |
| 263 |
} |
| 264 |
|
| 265 |
/* find a user element based on the provided key - this is the |
| 266 |
* internal variant, which also tracks the last element pointer |
| 267 |
* before the found element. This is necessary to delete elements. |
| 268 |
* NULL means there is no element in front of it, aka the found elt |
| 269 |
* is the root elt. |
| 270 |
* rgerhards, 2007-11-21 |
| 271 |
*/ |
| 272 |
static rsRetVal llFindElt(linkedList_t *pThis, void *pKey, llElt_t **ppElt, llElt_t **ppEltPrev) { |
| 273 |
DEFiRet; |
| 274 |
llElt_t *pElt; |
| 275 |
llElt_t *pEltPrev = NULL; |
| 276 |
int bFound = 0; |
| 277 |
|
| 278 |
assert(pThis != NULL); |
| 279 |
assert(pKey != NULL); |
| 280 |
assert(ppElt != NULL); |
| 281 |
assert(ppEltPrev != NULL); |
| 282 |
|
| 283 |
pElt = pThis->pRoot; |
| 284 |
while (pElt != NULL && bFound == 0) { |
| 285 |
if (pThis->cmpOp(pKey, pElt->pKey) == 0) |
| 286 |
bFound = 1; |
| 287 |
else { |
| 288 |
pEltPrev = pElt; |
| 289 |
pElt = pElt->pNext; |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
if (bFound == 1) { |
| 294 |
*ppElt = pElt; |
| 295 |
*ppEltPrev = pEltPrev; |
| 296 |
} else |
| 297 |
iRet = RS_RET_NOT_FOUND; |
| 298 |
|
| 299 |
RETiRet; |
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
/* find a user element based on the provided key |
| 304 |
*/ |
| 305 |
rsRetVal llFind(linkedList_t *pThis, void *pKey, void **ppData) { |
| 306 |
DEFiRet; |
| 307 |
llElt_t *pElt; |
| 308 |
llElt_t *pEltPrev; |
| 309 |
|
| 310 |
CHKiRet(llFindElt(pThis, pKey, &pElt, &pEltPrev)); |
| 311 |
|
| 312 |
/* if we reach this point, we have found the element */ |
| 313 |
*ppData = pElt->pData; |
| 314 |
|
| 315 |
finalize_it: |
| 316 |
RETiRet; |
| 317 |
} |
| 318 |
|
| 319 |
|
| 320 |
/* find a delete an element based on user-provided key. The element is |
| 321 |
* delete, the caller does not receive anything. If we need to receive |
| 322 |
* the element before destruction, we may implement an llFindAndUnlink() |
| 323 |
* at that time. |
| 324 |
* rgerhards, 2007-11-21 |
| 325 |
*/ |
| 326 |
rsRetVal llFindAndDelete(linkedList_t *pThis, void *pKey) { |
| 327 |
DEFiRet; |
| 328 |
llElt_t *pElt; |
| 329 |
llElt_t *pEltPrev; |
| 330 |
|
| 331 |
CHKiRet(llFindElt(pThis, pKey, &pElt, &pEltPrev)); |
| 332 |
|
| 333 |
/* if we reach this point, we have found an element */ |
| 334 |
CHKiRet(llUnlinkAndDelteElt(pThis, pElt, pEltPrev)); |
| 335 |
|
| 336 |
finalize_it: |
| 337 |
RETiRet; |
| 338 |
} |
| 339 |
|
| 340 |
|
| 341 |
/* provide the count of linked list elements |
| 342 |
*/ |
| 343 |
rsRetVal llGetNumElts(linkedList_t *pThis, int *piCnt) { |
| 344 |
DEFiRet; |
| 345 |
|
| 346 |
assert(pThis != NULL); |
| 347 |
assert(piCnt != NULL); |
| 348 |
|
| 349 |
*piCnt = pThis->iNumElts; |
| 350 |
|
| 351 |
RETiRet; |
| 352 |
} |
| 353 |
|
| 354 |
|
| 355 |
/* execute a function on all list members. The functions receives a |
| 356 |
* user-supplied parameter, which may be either a simple value |
| 357 |
* or a pointer to a structure with more data. If the user-supplied |
| 358 |
* function does not return RS_RET_OK, this function here terminates. |
| 359 |
* rgerhards, 2007-08-02 |
| 360 |
* rgerhards, 2007-11-21: added functionality to delete a list element. |
| 361 |
* If the called user function returns RS_RET_OK_DELETE_LISTENTRY the current element |
| 362 |
* is deleted. |
| 363 |
*/ |
| 364 |
rsRetVal llExecFunc(linkedList_t *pThis, rsRetVal (*pFunc)(void *, void *), void *pParam) { |
| 365 |
DEFiRet; |
| 366 |
rsRetVal iRetLL; |
| 367 |
void *pData; |
| 368 |
linkedListCookie_t llCookie = NULL; |
| 369 |
linkedListCookie_t llCookiePrev = NULL; /* previous list element (needed for deletion, NULL = at root) */ |
| 370 |
|
| 371 |
assert(pThis != NULL); |
| 372 |
assert(pFunc != NULL); |
| 373 |
|
| 374 |
while ((iRetLL = llGetNextElt(pThis, &llCookie, (void **)&pData)) == RS_RET_OK) { |
| 375 |
iRet = pFunc(pData, pParam); |
| 376 |
if (iRet == RS_RET_OK_DELETE_LISTENTRY) { |
| 377 |
/* delete element */ |
| 378 |
CHKiRet(llUnlinkAndDelteElt(pThis, llCookie, llCookiePrev)); |
| 379 |
/* we need to revert back, as we have just deleted the current element. |
| 380 |
* So the actual current element is the one before it, which happens to be |
| 381 |
* stored in llCookiePrev. -- rgerhards, 2007-11-21 |
| 382 |
*/ |
| 383 |
llCookie = llCookiePrev; |
| 384 |
} else if (iRet != RS_RET_OK) { |
| 385 |
FINALIZE; |
| 386 |
} |
| 387 |
llCookiePrev = llCookie; |
| 388 |
} |
| 389 |
|
| 390 |
if (iRetLL != RS_RET_END_OF_LINKEDLIST) iRet = iRetLL; |
| 391 |
|
| 392 |
finalize_it: |
| 393 |
RETiRet; |
| 394 |
} |
| 395 |
|
| 396 |
/* vim:set ai: |
| 397 |
*/ |