]>
git.proxmox.com Git - mirror_corosync-qdevice.git/blob - qdevices/send-buffer-list.c
2 * Copyright (c) 2015-2016 Red Hat, Inc.
6 * Author: Jan Friesse (jfriesse@redhat.com)
8 * This software licensed under BSD license, the text of which follows:
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are met:
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.
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.
39 #include "send-buffer-list.h"
42 send_buffer_list_init(struct send_buffer_list
*sblist
, size_t max_list_entries
,
43 size_t max_buffer_size
)
46 memset(sblist
, 0, sizeof(*sblist
));
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
);
55 struct send_buffer_list_entry
*
56 send_buffer_list_get_new(struct send_buffer_list
*sblist
)
58 struct send_buffer_list_entry
*entry
;
60 if (!TAILQ_EMPTY(&sblist
->free_list
)) {
64 entry
= TAILQ_FIRST(&sblist
->free_list
);
65 TAILQ_REMOVE(&sblist
->free_list
, entry
, entries
);
67 dynar_clean(&entry
->buffer
);
68 dynar_set_max_size(&entry
->buffer
, sblist
->max_buffer_size
);
70 if (sblist
->allocated_list_entries
+ 1 > sblist
->max_list_entries
) {
74 sblist
->allocated_list_entries
++;
79 entry
= malloc(sizeof(*entry
));
84 dynar_init(&entry
->buffer
, sblist
->max_buffer_size
);
87 entry
->msg_already_sent_bytes
= 0;
93 send_buffer_list_put(struct send_buffer_list
*sblist
, struct send_buffer_list_entry
*sblist_entry
)
96 TAILQ_INSERT_TAIL(&sblist
->list
, sblist_entry
, entries
);
100 send_buffer_list_discard_new(struct send_buffer_list
*sblist
, struct send_buffer_list_entry
*sblist_entry
)
103 TAILQ_INSERT_HEAD(&sblist
->free_list
, sblist_entry
, entries
);
106 struct send_buffer_list_entry
*
107 send_buffer_list_get_active(const struct send_buffer_list
*sblist
)
109 struct send_buffer_list_entry
*entry
;
111 entry
= TAILQ_FIRST(&sblist
->list
);
117 send_buffer_list_delete(struct send_buffer_list
*sblist
,
118 struct send_buffer_list_entry
*sblist_entry
)
122 * Move item to free list
124 TAILQ_REMOVE(&sblist
->list
, sblist_entry
, entries
);
125 TAILQ_INSERT_HEAD(&sblist
->free_list
, sblist_entry
, entries
);
129 send_buffer_list_empty(const struct send_buffer_list
*sblist
)
132 return (TAILQ_EMPTY(&sblist
->list
));
136 send_buffer_list_free(struct send_buffer_list
*sblist
)
138 struct send_buffer_list_entry
*entry
;
139 struct send_buffer_list_entry
*entry_next
;
141 entry
= TAILQ_FIRST(&sblist
->list
);
143 while (entry
!= NULL
) {
144 entry_next
= TAILQ_NEXT(entry
, entries
);
146 dynar_destroy(&entry
->buffer
);
152 entry
= TAILQ_FIRST(&sblist
->free_list
);
154 while (entry
!= NULL
) {
155 entry_next
= TAILQ_NEXT(entry
, entries
);
157 dynar_destroy(&entry
->buffer
);
163 sblist
->allocated_list_entries
= 0;
164 TAILQ_INIT(&sblist
->list
);
165 TAILQ_INIT(&sblist
->free_list
);
169 send_buffer_list_set_max_buffer_size(struct send_buffer_list
*sblist
, size_t max_buffer_size
)
172 sblist
->max_buffer_size
= max_buffer_size
;
176 send_buffer_list_set_max_list_entries(struct send_buffer_list
*sblist
, size_t max_list_entries
)
179 sblist
->max_list_entries
= max_list_entries
;