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)
This commit is contained in:
Naman Dixit
2020-06-06 17:04:16 +05:30
parent 94acbce822
commit bd9d4c96be
6 changed files with 69 additions and 25 deletions

View File

@@ -2,8 +2,17 @@
#include "libco.h" #include "libco.h"
#include "settings.h" #include "settings.h"
#if !defined(LIBCO_ASSERT)
#include <assert.h> #include <assert.h>
#define LIBCO_ASSERT(...) assert(__VA_ARGS__)
#endif
#if !defined(LIBCO_MALLOC) || !defined(LIBCO_FREE)
#include <stdlib.h> #include <stdlib.h>
#define LIBCO_MALLOC(...) malloc(__VA_ARGS__)
#define LIBCO_FREE(...) free(__VA_ARGS__)
#endif
#include <stdint.h> #include <stdint.h>
#ifdef LIBCO_MPROTECT #ifdef LIBCO_MPROTECT
#include <unistd.h> #include <unistd.h>
@@ -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)) { 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; if(!memory) return (cothread_t)0;
return co_derive(memory, size, entrypoint); return co_derive(memory, size, entrypoint);
} }
void co_delete(cothread_t handle) { void co_delete(cothread_t handle) {
free(handle); LIBCO_FREE(handle);
} }
void co_switch(cothread_t handle) { void co_switch(cothread_t handle) {

14
amd64.c
View File

@@ -2,8 +2,16 @@
#include "libco.h" #include "libco.h"
#include "settings.h" #include "settings.h"
#if !defined(LIBCO_ASSERT)
#include <assert.h> #include <assert.h>
#define LIBCO_ASSERT(...) assert(__VA_ARGS__)
#endif
#if !defined(LIBCO_MALLOC) || !defined(LIBCO_FREE)
#include <stdlib.h> #include <stdlib.h>
#define LIBCO_MALLOC(...) malloc(__VA_ARGS__)
#define LIBCO_FREE(...) free(__VA_ARGS__)
#endif
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@@ -114,7 +122,7 @@ static void (*co_swap)(cothread_t, cothread_t) = 0;
#endif #endif
static void crash() { 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() { 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)) { 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; if(!memory) return (cothread_t)0;
return co_derive(memory, size, entrypoint); return co_derive(memory, size, entrypoint);
} }
void co_delete(cothread_t handle) { void co_delete(cothread_t handle) {
free(handle); LIBCO_FREE(handle);
} }
void co_switch(cothread_t handle) { void co_switch(cothread_t handle) {

13
arm.c
View File

@@ -2,8 +2,17 @@
#include "libco.h" #include "libco.h"
#include "settings.h" #include "settings.h"
#if !defined(LIBCO_ASSERT)
#include <assert.h> #include <assert.h>
#define LIBCO_ASSERT(...) assert(__VA_ARGS__)
#endif
#if !defined(LIBCO_MALLOC) || !defined(LIBCO_FREE)
#include <stdlib.h> #include <stdlib.h>
#define LIBCO_MALLOC(...) malloc(__VA_ARGS__)
#define LIBCO_FREE(...) free(__VA_ARGS__)
#endif
#ifdef LIBCO_MPROTECT #ifdef LIBCO_MPROTECT
#include <unistd.h> #include <unistd.h>
#include <sys/mman.h> #include <sys/mman.h>
@@ -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)) { 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; if(!memory) return (cothread_t)0;
return co_derive(memory, size, entrypoint); return co_derive(memory, size, entrypoint);
} }
void co_delete(cothread_t handle) { void co_delete(cothread_t handle) {
free(handle); LIBCO_FREE(handle);
} }
void co_switch(cothread_t handle) { void co_switch(cothread_t handle) {

9
ppc.c
View File

@@ -4,7 +4,12 @@
#include "libco.h" #include "libco.h"
#include "settings.h" #include "settings.h"
#if !defined(LIBCO_MALLOC) || !defined(LIBCO_FREE)
#include <stdlib.h> #include <stdlib.h>
#define LIBCO_MALLOC(...) malloc(__VA_ARGS__)
#define LIBCO_FREE(...) free(__VA_ARGS__)
#endif
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
@@ -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) { static uint32_t* co_create_(unsigned size, uintptr_t entry) {
(void)entry; (void)entry;
uint32_t* t = (uint32_t*)malloc(size); uint32_t* t = (uint32_t*)LIBCO_MALLOC(size);
#if LIBCO_PPCDESC #if LIBCO_PPCDESC
if(t) { if(t) {
@@ -390,7 +395,7 @@ cothread_t co_create(unsigned int size, void (*entry_)(void)) {
} }
void co_delete(cothread_t t) { void co_delete(cothread_t t) {
free(t); LIBCO_FREE(t);
} }
static void co_init_(void) { static void co_init_(void) {

View File

@@ -5,7 +5,12 @@
#include "settings.h" #include "settings.h"
#include <stdint.h> #include <stdint.h>
#if !defined(LIBCO_MALLOC) || !defined(LIBCO_FREE)
#include <stdlib.h> #include <stdlib.h>
#define LIBCO_MALLOC(...) malloc(__VA_ARGS__)
#define LIBCO_FREE(...) free(__VA_ARGS__)
#endif
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@@ -223,7 +228,7 @@ __asm__(
cothread_t co_active() { cothread_t co_active() {
if(!co_active_handle) { 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; 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)) { 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; if(!memory) return (cothread_t)0;
return co_derive(memory, size, coentry); return co_derive(memory, size, coentry);
} }
void co_delete(cothread_t handle) { void co_delete(cothread_t handle) {
free(handle); LIBCO_FREE(handle);
} }
void co_switch(cothread_t to) { void co_switch(cothread_t to) {

14
x86.c
View File

@@ -2,8 +2,16 @@
#include "libco.h" #include "libco.h"
#include "settings.h" #include "settings.h"
#if !defined(LIBCO_ASSERT)
#include <assert.h> #include <assert.h>
#define LIBCO_ASSERT(...) assert(__VA_ARGS__)
#endif
#if !defined(LIBCO_MALLOC) || !defined(LIBCO_FREE)
#include <stdlib.h> #include <stdlib.h>
#define LIBCO_MALLOC(...) malloc(__VA_ARGS__)
#define LIBCO_FREE(...) free(__VA_ARGS__)
#endif
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@@ -68,7 +76,7 @@ static const unsigned char co_swap_function[4096] = {
#endif #endif
static void crash() { 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() { 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)) { 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; if(!memory) return (cothread_t)0;
return co_derive(memory, size, entrypoint); return co_derive(memory, size, entrypoint);
} }
void co_delete(cothread_t handle) { void co_delete(cothread_t handle) {
free(handle); LIBCO_FREE(handle);
} }
void co_switch(cothread_t handle) { void co_switch(cothread_t handle) {