Skip to content

Commit

Permalink
Improvements and cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
Thalhammer committed Dec 5, 2019
1 parent 0d5e23d commit e374ee4
Show file tree
Hide file tree
Showing 29 changed files with 426 additions and 125 deletions.
13 changes: 13 additions & 0 deletions api/include/stdlib/assert.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once

#ifdef __cplusplus
extern "C" void abort_with_message(const char*);
#else
extern void abort_with_message(const char*);
#endif

#define xstr(s) str(s)
#define str(s) #s

#define assert(x) \
if(!(x)) abort_with_message("Assertion \"" #x "\" failed! file: \"" __FILE__ "\" line:" xstr(__LINE__));
2 changes: 2 additions & 0 deletions api/include/stdlib/cassert
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#pragma once
#include "assert.h"
2 changes: 2 additions & 0 deletions api/include/stdlib/climits
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#pragma once
#include "limits.h"
2 changes: 2 additions & 0 deletions api/include/stdlib/cstddef
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#pragma once
#include "stddef.h"
2 changes: 2 additions & 0 deletions api/include/stdlib/cstdint
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#pragma once
#include "stdint.h"
10 changes: 10 additions & 0 deletions api/include/stdlib/cstdlib
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#pragma once
#include "stdlib.h"

#ifdef __cplusplus
namespace std {
inline void abort() {
::abort();
}
}
#endif
2 changes: 2 additions & 0 deletions api/include/stdlib/cstring
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#pragma once
#include "string.h"
74 changes: 74 additions & 0 deletions api/include/stdlib/limits.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#pragma once

#undef CHAR_BIT
#define CHAR_BIT __CHAR_BIT__

/* Maximum length of a multibyte character. */
#ifndef MB_LEN_MAX
#define MB_LEN_MAX 1
#endif

/* Minimum and maximum values a `signed char' can hold. */
#undef SCHAR_MIN
#define SCHAR_MIN (-SCHAR_MAX - 1)
#undef SCHAR_MAX
#define SCHAR_MAX __SCHAR_MAX__

/* Maximum value an `unsigned char' can hold. (Minimum is 0). */
#undef UCHAR_MAX
#define UCHAR_MAX (SCHAR_MAX * 2 + 1)

/* Minimum and maximum values a `char' can hold. */
#ifdef __CHAR_UNSIGNED__
# undef CHAR_MIN
# if __SCHAR_MAX__ == __INT_MAX__
# define CHAR_MIN 0U
# else
# define CHAR_MIN 0
# endif
# undef CHAR_MAX
# define CHAR_MAX UCHAR_MAX
#else
# undef CHAR_MIN
# define CHAR_MIN SCHAR_MIN
# undef CHAR_MAX
# define CHAR_MAX SCHAR_MAX
#endif

/* Minimum and maximum values a `signed short int' can hold. */
#undef SHRT_MIN
#define SHRT_MIN (-SHRT_MAX - 1)
#undef SHRT_MAX
#define SHRT_MAX __SHRT_MAX__

#define USHRT_MAX (SHRT_MAX * 2 + 1)

/* Minimum and maximum values a `signed int' can hold. */
#undef INT_MIN
#define INT_MIN (-INT_MAX - 1)
#undef INT_MAX
#define INT_MAX __INT_MAX__

/* Maximum value an `unsigned int' can hold. (Minimum is 0). */
#undef UINT_MAX
#define UINT_MAX (INT_MAX * 2U + 1U)

/* Minimum and maximum values a `signed long int' can hold.
(Same as `int'). */
#undef LONG_MIN
#define LONG_MIN (-LONG_MAX - 1L)
#undef LONG_MAX
#define LONG_MAX __LONG_MAX__

/* Maximum value an `unsigned long int' can hold. (Minimum is 0). */
#undef ULONG_MAX
#define ULONG_MAX (LONG_MAX * 2UL + 1UL)

# undef LLONG_MIN
# define LLONG_MIN (-LLONG_MAX - 1LL)
# undef LLONG_MAX
# define LLONG_MAX __LONG_LONG_MAX__

/* Maximum value an `unsigned long long int' can hold. (Minimum is 0). */
# undef ULLONG_MAX
# define ULLONG_MAX (LLONG_MAX * 2ULL + 1ULL)
18 changes: 18 additions & 0 deletions api/include/stdlib/stddef.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
#pragma once

typedef __PTRDIFF_TYPE__ ptrdiff_t;
typedef __SIZE_TYPE__ size_t;
#ifndef __cplusplus
typedef __WCHAR_TYPE__ wchar_t;
#endif

#ifndef __cplusplus
#define NULL ((void *)0)
Expand All @@ -10,4 +16,16 @@ typedef __SIZE_TYPE__ size_t;

#if defined(__cplusplus) && __cplusplus >= 201103L
typedef decltype(nullptr) nullptr_t;
#endif

typedef struct {
long long __max_align_ll __attribute__((__aligned__(__alignof__(long long))));
long double __max_align_ld __attribute__((__aligned__(__alignof__(long double))));
} max_align_t;

#if defined(__cplusplus)
namespace std {
using ptrdiff_t = ::ptrdiff_t;
using size_t = ::size_t;
}
#endif
8 changes: 8 additions & 0 deletions api/include/stdlib/stdint.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#pragma once

#ifdef __cplusplus__
extern "C" {
#endif

#ifdef __INT8_TYPE__
typedef __INT8_TYPE__ int8_t;
#endif
Expand Down Expand Up @@ -111,3 +115,7 @@ typedef __UINTMAX_TYPE__ uintmax_t;

#define WINT_MAX __WINT_MAX__
#define WINT_MIN __WINT_MIN__

#ifdef __cplusplus__
}
#endif
6 changes: 6 additions & 0 deletions api/include/stdlib/stdlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,20 @@ extern void* malloc(size_t size);
extern void* calloc(size_t num, size_t size);
extern void free(void* ptr);

extern void abort();

// Non standard interface
typedef struct {
size_t bytes_total;
size_t bytes_free;
size_t num_current_allocations;
} malloc_stats_t;
struct TX_BYTE_POOL_STRUCT;
extern int malloc_init(void* baseptr, size_t len);
extern int malloc_stats(malloc_stats_t* stats);
extern struct TX_BYTE_POOL_STRUCT* malloc_get_heap_pool_raw();
#ifdef __cplusplus
}
namespace txpp { class byte_pool; }
extern txpp::byte_pool& malloc_get_heap_pool();
#endif
7 changes: 7 additions & 0 deletions api/include/stdlib/string.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
#pragma once
#include <stddef.h>

#ifdef __cplusplus
extern "C" {
#endif
extern size_t strlen(const char* str);
extern void* memset(void* ptr, int value, size_t num);
extern void* memcpy(void* destination, const void* source, size_t num);
extern int memcmp(const void* destination, const void* source, size_t num);

extern int strcmp(const char* str1, const char* str2);
extern char * strchr ( const char *, int );

#ifdef __cplusplus
}
#endif
6 changes: 2 additions & 4 deletions api/include/threadx_api/txm_module.h
Original file line number Diff line number Diff line change
Expand Up @@ -827,12 +827,10 @@ ULONG _txm_module_system_call12(ULONG request, ULONG param_1, ULONG param_2, UL

#define txm_module_thread_system_suspend(t) ((UINT) (_txm_module_kernel_call_dispatcher)(TXM_THREAD_SYSTEM_SUSPEND_CALL, (ULONG) t, (ULONG) 0, (ULONG) 0))
#define txm_module_object_pointer_get(o,n,p) ((UINT) (_txm_module_kernel_call_dispatcher)(TXM_MODULE_OBJECT_POINTER_GET_CALL, (ULONG) o, (ULONG) n, (ULONG) p))
//#define txm_module_object_allocate(o,s) ((UINT) (_txm_module_kernel_call_dispatcher)(TXM_MODULE_OBJECT_ALLOCATE_CALL, (ULONG) o, (ULONG) s, (ULONG) 0))
#define txm_module_object_allocate(o,s) ((UINT) (_txm_module_kernel_call_dispatcher)(TXM_MODULE_OBJECT_ALLOCATE_CALL, (ULONG) o, (ULONG) s, (ULONG) 0))
#define txm_module_object_deallocate(o) ((UINT) (_txm_module_kernel_call_dispatcher)(TXM_MODULE_OBJECT_DEALLOCATE_CALL, (ULONG) o, (ULONG) 0, (ULONG) 0))
#define txm_module_application_request(i,a,b,c) ((UINT) (_txm_module_kernel_call_dispatcher)((ULONG) TXM_APPLICATION_REQUEST_ID_BASE+i, (ULONG) a, (ULONG) b, (ULONG) c))

inline UINT txm_module_object_allocate(void** o,ULONG s) { return ((UINT)(_txm_module_kernel_call_dispatcher)(TXM_MODULE_OBJECT_ALLOCATE_CALL, (ULONG) o, (ULONG) s, (ULONG) 0)); }

#define nx_arp_dynamic_entries_invalidate(i) ((UINT) (_txm_module_kernel_call_dispatcher)(TXM_ARP_DYNAMIC_ENTRIES_INVALIDATE_CALL, (ULONG) i, (ULONG) 0, (ULONG) 0))
#define nx_arp_dynamic_entry_set(i,a,m,l) ((UINT) _txm_module_system_call4(TXM_ARP_DYNAMIC_ENTRY_SET_CALL, (ULONG) i, (ULONG) a, (ULONG) m, (ULONG) l))
#define nx_arp_enable(i,c,s) ((UINT) (_txm_module_kernel_call_dispatcher)(TXM_ARP_ENABLE_CALL, (ULONG) i, (ULONG) c, (ULONG) s))
Expand Down Expand Up @@ -1073,7 +1071,7 @@ TXM_MODULE_MANAGER_ADDITIONAL_PROTOTYPES
/* Determine if a C++ compiler is being used. If so, complete the standard
C conditional started above. */
#ifdef __cplusplus
}
}
#endif

