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> |
Harald Welte | 404d816 | 2009-01-01 00:33:02 +0000 | [diff] [blame] | 22 | #include <string.h> |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 23 | #include <stdlib.h> |
| 24 | #include <sys/types.h> |
| 25 | |
Harald Welte | dfe6c7d | 2010-02-20 16:24:02 +0100 | [diff] [blame] | 26 | #include <osmocore/msgb.h> |
| 27 | //#include <openbsc/gsm_data.h> |
| 28 | #include <osmocore/talloc.h> |
| 29 | //#include <openbsc/debug.h> |
Harald Welte | 2cf161b | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 30 | |
Harald Welte | 30d2ebd | 2009-12-24 09:43:25 +0100 | [diff] [blame] | 31 | void *tall_msgb_ctx; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 32 | |
Harald Welte | 966636f | 2009-06-26 19:39:35 +0200 | [diff] [blame] | 33 | struct msgb *msgb_alloc(u_int16_t size, const char *name) |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 34 | { |
Harald Welte | 2cf161b | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 35 | struct msgb *msg; |
| 36 | |
Harald Welte | 966636f | 2009-06-26 19:39:35 +0200 | [diff] [blame] | 37 | msg = _talloc_zero(tall_msgb_ctx, sizeof(*msg) + size, name); |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 38 | |
Harald Welte | b1d4c8e | 2009-12-17 23:10:46 +0100 | [diff] [blame] | 39 | if (!msg) { |
Harald Welte | dfe6c7d | 2010-02-20 16:24:02 +0100 | [diff] [blame] | 40 | //LOGP(DRSL, LOGL_FATAL, "unable to allocate msgb\n"); |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 41 | return NULL; |
Harald Welte | b1d4c8e | 2009-12-17 23:10:46 +0100 | [diff] [blame] | 42 | } |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 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 Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 51 | msg->tail = msg->data; |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 52 | //msg->end = msg->tail + size; |
| 53 | |
| 54 | return msg; |
| 55 | } |
| 56 | |
| 57 | void msgb_free(struct msgb *m) |
| 58 | { |
Harald Welte | 2cf161b | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 59 | talloc_free(m); |
Harald Welte | 52b1f98 | 2008-12-23 20:25:15 +0000 | [diff] [blame] | 60 | } |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 61 | |
| 62 | void msgb_enqueue(struct llist_head *queue, struct msgb *msg) |
| 63 | { |
| 64 | llist_add_tail(&msg->list, queue); |
| 65 | } |
| 66 | |
| 67 | struct 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 Welte | ad38464 | 2008-12-26 10:20:07 +0000 | [diff] [blame] | 75 | llist_del(lh); |
| 76 | |
Harald Welte | 8470bf2 | 2008-12-25 23:28:35 +0000 | [diff] [blame] | 77 | return llist_entry(lh, struct msgb, list); |
| 78 | } |
Harald Welte | 7bfc267 | 2009-07-28 00:41:45 +0200 | [diff] [blame] | 79 | |
Holger Hans Peter Freyther | cbbd498 | 2009-10-09 07:33:36 +0200 | [diff] [blame] | 80 | void msgb_reset(struct msgb *msg) |
| 81 | { |
| 82 | msg->len = 0; |
| 83 | msg->len = 0; |
| 84 | msg->data = msg->_data; |
| 85 | |
| 86 | msg->head = msg->data; |
| 87 | msg->data = msg->data; |
| 88 | /* reset tail pointer */ |
| 89 | msg->tail = msg->data; |
| 90 | |
| 91 | /* reset pointers */ |
| 92 | msg->bts_link = NULL; |
| 93 | msg->trx = NULL; |
| 94 | msg->lchan = NULL; |
| 95 | msg->l2h = NULL; |
| 96 | msg->l3h = NULL; |
| 97 | msg->smsh = NULL; |
| 98 | } |