Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [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 <string.h> |
| 23 | #include <stdlib.h> |
| 24 | #include <sys/types.h> |
| 25 | |
| 26 | #include <openbsc/msgb.h> |
Harald Welte | a837977 | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 27 | #include <openbsc/gsm_data.h> |
| 28 | #include <openbsc/talloc.h> |
| 29 | |
| 30 | static void *tall_msgb_ctx; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 31 | |
Harald Welte | 9cfc935 | 2009-06-26 19:39:35 +0200 | [diff] [blame] | 32 | struct msgb *msgb_alloc(u_int16_t size, const char *name) |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 33 | { |
Harald Welte | a837977 | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 34 | struct msgb *msg; |
| 35 | |
Harald Welte | 9cfc935 | 2009-06-26 19:39:35 +0200 | [diff] [blame] | 36 | msg = _talloc_zero(tall_msgb_ctx, sizeof(*msg) + size, name); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 37 | |
| 38 | if (!msg) |
| 39 | return NULL; |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 40 | |
| 41 | msg->data_len = size; |
| 42 | msg->len = 0; |
| 43 | msg->data = msg->_data; |
| 44 | |
| 45 | msg->head = msg->data; |
| 46 | msg->data = msg->data; |
| 47 | /* reset tail pointer */ |
| 48 | msg->tail = msg->data; |
| 49 | //msg->end = msg->tail + size; |
| 50 | |
| 51 | return msg; |
| 52 | } |
| 53 | |
| 54 | void msgb_free(struct msgb *m) |
| 55 | { |
Harald Welte | a837977 | 2009-06-20 22:36:41 +0200 | [diff] [blame] | 56 | talloc_free(m); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | void msgb_enqueue(struct llist_head *queue, struct msgb *msg) |
| 60 | { |
| 61 | llist_add_tail(&msg->list, queue); |
| 62 | } |
| 63 | |
| 64 | struct msgb *msgb_dequeue(struct llist_head *queue) |
| 65 | { |
| 66 | struct llist_head *lh; |
| 67 | |
| 68 | if (llist_empty(queue)) |
| 69 | return NULL; |
| 70 | |
| 71 | lh = queue->next; |
| 72 | llist_del(lh); |
| 73 | |
| 74 | return llist_entry(lh, struct msgb, list); |
| 75 | } |
Harald Welte | 932e20d | 2009-07-28 00:41:45 +0200 | [diff] [blame] | 76 | |
Holger Hans Peter Freyther | ae780bb | 2009-10-09 07:33:36 +0200 | [diff] [blame] | 77 | void msgb_reset(struct msgb *msg) |
| 78 | { |
| 79 | msg->len = 0; |
| 80 | msg->len = 0; |
| 81 | msg->data = msg->_data; |
| 82 | |
| 83 | msg->head = msg->data; |
| 84 | msg->data = msg->data; |
| 85 | /* reset tail pointer */ |
| 86 | msg->tail = msg->data; |
| 87 | |
| 88 | /* reset pointers */ |
| 89 | msg->bts_link = NULL; |
| 90 | msg->trx = NULL; |
| 91 | msg->lchan = NULL; |
| 92 | msg->l2h = NULL; |
| 93 | msg->l3h = NULL; |
| 94 | msg->smsh = NULL; |
| 95 | } |
| 96 | |
Harald Welte | 932e20d | 2009-07-28 00:41:45 +0200 | [diff] [blame] | 97 | static __attribute__((constructor)) void on_dso_load_trau_msgb(void) |
| 98 | { |
| 99 | tall_msgb_ctx = talloc_named_const(tall_bsc_ctx, 1, "msgb"); |
| 100 | } |