blob: ae13346141dbc994fa7897382d580478a8aae53e [file] [log] [blame]
Harald Welte52b1f982008-12-23 20:25:15 +00001/* (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>
Harald Welte404d8162009-01-01 00:33:02 +000022#include <string.h>
Harald Welte52b1f982008-12-23 20:25:15 +000023#include <stdlib.h>
24#include <sys/types.h>
25
Harald Welte8470bf22008-12-25 23:28:35 +000026#include <openbsc/msgb.h>
Harald Welte2cf161b2009-06-20 22:36:41 +020027#include <openbsc/gsm_data.h>
28#include <openbsc/talloc.h>
29
30static void *tall_msgb_ctx;
Harald Welte52b1f982008-12-23 20:25:15 +000031
Harald Welte966636f2009-06-26 19:39:35 +020032struct msgb *msgb_alloc(u_int16_t size, const char *name)
Harald Welte52b1f982008-12-23 20:25:15 +000033{
Harald Welte2cf161b2009-06-20 22:36:41 +020034 struct msgb *msg;
35
36 if (!tall_msgb_ctx)
37 tall_msgb_ctx = talloc_named_const(tall_bsc_ctx, 1, "msgb");
38
Harald Welte966636f2009-06-26 19:39:35 +020039 msg = _talloc_zero(tall_msgb_ctx, sizeof(*msg) + size, name);
Harald Welte52b1f982008-12-23 20:25:15 +000040
41 if (!msg)
42 return NULL;
43
44 msg->data_len = size;
45 msg->len = 0;
46 msg->data = msg->_data;
47
48 msg->head = msg->data;
49 msg->data = msg->data;
50 /* reset tail pointer */
Harald Welte8470bf22008-12-25 23:28:35 +000051 msg->tail = msg->data;
Harald Welte52b1f982008-12-23 20:25:15 +000052 //msg->end = msg->tail + size;
53
54 return msg;
55}
56
57void msgb_free(struct msgb *m)
58{
Harald Welte2cf161b2009-06-20 22:36:41 +020059 talloc_free(m);
Harald Welte52b1f982008-12-23 20:25:15 +000060}
Harald Welte8470bf22008-12-25 23:28:35 +000061
62void msgb_enqueue(struct llist_head *queue, struct msgb *msg)
63{
64 llist_add_tail(&msg->list, queue);
65}
66
67struct msgb *msgb_dequeue(struct llist_head *queue)
68{
69 struct llist_head *lh;
70
71 if (llist_empty(queue))
72 return NULL;
73
74 lh = queue->next;
Harald Weltead384642008-12-26 10:20:07 +000075 llist_del(lh);
76
Harald Welte8470bf22008-12-25 23:28:35 +000077 return llist_entry(lh, struct msgb, list);
78}