testing multilang

lean
Avril 4 years ago
parent 6fcf9f58f1
commit 30016c2f7c
Signed by: flanchan
GPG Key ID: 284488987C31F630

@ -0,0 +1,12 @@
test:
gcc -c src/*.c -Iinclude -Wall -pedantic --std=gnu11
g++ -c src/*.cpp -Iinclude -Wall -pedantic --std=gnu11
gcc -o $@ *.o
clean:
rm -f test
rm -f *.o

@ -0,0 +1,26 @@
#ifndef _MAP_H
#define _MAP_H
#ifdef __cplusplus
extern "C" {
#define restrict __restrict__
#endif
#include <stddef.h>
typedef struct mmap {
int fd;
void* ptr;
size_t len;
} mmap_t;
int open_and_map(const char* file, mmap_t* restrict ptr);
int unmap_and_close(mmap_t map);
#ifdef _cplusplus
}
#undef restrict
#endif
#endif /* _MAP_H */

@ -0,0 +1,21 @@
#ifndef _PANIC_H
#define _PANIC_H
#ifdef __cplusplus
extern "C" {
#endif
struct panicinfo {
const char* file;
const char* function;
int line;
};
void _do_panic(struct panicinfo pi, const char* fmt, ...) __attribute__((noreturn));
#define panic(fmt, ...) _do_panic( (struct panicinfo){.file = __FILE__, .function = __func__, .line = __LINE__}, fmt __VA_OPT__(,) __VA_ARGS__)
#ifdef __cplusplus
}
#endif
#endif

@ -0,0 +1,52 @@
#ifndef _REINTERPRET_H
#define _REINTERPRET_H
#include <panic.h>
#ifdef __cplusplus
template<typename T>
struct span {
inline span(T* ptr, std::size_t len) : ptr(ptr), len(len) {}
template<typename U>
inline span<U> reinterpret() const
{
auto bytes = size_bytes();
//if (len_b % sizeof(U) != 0) panic("Cannot reinterpret T to U due to unmatch sizing constraints.");
return span<U>(ptr, bytes / sizeof(U));
}
inline const T& operator[](std::size_t idx) const
{
if (idx >= len) panic("Out of bounds access");
return ptr[i];
}
inline T& operator[](std::size_t idx)
{
if(idx >= len) panic("Out of bounds access");
return ptr[i];
}
inline const T* as_ptr() const { return ptr; }
inline T* as_ptr() { return ptr; }
inline const T* operator&() const { return as_ptr(); }
inline T* operator&() { return as_ptr(); }
inline std::size_t size_bytes() const { return len * sizeof(T); }
inline std::size_t size() const { return len; }
private:
T* const ptr;
const std::size_t len;
};
extern "C" {
#endif
uint64_t* bytes_to_long(uint8_t* ptr, size_t ptr_sz, size_t* restrict_ nsize);
float* bytes_to_float(uint8_t* ptr, size_t ptr_sz, size_t* restrict nsize);
#ifdef __cplusplus
}
#endif
#endif /* _REINTERPRET_H */

@ -14,14 +14,7 @@ extern "C" {
#define _FORCE_INLINE __attribute__((gnu_inline)) extern inline #define _FORCE_INLINE __attribute__((gnu_inline)) extern inline
#endif #endif
struct panicinfo {
const char* file;
const char* function;
int line;
};
void _do_panic(struct panicinfo pi, const char* fmt, ...) __attribute__((noreturn, cold));
#define panic(fmt, ...) _do_panic( (struct panicinfo){.file = __FILE__, .function = __func__, .line = __LINE__}, fmt __VA_OPT__(,) __VA_ARGS__)
#ifdef __cplusplus #ifdef __cplusplus
} }

@ -1,5 +1,6 @@
#include <shuffle3.h> #include <shuffle3.h>
#include <panic.h>
static void shuffle_file(const char* filename) static void shuffle_file(const char* filename)
{ {
@ -13,5 +14,6 @@ static void unshuffle_file(const char* filename)
int main(int argc, char** argv) int main(int argc, char** argv)
{ {
return 0; return 0;
} }

@ -0,0 +1,56 @@
#define _GNU_SOURCE
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include<sys/mman.h>
#include<fcntl.h>
#define FILEMODE S_IRWXU | S_IRGRP | S_IROTH
#include <map.h>
int open_and_map(const char* file, mmap_t* restrict ptr)
{
int fd;
struct stat st;
if ((fd = open(file, O_RDONLY, FILEMODE)) < 0) {
perror("Failed to open file");
return 0;
}
if (fstat(fd, &st) < 0) {
perror("Failed to stat file");
close(fd);
return 0;
}
register struct mmap map = { .fd = fd, .ptr = NULL, .len = st.st_size };
if ((map.ptr = mmap(NULL, map.len, PROT_READ, MAP_SHARED,fd, 0)) == MAP_FAILED) {
perror("mmap() failed");
close(fd);
return 0;
}
*ptr = map;
return 1;
}
int unmap_and_close(mmap_t map)
{
register int rval=1;
if (munmap(map.ptr, map.len) < 0) {
perror("munmap() failed");
rval=0;
}
if (close(map.fd) <0) {
perror("Failed to close fd");
rval=0;
}
return rval;
}

@ -2,9 +2,9 @@
#include <stdio.h> #include <stdio.h>
#include <stdarg.h> #include <stdarg.h>
#include <shuffle3.h> #include <panic.h>
__attribute__((noreturn, cold)) void _do_panic(struct panicinfo info, const char* fmt, ...) __attribute__((noreturn)) void _do_panic(struct panicinfo info, const char* fmt, ...)
{ {
va_list li; va_list li;
va_start(li, fmt); va_start(li, fmt);

@ -0,0 +1,22 @@
#include <shuffle3.h>
#include <reinterpret.hpp>
template<typename T>
static inline T* bytes_to_t(std::uint8_t* ptr, std::size_t ptr_sz, std::size_t* restrict nsize)
{
span<uint8_t> bytes(ptr, ptr_sz);
auto tout = bytes.reinterpret<T>();
*nsize = tout.size();
return longs.as_ptr();
}
extern "C" {
uint64_t* bytes_to_long(uint8_t* ptr, size_t ptr_sz, size_t* restrict nsize)
{
return bytes_to_t<uint64_t>(ptr, ptr_sz, nsize);
}
float* bytes_to_float(uint8_t* ptr, size_t ptr_sz, size_t* restrict nsize)
{
return bytes_to_t<float>(ptr, ptr_sz, nsize);
}
}
Loading…
Cancel
Save