aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorKevin O'Connor <kevin@koconnor.net>2017-07-19 23:10:08 -0400
committerKevin O'Connor <kevin@koconnor.net>2017-07-20 10:44:31 -0400
commit649d26e093497991ebc1a89f4a5cfeb29d4602d4 (patch)
tree7efde06f1f198198f1497ceb4b1db1a00ae4690a /src
parent2ee42997e4b8ac4d8ca84cc53ecc9859baafbbbd (diff)
downloadkutter-649d26e093497991ebc1a89f4a5cfeb29d4602d4.tar.gz
kutter-649d26e093497991ebc1a89f4a5cfeb29d4602d4.tar.xz
kutter-649d26e093497991ebc1a89f4a5cfeb29d4602d4.zip
basecmd: Move low-level alloc code into basecmd.c
Implement new dynmem_start() and dynmem_end() functions instead of alloc_chunk() and alloc_chunks() in the board code. This simplifies the board code. Signed-off-by: Kevin O'Connor <kevin@koconnor.net>
Diffstat (limited to 'src')
-rw-r--r--src/avr/main.c33
-rw-r--r--src/basecmd.c50
-rw-r--r--src/generic/alloc.c32
-rw-r--r--src/generic/misc.h4
-rw-r--r--src/pru/main.c34
-rw-r--r--src/sam3x8e/main.c11
6 files changed, 74 insertions, 90 deletions
diff --git a/src/avr/main.c b/src/avr/main.c
index a210300f..e3de4c03 100644
--- a/src/avr/main.c
+++ b/src/avr/main.c
@@ -5,10 +5,9 @@
// 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_MCU
+#include "board/misc.h" // dynmem_start
#include "command.h" // DECL_CONSTANT
#include "irq.h" // irq_enable
#include "sched.h" // sched_main
@@ -17,36 +16,22 @@ DECL_CONSTANT(MCU, CONFIG_MCU);
/****************************************************************
- * Memmory allocation
+ * Dynamic memory
****************************************************************/
-// Allocate an area of memory
+// Return the start of memory available for dynamic allocations
void *
-alloc_chunk(size_t size)
+dynmem_start(void)
{
- void *data = malloc(size);
- if (!data)
- shutdown("alloc_chunk failed");
- memset(data, 0, size);
- return data;
+ extern char _end;
+ return &_end;
}
-// Allocate an array of chunks
+// Return the end of memory available for dynamic allocations
void *
-alloc_chunks(size_t size, size_t count, size_t *avail)
+dynmem_end(void)
{
- uint16_t memend = ALIGN(AVR_STACK_POINTER_REG, 256);
- __malloc_heap_end = (void*)memend - CONFIG_AVR_STACK_SIZE;
- extern char *__brkval;
- 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);
+ return (void*)ALIGN(AVR_STACK_POINTER_REG, 256) - CONFIG_AVR_STACK_SIZE;
}
diff --git a/src/basecmd.c b/src/basecmd.c
index 7a2f7882..da21fad0 100644
--- a/src/basecmd.c
+++ b/src/basecmd.c
@@ -4,6 +4,7 @@
//
// This file may be distributed under the terms of the GNU GPLv3 license.
+#include <string.h> // memset
#include "basecmd.h" // oid_lookup
#include "board/irq.h" // irq_save
#include "board/misc.h" // alloc_maxsize
@@ -13,6 +14,47 @@
/****************************************************************
+ * Low level allocation
+ ****************************************************************/
+
+static void *alloc_end;
+
+void
+alloc_init(void)
+{
+ alloc_end = (void*)ALIGN((size_t)dynmem_start(), __alignof__(void*));
+}
+DECL_INIT(alloc_init);
+
+// Allocate an area of memory
+static void *
+alloc_chunk(size_t size)
+{
+ if (alloc_end + size > dynmem_end())
+ shutdown("alloc_chunk failed");
+ void *data = alloc_end;
+ alloc_end += ALIGN(size, __alignof__(void*));
+ memset(data, 0, size);
+ return data;
+}
+
+// Allocate an array of chunks
+static void *
+alloc_chunks(size_t size, size_t count, uint16_t *avail)
+{
+ size_t can_alloc = 0;
+ void *p = alloc_end, *end = dynmem_end();
+ while (can_alloc < count && p + size <= end)
+ can_alloc++, p += size;
+ if (!can_alloc)
+ shutdown("alloc_chunks failed");
+ void *data = alloc_chunk(p - alloc_end);
+ *avail = can_alloc;
+ return data;
+}
+
+
+/****************************************************************
* Move queue
****************************************************************/
@@ -85,10 +127,10 @@ DECL_SHUTDOWN(move_reset);
static void
move_finalize(void)
{
+ if (is_finalized())
+ shutdown("Already finalized");
move_request_size(sizeof(*move_free_list));
- size_t count;
- move_list = alloc_chunks(move_item_size, 1024, &count);
- move_count = count;
+ move_list = alloc_chunks(move_item_size, 1024, &move_count);
move_reset();
}
@@ -167,8 +209,6 @@ DECL_COMMAND_FLAGS(command_get_config, HF_IN_SHUTDOWN, "get_config");
void
command_finalize_config(uint32_t *args)
{
- if (!oids || is_finalized())
- shutdown("Can't finalize");
move_finalize();
config_crc = args[0];
command_get_config(NULL);
diff --git a/src/generic/alloc.c b/src/generic/alloc.c
index 37de4d6a..1ff75113 100644
--- a/src/generic/alloc.c
+++ b/src/generic/alloc.c
@@ -1,37 +1,23 @@
-// Dummy implementation for alloc commands
+// Generic implementation of dynamic memory pool
//
// 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 "command.h" // shutdown
-#include "misc.h" // alloc_chunk
-#include "sched.h" // sched_shutdown
+#include "misc.h" // dynmem_start
-// Return the maximum allocation size that can succeed up to 'reqsize'
-size_t
-alloc_chunk_maxsize(size_t reqsize)
-{
- return reqsize;
-}
+static char dynmem_pool[20 * 1024];
-// Allocate an area of memory
+// Return the start of memory available for dynamic allocations
void *
-alloc_chunk(size_t size)
+dynmem_start(void)
{
- void *data = malloc(size);
- if (!data)
- shutdown("alloc_chunk failed");
- memset(data, 0, size);
- return data;
+ return dynmem_pool;
}
-// Allocate an array of chunks
+// Return the end of memory available for dynamic allocations
void *
-alloc_chunks(size_t size, size_t count, size_t *avail)
+dynmem_end(void)
{
- *avail = count;
- return alloc_chunk(size * count);
+ return &dynmem_pool[sizeof(dynmem_pool)];
}
diff --git a/src/generic/misc.h b/src/generic/misc.h
index 960eb6a2..b2f1c050 100644
--- a/src/generic/misc.h
+++ b/src/generic/misc.h
@@ -13,8 +13,8 @@ uint8_t timer_is_before(uint32_t time1, uint32_t time2);
uint32_t timer_read_time(void);
void timer_periodic(void);
-void *alloc_chunk(size_t size);
-void *alloc_chunks(size_t size, size_t count, size_t *avail);
+void *dynmem_start(void);
+void *dynmem_end(void);
uint16_t crc16_ccitt(char *buf, uint8_t len);
diff --git a/src/pru/main.c b/src/pru/main.c
index 021bee3f..a95bde1b 100644
--- a/src/pru/main.c
+++ b/src/pru/main.c
@@ -5,12 +5,11 @@
// This file may be distributed under the terms of the GNU GPLv3 license.
#include <stdint.h> // uint32_t
-#include <string.h> // memset
#include <pru/io.h> // read_r31
#include <pru_iep.h> // CT_IEP
#include <pru_intc.h> // CT_INTC
#include <rsc_types.h> // resource_table
-#include "board/misc.h" // alloc_chunk
+#include "board/misc.h" // dynmem_start
#include "board/io.h" // readl
#include "board/irq.h" // irq_disable
#include "command.h" // shutdown
@@ -175,39 +174,24 @@ const struct command_parser shutdown_request = {
/****************************************************************
- * Allocator
+ * Dynamic memory
****************************************************************/
-extern char _heap_start;
-static void *heap_ptr = &_heap_start;
-
#define STACK_SIZE 256
-#define END_MEM ((void*)(8*1024 - STACK_SIZE))
-// Allocate an area of memory
+// Return the start of memory available for dynamic allocations
void *
-alloc_chunk(size_t size)
+dynmem_start(void)
{
- if (heap_ptr + size > END_MEM)
- shutdown("alloc_chunk failed");
- void *data = heap_ptr;
- heap_ptr += size;
- memset(data, 0, size);
- return data;
+ extern char _heap_start;
+ return &_heap_start;
}
-// Allocate an array of chunks
+// Return the end of memory available for dynamic allocations
void *
-alloc_chunks(size_t size, size_t count, size_t *avail)
+dynmem_end(void)
{
- size_t can_alloc = 0;
- void *p = heap_ptr;
- for (; can_alloc <= count && p + size <= END_MEM; can_alloc++, p += size)
- ;
- if (!can_alloc)
- shutdown("alloc_chunks failed");
- *avail = can_alloc;
- return alloc_chunk(size * can_alloc);
+ return (void*)(8*1024 - STACK_SIZE);
}
diff --git a/src/sam3x8e/main.c b/src/sam3x8e/main.c
index 63c27cd7..e8bd30ba 100644
--- a/src/sam3x8e/main.c
+++ b/src/sam3x8e/main.c
@@ -4,7 +4,6 @@
//
// This file may be distributed under the terms of the GNU GPLv3 license.
-#include "board/misc.h" // alloc_maxsize
#include "command.h" // DECL_CONSTANT
#include "sam3x8e.h" // WDT
#include "sched.h" // sched_main
@@ -43,16 +42,6 @@ command_reset(uint32_t *args)
}
DECL_COMMAND_FLAGS(command_reset, HF_IN_SHUTDOWN, "reset");
-void * __visible
-_sbrk(int nbytes)
-{
- extern char _end;
- static void *heap_ptr = (void *)&_end;
- void *pos = heap_ptr;
- heap_ptr = pos + nbytes;
- return pos;
-}
-
// Main entry point
int
main(void)