Skip to content
Snippets Groups Projects
Commit e8bf4ef6 authored by René Kijewski's avatar René Kijewski
Browse files

Merge pull request #1182 from Kijewski/msp-oneway-malloc

msp430: provide oneway-malloc implicitly
parents 9f40d019 f3eeb98a
No related branches found
No related tags found
No related merge requests found
/*
* Copyright (C) 2013 Freie Universität Berlin
*
* This file is subject to the terms and conditions of the GNU Lesser General
* Public License. See the file LICENSE in the top level directory for more
* details.
*/
/**
* @addtogroup core_util
* @{
*
* @file oneway_malloc.h
* @brief Simple malloc wrapper for sbrk
*
* @author Freie Universität Berlin, Computer Systems & Telematics
* @author Kaspar Schleiser <kaspar@schleiser.de>
*
* @detail Simple malloc implementation for plattforms
* without malloc in libc, e.g. msb430.
*/
#ifndef _MALLOC_H_
#define _MALLOC_H_
#include <stddef.h>
/**
* @brief Allocates a block of `size` bytes of memory
*
* @param[in] Size of the memory block, in bytes.
*
* @return On success, a pointer to the memory block allocated by the function.
* @return NULL otherwise.
*/
void *_malloc(size_t size);
/**
* @brief Changes the size of the memory block pointed to by ptr.
*
* @param[in] Pointer to a memory block previously allocated with malloc,
* calloc or realloc.
*
* @param[in] New size for the memory block, in bytes.
*
* @return A pointer to the reallocated memory block, which may be either
* the same as ptr or a new location.
*/
void *_realloc(void *ptr, size_t size);
/**
* @brief Allocates a block of memory for an array of num elements,
* each of them size bytes long, and initializes all its bits to zero.
*
* @param[in] Number of elements to allocate.
* @param[in] Size of each element.
*
* @return On success, a pointer to the memory block allocated by the function.
* If the function failed to allocate the requested block of memory,
* a null pointer is returned.
*/
void *_calloc(int size, size_t cnt);
/**
* @brief A block of memory previously allocated by a call to malloc,
* calloc or realloc is deallocated, making it available again
* for further allocations.
*
* @param[in] Pointer to a memory block previously allocated with malloc,
* calloc or realloc.
*/
void _free(void *ptr);
/** @} */
#endif /* _MALLOC_H_ */
INCLUDES += -I$(RIOTBASE)/cpu/msp430-common/include/ INCLUDES += -I$(RIOTBASE)/cpu/msp430-common/include/
DEFAULT_MODULE += oneway_malloc
#ifndef __MALLOC_H
#define __MALLOC_H
#include "oneway_malloc.h"
#define malloc _malloc
#define calloc _calloc
#define realloc _realloc
#define free _free
#endif /* __MALLOC_H */
...@@ -89,6 +89,9 @@ endif ...@@ -89,6 +89,9 @@ endif
ifneq (,$(filter quad_math,$(USEMODULE))) ifneq (,$(filter quad_math,$(USEMODULE)))
DIRS += quad_math DIRS += quad_math
endif endif
ifneq (,$(filter oneway_malloc,$(USEMODULE)))
DIRS += oneway-malloc
endif
all: $(BINDIR)$(MODULE).a all: $(BINDIR)$(MODULE).a
@for i in $(DIRS) ; do "$(MAKE)" -C $$i || exit 1; done ; @for i in $(DIRS) ; do "$(MAKE)" -C $$i || exit 1; done ;
......
...@@ -43,3 +43,7 @@ endif ...@@ -43,3 +43,7 @@ endif
ifneq (,$(filter pthread,$(USEMODULE))) ifneq (,$(filter pthread,$(USEMODULE)))
USEMODULE_INCLUDES += $(RIOTBASE)/sys/posix/pthread/include USEMODULE_INCLUDES += $(RIOTBASE)/sys/posix/pthread/include
endif endif
ifneq (,$(filter oneway_malloc,$(USEMODULE)))
USEMODULE_INCLUDES += $(RIOTBASE)/sys/oneway-malloc/include
endif
MODULE = oneway_malloc
include $(RIOTBASE)/Makefile.base
/*
* Copyright (C) 2014 Freie Universität Berlin
*
* This file is subject to the terms and conditions of the GNU Lesser General
* Public License. See the file LICENSE in the top level directory for more
* details.
*/
/**
* @addtogroup oneway_malloc
* @{
* @file malloc.h
* @brief A malloc implementation for MSP-430 boards without free.
*
* @details The toolchain of MSP-430 does not contain malloc() and friends.
* These functions provide the same interface as the stdlib functions,
* but the option to free memory.
*
* @note You should prefer statically allocated memory whenever possible.
*
* @author Kaspar Schleiser <kaspar@schleiser.de>
* @author René Kijewski <rene.kijewski@fu-berlin.de>
*/
#ifndef __MALLOC_H
#define __MALLOC_H
#include <stdlib.h>
/**
* @brief Allocation a block of memory.
* @param[in] size Size of the block to allocate in bytes.
* @returns The new memory block. `NULL` if the "heap" is exhausted.
*/
void *malloc(size_t size);
/**
* @brief Allocated a new block of memory and move the existing content.
* @details This function allocates a new block of memory and memcpy()s the content of the ond `ptr` there.
*
* We do not know the size of the old block, so illegal reads would be likely,
* if it was not for the fact that the memory heap up.
* @param[in] ptr Old memory block that was allocated with malloc(), calloc() or realloc().
* @param[in] size Size of the new block to allocted in bytes.
* @returns The new memory block. `NULL` if the "heap" is exhausted.
*/
void *realloc(void *ptr, size_t size);
/**
* @brief Allocate a memory block and set all its content to zeroes.
* @details Please see malloc() for more information.
* @note This implementation of calloc() does not catch integer overflows
* @param[in] size One factor of the number of bytes to allocated.
* @param[in] cnt The other factor of the number of bytes to allocated.
* @returns The new memory block. `NULL` if the "heap" is exhausted.
*/
void *calloc(int size, size_t cnt);
/**
* @brief This is a no-op.
* @details You read correctly: This function does noting.
* @note Keep in mind that this function does not free the memory. It does nothing.
* @param[in] ptr The ignored argument.
*/
void free(void *ptr);
#endif /* __MALLOC_H */
/**
* @}
*/
...@@ -7,7 +7,8 @@ ...@@ -7,7 +7,8 @@
*/ */
/** /**
* @ingroup core_util * @addtogroup oneway_malloc
* @ingroup sys
* @{ * @{
* *
* @file oneway_malloc.c * @file oneway_malloc.c
...@@ -18,50 +19,60 @@ ...@@ -18,50 +19,60 @@
* @} * @}
*/ */
#include <stdio.h>
#include <string.h> #include <string.h>
#include <stdlib.h>
#include "malloc.h"
#define ENABLE_DEBUG (0) #define ENABLE_DEBUG (0)
#include "debug.h" #include "debug.h"
extern void *sbrk(int incr); extern void *sbrk(int incr);
void *_malloc(size_t size) void __attribute__((weak)) *malloc(size_t size)
{ {
void *ptr = sbrk(size); if (size != 0) {
void *ptr = sbrk(size);
DEBUG("_malloc(): allocating block of size %u at 0x%X.\n", (unsigned int) size, (unsigned int)ptr); DEBUG("malloc(): allocating block of size %u at %p.\n", (unsigned int) size, ptr);
if (ptr != (void*) - 1) { if (ptr != (void*) -1) {
return ptr; return ptr;
} }
else {
return NULL;
} }
return NULL;
} }
void *_realloc(void *ptr, size_t size) void __attribute__((weak)) *realloc(void *ptr, size_t size)
{ {
void *newptr = _malloc(size); if (ptr == NULL) {
memcpy(newptr, ptr, size); return malloc(size);
free(ptr); }
return newptr; else if (size == 0) {
free(ptr);
return NULL;
}
else {
void *newptr = malloc(size);
if (newptr) {
memcpy(newptr, ptr, size);
}
return newptr;
}
} }
void *_calloc(int size, size_t cnt) void __attribute__((weak)) *calloc(int size, size_t cnt)
{ {
void *mem = _malloc(size * cnt); void *mem = malloc(size * cnt);
if (mem) { if (mem) {
memset(mem, 0, size * cnt); memset(mem, 0, size * cnt);
} }
return mem; return mem;
} }
void _free(void *ptr) void __attribute__((weak)) free(void *ptr)
{ {
/* who cares about pointers? */ /* who cares about pointers? */
(void) ptr; (void) ptr;
DEBUG("_free(): block at 0x%X lost.\n", (unsigned int)ptr); DEBUG("free(): block at %p lost.\n", ptr);
} }
export PROJECT = test_bloom_bytes export PROJECT = test_bloom_bytes
include ../Makefile.tests_common include ../Makefile.tests_common
BOARD_BLACKLIST := chronos msb-430 msb-430h \
telosb wsn430-v1_3b wsn430-v1_4 z1
# chronos: https://github.com/RIOT-OS/RIOT/issues/1061
# msb-430: https://github.com/RIOT-OS/RIOT/issues/1061
# msb-430h: https://github.com/RIOT-OS/RIOT/issues/1061
# telosb: https://github.com/RIOT-OS/RIOT/issues/1061
# wsn430-v1_3b: https://github.com/RIOT-OS/RIOT/issues/1061
# wsn430-v1_4: https://github.com/RIOT-OS/RIOT/issues/1061
# z1: https://github.com/RIOT-OS/RIOT/issues/1061
USEMODULE += hashes USEMODULE += hashes
USEMODULE += bloom USEMODULE += bloom
USEMODULE += random USEMODULE += random
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment