Add 'src/shared/libosmocore/' from commit 'ed00fe4449ac2309ad80c32f0ba7aa80b2b8895a'
[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 #include <delay.h>
28
29 #include <osmocore/msgb.h>
30
31 #include <calypso/backlight.h>
32
33 #define NO_TALLOC
34
35 void *tall_msgb_ctx;
36
37 #ifdef NO_TALLOC
38 /* This is a poor mans static allocator for msgb objects */
39 #define MSGB_DATA_SIZE  256+4
40 #define MSGB_NUM        32
41 struct supermsg {
42         uint8_t allocated;
43         struct msgb msg;
44         uint8_t buf[MSGB_DATA_SIZE];
45 };
46 static struct supermsg msgs[MSGB_NUM];
47 void *_talloc_zero(void *ctx, unsigned int size, const char *name)
48 {
49         unsigned int i;
50         if (size > sizeof(struct msgb) + MSGB_DATA_SIZE)
51                 goto panic;
52
53         while (1) {
54                 for (i = 0; i < ARRAY_SIZE(msgs); i++) {
55                         if (!msgs[i].allocated) {
56                                 msgs[i].allocated = 1;
57                                 memset(&msgs[i].msg, 0, sizeof(&msgs[i].msg));
58                                 memset(&msgs[i].buf, 0, sizeof(&msgs[i].buf));
59                                 return &msgs[i].msg;
60                         }
61                 }
62                 cons_puts("unable to allocate msgb\n");
63                 bl_level(++i % 50);
64                 delay_ms(50);
65         }
66 panic:
67         return NULL;
68 }
69 void talloc_free(void *msg)
70 {
71         struct supermsg *smsg = container_of(msg, struct supermsg, msg);
72         smsg->allocated = 0;
73 }
74 #endif