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 "settings.h"
#include <assert.h>
#include <stdlib.h>
#if !defined(LIBCO_ASSERT)
#include <assert.h>
#define LIBCO_ASSERT(...) assert(__VA_ARGS__)
#endif
#if !defined(LIBCO_MALLOC) || !defined(LIBCO_FREE)
#include <stdlib.h>
#define LIBCO_MALLOC(...) malloc(__VA_ARGS__)
#define LIBCO_FREE(...) free(__VA_ARGS__)
#endif
#include <stdint.h>
#ifdef LIBCO_MPROTECT
#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)) {
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) {

18
amd64.c
View File

@ -2,8 +2,16 @@
#include "libco.h"
#include "settings.h"
#include <assert.h>
#include <stdlib.h>
#if !defined(LIBCO_ASSERT)
#include <assert.h>
#define LIBCO_ASSERT(...) assert(__VA_ARGS__)
#endif
#if !defined(LIBCO_MALLOC) || !defined(LIBCO_FREE)
#include <stdlib.h>
#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) {

17
arm.c
View File

@ -2,8 +2,17 @@
#include "libco.h"
#include "settings.h"
#include <assert.h>
#include <stdlib.h>
#if !defined(LIBCO_ASSERT)
#include <assert.h>
#define LIBCO_ASSERT(...) assert(__VA_ARGS__)
#endif
#if !defined(LIBCO_MALLOC) || !defined(LIBCO_FREE)
#include <stdlib.h>
#define LIBCO_MALLOC(...) malloc(__VA_ARGS__)
#define LIBCO_FREE(...) free(__VA_ARGS__)
#endif
#ifdef LIBCO_MPROTECT
#include <unistd.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)) {
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) {

11
ppc.c
View File

@ -4,7 +4,12 @@
#include "libco.h"
#include "settings.h"
#include <stdlib.h>
#if !defined(LIBCO_MALLOC) || !defined(LIBCO_FREE)
#include <stdlib.h>
#define LIBCO_MALLOC(...) malloc(__VA_ARGS__)
#define LIBCO_FREE(...) free(__VA_ARGS__)
#endif
#include <stdint.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) {
(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) {

View File

@ -5,7 +5,12 @@
#include "settings.h"
#include <stdint.h>
#include <stdlib.h>
#if !defined(LIBCO_MALLOC) || !defined(LIBCO_FREE)
#include <stdlib.h>
#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) {

18
x86.c
View File

@ -2,8 +2,16 @@
#include "libco.h"
#include "settings.h"
#include <assert.h>
#include <stdlib.h>
#if !defined(LIBCO_ASSERT)
#include <assert.h>
#define LIBCO_ASSERT(...) assert(__VA_ARGS__)
#endif
#if !defined(LIBCO_MALLOC) || !defined(LIBCO_FREE)
#include <stdlib.h>
#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) {