]> git.proxmox.com Git - mirror_corosync-qdevice.git/blob - qdevices/send-buffer-list.c
init: Fix init scripts to work with containers
[mirror_corosync-qdevice.git] / qdevices / send-buffer-list.c
1 /*
2 * Copyright (c) 2015-2016 Red Hat, Inc.
3 *
4 * All rights reserved.
5 *
6 * Author: Jan Friesse (jfriesse@redhat.com)
7 *
8 * This software licensed under BSD license, the text of which follows:
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are met:
12 *
13 * - Redistributions of source code must retain the above copyright notice,
14 * this list of conditions and the following disclaimer.
15 * - Redistributions in binary form must reproduce the above copyright notice,
16 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
18 * - Neither the name of the Red Hat, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived from this
20 * software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 * THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35 #include <string.h>
36 #include <stdlib.h>
37 #include <assert.h>
38
39 #include "send-buffer-list.h"
40
41 void
42 send_buffer_list_init(struct send_buffer_list *sblist, size_t max_list_entries,
43 size_t max_buffer_size)
44 {
45
46 memset(sblist, 0, sizeof(*sblist));
47
48 sblist->max_list_entries = max_list_entries;
49 sblist->allocated_list_entries = 0;
50 sblist->max_buffer_size = max_buffer_size;
51 TAILQ_INIT(&sblist->list);
52 TAILQ_INIT(&sblist->free_list);
53 }
54
55 struct send_buffer_list_entry *
56 send_buffer_list_get_new(struct send_buffer_list *sblist)
57 {
58 struct send_buffer_list_entry *entry;
59
60 if (!TAILQ_EMPTY(&sblist->free_list)) {
61 /*
62 * Use free list entry
63 */
64 entry = TAILQ_FIRST(&sblist->free_list);
65 TAILQ_REMOVE(&sblist->free_list, entry, entries);
66
67 dynar_clean(&entry->buffer);
68 dynar_set_max_size(&entry->buffer, sblist->max_buffer_size);
69 } else {
70 if (sblist->allocated_list_entries + 1 > sblist->max_list_entries) {
71 return (NULL);
72 }
73
74 sblist->allocated_list_entries++;
75
76 /*
77 * Alloc new entry
78 */
79 entry = malloc(sizeof(*entry));
80 if (entry == NULL) {
81 return (NULL);
82 }
83
84 dynar_init(&entry->buffer, sblist->max_buffer_size);
85 }
86
87 entry->msg_already_sent_bytes = 0;
88
89 return (entry);
90 }
91
92 void
93 send_buffer_list_put(struct send_buffer_list *sblist, struct send_buffer_list_entry *sblist_entry)
94 {
95
96 TAILQ_INSERT_TAIL(&sblist->list, sblist_entry, entries);
97 }
98
99 void
100 send_buffer_list_discard_new(struct send_buffer_list *sblist, struct send_buffer_list_entry *sblist_entry)
101 {
102
103 TAILQ_INSERT_HEAD(&sblist->free_list, sblist_entry, entries);
104 }
105
106 struct send_buffer_list_entry *
107 send_buffer_list_get_active(const struct send_buffer_list *sblist)
108 {
109 struct send_buffer_list_entry *entry;
110
111 entry = TAILQ_FIRST(&sblist->list);
112
113 return (entry);
114 }
115
116 void
117 send_buffer_list_delete(struct send_buffer_list *sblist,
118 struct send_buffer_list_entry *sblist_entry)
119 {
120
121 /*
122 * Move item to free list
123 */
124 TAILQ_REMOVE(&sblist->list, sblist_entry, entries);
125 TAILQ_INSERT_HEAD(&sblist->free_list, sblist_entry, entries);
126 }
127
128 int
129 send_buffer_list_empty(const struct send_buffer_list *sblist)
130 {
131
132 return (TAILQ_EMPTY(&sblist->list));
133 }
134
135 void
136 send_buffer_list_free(struct send_buffer_list *sblist)
137 {
138 struct send_buffer_list_entry *entry;
139 struct send_buffer_list_entry *entry_next;
140
141 entry = TAILQ_FIRST(&sblist->list);
142
143 while (entry != NULL) {
144 entry_next = TAILQ_NEXT(entry, entries);
145
146 dynar_destroy(&entry->buffer);
147 free(entry);
148
149 entry = entry_next;
150 }
151
152 entry = TAILQ_FIRST(&sblist->free_list);
153
154 while (entry != NULL) {
155 entry_next = TAILQ_NEXT(entry, entries);
156
157 dynar_destroy(&entry->buffer);
158 free(entry);
159
160 entry = entry_next;
161 }
162
163 sblist->allocated_list_entries = 0;
164 TAILQ_INIT(&sblist->list);
165 TAILQ_INIT(&sblist->free_list);
166 }
167
168 void
169 send_buffer_list_set_max_buffer_size(struct send_buffer_list *sblist, size_t max_buffer_size)
170 {
171
172 sblist->max_buffer_size = max_buffer_size;
173 }
174
175 void
176 send_buffer_list_set_max_list_entries(struct send_buffer_list *sblist, size_t max_list_entries)
177 {
178
179 sblist->max_list_entries = max_list_entries;
180 }