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>
This commit is contained in:
parent
8f1d0c2a7c
commit
f331936969
|
@ -1,15 +1,18 @@
|
||||||
// AVR miscellaneous platform code
|
// 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.
|
// This file may be distributed under the terms of the GNU GPLv3 license.
|
||||||
|
|
||||||
#include <avr/io.h> // AVR_STACK_POINTER_REG
|
#include <avr/io.h> // AVR_STACK_POINTER_REG
|
||||||
#include <stdlib.h> // __malloc_heap_end
|
#include <stdlib.h> // __malloc_heap_end
|
||||||
|
#include <string.h> // memset
|
||||||
#include <util/crc16.h> // _crc_ccitt_update
|
#include <util/crc16.h> // _crc_ccitt_update
|
||||||
#include "autoconf.h" // CONFIG_AVR_STACK_SIZE
|
#include "autoconf.h" // CONFIG_AVR_STACK_SIZE
|
||||||
#include "board/misc.h" // alloc_maxsize
|
#include "board/misc.h" // alloc_maxsize
|
||||||
|
#include "command.h" // shutdown
|
||||||
#include "compiler.h" // ALIGN
|
#include "compiler.h" // ALIGN
|
||||||
|
#include "sched.h" // sched_shutdown
|
||||||
|
|
||||||
// Optimized crc16_ccitt for the avr processor
|
// Optimized crc16_ccitt for the avr processor
|
||||||
uint16_t
|
uint16_t
|
||||||
|
@ -21,17 +24,31 @@ crc16_ccitt(char *buf, uint8_t len)
|
||||||
return crc;
|
return crc;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return the maximum allocation size that can succeed up to 'reqsize'
|
// Allocate an area of memory
|
||||||
size_t
|
void *
|
||||||
alloc_maxsize(size_t reqsize)
|
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);
|
uint16_t memend = ALIGN(AVR_STACK_POINTER_REG, 256);
|
||||||
__malloc_heap_end = (void*)memend - CONFIG_AVR_STACK_SIZE;
|
__malloc_heap_end = (void*)memend - CONFIG_AVR_STACK_SIZE;
|
||||||
extern char *__brkval;
|
extern char *__brkval;
|
||||||
int16_t maxsize = __malloc_heap_end - __brkval - 2;
|
uint16_t maxsize = __malloc_heap_end - __brkval - 2;
|
||||||
if (maxsize < 0)
|
if ((int16_t)maxsize < 0)
|
||||||
return 0;
|
maxsize = 0;
|
||||||
if (reqsize < maxsize)
|
if (count * size > maxsize)
|
||||||
return reqsize;
|
count = maxsize / size;
|
||||||
return maxsize;
|
if (!count)
|
||||||
|
shutdown("alloc_chunks failed");
|
||||||
|
*avail = count;
|
||||||
|
return alloc_chunk(count * size);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,9 @@
|
||||||
// Basic infrastructure commands.
|
// 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.
|
// 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 "basecmd.h" // oid_lookup
|
||||||
#include "board/irq.h" // irq_save
|
#include "board/irq.h" // irq_save
|
||||||
#include "board/misc.h" // alloc_maxsize
|
#include "board/misc.h" // alloc_maxsize
|
||||||
|
@ -87,10 +85,8 @@ static void
|
||||||
move_finalize(void)
|
move_finalize(void)
|
||||||
{
|
{
|
||||||
move_request_size(sizeof(*move_free_list));
|
move_request_size(sizeof(*move_free_list));
|
||||||
uint16_t count = alloc_maxsize(move_item_size*1024) / move_item_size;
|
size_t count;
|
||||||
move_list = malloc(count * move_item_size);
|
move_list = alloc_chunks(move_item_size, 1024, &count);
|
||||||
if (!count || !move_list)
|
|
||||||
shutdown("move queue malloc failed");
|
|
||||||
move_count = count;
|
move_count = count;
|
||||||
move_reset();
|
move_reset();
|
||||||
}
|
}
|
||||||
|
@ -115,23 +111,14 @@ oid_lookup(uint8_t oid, void *type)
|
||||||
return oids[oid].data;
|
return oids[oid].data;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
void *
|
||||||
oid_assign(uint8_t oid, void *type, void *data)
|
oid_alloc(uint8_t oid, void *type, uint16_t size)
|
||||||
{
|
{
|
||||||
if (oid >= oid_count || oids[oid].type || is_finalized())
|
if (oid >= oid_count || oids[oid].type || is_finalized())
|
||||||
shutdown("Can't assign oid");
|
shutdown("Can't assign oid");
|
||||||
oids[oid].type = type;
|
oids[oid].type = type;
|
||||||
|
void *data = alloc_chunk(size);
|
||||||
oids[oid].data = data;
|
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;
|
return data;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -156,10 +143,7 @@ command_allocate_oids(uint32_t *args)
|
||||||
if (oids)
|
if (oids)
|
||||||
shutdown("oids already allocated");
|
shutdown("oids already allocated");
|
||||||
uint8_t count = args[0];
|
uint8_t count = args[0];
|
||||||
oids = malloc(sizeof(oids[0]) * count);
|
oids = alloc_chunk(sizeof(oids[0]) * count);
|
||||||
if (!oids)
|
|
||||||
shutdown("malloc failed");
|
|
||||||
memset(oids, 0, sizeof(oids[0]) * count);
|
|
||||||
oid_count = count;
|
oid_count = count;
|
||||||
}
|
}
|
||||||
DECL_COMMAND(command_allocate_oids, "allocate_oids count=%c");
|
DECL_COMMAND(command_allocate_oids, "allocate_oids count=%c");
|
||||||
|
|
|
@ -1,13 +1,37 @@
|
||||||
// Dummy implementation for alloc commands
|
// 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.
|
// 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'
|
// 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;
|
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);
|
||||||
|
}
|
||||||
|
|
|
@ -14,7 +14,8 @@ uint8_t timer_is_before(uint32_t time1, uint32_t time2);
|
||||||
uint32_t timer_read_time(void);
|
uint32_t timer_read_time(void);
|
||||||
void timer_periodic(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);
|
uint16_t crc16_ccitt(char *buf, uint8_t len);
|
||||||
|
|
||||||
|
|
|
@ -16,7 +16,8 @@ LDFLAGS-y += --specs=nano.specs --specs=nosys.specs
|
||||||
|
|
||||||
# Add source files
|
# Add source files
|
||||||
src-y += sam3x8e/main.c sam3x8e/timer.c sam3x8e/gpio.c
|
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/system_sam3xa.c
|
||||||
src-y += ../lib/cmsis-sam3x8e/source/gcc/startup_sam3xa.c
|
src-y += ../lib/cmsis-sam3x8e/source/gcc/startup_sam3xa.c
|
||||||
src-$(CONFIG_SERIAL) += sam3x8e/serial.c
|
src-$(CONFIG_SERIAL) += sam3x8e/serial.c
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
// Main starting point for SAM3x8e boards.
|
// 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.
|
// 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");
|
DECL_COMMAND_FLAGS(command_reset, HF_IN_SHUTDOWN, "reset");
|
||||||
|
|
||||||
size_t
|
|
||||||
alloc_maxsize(size_t reqsize)
|
|
||||||
{
|
|
||||||
return reqsize;
|
|
||||||
}
|
|
||||||
|
|
||||||
void * __visible
|
void * __visible
|
||||||
_sbrk(int nbytes)
|
_sbrk(int nbytes)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue