diff --git a/.gitignore b/.gitignore index 382a957..9c00058 100644 --- a/.gitignore +++ b/.gitignore @@ -6,3 +6,10 @@ dist-newstyle **/#*# **/*~ cabal.project.local* + + +# Added by cargo + +/target +/Cargo.lock +lib/ diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..3b427b3 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,4 @@ +[package] +name = "glasalloc" +version = "0.1.0" +edition = "2021" diff --git a/README.md b/README.md index 980b0aa..bf619b5 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # libglaswegian -Use GHC's Memory Allocator from C/C++. Yes, I'm serious. You can do this: +Use GHC's Memory Allocator from C, C++, and Rust. Yes, I'm serious. You can do this: ```c++ #include diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..a865730 --- /dev/null +++ b/build.rs @@ -0,0 +1,4 @@ +fn main() { + println!("cargo:rustc-link-search=./lib"); + println!("cargo:rustc-link-lib=glaswegian"); +} diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..0193fcf --- /dev/null +++ b/flake.lock @@ -0,0 +1,492 @@ +{ + "nodes": { + "HTTP": { + "flake": false, + "locked": { + "lastModified": 1451647621, + "narHash": "sha256-oHIyw3x0iKBexEo49YeUDV1k74ZtyYKGR2gNJXXRxts=", + "owner": "phadej", + "repo": "HTTP", + "rev": "9bc0996d412fef1787449d841277ef663ad9a915", + "type": "github" + }, + "original": { + "owner": "phadej", + "repo": "HTTP", + "type": "github" + } + }, + "cabal-32": { + "flake": false, + "locked": { + "lastModified": 1603716527, + "narHash": "sha256-X0TFfdD4KZpwl0Zr6x+PLxUt/VyKQfX7ylXHdmZIL+w=", + "owner": "haskell", + "repo": "cabal", + "rev": "48bf10787e27364730dd37a42b603cee8d6af7ee", + "type": "github" + }, + "original": { + "owner": "haskell", + "ref": "3.2", + "repo": "cabal", + "type": "github" + } + }, + "cabal-34": { + "flake": false, + "locked": { + "lastModified": 1640353650, + "narHash": "sha256-N1t6M3/wqj90AEdRkeC8i923gQYUpzSr8b40qVOZ1Rk=", + "owner": "haskell", + "repo": "cabal", + "rev": "942639c18c0cd8ec53e0a6f8d120091af35312cd", + "type": "github" + }, + "original": { + "owner": "haskell", + "ref": "3.4", + "repo": "cabal", + "type": "github" + } + }, + "cabal-36": { + "flake": false, + "locked": { + "lastModified": 1641652457, + "narHash": "sha256-BlFPKP4C4HRUJeAbdembX1Rms1LD380q9s0qVDeoAak=", + "owner": "haskell", + "repo": "cabal", + "rev": "f27667f8ec360c475027dcaee0138c937477b070", + "type": "github" + }, + "original": { + "owner": "haskell", + "ref": "3.6", + "repo": "cabal", + "type": "github" + } + }, + "cardano-shell": { + "flake": false, + "locked": { + "lastModified": 1608537748, + "narHash": "sha256-PulY1GfiMgKVnBci3ex4ptk2UNYMXqGjJOxcPy2KYT4=", + "owner": "input-output-hk", + "repo": "cardano-shell", + "rev": "9392c75087cb9a3d453998f4230930dea3a95725", + "type": "github" + }, + "original": { + "owner": "input-output-hk", + "repo": "cardano-shell", + "type": "github" + } + }, + "fenix": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ], + "rust-analyzer-src": "rust-analyzer-src" + }, + "locked": { + "lastModified": 1662123511, + "narHash": "sha256-s7iHG3VZjfpXMVK3cAfBPyXElW1fYlFiwvTW4ykNWEc=", + "owner": "nix-community", + "repo": "fenix", + "rev": "772db8b65364676ba4e89dcbcc29749716a8b300", + "type": "github" + }, + "original": { + "owner": "nix-community", + "repo": "fenix", + "type": "github" + } + }, + "flake-compat": { + "flake": false, + "locked": { + "lastModified": 1650374568, + "narHash": "sha256-Z+s0J8/r907g149rllvwhb4pKi8Wam5ij0st8PwAh+E=", + "owner": "edolstra", + "repo": "flake-compat", + "rev": "b4a34015c698c7793d592d66adbab377907a2be8", + "type": "github" + }, + "original": { + "owner": "edolstra", + "repo": "flake-compat", + "type": "github" + } + }, + "flake-utils": { + "locked": { + "lastModified": 1659877975, + "narHash": "sha256-zllb8aq3YO3h8B/U0/J1WBgAL8EX5yWf5pMj3G0NAmc=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "c0e246b9b83f637f4681389ecabcb2681b4f3af0", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "flake-utils_2": { + "locked": { + "lastModified": 1644229661, + "narHash": "sha256-1YdnJAsNy69bpcjuoKdOYQX0YxZBiCYZo4Twxerqv7k=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "3cecb5b042f7f209c56ffd8371b2711a290ec797", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "ghc-8.6.5-iohk": { + "flake": false, + "locked": { + "lastModified": 1600920045, + "narHash": "sha256-DO6kxJz248djebZLpSzTGD6s8WRpNI9BTwUeOf5RwY8=", + "owner": "input-output-hk", + "repo": "ghc", + "rev": "95713a6ecce4551240da7c96b6176f980af75cae", + "type": "github" + }, + "original": { + "owner": "input-output-hk", + "ref": "release/8.6.5-iohk", + "repo": "ghc", + "type": "github" + } + }, + "hackage": { + "flake": false, + "locked": { + "lastModified": 1662167627, + "narHash": "sha256-Wwq4oVlvBXkZbhABSdakt0KyRh5VOULvNFXoYR9+88g=", + "owner": "input-output-hk", + "repo": "hackage.nix", + "rev": "be51362f6e0dda9007d140d82ad57d314a08491f", + "type": "github" + }, + "original": { + "owner": "input-output-hk", + "repo": "hackage.nix", + "type": "github" + } + }, + "haskellNix": { + "inputs": { + "HTTP": "HTTP", + "cabal-32": "cabal-32", + "cabal-34": "cabal-34", + "cabal-36": "cabal-36", + "cardano-shell": "cardano-shell", + "flake-utils": "flake-utils_2", + "ghc-8.6.5-iohk": "ghc-8.6.5-iohk", + "hackage": "hackage", + "hpc-coveralls": "hpc-coveralls", + "hydra": "hydra", + "nix-tools": "nix-tools", + "nixpkgs": [ + "haskellNix", + "nixpkgs-unstable" + ], + "nixpkgs-2003": "nixpkgs-2003", + "nixpkgs-2105": "nixpkgs-2105", + "nixpkgs-2111": "nixpkgs-2111", + "nixpkgs-2205": "nixpkgs-2205", + "nixpkgs-unstable": "nixpkgs-unstable", + "old-ghc-nix": "old-ghc-nix", + "stackage": "stackage" + }, + "locked": { + "lastModified": 1662168467, + "narHash": "sha256-GNRTZWMICMxy1Xf5BQjyTnpQLBvLz5x0h+BukgzFqoU=", + "owner": "input-output-hk", + "repo": "haskell.nix", + "rev": "b3df33abbcb736437cb06d5f53a10f6bb271bc51", + "type": "github" + }, + "original": { + "owner": "input-output-hk", + "repo": "haskell.nix", + "type": "github" + } + }, + "hpc-coveralls": { + "flake": false, + "locked": { + "lastModified": 1607498076, + "narHash": "sha256-8uqsEtivphgZWYeUo5RDUhp6bO9j2vaaProQxHBltQk=", + "owner": "sevanspowell", + "repo": "hpc-coveralls", + "rev": "14df0f7d229f4cd2e79f8eabb1a740097fdfa430", + "type": "github" + }, + "original": { + "owner": "sevanspowell", + "repo": "hpc-coveralls", + "type": "github" + } + }, + "hydra": { + "inputs": { + "nix": "nix", + "nixpkgs": [ + "haskellNix", + "hydra", + "nix", + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1646878427, + "narHash": "sha256-KtbrofMtN8GlM7D+n90kixr7QpSlVmdN+vK5CA/aRzc=", + "owner": "NixOS", + "repo": "hydra", + "rev": "28b682b85b7efc5cf7974065792a1f22203a5927", + "type": "github" + }, + "original": { + "id": "hydra", + "type": "indirect" + } + }, + "lowdown-src": { + "flake": false, + "locked": { + "lastModified": 1633514407, + "narHash": "sha256-Dw32tiMjdK9t3ETl5fzGrutQTzh2rufgZV4A/BbxuD4=", + "owner": "kristapsdz", + "repo": "lowdown", + "rev": "d2c2b44ff6c27b936ec27358a2653caaef8f73b8", + "type": "github" + }, + "original": { + "owner": "kristapsdz", + "repo": "lowdown", + "type": "github" + } + }, + "nix": { + "inputs": { + "lowdown-src": "lowdown-src", + "nixpkgs": "nixpkgs", + "nixpkgs-regression": "nixpkgs-regression" + }, + "locked": { + "lastModified": 1643066034, + "narHash": "sha256-xEPeMcNJVOeZtoN+d+aRwolpW8mFSEQx76HTRdlhPhg=", + "owner": "NixOS", + "repo": "nix", + "rev": "a1cd7e58606a41fcf62bf8637804cf8306f17f62", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "2.6.0", + "repo": "nix", + "type": "github" + } + }, + "nix-tools": { + "flake": false, + "locked": { + "lastModified": 1659569011, + "narHash": "sha256-wHS0H5+TERmDnPCfzH4A+rSR5TvjYMWus9BNeNAMyUM=", + "owner": "input-output-hk", + "repo": "nix-tools", + "rev": "555d57e1ea81b79945f2608aa261df20f6b602a5", + "type": "github" + }, + "original": { + "owner": "input-output-hk", + "repo": "nix-tools", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1632864508, + "narHash": "sha256-d127FIvGR41XbVRDPVvozUPQ/uRHbHwvfyKHwEt5xFM=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "82891b5e2c2359d7e58d08849e4c89511ab94234", + "type": "github" + }, + "original": { + "id": "nixpkgs", + "ref": "nixos-21.05-small", + "type": "indirect" + } + }, + "nixpkgs-2003": { + "locked": { + "lastModified": 1620055814, + "narHash": "sha256-8LEHoYSJiL901bTMVatq+rf8y7QtWuZhwwpKE2fyaRY=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "1db42b7fe3878f3f5f7a4f2dc210772fd080e205", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixpkgs-20.03-darwin", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs-2105": { + "locked": { + "lastModified": 1655034179, + "narHash": "sha256-rf1/7AbzuYDw6+8Xvvf3PtEOygymLBrFsFxvext5ZjI=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "046ee4af7a9f016a364f8f78eeaa356ba524ac31", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixpkgs-21.05-darwin", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs-2111": { + "locked": { + "lastModified": 1656782578, + "narHash": "sha256-1eMCBEqJplPotTo/SZ/t5HU6Sf2I8qKlZi9MX7jv9fw=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "573603b7fdb9feb0eb8efc16ee18a015c667ab1b", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixpkgs-21.11-darwin", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs-2205": { + "locked": { + "lastModified": 1657876628, + "narHash": "sha256-URmf0O2cQ/3heg2DJOeLyU/JmfVMqG4X5t9crQXMaeY=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "549d82bdd40f760a438c3c3497c1c61160f3de55", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixpkgs-22.05-darwin", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs-regression": { + "locked": { + "lastModified": 1643052045, + "narHash": "sha256-uGJ0VXIhWKGXxkeNnq4TvV3CIOkUJ3PAoLZ3HMzNVMw=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "215d4d0fd80ca5163643b03a33fde804a29cc1e2", + "type": "github" + }, + "original": { + "id": "nixpkgs", + "rev": "215d4d0fd80ca5163643b03a33fde804a29cc1e2", + "type": "indirect" + } + }, + "nixpkgs-unstable": { + "locked": { + "lastModified": 1657888067, + "narHash": "sha256-GnwJoFBTPfW3+mz7QEeJEEQ9OMHZOiIJ/qDhZxrlKh8=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "65fae659e31098ca4ac825a6fef26d890aaf3f4e", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "old-ghc-nix": { + "flake": false, + "locked": { + "lastModified": 1631092763, + "narHash": "sha256-sIKgO+z7tj4lw3u6oBZxqIhDrzSkvpHtv0Kki+lh9Fg=", + "owner": "angerman", + "repo": "old-ghc-nix", + "rev": "af48a7a7353e418119b6dfe3cd1463a657f342b8", + "type": "github" + }, + "original": { + "owner": "angerman", + "ref": "master", + "repo": "old-ghc-nix", + "type": "github" + } + }, + "root": { + "inputs": { + "fenix": "fenix", + "flake-compat": "flake-compat", + "flake-utils": "flake-utils", + "haskellNix": "haskellNix", + "nixpkgs": [ + "haskellNix", + "nixpkgs-unstable" + ] + } + }, + "rust-analyzer-src": { + "flake": false, + "locked": { + "lastModified": 1662066014, + "narHash": "sha256-DE4FsE2sxd9nFtG+8+lnv/IBbtf+6rAlKjIdfpWN488=", + "owner": "rust-lang", + "repo": "rust-analyzer", + "rev": "93c52e41ec0d297c7512adf5936d8c464c820618", + "type": "github" + }, + "original": { + "owner": "rust-lang", + "ref": "nightly", + "repo": "rust-analyzer", + "type": "github" + } + }, + "stackage": { + "flake": false, + "locked": { + "lastModified": 1662168405, + "narHash": "sha256-qjtrcff65O7gczkGtkdm8NByCn9GvI1EpVo1YvEJz7s=", + "owner": "input-output-hk", + "repo": "stackage.nix", + "rev": "20b2b927e744310916eea9d0dbc08255a5f431c4", + "type": "github" + }, + "original": { + "owner": "input-output-hk", + "repo": "stackage.nix", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..04d3514 --- /dev/null +++ b/flake.nix @@ -0,0 +1,52 @@ +{ + description = "libglaswegian"; + inputs = { + haskellNix.url = "github:input-output-hk/haskell.nix"; + nixpkgs.follows = "haskellNix/nixpkgs-unstable"; + flake-utils.url = "github:numtide/flake-utils"; + flake-compat = { + url = "github:edolstra/flake-compat"; + flake = false; + }; + fenix = { + url = "github:nix-community/fenix"; + inputs.nixpkgs.follows = "nixpkgs"; + }; + }; + + outputs = { self, nixpkgs, flake-utils, haskellNix, fenix, ... }: + flake-utils.lib.eachSystem [ "x86_64-linux" ] (system: + let + overlays = [ haskellNix.overlay + (final: prev: { + libglaswegian = + final.haskell-nix.project' { + src = ./.; + compiler-nix-name = "ghc8107"; + shell.tools = { + cabal = "3.8.1.0"; + hlint = "3.4.1"; + haskell-language-server = "1.7.0.0"; + }; + }; + }) + fenix.overlay + ]; + pkgs = import nixpkgs { + inherit system overlays; + inherit (haskellNix) config; + }; + rust = fenix.packages."${system}"; + flake = pkgs.libglaswegian.flake {}; + in flake // { + defaultPackage = flake.packages."libglaswegian:foreign-library:glaswegian"; + devShell = pkgs.mkShell { + nativeBuildInputs = with pkgs; [ + rustPlatform.bindgenHook + rust.stable.toolchain + rust-analyzer-nightly + ]; + inputsFrom = [ ]; + }; + }); +} diff --git a/src/Glaswegian.hs b/hsrc/Glaswegian.hs similarity index 100% rename from src/Glaswegian.hs rename to hsrc/Glaswegian.hs diff --git a/include/glaswegian.h b/include/glaswegian.h index 55007a7..a5dd297 100644 --- a/include/glaswegian.h +++ b/include/glaswegian.h @@ -1,3 +1,5 @@ +#include + // Start the RTS. void ghc_init(); @@ -5,7 +7,7 @@ void ghc_init(); void ghc_cleanup(); // Like malloc() but better! -void* ghc_alloc(size_t); +void* ghc_alloc(size_t s); // Like free() but better! -void ghc_free(void*); +void ghc_free(void* p); diff --git a/libglaswegian.cabal b/libglaswegian.cabal index 1d45c3d..418e33b 100644 --- a/libglaswegian.cabal +++ b/libglaswegian.cabal @@ -1,4 +1,4 @@ -cabal-version: 2.2 +cabal-version: 3.6 name: libglaswegian version: 0.1.0.0 @@ -18,10 +18,10 @@ foreign-library glaswegian type: native-shared -- options: standalone other-modules: Glaswegian - build-depends: base ^>=4.13.0.0 + build-depends: base , containers , primitive - hs-source-dirs: src + hs-source-dirs: hsrc default-language: Haskell2010 c-sources: cbits/ghc_init.c cbits/ghc_cleanup.c diff --git a/scripts/configure.sh b/scripts/configure.sh new file mode 100755 index 0000000..405abc0 --- /dev/null +++ b/scripts/configure.sh @@ -0,0 +1,4 @@ +#!/usr/bin/env bash + +mkdir -p lib +find ./dist-newstyle -type f -name libglaswegian.so -exec cp {} lib \; diff --git a/scripts/run.sh b/scripts/run.sh new file mode 100755 index 0000000..148a340 --- /dev/null +++ b/scripts/run.sh @@ -0,0 +1,3 @@ +#!/usr/bin/env bash + +LD_LIBRARY_PATH=./lib cargo run diff --git a/shell.nix b/shell.nix index d15f665..d7428a6 100644 --- a/shell.nix +++ b/shell.nix @@ -14,5 +14,7 @@ runCommand "libglaswegian-env" ]); in [ binutils thisghc + rustc + cargo ]; } "" diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..5ccd7ac --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,55 @@ +use std::{ + alloc::{GlobalAlloc, Layout}, + ffi::c_void, + os::raw::c_ulong as size_t, +}; + +pub mod hask { + #[link(name = "glaswegian", kind = "dylib")] + extern "C" { + pub fn ghc_init(); + pub fn ghc_cleanup(); + pub fn ghc_alloc(s: crate::size_t) -> *mut ::std::os::raw::c_void; + pub fn ghc_free(p: *mut ::std::os::raw::c_void); + } +} + +pub struct HaskAlloc; + +#[cfg_attr(any(target_os = "linux"), link_section = ".init_array")] +#[allow(non_upper_case_globals)] +static __rust_mod_init: unsafe extern "C" fn() = { + #[cfg_attr(any(target_os = "linux"), link_section = ".text.startup")] + unsafe extern "C" fn __rust_ghc_init() { + hask::ghc_init(); + } + __rust_ghc_init +}; + +#[cfg_attr(target_os = "linux", link_section = ".init_array")] +#[allow(non_upper_case_globals)] +static __rust_mod_at_exit: unsafe extern "C" fn() = { + extern "C" { + fn atexit(cb: unsafe extern "C" fn()); + } + #[cfg_attr(target_os = "linux", link_section = ".text.exit")] + unsafe extern "C" fn __rust_ghc_cleanup() { + hask::ghc_cleanup(); + } + #[cfg_attr(target_os = "linux", link_section = ".text.startup")] + unsafe extern "C" fn __dtor_atexit() { + atexit(__rust_ghc_cleanup) + } + __dtor_atexit +}; + +unsafe impl GlobalAlloc for HaskAlloc { + #[inline] + unsafe fn alloc(&self, layout: Layout) -> *mut u8 { + hask::ghc_alloc(layout.size() as u64) as *mut u8 + } + + unsafe fn dealloc(&self, ptr: *mut u8, _layout: Layout) { + hask::ghc_free(ptr as *mut c_void) + } +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..e15c66b --- /dev/null +++ b/src/main.rs @@ -0,0 +1,11 @@ +use glasalloc::HaskAlloc; + +#[global_allocator] +pub static ALLOCATOR: HaskAlloc = HaskAlloc {}; + +fn main() { + let mut v = vec![]; + for i in [0..2048] { + v.push(i); + } +}