#endif
7 changes: 0 additions & 7 deletions api/include/txpp/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,3 @@
#endif

#include "threadx_api/txm_module.h"

//#undef txm_module_object_allocate

template<typename T>
inline UINT txm_module_object_allocate(T** o) {
return ((UINT)(_txm_module_kernel_call_dispatcher)(TXM_MODULE_OBJECT_ALLOCATE_CALL, (ULONG) o, (ULONG) sizeof(T), (ULONG) 0));
}
5 changes: 4 additions & 1 deletion api/include/txpp/byte_pool.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ namespace txpp {
{
if(ptr == NULL || len == 0) return false;
if(m_pool != nullptr) return false;
if(txm_module_object_allocate(&m_pool) != TX_SUCCESS) return false;
if(txm_module_object_allocate(&m_pool, sizeof(TX_BYTE_POOL)) != TX_SUCCESS) return false;
if(tx_byte_pool_create(m_pool, name, ptr, len) != TX_SUCCESS) {
txm_module_object_deallocate(m_pool);
m_pool = nullptr;
Expand Down Expand Up @@ -102,6 +102,8 @@ namespace txpp {
info.total_bytes = m_length;
return true;
}

TX_BYTE_POOL* raw_pool() noexcept { return m_pool; }
};

template<size_t NumBytes>
Expand All @@ -122,5 +124,6 @@ namespace txpp {
void* malloc(size_t len, unsigned long timeout = TX_NO_WAIT) noexcept { return byte_pool::malloc(len, timeout); }
void* calloc(size_t num, size_t size, unsigned long timeout = TX_NO_WAIT) noexcept { return byte_pool::calloc(num, size, timeout); }
bool get_info(pool_info& info) const noexcept { return byte_pool::get_info(info); }
TX_BYTE_POOL* raw_pool() noexcept { return byte_pool::raw_pool(); }
};
}
12 changes: 6 additions & 6 deletions api/src/qapi/qapi_timer_user.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,15 @@ qapi_Status_t qapi_Timer_Undef(qapi_TIMER_handle_t timer_handle) {

qapi_Status_t qapi_Timer_Def(qapi_TIMER_handle_t* timer_handle, qapi_TIMER_define_attr_t* timer_attr) {
if(timer_handle == NULL) return QAPI_ERR_INVALID_PARAM;
int handle_size; // this value was cached in the original however this caused problems with gcc so we get it everytime.
_txm_module_system_call12(TXM_QAPI_TIMER_GET_TIMER_TYPE_SIZE, (ULONG)&handle_size, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
if(handle_size == 0) return QAPI_ERR_TIMEOUT;
static int handle_size = 0;
if(handle_size == 0) {
_txm_module_system_call12(TXM_QAPI_TIMER_GET_TIMER_TYPE_SIZE, (ULONG)&handle_size, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
if(handle_size == 0) return QAPI_ERR_TIMEOUT;
}
if(txm_module_object_allocate(timer_handle, handle_size) != 0 || *timer_handle == NULL) return QAPI_ERR_NO_MEMORY;
qapi_Status_t res = _txm_module_system_call12(TXM_QAPI_TIMER_DEF, (ULONG)timer_handle, (ULONG)timer_attr, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
if(res != QAPI_OK) {
// Is this correct ?
// Seems to be...
txm_module_object_deallocate(timer_handle);
txm_module_object_deallocate(*timer_handle);
}
return res;
}
52 changes: 51 additions & 1 deletion api/src/stdlib/abort.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,54 @@
#include "util/trace.h"
#include "threadx_api/txm_module.h"
#include <cstdint>

#define TRACE_TAG "abort"

void backtrace() {
if(_txm_module_kernel_call_dispatcher == NULL) return;

uint32_t* fp = 0;
asm volatile("mov %0, fp":"=r"(fp));
uint32_t lr = 0;
asm volatile("mov %0, lr":"=r"(lr));

TX_THREAD *cthread = tx_thread_identify();
char* name = NULL;
if(cthread == NULL || tx_thread_info_get(cthread, &name, NULL, NULL, NULL, NULL, NULL, NULL, NULL) != TX_SUCCESS)
TRACE("failed to get thread name\r\n");
else TRACE("backtrace of thread %x %s\r\n", cthread, name);

TRACE("fp=%x lr=%x\r\n", fp, lr);
if(fp == NULL || fp[0] != lr) {
TRACE("fp does not match link register, compile with frame pointer enabled!\r\n");
return;
}

int i=0;
uint32_t pc = 0;
asm volatile("mov %0, pc":"=r"(pc));
TRACE("[%d] %x\r\n", i++, pc);
while(fp != NULL && fp[0] > 0x42000000 && fp[0] < 0x42300000) {
TRACE("[%d] %x\r\n", i++, fp[0]);
fp = (uint32_t*)fp[1];
}
TRACE("done\r\n");
}

void abort(void) {
if(_txm_module_kernel_call_dispatcher) TRACE("abort called\r\n");
backtrace();
while(_txm_module_kernel_call_dispatcher) {
tx_thread_sleep(100);
}
for(;;) {}
}

void abort_with_message(const char* msg) {
if(msg && _txm_module_kernel_call_dispatcher) TRACE("abort called: %s\r\n", msg);
backtrace();
while(_txm_module_kernel_call_dispatcher) {
tx_thread_sleep(100);
}
for(;;) {}
}
}
31 changes: 11 additions & 20 deletions api/src/stdlib/cpp.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#include "util/debug.h"
#include "util/trace.h"

#define TRACE_TAG "cxx_api"
Expand Down Expand Up @@ -28,8 +27,18 @@ void __cxa_finalize(void *f)
(void)f;
for (unsigned int i = 0; i < __atexit_funcs_count; i++)
{
if (__atexit_funcs[i].destructor_func)

if (__atexit_funcs[i].destructor_func) {
#if __cpp_exceptions >= 199711
try {
#endif
__atexit_funcs[i].destructor_func(__atexit_funcs[i].obj_ptr);
#if __cpp_exceptions >= 199711
} catch(...) {
TRACE("exception handling atexit function %x\r\n", __atexit_funcs[i].destructor_func)
}
#endif
}
}
__atexit_funcs_count = 0;
}
Expand All @@ -50,21 +59,3 @@ int __aeabi_atexit(void *arg, void (*func)(void *), void *d)
{
return __cxa_atexit(func, arg, d);
}

int __cxa_guard_acquire(char* g)
{
if(__atomic_test_and_set(&g[1], __ATOMIC_ACQUIRE)) return 0;
return !g[0];
}

void __cxa_guard_release(char* g)
{
g[0] = 1;
__atomic_clear(&g[1], __ATOMIC_RELEASE);
}

void __cxa_guard_abort(char* g)
{
g[0] = 0;
__atomic_clear(&g[1], __ATOMIC_RELEASE);
}
Loading

0 comments on commit e374ee4

Please sign in to comment.