From bd9d4c96be27b746bc40dcd205c2585cef0e141b Mon Sep 17 00:00:00 2001 From: Naman Dixit Date: Sat, 6 Jun 2020 17:04:16 +0530 Subject: [PATCH 1/2] Made the dependency on stdlib.h and assert.h optional Some platforms still depend on Libc (if they are exclusively POSIX/Unix/Linux), or need some other functionality (e.g., memcpy) --- aarch64.c | 17 +++++++++++++---- amd64.c | 18 +++++++++++++----- arm.c | 17 +++++++++++++---- ppc.c | 11 ++++++++--- ppc64v2.c | 13 +++++++++---- x86.c | 18 +++++++++++++----- 6 files changed, 69 insertions(+), 25 deletions(-) diff --git a/aarch64.c b/aarch64.c index 8505b675..a77bb075 100644 --- a/aarch64.c +++ b/aarch64.c @@ -2,8 +2,17 @@ #include "libco.h" #include "settings.h" -#include -#include +#if !defined(LIBCO_ASSERT) + #include + #define LIBCO_ASSERT(...) assert(__VA_ARGS__) +#endif + +#if !defined(LIBCO_MALLOC) || !defined(LIBCO_FREE) + #include + #define LIBCO_MALLOC(...) malloc(__VA_ARGS__) + #define LIBCO_FREE(...) free(__VA_ARGS__) +#endif + #include #ifdef LIBCO_MPROTECT #include @@ -85,13 +94,13 @@ cothread_t co_derive(void* memory, unsigned int size, void (*entrypoint)(void)) } cothread_t co_create(unsigned int size, void (*entrypoint)(void)) { - void* memory = malloc(size); + void* memory = LIBCO_MALLOC(size); if(!memory) return (cothread_t)0; return co_derive(memory, size, entrypoint); } void co_delete(cothread_t handle) { - free(handle); + LIBCO_FREE(handle); } void co_switch(cothread_t handle) { diff --git a/amd64.c b/amd64.c index e96e5247..833b5e2e 100644 --- a/amd64.c +++ b/amd64.c @@ -2,8 +2,16 @@ #include "libco.h" #include "settings.h" -#include -#include +#if !defined(LIBCO_ASSERT) + #include + #define LIBCO_ASSERT(...) assert(__VA_ARGS__) +#endif + +#if !defined(LIBCO_MALLOC) || !defined(LIBCO_FREE) + #include + #define LIBCO_MALLOC(...) malloc(__VA_ARGS__) + #define LIBCO_FREE(...) free(__VA_ARGS__) +#endif #ifdef __cplusplus extern "C" { @@ -114,7 +122,7 @@ static void (*co_swap)(cothread_t, cothread_t) = 0; #endif static void crash() { - assert(0); /* called only if cothread_t entrypoint returns */ + LIBCO_ASSERT(0); /* called only if cothread_t entrypoint returns */ } cothread_t co_active() { @@ -142,13 +150,13 @@ cothread_t co_derive(void* memory, unsigned int size, void (*entrypoint)(void)) } cothread_t co_create(unsigned int size, void (*entrypoint)(void)) { - void* memory = malloc(size); + void* memory = LIBCO_MALLOC(size); if(!memory) return (cothread_t)0; return co_derive(memory, size, entrypoint); } void co_delete(cothread_t handle) { - free(handle); + LIBCO_FREE(handle); } void co_switch(cothread_t handle) { diff --git a/arm.c b/arm.c index 1c9dff5c..4a4aef51 100644 --- a/arm.c +++ b/arm.c @@ -2,8 +2,17 @@ #include "libco.h" #include "settings.h" -#include -#include +#if !defined(LIBCO_ASSERT) + #include + #define LIBCO_ASSERT(...) assert(__VA_ARGS__) +#endif + +#if !defined(LIBCO_MALLOC) || !defined(LIBCO_FREE) + #include + #define LIBCO_MALLOC(...) malloc(__VA_ARGS__) + #define LIBCO_FREE(...) free(__VA_ARGS__) +#endif + #ifdef LIBCO_MPROTECT #include #include @@ -61,13 +70,13 @@ cothread_t co_derive(void* memory, unsigned int size, void (*entrypoint)(void)) } cothread_t co_create(unsigned int size, void (*entrypoint)(void)) { - void* memory = malloc(size); + void* memory = LIBCO_MALLOC(size); if(!memory) return (cothread_t)0; return co_derive(memory, size, entrypoint); } void co_delete(cothread_t handle) { - free(handle); + LIBCO_FREE(handle); } void co_switch(cothread_t handle) { diff --git a/ppc.c b/ppc.c index ee6a9a87..bcbdf966 100644 --- a/ppc.c +++ b/ppc.c @@ -4,7 +4,12 @@ #include "libco.h" #include "settings.h" -#include +#if !defined(LIBCO_MALLOC) || !defined(LIBCO_FREE) + #include + #define LIBCO_MALLOC(...) malloc(__VA_ARGS__) + #define LIBCO_FREE(...) free(__VA_ARGS__) +#endif + #include #include @@ -327,7 +332,7 @@ cothread_t co_derive(void* memory, unsigned int size, void (*entry_)(void)) { static uint32_t* co_create_(unsigned size, uintptr_t entry) { (void)entry; - uint32_t* t = (uint32_t*)malloc(size); + uint32_t* t = (uint32_t*)LIBCO_MALLOC(size); #if LIBCO_PPCDESC if(t) { @@ -390,7 +395,7 @@ cothread_t co_create(unsigned int size, void (*entry_)(void)) { } void co_delete(cothread_t t) { - free(t); + LIBCO_FREE(t); } static void co_init_(void) { diff --git a/ppc64v2.c b/ppc64v2.c index 3c296959..1cac8843 100644 --- a/ppc64v2.c +++ b/ppc64v2.c @@ -5,7 +5,12 @@ #include "settings.h" #include -#include + +#if !defined(LIBCO_MALLOC) || !defined(LIBCO_FREE) + #include + #define LIBCO_MALLOC(...) malloc(__VA_ARGS__) + #define LIBCO_FREE(...) free(__VA_ARGS__) +#endif #ifdef __cplusplus extern "C" { @@ -223,7 +228,7 @@ __asm__( cothread_t co_active() { if(!co_active_handle) { - co_active_handle = (struct ppc64_context*)malloc(MIN_STACK + sizeof(struct ppc64_context)); + co_active_handle = (struct ppc64_context*)LIBCO_MALLOC(MIN_STACK + sizeof(struct ppc64_context)); } return (cothread_t)co_active_handle; } @@ -255,13 +260,13 @@ cothread_t co_derive(void* memory, unsigned int size, void (*coentry)(void)) { } cothread_t co_create(unsigned int size, void (*coentry)(void)) { - void* memory = malloc(size); + void* memory = LIBCO_MALLOC(size); if(!memory) return (cothread_t)0; return co_derive(memory, size, coentry); } void co_delete(cothread_t handle) { - free(handle); + LIBCO_FREE(handle); } void co_switch(cothread_t to) { diff --git a/x86.c b/x86.c index c539a299..d2c760ad 100644 --- a/x86.c +++ b/x86.c @@ -2,8 +2,16 @@ #include "libco.h" #include "settings.h" -#include -#include +#if !defined(LIBCO_ASSERT) + #include + #define LIBCO_ASSERT(...) assert(__VA_ARGS__) +#endif + +#if !defined(LIBCO_MALLOC) || !defined(LIBCO_FREE) + #include + #define LIBCO_MALLOC(...) malloc(__VA_ARGS__) + #define LIBCO_FREE(...) free(__VA_ARGS__) +#endif #ifdef __cplusplus extern "C" { @@ -68,7 +76,7 @@ static const unsigned char co_swap_function[4096] = { #endif static void crash() { - assert(0); /* called only if cothread_t entrypoint returns */ + LIBCO_ASSERT(0); /* called only if cothread_t entrypoint returns */ } cothread_t co_active() { @@ -96,13 +104,13 @@ cothread_t co_derive(void* memory, unsigned int size, void (*entrypoint)(void)) } cothread_t co_create(unsigned int size, void (*entrypoint)(void)) { - void* memory = malloc(size); + void* memory = LIBCO_MALLOC(size); if(!memory) return (cothread_t)0; return co_derive(memory, size, entrypoint); } void co_delete(cothread_t handle) { - free(handle); + LIBCO_FREE(handle); } void co_switch(cothread_t handle) { From fb700978ac6a415f23e398071057416b13d9b219 Mon Sep 17 00:00:00 2001 From: Naman Dixit Date: Sat, 6 Jun 2020 18:57:44 +0530 Subject: [PATCH 2/2] Moved the libc override macros to settings.h, and removed __VA_ARGS__ --- aarch64.c | 11 ----------- amd64.c | 11 ----------- arm.c | 11 ----------- ppc.c | 6 ------ ppc64v2.c | 6 ------ settings.h | 12 ++++++++++++ x86.c | 11 ----------- 7 files changed, 12 insertions(+), 56 deletions(-) diff --git a/aarch64.c b/aarch64.c index a77bb075..980686b8 100644 --- a/aarch64.c +++ b/aarch64.c @@ -2,17 +2,6 @@ #include "libco.h" #include "settings.h" -#if !defined(LIBCO_ASSERT) - #include - #define LIBCO_ASSERT(...) assert(__VA_ARGS__) -#endif - -#if !defined(LIBCO_MALLOC) || !defined(LIBCO_FREE) - #include - #define LIBCO_MALLOC(...) malloc(__VA_ARGS__) - #define LIBCO_FREE(...) free(__VA_ARGS__) -#endif - #include #ifdef LIBCO_MPROTECT #include diff --git a/amd64.c b/amd64.c index 833b5e2e..fa454809 100644 --- a/amd64.c +++ b/amd64.c @@ -2,17 +2,6 @@ #include "libco.h" #include "settings.h" -#if !defined(LIBCO_ASSERT) - #include - #define LIBCO_ASSERT(...) assert(__VA_ARGS__) -#endif - -#if !defined(LIBCO_MALLOC) || !defined(LIBCO_FREE) - #include - #define LIBCO_MALLOC(...) malloc(__VA_ARGS__) - #define LIBCO_FREE(...) free(__VA_ARGS__) -#endif - #ifdef __cplusplus extern "C" { #endif diff --git a/arm.c b/arm.c index 4a4aef51..6f0aa24d 100644 --- a/arm.c +++ b/arm.c @@ -2,17 +2,6 @@ #include "libco.h" #include "settings.h" -#if !defined(LIBCO_ASSERT) - #include - #define LIBCO_ASSERT(...) assert(__VA_ARGS__) -#endif - -#if !defined(LIBCO_MALLOC) || !defined(LIBCO_FREE) - #include - #define LIBCO_MALLOC(...) malloc(__VA_ARGS__) - #define LIBCO_FREE(...) free(__VA_ARGS__) -#endif - #ifdef LIBCO_MPROTECT #include #include diff --git a/ppc.c b/ppc.c index bcbdf966..a39d558e 100644 --- a/ppc.c +++ b/ppc.c @@ -4,12 +4,6 @@ #include "libco.h" #include "settings.h" -#if !defined(LIBCO_MALLOC) || !defined(LIBCO_FREE) - #include - #define LIBCO_MALLOC(...) malloc(__VA_ARGS__) - #define LIBCO_FREE(...) free(__VA_ARGS__) -#endif - #include #include diff --git a/ppc64v2.c b/ppc64v2.c index 1cac8843..d302a28a 100644 --- a/ppc64v2.c +++ b/ppc64v2.c @@ -6,12 +6,6 @@ #include -#if !defined(LIBCO_MALLOC) || !defined(LIBCO_FREE) - #include - #define LIBCO_MALLOC(...) malloc(__VA_ARGS__) - #define LIBCO_FREE(...) free(__VA_ARGS__) -#endif - #ifdef __cplusplus extern "C" { #endif diff --git a/settings.h b/settings.h index d8037bc4..695ebfb7 100644 --- a/settings.h +++ b/settings.h @@ -26,6 +26,17 @@ #define alignas(bytes) #endif +#if !defined(LIBCO_ASSERT) + #include + #define LIBCO_ASSERT assert +#endif + +#if !defined(LIBCO_MALLOC) || !defined(LIBCO_FREE) + #include + #define LIBCO_MALLOC malloc + #define LIBCO_FREE free +#endif + #if defined(_MSC_VER) #define section(name) __declspec(allocate("." #name)) #elif defined(__APPLE__) @@ -34,5 +45,6 @@ #define section(name) __attribute__((section("." #name "#"))) #endif + /* if defined(LIBCO_C) */ #endif diff --git a/x86.c b/x86.c index d2c760ad..d31b4ac3 100644 --- a/x86.c +++ b/x86.c @@ -2,17 +2,6 @@ #include "libco.h" #include "settings.h" -#if !defined(LIBCO_ASSERT) - #include - #define LIBCO_ASSERT(...) assert(__VA_ARGS__) -#endif - -#if !defined(LIBCO_MALLOC) || !defined(LIBCO_FREE) - #include - #define LIBCO_MALLOC(...) malloc(__VA_ARGS__) - #define LIBCO_FREE(...) free(__VA_ARGS__) -#endif - #ifdef __cplusplus extern "C" { #endif