blob: 86720064155dc777e818c7f294afbeafb97d9d3c [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>
25#include <sys/types.h>
26
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010027#include <osmocom/core/msgb.h>
Harald Welteec8b4502010-02-20 20:34:29 +010028//#include <openbsc/gsm_data.h>
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010029#include <osmocom/core/talloc.h>
Harald Welteec8b4502010-02-20 20:34:29 +010030//#include <openbsc/debug.h>
31
32void *tall_msgb_ctx;
33
34struct 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;
Sylvain Munaut17a5a282010-02-24 22:57:46 +010048 msg->head = msg->_data;
49 msg->tail = msg->_data;
Harald Welteec8b4502010-02-20 20:34:29 +010050
51 return msg;
52}
53
54void msgb_free(struct msgb *m)
55{
56 talloc_free(m);
57}
58
59void msgb_enqueue(struct llist_head *queue, struct msgb *msg)
60{
61 llist_add_tail(&msg->list, queue);
62}
63
64struct 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
77void msgb_reset(struct msgb *msg)
78{
79 msg->len = 0;
Harald Welteec8b4502010-02-20 20:34:29 +010080 msg->data = msg->_data;
Sylvain Munaut17a5a282010-02-24 22:57:46 +010081 msg->head = msg->_data;
82 msg->tail = msg->_data;
Harald Welteec8b4502010-02-20 20:34:29 +010083
Harald Welteec8b4502010-02-20 20:34:29 +010084 msg->trx = NULL;
85 msg->lchan = NULL;
86 msg->l2h = NULL;
87 msg->l3h = NULL;
Harald Weltebb77c9d2010-04-30 14:26:12 +020088 msg->l4h = NULL;
Harald Welte95df5c02010-05-01 23:53:26 +020089
90 memset(&msg->cb, 0, sizeof(msg->cb));
Harald Welteec8b4502010-02-20 20:34:29 +010091}
Holger Hans Peter Freytheracffb602010-10-18 18:22:31 +020092
93uint8_t *msgb_data(const struct msgb *msg)
94{
95 return msg->data;
96}
97
98uint16_t msgb_length(const struct msgb *msg)
99{
100 return msg->len;
101}