aboutsummaryrefslogtreecommitdiff
path: root/src/core/lmq.c
blob: d01afe50ccb0abfea1db1f07095973bd24717eda (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//
// Copyright 2021 Staysail Systems, Inc. <info@staysail.tech>
//
// This software is supplied under the terms of the MIT License, a
// copy of which should be located in the distribution where this
// file was obtained (LICENSE.txt).  A copy of the license may also be
// found online at https://opensource.org/licenses/MIT.
//

#include "nng_impl.h"

// Light-weight message queue. These are derived from our heavy-weight
// message queues, but are less "featured", but more useful for
// performance sensitive contexts.  Locking must be done by the caller.


// Note that initialization of a queue is guaranteed to succeed.
// However, if the requested capacity is larger than 2, and memory
// cannot be allocated, then the capacity will only be 2.
void
nni_lmq_init(nni_lmq *lmq, size_t cap)
{
	lmq->lmq_len = 0;
	lmq->lmq_get = 0;
	lmq->lmq_put = 0;
	lmq->lmq_alloc = 0;
	lmq->lmq_mask = 0;
	lmq->lmq_msgs = NULL;
	lmq->lmq_msgs = lmq->lmq_buf;
	lmq->lmq_cap = 2;
	lmq->lmq_mask = 0x1; // only index 0 and 1
	if (cap > 2) {
		(void) nni_lmq_resize(lmq, cap);
	} else {
		lmq->lmq_cap = cap;
	}
}

void
nni_lmq_fini(nni_lmq *lmq)
{
	if (lmq == NULL) {
		return;
	}

	/* Free any orphaned messages. */
	while (lmq->lmq_len > 0) {
		nng_msg *msg = lmq->lmq_msgs[lmq->lmq_get++];
		lmq->lmq_get &= lmq->lmq_mask;
		lmq->lmq_len--;
		nni_msg_free(msg);
	}
	if (lmq->lmq_alloc > 0) {
		nni_free(lmq->lmq_msgs, lmq->lmq_alloc * sizeof(nng_msg *));
	}
}

void
nni_lmq_flush(nni_lmq *lmq)
{
	while (lmq->lmq_len > 0) {
		nng_msg *msg = lmq->lmq_msgs[lmq->lmq_get++];
		lmq->lmq_get &= lmq->lmq_mask;
		lmq->lmq_len--;
		nni_msg_free(msg);
	}
}

size_t
nni_lmq_len(nni_lmq *lmq)
{
	return (lmq->lmq_len);
}

size_t
nni_lmq_cap(nni_lmq *lmq)
{
	return (lmq->lmq_cap);
}

bool
nni_lmq_full(nni_lmq *lmq)
{
	return (lmq->lmq_len >= lmq->lmq_cap);
}

bool
nni_lmq_empty(nni_lmq *lmq)
{
	return (lmq->lmq_len == 0);
}

int
nni_lmq_put(nni_lmq *lmq, nng_msg *msg)
{
	if (lmq->lmq_len >= lmq->lmq_cap) {
		return (NNG_EAGAIN);
	}
	lmq->lmq_msgs[lmq->lmq_put++] = msg;
	lmq->lmq_len++;
	lmq->lmq_put &= lmq->lmq_mask;
	return (0);
}

int
nni_lmq_get(nni_lmq *lmq, nng_msg **mp)
{
	nng_msg *msg;
	if (lmq->lmq_len == 0) {
		return (NNG_EAGAIN);
	}
	msg = lmq->lmq_msgs[lmq->lmq_get++];
	lmq->lmq_get &= lmq->lmq_mask;
	lmq->lmq_len--;
	*mp = msg;
	return (0);
}

int
nni_lmq_resize(nni_lmq *lmq, size_t cap)
{
	nng_msg  *msg;
	nng_msg **new_q;
	size_t    alloc;
	size_t    len;

	alloc = 2;
	while (alloc < cap) {
		alloc *= 2;
	}

	if ((new_q = nni_alloc(sizeof(nng_msg *) * alloc)) == NULL) {
		return (NNG_ENOMEM);
	}

	len = 0;
	while ((len < cap) && (nni_lmq_get(lmq, &msg) == 0)) {
		new_q[len++] = msg;
	}

	// Flush anything left over.
	nni_lmq_flush(lmq);

	if (lmq->lmq_alloc > 0) {
		nni_free(lmq->lmq_msgs, lmq->lmq_alloc * sizeof(nng_msg *));
	}
	lmq->lmq_msgs  = new_q;
	lmq->lmq_cap   = cap;
	lmq->lmq_alloc = alloc;
	lmq->lmq_mask  = alloc - 1;
	lmq->lmq_len   = len;
	lmq->lmq_put   = len;
	lmq->lmq_get   = 0;

	return (0);
}