blob: ce390e84a4c00fdc75cd455760f966de4268ec8d [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 Welte52b1f982008-12-23 20:25:15 +000027
28struct msgb *msgb_alloc(u_int16_t size)
29{
30 struct msgb *msg = malloc(sizeof(*msg) + size);
31
32 if (!msg)
33 return NULL;
Harald Welte702d8702008-12-26 20:25:35 +000034 memset(msg, 0, sizeof(*msg)+size);
Harald Welte52b1f982008-12-23 20:25:15 +000035
36 msg->data_len = size;
37 msg->len = 0;
38 msg->data = msg->_data;
39
40 msg->head = msg->data;
41 msg->data = msg->data;
42 /* reset tail pointer */
Harald Welte8470bf22008-12-25 23:28:35 +000043 msg->tail = msg->data;
Harald Welte52b1f982008-12-23 20:25:15 +000044 //msg->end = msg->tail + size;
45
46 return msg;
47}
48
49void msgb_free(struct msgb *m)
50{
51 free(m);
52}
Harald Welte8470bf22008-12-25 23:28:35 +000053
54void msgb_enqueue(struct llist_head *queue, struct msgb *msg)
55{
56 llist_add_tail(&msg->list, queue);
57}
58
59struct msgb *msgb_dequeue(struct llist_head *queue)
60{
61 struct llist_head *lh;
62
63 if (llist_empty(queue))
64 return NULL;
65
66 lh = queue->next;
Harald Weltead384642008-12-26 10:20:07 +000067 llist_del(lh);
68
Harald Welte8470bf22008-12-25 23:28:35 +000069 return llist_entry(lh, struct msgb, list);
70}