aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorKevin O'Connor <kevin@koconnor.net>2017-05-06 22:29:08 -0400
committerKevin O'Connor <kevin@koconnor.net>2017-05-11 13:56:21 -0400
commitf331936969552c9864e8ef58ec329e7060a5af60 (patch)
treef46b4f64ee7672343b7eece9a9c1aa18f76b84b4 /src
parent8f1d0c2a7c49b596cb5a9a8b92b3200485049cf6 (diff)
downloadkutter-f331936969552c9864e8ef58ec329e7060a5af60.tar.gz
kutter-f331936969552c9864e8ef58ec329e7060a5af60.tar.xz
kutter-f331936969552c9864e8ef58ec329e7060a5af60.zip
basecmd: Avoid calling malloc() from main code
Introduce a new board function alloc_chunk() to allocate dynamic memory. This allows the board code to implement memory allocations without using the standard malloc() interface. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Diffstat (limited to 'src')
-rw-r--r--src/avr/misc.c37
-rw-r--r--src/basecmd.c30
-rw-r--r--src/generic/alloc.c30
-rw-r--r--src/generic/misc.h3
-rw-r--r--src/sam3x8e/Makefile3
-rw-r--r--src/sam3x8e/main.c8
6 files changed, 66 insertions, 45 deletions
diff --git a/src/avr/misc.c b/src/avr/misc.c
index 682ef29d..9740a806 100644
--- a/src/avr/misc.c
+++ b/src/avr/misc.c
@@ -1,15 +1,18 @@
// AVR miscellaneous platform code
//
-// Copyright (C) 2016 Kevin O'Connor <kevin@koconnor.net>
+// Copyright (C) 2016,2017 Kevin O'Connor <kevin@koconnor.net>
//
// This file may be distributed under the terms of the GNU GPLv3 license.
#include <avr/io.h> // AVR_STACK_POINTER_REG
#include <stdlib.h> // __malloc_heap_end
+#include <string.h> // memset
#include <util/crc16.h> // _crc_ccitt_update
#include "autoconf.h" // CONFIG_AVR_STACK_SIZE
#include "board/misc.h" // alloc_maxsize
+#include "command.h" // shutdown
#include "compiler.h" // ALIGN
+#include "sched.h" // sched_shutdown
// Optimized crc16_ccitt for the avr processor
uint16_t
@@ -21,17 +24,31 @@ crc16_ccitt(char *buf, uint8_t len)
return crc;
}
-// Return the maximum allocation size that can succeed up to 'reqsize'
-size_t
-alloc_maxsize(size_t reqsize)
+// Allocate an area of memory
+void *
+alloc_chunk(size_t size)
+{
+ void *data = malloc(size);
+ if (!data)
+ shutdown("alloc_chunk failed");
+ memset(data, 0, size);
+ return data;
+}
+
+// Allocate an array of chunks
+void *
+alloc_chunks(size_t size, size_t count, size_t *avail)
{
uint16_t memend = ALIGN(AVR_STACK_POINTER_REG, 256);
__malloc_heap_end = (void*)memend - CONFIG_AVR_STACK_SIZE;
extern char *__brkval;
- int16_t maxsize = __malloc_heap_end - __brkval - 2;
- if (maxsize < 0)
- return 0;
- if (reqsize < maxsize)
- return reqsize;
- return maxsize;
+ uint16_t maxsize = __malloc_heap_end - __brkval - 2;
+ if ((int16_t)maxsize < 0)
+ maxsize = 0;
+ if (count * size > maxsize)
+ count = maxsize / size;
+ if (!count)
+ shutdown("alloc_chunks failed");
+ *avail = count;
+ return alloc_chunk(count * size);
}
diff --git a/src/basecmd.c b/src/basecmd.c
index 10b04fad..becbe043 100644
--- a/src/basecmd.c
+++ b/src/basecmd.c
@@ -1,11 +1,9 @@
// Basic infrastructure commands.
//
-// Copyright (C) 2016 Kevin O'Connor <kevin@koconnor.net>
+// Copyright (C) 2016,2017 Kevin O'Connor <kevin@koconnor.net>
//
// This file may be distributed under the terms of the GNU GPLv3 license.
-#include <stdlib.h> // malloc
-#include <string.h> // memset
#include "basecmd.h" // oid_lookup
#include "board/irq.h" // irq_save
#include "board/misc.h" // alloc_maxsize
@@ -87,10 +85,8 @@ static void
move_finalize(void)
{
move_request_size(sizeof(*move_free_list));
- uint16_t count = alloc_maxsize(move_item_size*1024) / move_item_size;
- move_list = malloc(count * move_item_size);
- if (!count || !move_list)
- shutdown("move queue malloc failed");
+ size_t count;
+ move_list = alloc_chunks(move_item_size, 1024, &count);
move_count = count;
move_reset();
}
@@ -115,23 +111,14 @@ oid_lookup(uint8_t oid, void *type)
return oids[oid].data;
}
-static void
-oid_assign(uint8_t oid, void *type, void *data)
+void *
+oid_alloc(uint8_t oid, void *type, uint16_t size)
{
if (oid >= oid_count || oids[oid].type || is_finalized())
shutdown("Can't assign oid");
oids[oid].type = type;
+ void *data = alloc_chunk(size);
oids[oid].data = data;
-}
-
-void *
-oid_alloc(uint8_t oid, void *type, uint16_t size)
-{
- void *data = malloc(size);
- if (!data)
- shutdown("malloc failed");
- memset(data, 0, size);
- oid_assign(oid, type, data);
return data;
}
@@ -156,10 +143,7 @@ command_allocate_oids(uint32_t *args)
if (oids)
shutdown("oids already allocated");
uint8_t count = args[0];
- oids = malloc(sizeof(oids[0]) * count);
- if (!oids)
- shutdown("malloc failed");
- memset(oids, 0, sizeof(oids[0]) * count);
+ oids = alloc_chunk(sizeof(oids[0]) * count);
oid_count = count;
}
DECL_COMMAND(command_allocate_oids, "allocate_oids count=%c");
diff --git a/src/generic/alloc.c b/src/generic/alloc.c
index 826e4f9e..37de4d6a 100644
--- a/src/generic/alloc.c
+++ b/src/generic/alloc.c
@@ -1,13 +1,37 @@
// Dummy implementation for alloc commands
//
-// Copyright (C) 2016 Kevin O'Connor <kevin@koconnor.net>
+// Copyright (C) 2016,2017 Kevin O'Connor <kevin@koconnor.net>
//
// This file may be distributed under the terms of the GNU GPLv3 license.
-#include "misc.h"
+#include <stdlib.h> // malloc
+#include <string.h> // memset
+#include "command.h" // shutdown
+#include "misc.h" // alloc_chunk
+#include "sched.h" // sched_shutdown
// Return the maximum allocation size that can succeed up to 'reqsize'
-size_t alloc_maxsize(size_t reqsize)
+size_t
+alloc_chunk_maxsize(size_t reqsize)
{
return reqsize;
}
+
+// Allocate an area of memory
+void *
+alloc_chunk(size_t size)
+{
+ void *data = malloc(size);
+ if (!data)
+ shutdown("alloc_chunk failed");
+ memset(data, 0, size);
+ return data;
+}
+
+// Allocate an array of chunks
+void *
+alloc_chunks(size_t size, size_t count, size_t *avail)
+{
+ *avail = count;
+ return alloc_chunk(size * count);
+}
diff --git a/src/generic/misc.h b/src/generic/misc.h
index e8a13c00..4408a34a 100644
--- a/src/generic/misc.h
+++ b/src/generic/misc.h
@@ -14,7 +14,8 @@ uint8_t timer_is_before(uint32_t time1, uint32_t time2);
uint32_t timer_read_time(void);
void timer_periodic(void);
-size_t alloc_maxsize(size_t reqsize);
+void *alloc_chunk(size_t size);
+void *alloc_chunks(size_t size, size_t count, size_t *avail);
uint16_t crc16_ccitt(char *buf, uint8_t len);
diff --git a/src/sam3x8e/Makefile b/src/sam3x8e/Makefile
index f6986b1d..e3379630 100644
--- a/src/sam3x8e/Makefile
+++ b/src/sam3x8e/Makefile
@@ -16,7 +16,8 @@ LDFLAGS-y += --specs=nano.specs --specs=nosys.specs
# Add source files
src-y += sam3x8e/main.c sam3x8e/timer.c sam3x8e/gpio.c
-src-y += generic/crc16_ccitt.c generic/armcm_irq.c generic/timer_irq.c
+src-y += generic/crc16_ccitt.c generic/alloc.c
+src-y += generic/armcm_irq.c generic/timer_irq.c
src-y += ../lib/cmsis-sam3x8e/source/system_sam3xa.c
src-y += ../lib/cmsis-sam3x8e/source/gcc/startup_sam3xa.c
src-$(CONFIG_SERIAL) += sam3x8e/serial.c
diff --git a/src/sam3x8e/main.c b/src/sam3x8e/main.c
index a7e7ee55..50cc18b4 100644
--- a/src/sam3x8e/main.c
+++ b/src/sam3x8e/main.c
@@ -1,6 +1,6 @@
// Main starting point for SAM3x8e boards.
//
-// Copyright (C) 2016 Kevin O'Connor <kevin@koconnor.net>
+// Copyright (C) 2016,2017 Kevin O'Connor <kevin@koconnor.net>
//
// This file may be distributed under the terms of the GNU GPLv3 license.
@@ -43,12 +43,6 @@ command_reset(uint32_t *args)
}
DECL_COMMAND_FLAGS(command_reset, HF_IN_SHUTDOWN, "reset");
-size_t
-alloc_maxsize(size_t reqsize)
-{
- return reqsize;
-}
-
void * __visible
_sbrk(int nbytes)
{