1 /* (C) 2008-2010 by Harald Welte <laforge@gnumonks.org>
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.
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.
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.
24 #include <sys/types.h>
28 #include <comm/msgb.h>
30 #include <calypso/backlight.h>
37 /* This is a poor mans static allocator for msgb objects */
38 #define MSGB_DATA_SIZE 256+4
43 uint8_t buf[MSGB_DATA_SIZE];
45 static struct supermsg msgs[MSGB_NUM];
46 static void *_talloc_zero(void *ctx, unsigned int size, const char *name)
49 if (size > sizeof(struct msgb) + MSGB_DATA_SIZE)
53 for (i = 0; i < ARRAY_SIZE(msgs); i++) {
54 if (!msgs[i].allocated) {
55 msgs[i].allocated = 1;
56 memset(&msgs[i].msg, 0, sizeof(&msgs[i].msg));
57 memset(&msgs[i].buf, 0, sizeof(&msgs[i].buf));
61 cons_puts("unable to allocate msgb\n");
68 static void talloc_free(void *msg)
70 struct supermsg *smsg = container_of(msg, struct supermsg, msg);
75 struct msgb *msgb_alloc(uint16_t size, const char *name)
79 msg = _talloc_zero(tall_msgb_ctx, sizeof(*msg) + size, name);
88 msg->data = msg->_data;
89 msg->head = msg->_data;
90 msg->tail = msg->_data;
95 void msgb_free(struct msgb *m)
100 void msgb_enqueue(struct llist_head *queue, struct msgb *msg)
102 llist_add_tail(&msg->list, queue);
105 struct msgb *msgb_dequeue(struct llist_head *queue)
107 struct llist_head *lh;
109 if (llist_empty(queue))
115 return llist_entry(lh, struct msgb, list);
118 void msgb_reset(struct msgb *msg)
122 msg->data = msg->_data;
123 msg->head = msg->_data;
124 msg->tail = msg->_data;