diff options
-rw-r--r-- | CHANGELOG.txt | 12 | ||||
-rw-r--r-- | Makefile (renamed from GNUmakefile) | 8 | ||||
-rw-r--r-- | README.html | 66 | ||||
-rw-r--r-- | test.cc | 25 | ||||
-rw-r--r-- | zap/include-priv/zap/priv.h | 2 | ||||
-rw-r--r-- | zap/include/zap/base.h | 11 | ||||
-rw-r--r-- | zap/src/fndbyte.c | 4 | ||||
-rw-r--r-- | zap/src/fndchr.c | 4 | ||||
-rw-r--r-- | zap/src/foreach.c | 16 | ||||
-rw-r--r-- | zap/src/memcpy.c | 12 | ||||
-rw-r--r-- | zap/src/memeq.c | 2 | ||||
-rw-r--r-- | zap/src/memfill.c | 4 | ||||
-rw-r--r-- | zap/src/strcpy.c | 2 | ||||
-rw-r--r-- | zap/src/streq.c | 2 | ||||
-rw-r--r-- | zap/src/strlen.c | 2 |
15 files changed, 145 insertions, 27 deletions
diff --git a/CHANGELOG.txt b/CHANGELOG.txt index af69d4d..db401a1 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -1,3 +1,15 @@ +| B + +- Add function foreach: Run function on each element; +- Use directive globl instead of global in assembly; +- Use SIZE_MAX instead of negative one; +- Remove asflags from makefile; +- Don't require GNU Make; +- Change the type of fndbyte: (size_t (void const *,size_t,uint_least8_t)) => (size_t (void const *,size_t,unsigned char)); +- Change the type of memfill: (void (void *,size_t,uint_least8_t)) => (void (void *,size_t,unsigned char)); +- Update readme (add documentation for some functions); +- Update susinfo symbol names; + | C - Fix install target; @@ -1,9 +1,8 @@ # TOOLS -# TOOL FLAGS +#CC = clang -ASFLAGS = \ - -fPIC +# TOOL FLAGS CFLAGS = \ -Izap/include \ @@ -37,6 +36,7 @@ OBJS = \ zap/src/fastimpl.o \ zap/src/fndbyte.o \ zap/src/fndchr.o \ + zap/src/foreach.o \ zap/src/memcmp.o \ zap/src/memcpy.o \ zap/src/memdup.o \ @@ -56,7 +56,7 @@ LIB = libzap.a .PHONY: clean install purge $(LIB): $(OBJS) - ar r $@ $^ + ar r $@ $(OBJS) install: $(LIB) mkdir -pm755 $(HDRDIR)/zap diff --git a/README.html b/README.html index f460a0a..d924e6d 100644 --- a/README.html +++ b/README.html @@ -5,9 +5,73 @@ <p><i>Note: This library is still in it's early stages and is NOT anywhere near being fully optimised.</i></p> <br /> <h2>Building and installation</h2> - <p>zap uses GNU Make for building.</p> + <p>The provided makefile has been tested to work with both GNU Make and BSD Make.</p> <p>The default target builds the static library file (located at <i>zap/libzap.a</i>). The target <i>clean</i> removes object files, whilst <i>purge</i> removes object files and the static library file.</p> <p>Currently, zap doesn't support being compiled as a shared library out of the box, but the makefile could be modified to allow this.</p> <p>The <i>install</i> target installs the headers to <i>$(HDRDIR)</i> and the library file to <i>$(LIBDIR)</i>.</p> <p>Instructions for building the test program may be found on the first line of <i>test.c</i>.</p> + <h2>Documentation</h2> + <h3>fndbyte</h3> + <h4>Synopsis</h4> + <code>#include <zap/base.h><br />size_t zap_fndbtyte(void const * ptr,size_t num,unsigned char byte);</code> + <h4>Description</h4> + <p>Searches for the byte-value <i>byte</i> in the array pointed to by <i>ptr</i> within the bounds of <i>num</i>.</p> + <p>If <i>num</i> is larger (but not smaller) than the number of bytes in the array, the behaviour is undefined.</p> + <p>If the byte-value is found within the domain, the position of it's first occurrence (starting at zero) is returned. Otherwise <i>SIZE_MAX</i> is returned.</p> + <br /> + <h3>fndchr</h3> + <h4>Synopsis</h4> + <code>#include <zap/base.h><br />size_t zap_fndchr(char const * str,char chr);</code> + <h4>Description</h4> + <p>Searches for the character <i>chr</i> in the string <i>str</i>.</p> + <p>If the character is found in the string, the position of it's first occurrence (starting at zero) is returned. Otherwise <i>SIZE_MAX</i> is returned.</p> + <p>If <i>str</i> is not a valid pointer to a null-terminated string, the behaviour is undefined.</p> + <br /> + <h3>foreach</h3> + <h4>Synopsis</h4> + <code>#include <zap/base.h><br />void zap_foreach(void * ptr,size_t sz,size_t num,void (* fn)(void *));</code> + <h4>Description</h4> + <p>Iterates through the array pointed to by <i>ptr</i>, invoking the function <i>fn</i> with a pointer to the current element. Each pointer is equal to the laster pointer plus <i>sz</i>.</p> + <p>If the expression <code>(sz * num)</code> is not a valid object size, the behaviour is undefined.</p> + <p>If <i>fn</i> is not a valid function pointer, the behaviour is undefined.</p> + <br /> + <h3>memcmp</h3> + <h4>Synopsis</h4> + <code>#include <zap/base.h><br />int_least8_t zap_memcmp(void const * lptr,size_t num,void const * rptr);</code> + <h4>Description</h4> + <p>Compares <i>num</i>-bytes of the arrays pointed to by <i>lptr</i> and <i>rptr</i>.</p> + <p>The returned value is determined by the first byte found to be different in the two arrays. If the byte has a larger value in <i>lptr</i>, a negative (less than zero) value is returned. If it's the other way, a positive (greater than zero) value is returned. Otherwise (the arrays where represented the same), zero is returned.</p> + <p>If <i>lptr</i> or <i>rptr</i> (or both) are not valid pointers to arrays, the behaviour is undefined.</p> + <p>If <i>num</i> is larger (but not smaller) than the number of bytes of the smallest array, the behaviour is undefined.</p> + <br /> + <h3>memdup</h3> + <h4>Synopsis</h4> + <code>#include <zap/base.h><br />void * zap_memdup(void const * ptr,size_t num);</code> + <h4>Description</h4> + <p>Copies <i>num</i>-bytes from the array pointed to by <i>ptr</i> into a newly-allocated array. The new array is allocated by <i>malloc</i>.</p> + <p>The returned value is a pointer to the new array.</p> + <p>If <i>num</i> is larger (but not smaller) than the number of bytes in the array, the behaviour is undefined.</p> + <br /> + <h3>streq</h3> + <h4>Synopsis</h4> + <code>#include <zap/base.h><br />_Bool zap_streq(char const * lstr,char const * rstr);</code> + <h4>Description</h4> + <p>Checks the equality of the strings <i>lstr</i> and <i>rstr</i>.</p> + <p>If one of the strings has a length different from the other, or if any character in the two strings is different from the other (at the same offset), true is returned. Otherwise, false is returned.</p> + <p>If <i>lstr</i> or <i>rstr</i> (or both) are not valid pointers to null-terminated strings, the behaviour is undefined.</p> + <br /> + <h3>strfill</h3> + <h4>Synopsis</h4> + <code>#include <zap/base.h><br />void zap_strfill(char * str,char chr);</code> + <h4>Description</h4> + <p>Writes the character <i>chr</i> to every valid position in the string <i>str</i>, excluding that of the null-terminator.</p> + <p>If <i>str</i> is not a valid pointer to a null-terminated string, the behaviour is undefined.</p> + <br /> + <h3>strlen</h3> + <h4>Synopsis</h4> + <code>#include <zap/base.h><br />size_t zap_strlen(char const * str);</code> + <h4>Description</h4> + <p>Counts the number of characters in the string <i>str</i>.</p> + <p>Returns the number of characters (excluding the null-terminator) in the string.</p> + <p>If <i>str</i> is not a valid pointer to a null-terminated string, the behaviour is undefined.</p> </html>
\ No newline at end of file @@ -99,7 +99,7 @@ int main(void) { pos1 = ::zap_fndbyte(str,len,(::std::uint_least8_t)' '); ::std::fprintf(stderr,"pos0: %zX\n",pos0); ::std::fprintf(stderr,"pos1: %zX\n",pos1); - assert(pos0 == -0x1u); + assert(pos0 == SIZE_MAX); assert(pos1 == pos0); } ::std::fprintf(stderr,"\n"); @@ -156,5 +156,28 @@ int main(void) { assert(cmp5 < 0x0); } ::std::fprintf(stderr,"\n"); + { + int arr[] { + 0x0, + 0x1, + 0x2, + 0x3, + }; + ::zap_foreach(arr,sizeof (arr) / sizeof (int),sizeof (int),[](void * const _ptr) { + auto const ptr {static_cast<int *>(_ptr)}; + auto val {*ptr}; + val %= 0x2; + *ptr = val; + }); + ::std::fprintf(stderr,"arr[0]: %i\n",arr[0x0u]); + ::std::fprintf(stderr,"arr[1]: %i\n",arr[0x1u]); + ::std::fprintf(stderr,"arr[2]: %i\n",arr[0x2u]); + ::std::fprintf(stderr,"arr[3]: %i\n",arr[0x3u]); + assert(arr[0x0u] == 0x0); + assert(arr[0x1u] == 0x1); + assert(arr[0x2u] == 0x0); + assert(arr[0x3u] == 0x1); + } + ::std::fprintf(stderr,"\n"); ::std::fprintf(stderr,"All tests have passed!\n"); } diff --git a/zap/include-priv/zap/priv.h b/zap/include-priv/zap/priv.h index b051589..24718bb 100644 --- a/zap/include-priv/zap/priv.h +++ b/zap/include-priv/zap/priv.h @@ -12,7 +12,7 @@ #include <stddef.h> #include <stdint.h> -#if (defined(sus_cmpl_gnu) || defined(sus_cmpl_llvm)) && defined(sus_os_unix) && !defined(zap_priv_noasm) && (defined(sus_arch_amd64) || defined(sus_arch_ia32)) +#if (defined(sus_comp_gnu) || defined(sus_comp_llvm)) && defined(sus_os_unix) && !defined(zap_priv_noasm) && (defined(sus_arch_amd64) || defined(sus_arch_ia32)) #define zap_priv_fastimpl #endif diff --git a/zap/include/zap/base.h b/zap/include/zap/base.h index f909a45..979a01b 100644 --- a/zap/include/zap/base.h +++ b/zap/include/zap/base.h @@ -12,12 +12,13 @@ #if !defined(zap_hdr_base) #define zap_hdr_base -#define zap_ver ((uint_Least64_t)0xCu) +#define zap_ver ((uint_Least64_t)0xBu) #if defined(sus_lang_asm) .extern zap_fndbyte .extern zap_fndchr +.extern zap_foreach .extern zap_memcmp .extern zap_memcpy .extern zap_memeq @@ -36,13 +37,15 @@ extern "C" { extern bool const zap_fastimpl; -sus_attr_alloc sus_attr_allocsz(0x2) sus_attr_hot sus_attr_nothrw sus_attr_useret void * zap_memdup( void const * ptr, size_t num); -sus_attr_hot sus_attr_nothrw sus_attr_useret size_t zap_fndbyte( void const * ptr, size_t num, uint_least8_t byte); +/* Memory sequence functions: */ +sus_attr_hot sus_attr_nothrw sus_attr_useret size_t zap_fndbyte( void const * ptr, size_t num, unsigned char byte); sus_attr_hot sus_attr_nothrw sus_attr_useret size_t zap_fndchr( char const * str, char chr); +sus_attr_hot sus_attr_nothrw void zap_foreach( void * ptr, size_t sz, size_t num, void (* fn)(void *)); sus_attr_hot sus_attr_nothrw sus_attr_useret int_least8_t zap_memcmp( void const * lstr,size_t num, void const * rstr); sus_attr_hot sus_attr_nothrw void zap_memcpy( void const * in, size_t num, void * out); +sus_attr_alloc sus_attr_allocsz(0x2) sus_attr_hot sus_attr_nothrw sus_attr_useret void * zap_memdup( void const * ptr, size_t num); sus_attr_hot sus_attr_nothrw sus_attr_useret bool zap_memeq( void const * lptr,size_t num, void const * rptr); -sus_attr_hot sus_attr_nothrw void zap_memfill( void * ptr, size_t num, uint_least8_t val); +sus_attr_hot sus_attr_nothrw void zap_memfill( void * ptr, size_t num, unsigned char val); sus_attr_hot sus_attr_nothrw sus_attr_useret int_least8_t zap_strcmp( char const * lstr,char const * rstr); sus_attr_hot sus_attr_nothrw sus_attr_useret size_t zap_strcpy( char const * in, char * out); sus_attr_alloc sus_attr_hot sus_attr_nothrw sus_attr_useret char * zap_strdup( char const * str); diff --git a/zap/src/fndbyte.c b/zap/src/fndbyte.c index 917aeff..3283eec 100644 --- a/zap/src/fndbyte.c +++ b/zap/src/fndbyte.c @@ -11,7 +11,7 @@ #if defined(zap_priv_fastimpl) __asm__ ( - ".global zap_fndbyte\n" + ".globl zap_fndbyte\n" "zap_fndbyte:\n" /* @@ -72,6 +72,6 @@ size_t zap_fndbyte(void const * const _ptr,size_t const _num,uint_least8_t const uint_least8_t const * ptr = (uint_least8_t const *)_ptr; uint_least8_t const * const afterbuf = ptr + _num; for (;ptr != afterbuf;++ptr) {sus_unlikely (*ptr == _byte) {return ptr - (uint_least8_t const *)_ptr;}} - return -0x1u; + return SIZE_MAX; } #endif diff --git a/zap/src/fndchr.c b/zap/src/fndchr.c index 6af1b73..5cf78f8 100644 --- a/zap/src/fndchr.c +++ b/zap/src/fndchr.c @@ -11,7 +11,7 @@ #if defined(zap_priv_fastimpl) __asm__ ( - ".global zap_fndchr\n" + ".globl zap_fndchr\n" "zap_fndchr:\n" /* @@ -64,7 +64,7 @@ size_t zap_fndchr(char const * const _str,char const _chr) { for (;;++pos) { char const chr = *pos; sus_unlikely (chr == _chr) {return (size_t)(pos - _str);} - sus_unlikely (chr == '\x0') {return -0x1u;} + sus_unlikely (chr == '\x0') {return SIZE_MAX;} } sus_unreach(); } diff --git a/zap/src/foreach.c b/zap/src/foreach.c new file mode 100644 index 0000000..54911e9 --- /dev/null +++ b/zap/src/foreach.c @@ -0,0 +1,16 @@ +/* + Copyright 2022 Gabriel Jensen. + This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. + If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/. +*/ + +#include <zap/priv.h> + +#include <stddef.h> + +void zap_foreach(void * const _ptr,size_t const _sz,size_t const _num,void (* const _fn)(void *)) { + unsigned char * ptr = _ptr; + size_t const numbyte = _sz * _num; + void * const afterbuf = ptr + numbyte; + for (;ptr != afterbuf;ptr += _sz) {_fn(ptr);} +} diff --git a/zap/src/memcpy.c b/zap/src/memcpy.c index 1b40832..8fa98ae 100644 --- a/zap/src/memcpy.c +++ b/zap/src/memcpy.c @@ -11,14 +11,14 @@ #if defined(zap_priv_fastimpl) __asm__ ( - ".global zap_memcpy\n" + ".globl zap_memcpy\n" "zap_memcpy:\n" - /* - void const * in - size_t num - void * out - */ + /* + void const * in + size_t num + void * out + */ #if defined(sus_arch_amd64) /* rdi: Address of the current input element. */ /* rsi: Number of remaining elements. */ diff --git a/zap/src/memeq.c b/zap/src/memeq.c index f9717ad..75ecc12 100644 --- a/zap/src/memeq.c +++ b/zap/src/memeq.c @@ -12,7 +12,7 @@ #if defined(zap_priv_fastimpl) __asm__ ( - ".global zap_memeq\n" + ".globl zap_memeq\n" "zap_memeq:\n" /* diff --git a/zap/src/memfill.c b/zap/src/memfill.c index 70982c4..c9a9797 100644 --- a/zap/src/memfill.c +++ b/zap/src/memfill.c @@ -11,7 +11,7 @@ #if defined(zap_priv_fastimpl) __asm__ ( - ".global zap_memfill\n" + ".globl zap_memfill\n" "zap_memfill:\n" /* @@ -24,7 +24,7 @@ __asm__ ( /* rsi: Address of the element after the last element. */ "addq %rdi,%rsi\n" ".loop:\n" - "cmpq %rsi,%rdi\n" + "cmpq %rdi,%rsi\n" "je .done\n" /* Exit loop if we have reached the final element. */ "movb %dl,(%rdi)\n" "incq %rdi\n" diff --git a/zap/src/strcpy.c b/zap/src/strcpy.c index 3dc3e0f..943cb2c 100644 --- a/zap/src/strcpy.c +++ b/zap/src/strcpy.c @@ -10,7 +10,7 @@ #if defined(zap_priv_fastimpl) __asm__ ( - ".global zap_strcpy\n" + ".globl zap_strcpy\n" "zap_strcpy:\n" /* diff --git a/zap/src/streq.c b/zap/src/streq.c index 6425ef7..9221cec 100644 --- a/zap/src/streq.c +++ b/zap/src/streq.c @@ -11,7 +11,7 @@ #if defined(zap_priv_fastimpl) __asm__ ( - ".global zap_streq\n" + ".globl zap_streq\n" "zap_streq:\n" /* diff --git a/zap/src/strlen.c b/zap/src/strlen.c index c1e8959..eab12e6 100644 --- a/zap/src/strlen.c +++ b/zap/src/strlen.c @@ -10,7 +10,7 @@ #if defined(zap_priv_fastimpl) __asm__ ( - ".global zap_strlen\n" + ".globl zap_strlen\n" "zap_strlen:\n" /* |