UPnPsdk 0.1
Universal Plug and Play +, Software Development Kit
 
Loading...
Searching...
No Matches
FreeList.cpp
Go to the documentation of this file.
1/**************************************************************************
2 *
3 * Copyright (c) 2000-2003 Intel Corporation
4 * All rights reserved.
5 * Copyright (C) 2021+ GPL 3 and higher by Ingo Höft, <Ingo@Hoeft-online.de>
6 * Redistribution only with this Copyright remark. Last modified: 2024-04-24
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
10 *
11 * - Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above copyright notice,
14 * this list of conditions and the following disclaimer in the documentation
15 * and/or other materials provided with the distribution.
16 * - Neither name of Intel Corporation nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL INTEL OR
24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
28 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 **************************************************************************/
42#include "FreeList.hpp"
43#include <umock/stdlib.hpp>
44
46#include <cassert>
47#include <cstdlib>
49
50int FreeListInit(FreeList* free_list, size_t elementSize,
51 int maxFreeListLength) {
52 assert(free_list != NULL);
53
54 if (free_list == NULL)
55 return EINVAL;
56 free_list->element_size = elementSize;
57 free_list->maxFreeListLength = maxFreeListLength;
58 free_list->head = NULL;
59 free_list->freeListLength = 0;
60
61 return 0;
62}
63
64void* FreeListAlloc(FreeList* free_list) {
65 FreeListNode* ret = NULL;
66
67 assert(free_list != NULL);
68
69 if (free_list == NULL)
70 return NULL;
71
72 if (free_list->head) {
73 ret = free_list->head;
74 free_list->head = free_list->head->next;
75 free_list->freeListLength--;
76 } else {
77 ret = (FreeListNode*)umock::stdlib_h.malloc(free_list->element_size);
78 }
79
80 return ret;
81}
82
83int FreeListFree(FreeList* free_list, void* element) {
84 FreeListNode* temp = NULL;
85
86 assert(free_list != NULL);
87
88 if (free_list == NULL)
89 return EINVAL;
90 if (element != NULL &&
91 free_list->freeListLength + 1 < free_list->maxFreeListLength) {
92 free_list->freeListLength++;
93 temp = (FreeListNode*)element;
94 temp->next = free_list->head;
95 free_list->head = temp;
96 } else {
97 umock::stdlib_h.free(element);
98 }
99
100 return 0;
101}
102
103int FreeListDestroy(FreeList* free_list) {
104 FreeListNode* temp = NULL;
105
106 assert(free_list != NULL);
107
108 if (!free_list)
109 return EINVAL;
110 while (free_list->head) {
111 temp = free_list->head->next;
112 umock::stdlib_h.free(free_list->head);
113 free_list->head = temp;
114 }
115 free_list->freeListLength = 0;
116
117 return 0;
118}
int FreeListInit(FreeList *free_list, size_t elementSize, int maxFreeListLength)
Initializes Free List.
Definition FreeList.cpp:50
int FreeListFree(FreeList *free_list, void *element)
Returns an item to the Free List.
Definition FreeList.cpp:83
void * FreeListAlloc(FreeList *free_list)
Allocates chunk of set size.
Definition FreeList.cpp:64
int FreeListDestroy(FreeList *free_list)
Releases the resources stored with the free list.
Definition FreeList.cpp:103
Manage a free list (for internal use only).
Free list node. points to next free item.
Definition FreeList.hpp:55
Stores head and size of free list, as well as mutex for protection.
Definition FreeList.hpp:63