select.c: Change license terms from GPLv2 (from ulogd) to GPLv2+
[osmocom-bb.git] / src / msgb.c
1 /* (C) 2008 by Harald Welte <laforge@gnumonks.org>
2  * (C) 2010 by Holger Hans Peter Freyther <zecke@selfish.org>
3  * All Rights Reserved
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  */
20
21
22 #include <unistd.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <sys/types.h>
26
27 #include <osmocore/msgb.h>
28 //#include <openbsc/gsm_data.h>
29 #include <osmocore/talloc.h>
30 //#include <openbsc/debug.h>
31
32 void *tall_msgb_ctx;
33
34 struct msgb *msgb_alloc(uint16_t size, const char *name)
35 {
36         struct msgb *msg;
37
38         msg = _talloc_zero(tall_msgb_ctx, sizeof(*msg) + size, name);
39
40         if (!msg) {
41                 //LOGP(DRSL, LOGL_FATAL, "unable to allocate msgb\n");
42                 return NULL;
43         }
44
45         msg->data_len = size;
46         msg->len = 0;
47         msg->data = msg->_data;
48         msg->head = msg->_data;
49         msg->tail = msg->_data;
50
51         return msg;
52 }
53
54 void msgb_free(struct msgb *m)
55 {
56         talloc_free(m);
57 }
58
59 void msgb_enqueue(struct llist_head *queue, struct msgb *msg)
60 {
61         llist_add_tail(&msg->list, queue);
62 }
63
64 struct msgb *msgb_dequeue(struct llist_head *queue)
65 {
66         struct llist_head *lh;
67
68         if (llist_empty(queue))
69                 return NULL;
70
71         lh = queue->next;
72         llist_del(lh);
73         
74         return llist_entry(lh, struct msgb, list);
75 }
76
77 void msgb_reset(struct msgb *msg)
78 {
79         msg->len = 0;
80         msg->data = msg->_data;
81         msg->head = msg->_data;
82         msg->tail = msg->_data;
83
84         msg->trx = NULL;
85         msg->lchan = NULL;
86         msg->l2h = NULL;
87         msg->l3h = NULL;
88         msg->l4h = NULL;
89
90         memset(&msg->cb, 0, sizeof(msg->cb));
91 }
92
93 uint8_t *msgb_data(const struct msgb *msg)
94 {
95         return msg->data;
96 }
97
98 uint16_t msgb_length(const struct msgb *msg)
99 {
100         return msg->len;
101 }