Add 'src/shared/libosmocore/' from commit '3cae0398eaef6045de883849a236c38d1767cb41'
[osmocom-bb.git] / src / target / firmware / comm / msgb.c
1 /* (C) 2008-2010 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 <stdint.h>
22 #include <stdio.h>
23 #include <string.h>
24 #include <sys/types.h>
25
26 #include <debug.h>
27
28 #include <comm/msgb.h>
29
30 #include <calypso/backlight.h>
31
32 #define NO_TALLOC
33
34 void *tall_msgb_ctx;
35
36 #ifdef NO_TALLOC
37 /* This is a poor mans static allocator for msgb objects */
38 #define MSGB_DATA_SIZE  256
39 #define MSGB_NUM        16
40 struct supermsg {
41         uint8_t allocated;
42         struct msgb msg;
43         uint8_t buf[MSGB_DATA_SIZE];
44 };
45 static struct supermsg msgs[MSGB_NUM];
46 static void *_talloc_zero(void *ctx, unsigned int size, const char *name)
47 {
48         unsigned int i;
49         if (size > sizeof(struct msgb) + MSGB_DATA_SIZE)
50                 goto panic;
51         for (i = 0; i < ARRAY_SIZE(msgs); i++) {
52                 if (!msgs[i].allocated) {
53                         msgs[i].allocated = 1;
54                         memset(&msgs[i].msg, 0, sizeof(&msgs[i].msg));
55                         memset(&msgs[i].buf, 0, sizeof(&msgs[i].buf));
56                         return &msgs[i].msg;
57                 }
58         }
59
60 panic:
61         while (1) {
62                 bl_level(++i % 50);
63                 delay_ms(50);
64         }
65         return NULL;
66 }
67 static void talloc_free(void *msg)
68 {
69         struct supermsg *smsg = container_of(msg, struct supermsg, msg);
70         smsg->allocated = 0;
71 }
72 #endif
73
74 struct msgb *msgb_alloc(uint16_t size, const char *name)
75 {
76         struct msgb *msg;
77
78         msg = _talloc_zero(tall_msgb_ctx, sizeof(*msg) + size, name);
79
80         if (!msg) {
81                 cons_puts("unable to allocate msgb\n");
82                 return NULL;
83         }
84
85         msg->data_len = size;
86         msg->len = 0;
87         msg->data = msg->_data;
88
89         msg->head = msg->data;
90         msg->data = msg->data;
91         /* reset tail pointer */
92         msg->tail = msg->data;
93
94         return msg;
95 }
96
97 void msgb_free(struct msgb *m)
98 {
99         talloc_free(m);
100 }
101
102 void msgb_enqueue(struct llist_head *queue, struct msgb *msg)
103 {
104         llist_add_tail(&msg->list, queue);
105 }
106
107 struct msgb *msgb_dequeue(struct llist_head *queue)
108 {
109         struct llist_head *lh;
110
111         if (llist_empty(queue))
112                 return NULL;
113
114         lh = queue->next;
115         llist_del(lh);
116         
117         return llist_entry(lh, struct msgb, list);
118 }
119
120 void msgb_reset(struct msgb *msg)
121 {
122         msg->len = 0;
123         msg->len = 0;
124         msg->data = msg->_data;
125
126         msg->head = msg->data;
127         msg->data = msg->data;
128         /* reset tail pointer */
129         msg->tail = msg->data;
130
131         /* reset pointers */
132         msg->l2h = NULL;
133         msg->l3h = NULL;
134 }