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 | |
| 32 | struct msgb *msgb_alloc(u_int16_t size) |
| 33 | { |
Harald Welte | a837977 | 2009-06-20 22:36:41 +0200 | [diff] [blame^] | 34 | struct msgb *msg; |
| 35 | |
| 36 | if (!tall_msgb_ctx) |
| 37 | tall_msgb_ctx = talloc_named_const(tall_bsc_ctx, 1, "msgb"); |
| 38 | |
| 39 | msg = talloc_size(tall_msgb_ctx, sizeof(*msg) + size); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 40 | |
| 41 | if (!msg) |
| 42 | return NULL; |
| 43 | memset(msg, 0, sizeof(*msg)+size); |
| 44 | |
| 45 | msg->data_len = size; |
| 46 | msg->len = 0; |
| 47 | msg->data = msg->_data; |
| 48 | |
| 49 | msg->head = msg->data; |
| 50 | msg->data = msg->data; |
| 51 | /* reset tail pointer */ |
| 52 | msg->tail = msg->data; |
| 53 | //msg->end = msg->tail + size; |
| 54 | |
| 55 | return msg; |
| 56 | } |
| 57 | |
| 58 | void msgb_free(struct msgb *m) |
| 59 | { |
Harald Welte | a837977 | 2009-06-20 22:36:41 +0200 | [diff] [blame^] | 60 | talloc_free(m); |
Harald Welte | 59b0468 | 2009-06-10 05:40:52 +0800 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | void msgb_enqueue(struct llist_head *queue, struct msgb *msg) |
| 64 | { |
| 65 | llist_add_tail(&msg->list, queue); |
| 66 | } |
| 67 | |
| 68 | struct msgb *msgb_dequeue(struct llist_head *queue) |
| 69 | { |
| 70 | struct llist_head *lh; |
| 71 | |
| 72 | if (llist_empty(queue)) |
| 73 | return NULL; |
| 74 | |
| 75 | lh = queue->next; |
| 76 | llist_del(lh); |
| 77 | |
| 78 | return llist_entry(lh, struct msgb, list); |
| 79 | } |