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>
This commit is contained in:
parent
2ee42997e4
commit
649d26e093
|
@ -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;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -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
|
||||
|
@ -12,6 +13,47 @@
|
|||
#include "sched.h" // sched_clear_shutdown
|
||||
|
||||
|
||||
/****************************************************************
|
||||
* 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);
|
||||
|
|
|
@ -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)];
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue