-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add a really simple data type base for strings, buffers and utf-32 intended for the con4m runtime (non-memory managed, c string compatable, but tracks length) * Add some basic stuff here to support con4m * Add libffi for x86 linux * Some visibility changes * linux-arm fully built * Helps if I add in the actual build .a files * Have the 'add' function call the right API --------- Co-authored-by: John Viega <[email protected]> Co-authored-by: John Viega <[email protected]> Co-authored-by: John Viega <[email protected]>
- Loading branch information
1 parent
4ac970d
commit 516dc45
Showing
11 changed files
with
136 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#include <stdint.h> | ||
#include <string.h> | ||
#include <stdlib.h> | ||
|
||
typedef struct { | ||
int64_t len; | ||
char data[]; | ||
} real_str_t; | ||
|
||
typedef char str_t; | ||
|
||
str_t * | ||
c4string_new(int64_t *len) | ||
{ | ||
real_str_t *real_obj = calloc(len + sizeof(int64_t) + 1, 1); | ||
|
||
real_obj->len = len; | ||
return real_obj->data; | ||
} | ||
|
||
str_t * | ||
c4string_from_cstr(char *s) { | ||
int64_t l = (int64_t)strlen(s); | ||
str_t *result = c4string_new(l); | ||
|
||
memcpy(result, s, (size_t)(l + 1)); | ||
|
||
return result; | ||
} | ||
|
||
int64_t | ||
c4string_len(str_t *s) | ||
{ | ||
real_str_t *p = (real_str_t *)(s - sizeof(int64_t)); | ||
return p->len; | ||
} | ||
|
||
void | ||
c4string_free(str_t *s) | ||
{ | ||
free((real_str_t *)(s - sizeof(int64_t))); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import os | ||
|
||
static: | ||
{.compile: joinPath(splitPath(currentSourcePath()).head, "c/strcontainer.c").} | ||
|
||
type C4Str* = pointer | ||
|
||
proc newC4Str*(l: int64): C4Str {.importc: "c4string_new", cdecl.} | ||
proc newC4Str*(s: cstring): C4Str {.importc: "c4string_from_cstr", cdecl.} | ||
proc c4str_len*(s: C4Str): int64 {.importc: "c4string_len", cdecl.} | ||
proc free*(s: C4Str) {.importc: "c4string_free", cdecl.} | ||
|
||
template len*(s: C4Str): int = int(s.c4str_len()) | ||
|
||
proc newC4Str*(s: string): C4Str {.cdecl, exportc.} = | ||
let l = s.len() | ||
result = newC4Str(l) | ||
if l != 0: | ||
copyMem(cast[pointer](result), addr s[0], l) | ||
|
||
proc c4str_eq*(s1, s2: C4Str): bool {.cdecl, exportc.} = | ||
if s1.len() != s2.len(): | ||
return false | ||
return cast[cstring](s1) == cast[cstring](s2) | ||
|
||
proc c4str_lt*(s1, s2: C4Str): bool {.cdecl, exportc.} = | ||
return cast[cstring](s1) < cast[cstring](s2) | ||
|
||
proc c4str_gt*(s1, s2: C4Str): bool {.cdecl, exportc.} = | ||
return cast[cstring](s1) > cast[cstring](s2) | ||
|
||
proc c4str_add*(s1, s2: C4Str): C4Str {.cdecl, exportc.} = | ||
let | ||
l1 = s1.len() | ||
l2 = s2.len() | ||
|
||
result = newC4Str(l1 + l2) | ||
|
||
# Where to start writing the 2nd string. | ||
let p = cast[pointer](cast[uint](cast[pointer](result)) + uint(l1)) | ||
|
||
copyMem(cast[pointer](result), cast[pointer](s1), l1) | ||
copyMem(p, cast[pointer](s2), l2) | ||
|
||
proc c4str_copy*(s1: C4Str): C4Str {.cdecl, exportc.} = | ||
let l = s1.len() | ||
|
||
result = newC4Str(l) | ||
copyMem(cast[pointer](result), cast[pointer](s1), l) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters