Skip to content

Commit

Permalink
Start a contrib/ with libgc and libregex (#127)
Browse files Browse the repository at this point in the history
  • Loading branch information
skeeto committed May 9, 2024
1 parent 089a75b commit 15ae179
Show file tree
Hide file tree
Showing 3 changed files with 222 additions and 0 deletions.
10 changes: 10 additions & 0 deletions contrib/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Unsupported w64devkit enhancements

The files in this directory enhance w64devkit with specially-built
third-party libraries, such as garbage collection and POSIX `regex.h`.
Falling outside the scope of w64devkit, these libraries are not included
in the distribution, but may be situationally useful.

The header of each file has the instructions for its use. The file itself
is mostly a custom build script, and you will need to obtain the library
sources yourself the usual way.
72 changes: 72 additions & 0 deletions contrib/libgc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#if 0
# Minimalist, stripped down, w64devkit, static, unity build of Boehm GC.
# Provides an easy-to-use garbage collector weighing ~24KiB. The API is
# two functions: GC_malloc and GC_realloc. Tested with gc 8.2.4.
#
# Place this source file in the Boehm GC source tree root, then invoke
# it with the shell to produce gc.h and libgc.a.
#
# $ sh libgc.c
#
# Philosophy: If the goal is ease-of-use, remove all knobs that are not
# strictly necessary. If performance isn't good enough, don't use garbage
# collection; switch to a region-based allocator.
#
# Ref: https://www.hboehm.info/gc/gc_source/gc-8.2.4.tar.gz
# This is free and unencumbered software released into the public domain.
set -ex

PREFIX="${PREFIX:-$(gcc -print-sysroot)}"

${CC:-cc} -c -Iinclude -fwhole-program ${CFLAGS:--Os} libgc.c
${STRIP-strip} -x libgc.o

mkdir -p "$PREFIX/lib"
rm -f "$PREFIX/lib/libgc.a"
${AR:-ar} -r "$PREFIX/lib/libgc.a" libgc.o

mkdir -p "$PREFIX/include"
tee "$PREFIX/include/gc.h" <<EOF
#ifndef GC_H
#define GC_H
// Single-threaded, garbage-collected, zero-initialized memory allocation.
#include <stddef.h>
void *GC_malloc(size_t) __attribute((alloc_size(1), malloc));
void *GC_realloc(void *, size_t) __attribute((alloc_size(2)));
#endif
EOF
exit 0
#endif

#define ALL_INTERIOR_POINTERS
#define DONT_USE_ATEXIT
#define GC_API
#define GC_NO_FINALIZATION
#define GC_TOGGLE_REFS_NOT_NEEDED
#define GC_USE_ENTIRE_HEAP
#define NO_CLOCK
#define NO_DEBUGGING
#define NO_GETENV
#define NO_MSGBOX_ON_ERROR

#include "allchblk.c"
#include "alloc.c"
#include "blacklst.c"
#include "dbg_mlc.c"
#include "dyn_load.c"
#include "finalize.c"
#include "headers.c"
#include "mach_dep.c"
#include "malloc.c"
#include "mallocx.c"
#include "mark.c"
#include "mark_rts.c"
#include "misc.c"
#include "new_hblk.c"
#include "obj_map.c"
#include "os_dep.c"
#include "ptr_chck.c"
#include "reclaim.c"

__attribute((used)) void *GC_malloc(size_t);
__attribute((used)) void *GC_realloc(void *, size_t);
140 changes: 140 additions & 0 deletions contrib/libregex.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
#if 0
# Stripped down, w64devkit, static, unity build of the PCRE2 POSIX regex
# library. This script builds and installs regex.h and libregex.a inside
# w64devkit. Link with -lregex. It is pretty bulky, nearly 300KiB.
#
# Copy and run this script in the root fo the PCRE source tree. Tested
# with PCRE 10.43.
#
# $ sh libregex.c
#
# Ref: https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/regex.h.html
# This is free and unencumbered software released into the public domain.
set -ex

PREFIX="${PREFIX:-$(gcc -print-sysroot)}"

cp src/pcre2.h.generic src/pcre2.h
${CC:-cc} -c -fwhole-program ${CFLAGS:--Os} "$0"
${STRIP:-strip} -x libregex.o

mkdir -p "$PREFIX/lib"
rm -f "$PREFIX/lib/libregex.a"
${AR:-ar} -r "$PREFIX/lib/libregex.a" libregex.o

mkdir -p "$PREFIX/include"
tee >nul $PREFIX/include/regex.h <<EOF
#pragma once
#include <stddef.h>

enum {
REG_EXTENDED = 0x0000,
REG_ICASE = 0x0001,
REG_NEWLINE = 0x0002,
REG_NOTBOL = 0x0004,
REG_NOTEOL = 0x0008,
REG_DOTALL = 0x0010,
REG_NOSUB = 0x0020,
REG_UTF = 0x0040,
REG_STARTEND = 0x0080,
REG_NOTEMPTY = 0x0100,
REG_UNGREEDY = 0x0200,
REG_UCP = 0x0400,
REG_PEND = 0x0800,
REG_NOSPEC = 0x1000,
};

enum {
REG_ASSERT = 1,
REG_BADBR,
REG_BADPAT,
REG_BADRPT,
REG_EBRACE,
REG_EBRACK,
REG_ECOLLATE,
REG_ECTYPE,
REG_EESCAPE,
REG_EMPTY,
REG_EPAREN,
REG_ERANGE,
REG_ESIZE,
REG_ESPACE,
REG_ESUBREG,
REG_INVARG,
REG_NOMATCH,
};

typedef struct {
void *re_pcre2_code;
void *re_match_data;
char *re_endp;
size_t re_nsub;
size_t re_erroffset;
int re_cflags;
} regex_t;

typedef int regoff_t;

typedef struct {
regoff_t rm_so;
regoff_t rm_eo;
} regmatch_t;

int regcomp(regex_t *, const char *, int);
size_t regerror(int, const regex_t *, char *, size_t);
int regexec(const regex_t *, const char *, size_t, regmatch_t *, int);
void regfree(regex_t *);
EOF
exit 0
#endif

#define PCRE2_EXP_DEFN static
#define PCRE2_EXP_DECL static
#define PCRE2POSIX_EXP_DECL
#define PCRE2POSIX_EXP_DEFN __attribute((used))

#define HAVE_BUILTIN_MUL_OVERFLOW
#define HAVE_MEMMOVE
#define HEAP_LIMIT 20000000
#define LINK_SIZE 2
#define MATCH_LIMIT 10000000
#define MATCH_LIMIT_DEPTH MATCH_LIMIT
#define MAX_NAME_COUNT 10000
#define MAX_NAME_SIZE 32
#define MAX_VARLOOKBEHIND 255
#define NEWLINE_DEFAULT 2
#define PARENS_NEST_LIMIT 250
#define PCRE2_CODE_UNIT_WIDTH 8
#define PCRE2_STATIC
#define SUPPORT_UNICODE

#include "src/pcre2posix.h"
#undef regcomp
#undef regexec
#undef regerror
#undef regfree
#define pcre2_regcomp regcomp
#define pcre2_regexec regexec
#define pcre2_regerror regerror
#define pcre2_regfree regfree
#include "src/pcre2posix.c"

#include "src/pcre2_auto_possess.c"
#include "src/pcre2_chartables.c.dist"
#include "src/pcre2_chkdint.c"
#include "src/pcre2_compile.c"
#include "src/pcre2_context.c"
#include "src/pcre2_extuni.c"
#include "src/pcre2_find_bracket.c"
#include "src/pcre2_match.c"
#include "src/pcre2_match_data.c"
#include "src/pcre2_newline.c"
#include "src/pcre2_ord2utf.c"
#include "src/pcre2_pattern_info.c"
#include "src/pcre2_script_run.c"
#include "src/pcre2_string_utils.c"
#include "src/pcre2_study.c"
#include "src/pcre2_tables.c"
#include "src/pcre2_ucd.c"
#include "src/pcre2_valid_utf.c"
#include "src/pcre2_xclass.c"

0 comments on commit 15ae179

Please sign in to comment.