blob: 9117a0ad0f16ae4375a0da98ef84d966bf84f9b4 [file] [log] [blame]
Harald Welteec8b4502010-02-20 20:34:29 +01001/* (C) 2008 by Harald Welte <laforge@gnumonks.org>
2 * All Rights Reserved
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 *
18 */
19
20
21#include <unistd.h>
22#include <string.h>
23#include <stdlib.h>
24#include <sys/types.h>
25
26#include <osmocore/msgb.h>
27//#include <openbsc/gsm_data.h>
28#include <osmocore/talloc.h>
29//#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 Welteec8b4502010-02-20 20:34:29 +010088}