From 43ecfce7d20360a5fdc53e5ced266eccc8723242 Mon Sep 17 00:00:00 2001 From: Nicholas Noll Date: Fri, 29 May 2020 14:41:05 -0700 Subject: blas code update --- sys/libn/error.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- sys/libn/memory.c | 20 ++++++++++++++++++++ sys/libn/string.c | 22 +++++++++++++++------- 3 files changed, 90 insertions(+), 8 deletions(-) (limited to 'sys/libn') diff --git a/sys/libn/error.c b/sys/libn/error.c index 15611a3..9b6a28a 100644 --- a/sys/libn/error.c +++ b/sys/libn/error.c @@ -2,7 +2,7 @@ #include void -errorf(const byte* fmt, ...) +errorf(byte* fmt, ...) { va_list args; va_start(args, fmt); @@ -13,3 +13,57 @@ errorf(const byte* fmt, ...) va_end(args); } + +void +panicf(byte* fmt, ...) +{ + int n; + va_list args; + static byte buf[4*1024]; + va_start(args, fmt); + + buf[0] = 'p'; + buf[1] = 'a'; + buf[2] = 'n'; + buf[3] = 'i'; + buf[4] = 'c'; + buf[5] = ':'; + buf[6] = ' '; + n = vsnprintf(buf+7, arrlen(buf)-8, fmt, args); + if (n < 0) + return; + buf[n] = 0; + + perror(buf); + va_end(args); + + /* TODO: portable stack unwinding */ + exit(1); +} + +void +vpanicf(byte* fmt, va_list args) +{ + int n; + va_list nargs; + static byte buf[4*1024]; + va_copy(nargs, args); + + buf[0] = 'p'; + buf[1] = 'a'; + buf[2] = 'n'; + buf[3] = 'i'; + buf[4] = 'c'; + buf[5] = ':'; + buf[6] = ' '; + n = vsnprintf(buf+7, arrlen(buf)-8, fmt, args); + if (n < 0) + return; + buf[n] = 0; + + perror(buf); + va_end(nargs); + + /* TODO: portable stack unwinding */ + exit(1); +} diff --git a/sys/libn/memory.c b/sys/libn/memory.c index dce0c36..7993ca2 100644 --- a/sys/libn/memory.c +++ b/sys/libn/memory.c @@ -1,6 +1,26 @@ #include #include +void +·free(void* _, void* ptr) { + return free(ptr); +} + +void * +·alloc(void* _, uint n, ulong size) { + return malloc(n*size); +} + +void * +·calloc(void* _, uint n, ulong size) { + return calloc(n, size); +} + +void * +·realloc(void* _, void *ptr, uint n, ulong size) { + return realloc(ptr, n*size); +} + // ------------------------------------------------------------------------- // Dynamic buffer. diff --git a/sys/libn/string.c b/sys/libn/string.c index 7c152e3..e2cdddf 100644 --- a/sys/libn/string.c +++ b/sys/libn/string.c @@ -349,25 +349,29 @@ str·fit(string *s) // structure. This variant can append a substring of length len of the given // string to our buffer. The result is reallocated if not enough room is present // in the buffer. -void +int str·appendlen(string *s, vlong n, const byte* b) { - vlong bl = strlen(b); + /* + bl = strlen(b); if (n > bl) panicf("attempted to make a substring longer than string"); + */ str·grow(s, n); - if (*s == nil) return; + if (*s == nil) return 0; Hdr* h = (Hdr*)(*s - sizeof(Hdr)); memcpy(*s + str·len(*s), b, n); h->len += n; (*s)[h->len] = '\0'; + + return n; } // Append will append the given null terminated C string to the string data // structure. This variant will append the entire string. -void +int str·append(string *s, const byte* b) { return str·appendlen(s, strlen(b), b); @@ -376,17 +380,19 @@ str·append(string *s, const byte* b) // AppendByte will append the given byte to our string. // NOTE: As the byte is on the stack, it is not null-terminated. // Can not pass to the above functions. -void +int str·appendbyte(string *s, const byte b) { str·grow(s, 1); - if (*s == nil) return; + if (*s == nil) return 0; Hdr* h = (Hdr*)(*s - sizeof(Hdr)); *(*s + str·len(*s)) = b; h->len++; (*s)[h->len] = '\0'; // NOTE: I don't think an explicit zero is required..? + + return 1; } /* @@ -394,7 +400,7 @@ str·appendbyte(string *s, const byte b) * Returns the newly minted string */ -void +int str·appendf(string *s, const byte* fmt, ...) { va_list args; @@ -415,6 +421,8 @@ str·appendf(string *s, const byte* fmt, ...) Hdr* h = (Hdr*)(*s - sizeof(Hdr)); h->len += n; + + return n; } // Equals returns true if string s and t are equivalent. -- cgit v1.2.1