blob: 862fffdb956f7d721ff01fced402772d954534b5 [file] [log] [blame]
Kévin Redon9a12d682018-07-08 13:21:16 +02001/* Memory allocation library
2 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
16 */
Harald Welte8e7fca32017-05-07 16:14:33 +020017#include <stdint.h>
18
19#include "talloc.h"
20#include "trace.h"
Harald Welteba2ad562017-11-28 19:49:41 +010021#include "utils.h"
Harald Welte9d90d282018-06-29 22:25:42 +020022#include <osmocom/core/utils.h>
Harald Welte8e7fca32017-05-07 16:14:33 +020023
24#define NUM_RCTX_SMALL 10
25#define RCTX_SIZE_SMALL 348
26
27static uint8_t msgb_data[NUM_RCTX_SMALL][RCTX_SIZE_SMALL] __attribute__((aligned(sizeof(long))));
28static uint8_t msgb_inuse[NUM_RCTX_SMALL];
29
30void *_talloc_zero(const void *ctx, size_t size, const char *name)
31{
32 unsigned int i;
Harald Welte7b363062017-11-04 00:06:03 +010033 unsigned long x;
Harald Welte8e7fca32017-05-07 16:14:33 +020034
Harald Welte7b363062017-11-04 00:06:03 +010035 local_irq_save(x);
Harald Welte8e7fca32017-05-07 16:14:33 +020036 if (size > RCTX_SIZE_SMALL) {
Harald Welte7b363062017-11-04 00:06:03 +010037 local_irq_restore(x);
Harald Welte8e7fca32017-05-07 16:14:33 +020038 TRACE_ERROR("%s() request too large(%d > %d)\r\n", __func__, size, RCTX_SIZE_SMALL);
39 return NULL;
40 }
41
42 for (i = 0; i < ARRAY_SIZE(msgb_inuse); i++) {
43 if (!msgb_inuse[i]) {
44 uint8_t *out = msgb_data[i];
45 msgb_inuse[i] = 1;
46 memset(out, 0, size);
Harald Welte7b363062017-11-04 00:06:03 +010047 local_irq_restore(x);
Harald Welte8e7fca32017-05-07 16:14:33 +020048 return out;
49 }
50 }
Harald Welte7b363062017-11-04 00:06:03 +010051 local_irq_restore(x);
Harald Welte8e7fca32017-05-07 16:14:33 +020052 TRACE_ERROR("%s() out of memory!\r\n", __func__);
53 return NULL;
54}
55
56int _talloc_free(void *ptr, const char *location)
57{
58 unsigned int i;
Harald Welte7b363062017-11-04 00:06:03 +010059 unsigned long x;
60
61 local_irq_save(x);
Harald Welte8e7fca32017-05-07 16:14:33 +020062 for (i = 0; i < ARRAY_SIZE(msgb_inuse); i++) {
63 if (ptr == msgb_data[i]) {
64 if (!msgb_inuse[i]) {
Harald Weltec3941092018-08-26 09:53:13 +020065 TRACE_ERROR("%s: double_free by %s\r\n", __func__, location);
Harald Welte8e7fca32017-05-07 16:14:33 +020066 } else {
67 msgb_inuse[i] = 0;
68 }
Harald Welte7b363062017-11-04 00:06:03 +010069 local_irq_restore(x);
Harald Welte8e7fca32017-05-07 16:14:33 +020070 return 0;
71 }
72 }
73
Harald Welte7b363062017-11-04 00:06:03 +010074 local_irq_restore(x);
Harald Welte8e7fca32017-05-07 16:14:33 +020075 TRACE_ERROR("%s: invalid pointer %p from %s\r\n", __func__, ptr, location);
76 return -1;
77}
78
79void talloc_set_name_const(const void *ptr, const char *name)
80{
81 /* do nothing */
82}
83
84#if 0
85void *talloc_named_const(const void *context, size_t size, const char *name)
86{
87 if (size)
88 TRACE_ERROR("%s: called with size!=0 from %s\r\n", __func__, name);
89 return NULL;
90}
91
92void *talloc_pool(const void *context, size_t size)
93{
94}
95#endif
96