Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 1 | /* (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 <stdlib.h> |
| 23 | #include <sys/types.h> |
| 24 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 25 | #include <openbsc/msgb.h> |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 26 | |
| 27 | struct msgb *msgb_alloc(u_int16_t size) |
| 28 | { |
| 29 | struct msgb *msg = malloc(sizeof(*msg) + size); |
| 30 | |
| 31 | if (!msg) |
| 32 | return NULL; |
| 33 | |
| 34 | msg->data_len = size; |
| 35 | msg->len = 0; |
| 36 | msg->data = msg->_data; |
| 37 | |
| 38 | msg->head = msg->data; |
| 39 | msg->data = msg->data; |
| 40 | /* reset tail pointer */ |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 41 | msg->tail = msg->data; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 42 | //msg->end = msg->tail + size; |
| 43 | |
| 44 | return msg; |
| 45 | } |
| 46 | |
| 47 | void msgb_free(struct msgb *m) |
| 48 | { |
| 49 | free(m); |
| 50 | } |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 51 | |
| 52 | void msgb_enqueue(struct llist_head *queue, struct msgb *msg) |
| 53 | { |
| 54 | llist_add_tail(&msg->list, queue); |
| 55 | } |
| 56 | |
| 57 | struct msgb *msgb_dequeue(struct llist_head *queue) |
| 58 | { |
| 59 | struct llist_head *lh; |
| 60 | |
| 61 | if (llist_empty(queue)) |
| 62 | return NULL; |
| 63 | |
| 64 | lh = queue->next; |
Harald Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 65 | llist_del(lh); |
| 66 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 67 | return llist_entry(lh, struct msgb, list); |
| 68 | } |