blob: f9841ed14b2fe237cba3a0aa326c25609e1235cb [file] [log] [blame]
Harald Welteec8b4502010-02-20 20:34:29 +01001/* (C) 2008 by Harald Welte <laforge@gnumonks.org>
Holger Hans Peter Freytheracffb602010-10-18 18:22:31 +02002 * (C) 2010 by Holger Hans Peter Freyther <zecke@selfish.org>
Harald Welteec8b4502010-02-20 20:34:29 +01003 * 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>
Harald Welteec8b4502010-02-20 20:34:29 +010025
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010026#include <osmocom/core/msgb.h>
Harald Welteec8b4502010-02-20 20:34:29 +010027//#include <openbsc/gsm_data.h>
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010028#include <osmocom/core/talloc.h>
Harald Welteec8b4502010-02-20 20:34:29 +010029//#include <openbsc/debug.h>
30
31void *tall_msgb_ctx;
32
33struct msgb *msgb_alloc(uint16_t size, const char *name)
34{
35 struct msgb *msg;
36
37 msg = _talloc_zero(tall_msgb_ctx, sizeof(*msg) + size, name);
38
39 if (!msg) {
40 //LOGP(DRSL, LOGL_FATAL, "unable to allocate msgb\n");
41 return NULL;
42 }
43
44 msg->data_len = size;
45 msg->len = 0;
46 msg->data = msg->_data;
Sylvain Munaut17a5a282010-02-24 22:57:46 +010047 msg->head = msg->_data;
48 msg->tail = msg->_data;
Harald Welteec8b4502010-02-20 20:34:29 +010049
50 return msg;
51}
52
53void msgb_free(struct msgb *m)
54{
55 talloc_free(m);
56}
57
58void msgb_enqueue(struct llist_head *queue, struct msgb *msg)
59{
60 llist_add_tail(&msg->list, queue);
61}
62
63struct msgb *msgb_dequeue(struct llist_head *queue)
64{
65 struct llist_head *lh;
66
67 if (llist_empty(queue))
68 return NULL;
69
70 lh = queue->next;
71 llist_del(lh);
72
73 return llist_entry(lh, struct msgb, list);
74}
75
76void msgb_reset(struct msgb *msg)
77{
78 msg->len = 0;
Harald Welteec8b4502010-02-20 20:34:29 +010079 msg->data = msg->_data;
Sylvain Munaut17a5a282010-02-24 22:57:46 +010080 msg->head = msg->_data;
81 msg->tail = msg->_data;
Harald Welteec8b4502010-02-20 20:34:29 +010082
Harald Welteec8b4502010-02-20 20:34:29 +010083 msg->trx = NULL;
84 msg->lchan = NULL;
85 msg->l2h = NULL;
86 msg->l3h = NULL;
Harald Weltebb77c9d2010-04-30 14:26:12 +020087 msg->l4h = NULL;
Harald Welte95df5c02010-05-01 23:53:26 +020088
89 memset(&msg->cb, 0, sizeof(msg->cb));
Harald Welteec8b4502010-02-20 20:34:29 +010090}
Holger Hans Peter Freytheracffb602010-10-18 18:22:31 +020091
92uint8_t *msgb_data(const struct msgb *msg)
93{
94 return msg->data;
95}
96
97uint16_t msgb_length(const struct msgb *msg)
98{
99 return msg->len;
100}
Harald Welte9e1f0602011-06-29 18:46:10 +0200101
102void msgb_set_talloc_ctx(void *ctx)
103{
104 tall_msgb_ctx = ctx;
105}