Skip to content

Commit

Permalink
make format: Add auto indentation for C files, and a CI formatting wo…
Browse files Browse the repository at this point in the history
…rkflow

Also reformats all C files with these commands

Signed-off-by: Andrii Sultanov <[email protected]>
  • Loading branch information
last-genius committed Oct 10, 2024
1 parent 10156bc commit bd2013b
Show file tree
Hide file tree
Showing 11 changed files with 657 additions and 526 deletions.
43 changes: 43 additions & 0 deletions .github/workflows/format.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: Check format

on:
push:
pull_request:

jobs:
ocaml-format:
name: Ocaml files
runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Pull configuration from xs-opam
run: |
curl --fail --silent https://raw.githubusercontent.com/xapi-project/xs-opam/master/tools/xs-opam-ci.env | cut -f2 -d " " > .env
- name: Load environment file
id: dotenv
uses: falti/dotenv-action@v1

- name: Update Ubuntu repositories
run: sudo apt-get update

- name: Use ocaml
uses: ocaml/setup-ocaml@v3
with:
ocaml-compiler: ${{ steps.dotenv.outputs.ocaml_version_full }}
opam-repositories: |
xs-opam: ${{ steps.dotenv.outputs.repository }}
dune-cache: true
opam-pin: false

- name: Install ocamlformat
run: opam install ocamlformat

- name: Check whether `make format` was run
run: opam exec -- dune build @fmt

- name: Check whether dune files are formatted
run: opam exec -- dune format-dune-file dune-project > $$$$ && mv $$$$ dune-project
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ utop:
format:
$(DUNE) build --auto-promote @fmt
dune format-dune-file dune-project > $$$$ && mv $$$$ dune-project
git ls-files '**/*.[ch]' | xargs -n1 indent -nut -i8
94 changes: 49 additions & 45 deletions gnt/gnttab_stubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,69 +39,73 @@

#define _G(__g) (*((xengnttab_handle**)Data_abstract_val(__g)))

CAMLprim value stub_gnttab_interface_open(void)
CAMLprim value
stub_gnttab_interface_open (void)
{
CAMLparam0();
CAMLlocal1(result);
xengnttab_handle *xgh;
CAMLparam0 ();
CAMLlocal1 (result);
xengnttab_handle *xgh;

xgh = xengnttab_open(NULL, 0);
if (xgh == NULL)
caml_failwith("Failed to open interface");
result = caml_alloc(1, Abstract_tag);
_G(result) = xgh;
xgh = xengnttab_open (NULL, 0);
if (xgh == NULL)
caml_failwith ("Failed to open interface");
result = caml_alloc (1, Abstract_tag);
_G (result) = xgh;

CAMLreturn(result);
CAMLreturn (result);
}

CAMLprim value stub_gnttab_interface_close(value xgh)
CAMLprim value
stub_gnttab_interface_close (value xgh)
{
CAMLparam1(xgh);
CAMLparam1 (xgh);

xengnttab_close(_G(xgh));
xengnttab_close (_G (xgh));

CAMLreturn(Val_unit);
CAMLreturn (Val_unit);
}

#define _M(__m) ((struct mmap_interface*)Data_abstract_val(__m))
#define XEN_PAGE_SHIFT 12

CAMLprim value stub_gnttab_unmap(value xgh, value array)
CAMLprim value
stub_gnttab_unmap (value xgh, value array)
{
CAMLparam2(xgh, array);
int result;
CAMLparam2 (xgh, array);
int result;

caml_enter_blocking_section();
result = xengnttab_unmap(_G(xgh), _M(array)->addr, _M(array)->len >> XEN_PAGE_SHIFT);
caml_leave_blocking_section();
caml_enter_blocking_section ();
result = xengnttab_unmap (_G (xgh), _M (array)->addr,
_M (array)->len >> XEN_PAGE_SHIFT);
caml_leave_blocking_section ();

if(result!=0) {
caml_failwith("Failed to unmap grant");
}
if (result != 0)
{
caml_failwith ("Failed to unmap grant");
}

CAMLreturn(Val_unit);
CAMLreturn (Val_unit);
}

CAMLprim value stub_gnttab_map_fresh(
value xgh,
value reference,
value domid,
value writable
)
CAMLprim value
stub_gnttab_map_fresh (value xgh,
value reference, value domid, value writable)
{
CAMLparam4(xgh, reference, domid, writable);
CAMLlocal1(contents);
void *map;

caml_enter_blocking_section();
map = xengnttab_map_grant_ref(_G(xgh), Int_val(domid), Int_val(reference),
Bool_val(writable)?PROT_READ | PROT_WRITE:PROT_READ);
caml_leave_blocking_section();

if(map==NULL) {
caml_failwith("Failed to map grant ref");
}
contents = stub_mmap_alloc(map, 1 << XEN_PAGE_SHIFT);
CAMLreturn(contents);
CAMLparam4 (xgh, reference, domid, writable);
CAMLlocal1 (contents);
void *map;

caml_enter_blocking_section ();
map = xengnttab_map_grant_ref (_G (xgh), Int_val (domid),
Int_val (reference),
Bool_val (writable) ? PROT_READ |
PROT_WRITE : PROT_READ);
caml_leave_blocking_section ();

if (map == NULL)
{
caml_failwith ("Failed to map grant ref");
}
contents = stub_mmap_alloc (map, 1 << XEN_PAGE_SHIFT);
CAMLreturn (contents);
}

6 changes: 3 additions & 3 deletions mmap/mmap_stubs.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,14 @@

struct mmap_interface
{
void *addr;
size_t len;
void *addr;
size_t len;
};

#ifndef Data_abstract_val
#define Data_abstract_val(x) ((void *)Op_val(x))
#endif

value stub_mmap_alloc(void *addr, size_t len);
value stub_mmap_alloc (void *addr, size_t len);

#endif
126 changes: 73 additions & 53 deletions mmap/xenmmap_stubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,70 +34,90 @@
#define Intf_val(a) ((struct mmap_interface *)Data_abstract_val(a))
#define Wsize_bsize_round(n) (Wsize_bsize( (n) + sizeof(value) - 1 ))

value stub_mmap_alloc(void *addr, size_t len)
value
stub_mmap_alloc (void *addr, size_t len)
{
CAMLparam0();
CAMLlocal1(result);
result = caml_alloc(Wsize_bsize_round(sizeof(struct mmap_interface)), Abstract_tag);
Intf_val(result)->addr = addr;
Intf_val(result)->len = len;
CAMLreturn(result);
CAMLparam0 ();
CAMLlocal1 (result);
result = caml_alloc (Wsize_bsize_round
(sizeof (struct mmap_interface)), Abstract_tag);
Intf_val (result)->addr = addr;
Intf_val (result)->len = len;
CAMLreturn (result);
}

CAMLprim value stub_mmap_init(value fd, value pflag, value mflag,
value len, value offset)
CAMLprim value
stub_mmap_init (value fd, value pflag, value mflag, value len, value offset)
{
CAMLparam5(fd, pflag, mflag, len, offset);
CAMLlocal1(result);
int c_pflag, c_mflag;
void* addr;
size_t length;

switch (Int_val(pflag)) {
case 0: c_pflag = PROT_READ; break;
case 1: c_pflag = PROT_WRITE; break;
case 2: c_pflag = PROT_READ|PROT_WRITE; break;
default: caml_invalid_argument("protectiontype");
}

switch (Int_val(mflag)) {
case 0: c_mflag = MAP_SHARED; break;
case 1: c_mflag = MAP_PRIVATE; break;
default: caml_invalid_argument("maptype");
}

static_assert((sizeof(struct mmap_interface) % sizeof(value)) == 0);

if (Int_val(len) < 0)
caml_invalid_argument("negative size");
if (Int_val(offset) < 0)
caml_invalid_argument("negative offset");
length = Int_val(len);

addr = mmap(NULL, length, c_pflag, c_mflag, Int_val(fd), Int_val(offset));
if (MAP_FAILED == addr)
uerror("mmap", Nothing);

result = stub_mmap_alloc(addr, length);
CAMLreturn(result);
CAMLparam5 (fd, pflag, mflag, len, offset);
CAMLlocal1 (result);
int c_pflag, c_mflag;
void *addr;
size_t length;

switch (Int_val (pflag))
{
case 0:
c_pflag = PROT_READ;
break;
case 1:
c_pflag = PROT_WRITE;
break;
case 2:
c_pflag = PROT_READ | PROT_WRITE;
break;
default:
caml_invalid_argument ("protectiontype");
}

switch (Int_val (mflag))
{
case 0:
c_mflag = MAP_SHARED;
break;
case 1:
c_mflag = MAP_PRIVATE;
break;
default:
caml_invalid_argument ("maptype");
}

static_assert ((sizeof (struct mmap_interface) % sizeof (value)) ==
0);

if (Int_val (len) < 0)
caml_invalid_argument ("negative size");
if (Int_val (offset) < 0)
caml_invalid_argument ("negative offset");
length = Int_val (len);

addr = mmap (NULL, length, c_pflag, c_mflag, Int_val (fd),
Int_val (offset));
if (MAP_FAILED == addr)
uerror ("mmap", Nothing);

result = stub_mmap_alloc (addr, length);
CAMLreturn (result);
}

CAMLprim value stub_mmap_final(value intf)
CAMLprim value
stub_mmap_final (value intf)
{
CAMLparam1(intf);
CAMLparam1 (intf);

if (Intf_val(intf)->addr != MAP_FAILED)
munmap(Intf_val(intf)->addr, Intf_val(intf)->len);
Intf_val(intf)->addr = MAP_FAILED;
if (Intf_val (intf)->addr != MAP_FAILED)
munmap (Intf_val (intf)->addr, Intf_val (intf)->len);
Intf_val (intf)->addr = MAP_FAILED;

CAMLreturn(Val_unit);
CAMLreturn (Val_unit);
}

CAMLprim value stub_mmap_getpagesize(value unit)
CAMLprim value
stub_mmap_getpagesize (value unit)
{
CAMLparam1(unit);
CAMLlocal1(data);
CAMLparam1 (unit);
CAMLlocal1 (data);

data = Val_int(getpagesize());
CAMLreturn(data);
data = Val_int (getpagesize ());
CAMLreturn (data);
}
Loading

0 comments on commit bd2013b

Please sign in to comment.