Skip to content

Commit

Permalink
Flexible c structs for ML strings and tables (#169)
Browse files Browse the repository at this point in the history
Use C99 flexible C structs for ML strings and tables.
  • Loading branch information
melsman authored Mar 6, 2024
1 parent 597da8c commit 85a57c5
Show file tree
Hide file tree
Showing 15 changed files with 97 additions and 95 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
## MLKit NEWS

* mael 2024-03-06: Use C99 flexible C structs for ML strings and
tables (internal cleanup).

### MLKit version 4.7.9 is released

* mael 2024-03-04: Fix issue with Char.isCntrl (issue #162).
Expand Down
27 changes: 13 additions & 14 deletions src/Runtime/Dlsym.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
uintptr_t
REG_POLY_FUN_HDR(sml_dlopen,uintptr_t pair, Region s, String file1, size_t flags1)
{
char *file = &(file1->data);
char *file = file1->data;
char *c;
void *p;
int flags = convertIntToC(flags1);
int use_lazy = RTLD_NOW;
int use_global = RTLD_LOCAL;
char *myfile = file;
if (flags & 0x1) use_lazy = RTLD_LAZY;
if (flags & 0x2) use_global = RTLD_GLOBAL;
if (flags & 0x2) use_global = RTLD_GLOBAL;
if (flags & 0x4) myfile = NULL;
dlerror();
p = dlopen(myfile, use_global | use_lazy);
Expand All @@ -30,7 +30,7 @@ REG_POLY_FUN_HDR(sml_dlopen,uintptr_t pair, Region s, String file1, size_t flags
if (c)
{
second(pair) = (uintptr_t) REG_POLY_CALL(convertStringToML,s, c);
} else
} else
{
second(pair) = 0;
}
Expand All @@ -48,32 +48,32 @@ DEFINE_NHASHMAP(dynamic_function_res_map, charhashfunction, mystreq)

static dynamic_function_res_map_hashtable_t *fnmap = NULL;

String
String
REG_POLY_FUN_HDR(resolveFun,Region sAddr, String our_name, String cname, void *libhandle)
{
char *c;
const void *fp = NULL;
LOCK_LOCK(FUNCTIONTABLEMUTEX);
if (fnmap == NULL) fnmap = dynamic_function_res_map_new();
if (fnmap == NULL)
if (fnmap == NULL)
{
LOCK_UNLOCK(FUNCTIONTABLEMUTEX);
return (String) REG_POLY_CALL(convertStringToML,sAddr, "Allocation Error in __FILE__:__LINE__");
}
if (dynamic_function_res_map_find(fnmap, &(our_name->data), &fp) == hash_DNE)
if (dynamic_function_res_map_find(fnmap, our_name->data, &fp) == hash_DNE)
{
dlerror();
fp = dlsym(libhandle, &(cname->data));
fp = dlsym(libhandle, cname->data);
c = dlerror();
if (c)
{
return (String) REG_POLY_CALL(convertStringToML,sAddr,c);
}
dynamic_function_res_map_update(fnmap, &(our_name->data), fp);
dynamic_function_res_map_update(fnmap, our_name->data, fp);
LOCK_UNLOCK(FUNCTIONTABLEMUTEX);
return NULL;
}
else
else
{ // hash_OK
LOCK_UNLOCK(FUNCTIONTABLEMUTEX);
return (String) REG_POLY_CALL(convertStringToML,sAddr, "Dynamic function already resolved");
Expand Down Expand Up @@ -103,13 +103,13 @@ localResolveLibFnAuto(const void **fp, const char *fname)
{
const void *myfp = NULL;
LOCK_LOCK(FUNCTIONTABLEMUTEX);
if (fnmap == NULL) return;
if (fnmap == NULL) return;
if (dynamic_function_res_map_find(fnmap, fname, &myfp) == hash_DNE)
{
LOCK_UNLOCK(FUNCTIONTABLEMUTEX);
return;
}
else
else
{
*fp = myfp;
LOCK_UNLOCK(FUNCTIONTABLEMUTEX);
Expand All @@ -122,12 +122,11 @@ localResolveLibFnAuto(const void **fp, const char *fname)
void
localResolveLibFnManual(const void **fp, String fname)
{
localResolveLibFnAuto(fp, &(fname->data));
localResolveLibFnAuto(fp, fname->data);
}

String
String
REG_POLY_FUN_HDR(fromCtoMLstring,Region sAddr, char *cs)
{
return (String) REG_POLY_CALL(convertStringToML,sAddr,cs);
}

14 changes: 7 additions & 7 deletions src/Runtime/Export.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@

static int
charEqual(const char *a, const char *b)
{
{
return (strcmp(a,b) ? 0 : 1);
}
}

DECLARE_NHASHMAP(exportmap,void *, char *, const, const)
DEFINE_NHASHMAP(exportmap,charhashfunction,charEqual)
Expand All @@ -24,19 +24,19 @@ sml_regCfuns(String name,void *f)
const char *e;
char *e1;
const void *f1;
// printf("sml_regCfuns %s\n", &(name->data));
if (!exportmap)
// printf("sml_regCfuns %s\n", name->data);
if (!exportmap)
{
exportmap = exportmap_new();
if (!exportmap) return;
}
e = &(name->data);

e = name->data;
if (exportmap_find(exportmap, e, &f1) == hash_DNE)
{
e1 = (char *) malloc(strlen(e) + 1);
if (!e1) return;
strcpy(e1, &(name->data));
strcpy(e1, name->data);
exportmap_insert(exportmap, e1, f);
}
return;
Expand Down
50 changes: 25 additions & 25 deletions src/Runtime/IO.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ uintptr_t
openInStream(Context ctx, String path, uintptr_t exn) /* SML Basis */
{
FILE *fileDesc;
if ((fileDesc = fopen(&(path->data), "r")) == NULL)
if ((fileDesc = fopen(path->data, "r")) == NULL)
{
raise_exn(ctx,exn);
}
Expand All @@ -40,7 +40,7 @@ uintptr_t
openOutStream(Context ctx, String path, uintptr_t exn) /* SML Basis */
{
FILE *fileDesc;
if ((fileDesc = fopen(&(path->data), "w")) == NULL)
if ((fileDesc = fopen(path->data, "w")) == NULL)
{
raise_exn(ctx,exn);
}
Expand All @@ -52,7 +52,7 @@ uintptr_t
openAppendStream(Context ctx, String path, uintptr_t exn) /* SML Basis */
{
FILE *fileDesc;
if ((fileDesc = fopen(&(path->data), "a")) == NULL)
if ((fileDesc = fopen(path->data, "a")) == NULL)
{
raise_exn(ctx,exn);
}
Expand All @@ -64,7 +64,7 @@ uintptr_t
openInBinStream(Context ctx, String path, uintptr_t exn) /* SML Basis */
{
FILE *fileDesc;
if ((fileDesc = fopen(&(path->data), "rb")) == NULL)
if ((fileDesc = fopen(path->data, "rb")) == NULL)
{
raise_exn(ctx,exn);
}
Expand All @@ -76,7 +76,7 @@ uintptr_t
openOutBinStream(Context ctx, String path, uintptr_t exn) /* SML Basis */
{
FILE *fileDesc;
if ((fileDesc = fopen(&(path->data), "wb")) == NULL)
if ((fileDesc = fopen(path->data, "wb")) == NULL)
{
raise_exn(ctx,exn);
}
Expand All @@ -88,7 +88,7 @@ uintptr_t
openAppendBinStream(Context ctx, String path, uintptr_t exn) /* SML Basis */
{
FILE *fileDesc;
if ((fileDesc = fopen(&(path->data), "ab")) == NULL)
if ((fileDesc = fopen(path->data, "ab")) == NULL)
{
raise_exn(ctx,exn);
}
Expand Down Expand Up @@ -197,7 +197,7 @@ size_t
outputStream(Context ctx, uintptr_t os1, String s, uintptr_t exn)
{
FILE *os = (FILE *)untag_scalar(os1);
if ( fputs(&(s->data), os) == EOF )
if ( fputs(s->data, os) == EOF )
{
fflush(os);
raise_exn(ctx,exn);
Expand Down Expand Up @@ -236,7 +236,7 @@ stdErrStream(uintptr_t dummy)
void
sml_chdir(Context ctx, String dirname, uintptr_t exn) /* SML Basis */
{
if ( chdir(&(dirname->data)) != 0 )
if ( chdir(dirname->data) != 0 )
{
raise_exn(ctx,exn);
}
Expand All @@ -247,7 +247,7 @@ void
sml_remove(Context ctx, String name, uintptr_t exn) /* SML Basis */
{
int ret;
ret = unlink(&(name->data));
ret = unlink(name->data);
if ( ret != 0 )
{
raise_exn(ctx,exn);
Expand All @@ -258,7 +258,7 @@ sml_remove(Context ctx, String name, uintptr_t exn) /* SML Basis
void
sml_rename(Context ctx, String oldname, String newname, uintptr_t exn) /* SML Basis */
{
if ( rename(&(oldname->data), &(newname->data)) != 0 )
if ( rename(oldname->data, newname->data) != 0 )
{
raise_exn(ctx,exn);
}
Expand All @@ -277,7 +277,7 @@ sml_access(String path, size_t permarg) /* ML */
{
perms = F_OK;
}
if (access(&(path->data), perms) == 0)
if (access(path->data, perms) == 0)
{
return mlTRUE;
}
Expand All @@ -302,7 +302,7 @@ size_t
sml_isdir(Context ctx, String path, uintptr_t exn) /* SML Basis */
{
struct stat buf;
if ( stat(&(path->data), &buf) == -1 )
if ( stat(path->data, &buf) == -1 )
{
raise_exn(ctx,exn);
}
Expand All @@ -316,7 +316,7 @@ sml_isdir(Context ctx, String path, uintptr_t exn) /* SML Basis */
void
sml_mkdir(Context ctx, String path, uintptr_t exn) /* SML Basis */
{
if ( mkdir(&(path->data), 0777) == -1 )
if ( mkdir(path->data, 0777) == -1 )
{
raise_exn(ctx,exn);
}
Expand All @@ -328,7 +328,7 @@ uintptr_t
sml_modtime(uintptr_t vAddr, Context ctx, String path, uintptr_t exn) /* SML Basis */
{
struct stat buf;
if ( stat(&(path->data), &buf) == -1 )
if ( stat(path->data, &buf) == -1 )
{
raise_exn(ctx,exn);
}
Expand All @@ -340,7 +340,7 @@ sml_modtime(uintptr_t vAddr, Context ctx, String path, uintptr_t exn)
void
sml_rmdir(Context ctx, String path, uintptr_t exn) /* SML Basis */
{
if ( rmdir(&(path->data)) == -1 )
if ( rmdir(path->data) == -1 )
{
raise_exn(ctx,exn);
}
Expand All @@ -352,7 +352,7 @@ sml_settime(Context ctx, String path, uintptr_t time, uintptr_t exn) /* SML
{
struct utimbuf tbuf;
tbuf.actime = tbuf.modtime = (long)(get_d(time));
if ( utime(&(path->data), &tbuf) == -1 )
if ( utime(path->data, &tbuf) == -1 )
{
raise_exn(ctx,exn);
}
Expand All @@ -363,7 +363,7 @@ size_t
sml_filesize(Context ctx, String path, uintptr_t exn) /* SML Basis */
{
struct stat buf;
if ( stat(&(path->data), &buf) == -1 )
if ( stat(path->data, &buf) == -1 )
{
raise_exn(ctx,exn);
}
Expand All @@ -374,7 +374,7 @@ uintptr_t
sml_opendir(Context ctx, String path, uintptr_t exn) /* SML Basis */
{
DIR * dstr;
dstr = opendir(&(path->data));
dstr = opendir(path->data);
if ( dstr == NULL )
{
raise_exn(ctx,exn);
Expand Down Expand Up @@ -447,7 +447,7 @@ size_t
sml_islink(Context ctx, String path, uintptr_t exn) /* SML Basis */
{
struct stat buf;
if (lstat(&(path->data), &buf) == -1)
if (lstat(path->data, &buf) == -1)
{
raise_exn(ctx,exn);
}
Expand Down Expand Up @@ -489,7 +489,7 @@ REG_POLY_FUN_HDR(sml_readlink, Region rAddr, Context ctx, String path, uintptr_t
{
char buffer[MAXPATHLEN];
long result;
result = readlink(&(path->data), buffer, MAXPATHLEN);
result = readlink(path->data, buffer, MAXPATHLEN);
if (result == -1 || result >= MAXPATHLEN)
{
raise_exn(ctx,exn);
Expand All @@ -505,7 +505,7 @@ REG_POLY_FUN_HDR(sml_realpath, Region rAddr, Context ctx, String path, uintptr_t
{
char buffer[MAXPATHLEN];
char *result;
result = realpath(&(path->data), buffer);
result = realpath(path->data, buffer);
if (result == NULL)
{
raise_exn(ctx,exn);
Expand All @@ -518,7 +518,7 @@ uintptr_t
sml_devinode(uintptr_t vAddr, Context ctx, String path, uintptr_t exn) /* SML Basis */
{
struct stat buf;
if (stat(&(path->data), &buf) == -1)
if (stat(path->data, &buf) == -1)
{
raise_exn(ctx,exn);
}
Expand All @@ -533,7 +533,7 @@ size_t
sml_system(String cmd) /* SML Basis */
{
int res;
res = system(&(cmd->data));
res = system(cmd->data);
if (res != 0)
{
res = -1;
Expand All @@ -545,7 +545,7 @@ String
REG_POLY_FUN_HDR(sml_getenv, Region rAddr, Context ctx, String var, uintptr_t exn) /* SML Basis */
{
char *res;
res = (char *)(getenv(&(var->data)));
res = (char *)(getenv(var->data));
if (res == NULL)
{
raise_exn(ctx,exn);
Expand All @@ -559,7 +559,7 @@ outputBinStream(Context ctx, uintptr_t os1, String s, uintptr_t exn)
FILE *os = (FILE *) os1;
strsize = sizeStringDefine(s);
os = (FILE *)untag_scalar(os);
if ( fwrite(&(s->data), 1, strsize, os) != strsize )
if ( fwrite(s->data, 1, strsize, os) != strsize )
{
fflush(os);
raise_exn(ctx,exn);
Expand Down
6 changes: 3 additions & 3 deletions src/Runtime/Math.c
Original file line number Diff line number Diff line change
Expand Up @@ -892,9 +892,9 @@ REG_POLY_FUN_HDR(stringOfFloat, Region rAddr, size_t arg)
String
REG_POLY_FUN_HDR(generalStringOfFloat, Region rAddr, String format, size_t f)
{
size_t size = snprintf(NULL, 0, &(format->data), get_d(f)) + 1;
size_t size = snprintf(NULL, 0, format->data, get_d(f)) + 1;
char *buf = malloc(size);
snprintf(buf, size, &(format->data), get_d(f));
snprintf(buf, size, format->data, get_d(f));

mkSMLMinus(buf);
String s;
Expand Down Expand Up @@ -932,7 +932,7 @@ size_t
sml_bytes_to_real(size_t d, String s)
{
double r;
char* a = &(s->data);
char* a = s->data;
r = ((double*)a)[0];
get_d(d) = r;
set_dtag(d);
Expand Down
Loading

0 comments on commit 85a57c5

Please sign in to comment.