From 281637e849da3b3a98194e52a70c5cd702d7c6a3 Mon Sep 17 00:00:00 2001 From: Michael Clift Date: Fri, 23 Aug 2024 19:24:00 +1000 Subject: [PATCH] cleaned up examples --- docs/strbuf-api.md | 2 +- examples/custom_allocator/Makefile | 20 +-- examples/custom_allocator/build_date.inc | 1 - examples/custom_allocator/build_number.inc | 1 - examples/custom_allocator/custom-alloc.c | 75 +++++++++ examples/custom_allocator/gcc_version.inc | 1 - examples/custom_allocator/version.c | 26 ---- examples/custom_allocator/version.h | 21 --- examples/default_custom_allocator/Makefile | 142 +++++++++++++++++ .../default-custom-alloc.c} | 13 +- examples/default_stdlib_allocator/Makefile | 144 ++++++++++++++++++ .../default-stdlib-alloc.c | 56 +++++++ examples/parse-uri/Makefile | 14 +- examples/parse-uri/build_date.inc | 1 - examples/parse-uri/build_number.inc | 1 - examples/parse-uri/gcc_version.inc | 1 - examples/parse-uri/version.c | 26 ---- examples/parse-uri/version.h | 21 --- examples/stack_buffer/Makefile | 16 +- examples/stack_buffer/build_date.inc | 1 - examples/stack_buffer/build_number.inc | 1 - examples/stack_buffer/gcc_version.inc | 1 - examples/stack_buffer/version.c | 26 ---- examples/stack_buffer/version.h | 20 --- examples/static_buffer/Makefile | 16 +- examples/static_buffer/build_date.inc | 1 - examples/static_buffer/build_number.inc | 1 - examples/static_buffer/gcc_version.inc | 1 - examples/static_buffer/version.c | 26 ---- examples/static_buffer/version.h | 20 --- examples/testnum/.vscode/launch.json | 9 -- examples/testnum/.vscode/tasks.json | 41 ----- examples/testnum/Makefile | 17 +-- examples/testnum/build_date.inc | 1 - examples/testnum/build_number.inc | 1 - examples/testnum/gcc_version.inc | 1 - strbuf.c | 11 +- 37 files changed, 450 insertions(+), 327 deletions(-) delete mode 100644 examples/custom_allocator/build_date.inc delete mode 100644 examples/custom_allocator/build_number.inc create mode 100644 examples/custom_allocator/custom-alloc.c delete mode 100644 examples/custom_allocator/gcc_version.inc delete mode 100644 examples/custom_allocator/version.c delete mode 100644 examples/custom_allocator/version.h create mode 100755 examples/default_custom_allocator/Makefile rename examples/{custom_allocator/heap-buf.c => default_custom_allocator/default-custom-alloc.c} (87%) create mode 100755 examples/default_stdlib_allocator/Makefile create mode 100644 examples/default_stdlib_allocator/default-stdlib-alloc.c delete mode 100644 examples/parse-uri/build_date.inc delete mode 100644 examples/parse-uri/build_number.inc delete mode 100644 examples/parse-uri/gcc_version.inc delete mode 100644 examples/parse-uri/version.c delete mode 100644 examples/parse-uri/version.h delete mode 100644 examples/stack_buffer/build_date.inc delete mode 100644 examples/stack_buffer/build_number.inc delete mode 100644 examples/stack_buffer/gcc_version.inc delete mode 100644 examples/stack_buffer/version.c delete mode 100644 examples/stack_buffer/version.h delete mode 100644 examples/static_buffer/build_date.inc delete mode 100644 examples/static_buffer/build_number.inc delete mode 100644 examples/static_buffer/gcc_version.inc delete mode 100644 examples/static_buffer/version.c delete mode 100644 examples/static_buffer/version.h delete mode 100644 examples/testnum/.vscode/launch.json delete mode 100644 examples/testnum/.vscode/tasks.json delete mode 100644 examples/testnum/build_date.inc delete mode 100644 examples/testnum/build_number.inc delete mode 100644 examples/testnum/gcc_version.inc diff --git a/docs/strbuf-api.md b/docs/strbuf-api.md index 22bbb96..5fe6708 100644 --- a/docs/strbuf-api.md +++ b/docs/strbuf-api.md @@ -74,7 +74,7 @@ As mybuffer is a pointer, members of the strbuf_t may be accessed using the arro # Providing an allocator for strbuf_create(). **strbuf_create()** *may* be passed an allocator. If you just want strbuf_create() to use stdlib's malloc and free, then simply add -DSTRBUF_DEFAULT_ALLOCATOR_STDLIB to your compiler options, and pass a NULL to the allocator parameter of strbuf_create(). If you want to check that stdlib's allocation/resize actually succeeded, you can also add -DSTRBUF_ASSERT_DEFAULT_ALLOCATOR_STDLIB which uses regular assert() to check this. - If you don't want to use stdlib's malloc and free, and also don't want to pass your custom allocator to every occurrence of strbuf_create(), then you can register a default allocator at the start of your application using **strbuf_register_default_allocator(strbuf_allocator_t allocator);** + If you don't want to use stdlib's malloc and free, and also don't want to pass your custom allocator to every occurrence of strbuf_create(), then you can provide a default allocator named **strbuf_default_allocator** (see the examples/ provided) The following __strbuf_allocator_t__ type is defined by __strbuf.h__ diff --git a/examples/custom_allocator/Makefile b/examples/custom_allocator/Makefile index c3762ee..87d7f33 100755 --- a/examples/custom_allocator/Makefile +++ b/examples/custom_allocator/Makefile @@ -1,14 +1,13 @@ #---------------------------------------------------------------------------- -# BEWARE: Messed up by makefile NOOB Michael Clift for Command line applications # # Target file name (without extension). -TARGET = heap-buf +TARGET = custom-alloc # List C source files here. (C dependencies are automatically generated.) # To exclude certain files in a folder remove the $(wildcard) and # list them seperated by spaces, ie src/main.c src/util.c -SRC = heap-buf.c ../../strbuf.c ../../strview.c +SRC = custom-alloc.c ../../strbuf.c ../../strview.c # List any extra directories to look for include files here. # Each directory must be seperated by a space. @@ -85,7 +84,7 @@ GENDEPFLAGS = -MMD -MP -MF .dep/$(@F).d ALL_CFLAGS = -I. $(CFLAGS) $(GENDEPFLAGS) # Default target. -all: begin gccversion buildinfo build end +all: begin gccversion build end build: tgt @@ -99,19 +98,9 @@ begin: @echo $(MSG_BEGIN) end: - @cat build_date.inc @echo $(MSG_END) @echo -# Gather information about build -buildinfo: - @$(CC) --version | grep gcc | awk '{print "\x22" $$0 "\x22"}' > gcc_version.inc - @date --iso-8601=seconds -u | awk '{print "\x22" $$0 "\x22"}' > build_date.inc - @read LASTNUM < build_number.inc; \ - NEWNUM=$$(($$LASTNUM + 1)); \ - echo "$$NEWNUM" > build_number.inc - @touch version.c - # Display compiler version information. gccversion : @$(CC) --version @@ -139,6 +128,7 @@ clean_list : @echo $(MSG_CLEANING) $(REMOVE) $(SRC:%.c=$(OBJLSTDIR)/%.o) $(REMOVE) $(SRC:%.c=$(OBJLSTDIR)/%.lst) + $(REMOVE) $(TARGET) $(REMOVEDIR) .dep # Create object files directory @@ -148,4 +138,4 @@ $(shell mkdir $(OBJLSTDIR) 2>/dev/null) -include $(shell mkdir .dep 2>/dev/null) $(wildcard .dep/*) # Listing of phony targets. -.PHONY : all begin end buildinfo gccversion build tgt clean clean_list +.PHONY : all begin end gccversion build tgt clean clean_list diff --git a/examples/custom_allocator/build_date.inc b/examples/custom_allocator/build_date.inc deleted file mode 100644 index 4fa9c7f..0000000 --- a/examples/custom_allocator/build_date.inc +++ /dev/null @@ -1 +0,0 @@ -"2023-03-20T09:54:43+00:00" diff --git a/examples/custom_allocator/build_number.inc b/examples/custom_allocator/build_number.inc deleted file mode 100644 index 60d3b2f..0000000 --- a/examples/custom_allocator/build_number.inc +++ /dev/null @@ -1 +0,0 @@ -15 diff --git a/examples/custom_allocator/custom-alloc.c b/examples/custom_allocator/custom-alloc.c new file mode 100644 index 0000000..11e7263 --- /dev/null +++ b/examples/custom_allocator/custom-alloc.c @@ -0,0 +1,75 @@ +/* +*/ + + #include + #include + #include + #include + + #include "strbuf.h" + #include "strview.h" + +//******************************************************************************************************** +// Configurable defines +//******************************************************************************************************** + + #define INITIAL_BUF_CAPACITY 16 + +//******************************************************************************************************** +// Local defines +//******************************************************************************************************** + + #define DBG(_fmtarg, ...) printf("%s:%.4i - "_fmtarg"\n" , __FILE__, __LINE__ ,##__VA_ARGS__) + +//******************************************************************************************************** +// Private prototypes +//******************************************************************************************************** + + static void* allocator(struct strbuf_allocator_t* this_allocator, void* ptr_to_free, size_t size); + +//******************************************************************************************************** +// Public functions +//******************************************************************************************************** + +int main(int argc, const char* argv[]) +{ + + strbuf_allocator_t custom_allocator = {.allocator = allocator}; + strbuf_t* buf; + + DBG("Creating buffer with initial capacity of %i", INITIAL_BUF_CAPACITY); + buf = strbuf_create(INITIAL_BUF_CAPACITY, &custom_allocator); + + strbuf_append(&buf, cstr("This ")); + strbuf_append(&buf, cstr("buffer ")); + strbuf_append(&buf, cstr("lives ")); + strbuf_append(&buf, cstr("on ")); + strbuf_append(&buf, cstr("the ")); + strbuf_append(&buf, cstr("heap. ")); + + // This can only be done with dynamic allocation, as it creates a temporary buffer + strbuf_cat(&buf, cstr("Let's say this twice {"), strbuf_view(&buf), strbuf_view(&buf), cstr("}")); + + DBG("%s", buf->cstr); + + strbuf_destroy(&buf); + + return 0; +} + +//******************************************************************************************************** +// Private functions +//******************************************************************************************************** + +static void* allocator(struct strbuf_allocator_t* this_allocator, void* ptr_to_free, size_t size) +{ + (void)this_allocator; + void* result = NULL; + DBG("(Custom allocator called)"); + if(size == 0) + free(ptr_to_free); + else + result = realloc(ptr_to_free, size); + assert(size==0 || result); // You need to catch a failed allocation here. + return result; +} diff --git a/examples/custom_allocator/gcc_version.inc b/examples/custom_allocator/gcc_version.inc deleted file mode 100644 index c14759c..0000000 --- a/examples/custom_allocator/gcc_version.inc +++ /dev/null @@ -1 +0,0 @@ -"gcc (Debian 8.3.0-6) 8.3.0" diff --git a/examples/custom_allocator/version.c b/examples/custom_allocator/version.c deleted file mode 100644 index 5c9c555..0000000 --- a/examples/custom_allocator/version.c +++ /dev/null @@ -1,26 +0,0 @@ -/* -*/ - - #include - #include "version.h" - -//******************************************************************************************************** -// Public variables -//******************************************************************************************************** - - const char version_name[] = "heap-buf example"; - - const uint32_t version_build_number = - #include "build_number.inc" - ; - - const char version_build_date[] = - #include "build_date.inc" - ; - - const char version_gcc_version[] = - #include "gcc_version.inc" - ; - - const uint8_t version_numbers[3] = {0,0,0}; // 0:major 1:minor 2:patch - const char version_string[] = "0,0,0"; diff --git a/examples/custom_allocator/version.h b/examples/custom_allocator/version.h deleted file mode 100644 index f023478..0000000 --- a/examples/custom_allocator/version.h +++ /dev/null @@ -1,21 +0,0 @@ - -//******************************************************************************************************** -// Public defines -//******************************************************************************************************** - - #define VERSION_NAME "ML-BASE" - #define VERSION_NUMBERS 6,0,0 - #define VERSION_STRING "V6.0.0b" - -//******************************************************************************************************** -// Public variables -//******************************************************************************************************** - -// String will be in PROGMEM for AVR platform - extern const char version_name[]; // "Name-of-product" - extern const uint32_t version_build_number; // 1-N - extern const char version_build_date[]; // "2019-06-02T01:38:59+00:00" - extern const uint8_t version_numbers[3]; // 0:major 1:minor 2:patch - extern const char version_string[]; // "V0.0.0" - extern const char version_gcc_version[]; // "gcc (Debian 6.3.0-18+deb9u1) 6.3.0 20170516" (first line of cc --version) - diff --git a/examples/default_custom_allocator/Makefile b/examples/default_custom_allocator/Makefile new file mode 100755 index 0000000..3b59cea --- /dev/null +++ b/examples/default_custom_allocator/Makefile @@ -0,0 +1,142 @@ +#---------------------------------------------------------------------------- +# + +# Target file name (without extension). +TARGET = default-custom-alloc + +# List C source files here. (C dependencies are automatically generated.) +# To exclude certain files in a folder remove the $(wildcard) and +# list them seperated by spaces, ie src/main.c src/util.c +SRC = default-custom-alloc.c ../../strbuf.c ../../strview.c + +# List any extra directories to look for include files here. +# Each directory must be seperated by a space. +# Use forward slashes for directory separators. +# For a directory that has spaces, enclose it in quotes. +EXTRAINCDIRS = . ../.. + +# Object and list files directory +# To put .o and .lst files alongside .c files use a dot (.), do NOT make +# this an empty or blank macro! +# If source files are in sub directories, matching subdirectories must exist under this folder for the .o files +# This is a pain, if you can fix this, please do and share. +OBJLSTDIR = . + +# Compiler flag to set the C Standard level. +# c89 = "ANSI" C +# gnu89 = c89 plus GCC extensions +# c99 = ISO C99 standard (not yet fully implemented) +# gnu99 = c99 plus GCC extensions +CSTANDARD = -std=gnu99 + +# Place -D or -U options here for C sources +CDEFS = -DPLATFORM_PC + +#---------------- Compiler Options C ---------------- +# -g debug information +# -f...: tuning, see GCC manual and avr-libc documentation +# -Wall...: warning level +CFLAGS += $(CDEFS) +CFLAGS += -Wall +CFLAGS += -Wno-unused-function +CFLAGS += -Wno-unused-but-set-variable +CFLAGS += $(CSTANDARD) +CFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS)) + +# List any extra directories to look for libraries here. +# Each directory must be seperated by a space. +# Use forward slashes for directory separators. +# For a directory that has spaces, enclose it in quotes. +EXTRALIBDIRS = . +EXTRALIBS = -lm + +#---------------- Linker Options ---------------- + +LDFLAGS = $(patsubst %,-L%,$(EXTRALIBDIRS)) +LDFLAGS += $(EXTRALIBS) + +#============================================================================ + +# Define programs and commands. +SHELL = sh +CC = gcc +REMOVE = rm -f +REMOVEDIR = rm -rf +COPY = cp + +# Define Messages +# English +MSG_ERRORS_NONE = Errors: none +MSG_BEGIN = -------- begin -------- +MSG_END = -------- end -------- +MSG_LINKING = Linking: +MSG_COMPILING = Compiling C: +MSG_CLEANING = Cleaning project: + +# Define all object files. +OBJ = $(SRC:%.c=$(OBJLSTDIR)/%.o) + +# Compiler flags to generate dependency files. +GENDEPFLAGS = -MMD -MP -MF .dep/$(@F).d + +# Combine all necessary flags and optional flags. +# Add target processor to flags. +ALL_CFLAGS = -I. $(CFLAGS) $(GENDEPFLAGS) + +# Default target. +all: begin gccversion build end + + +build: tgt + +tgt: $(TARGET) + +# Eye candy. +# the following magic strings to be generated by the compile job. +begin: + @echo + @echo $(MSG_BEGIN) + +end: + @echo $(MSG_END) + @echo + +# Display compiler version information. +gccversion : + @$(CC) --version + + +# Link: create output file from object files. +.SECONDARY : $(TARGET) +.PRECIOUS : $(OBJ) +$(TARGET): $(OBJ) + @echo + @echo $(MSG_LINKING) $@ + $(CC) $(ALL_CFLAGS) $^ --output $@ $(LDFLAGS) + +# Compile: create object files from C source files. +$(OBJLSTDIR)/%.o : %.c + @echo + @echo $(MSG_COMPILING) $< + $(CC) -c $(ALL_CFLAGS) $< -o $@ + +# Target: clean project. +clean: begin clean_list end + +clean_list : + @echo + @echo $(MSG_CLEANING) + $(REMOVE) $(SRC:%.c=$(OBJLSTDIR)/%.o) + $(REMOVE) $(SRC:%.c=$(OBJLSTDIR)/%.lst) + $(REMOVE) $(TARGET) + $(REMOVEDIR) .dep + + +# Create object files directory +$(shell mkdir $(OBJLSTDIR) 2>/dev/null) + +# Include the dependency files. +-include $(shell mkdir .dep 2>/dev/null) $(wildcard .dep/*) + +# Listing of phony targets. +.PHONY : all begin end gccversion build tgt clean clean_list diff --git a/examples/custom_allocator/heap-buf.c b/examples/default_custom_allocator/default-custom-alloc.c similarity index 87% rename from examples/custom_allocator/heap-buf.c rename to examples/default_custom_allocator/default-custom-alloc.c index 76054b1..d9816cf 100644 --- a/examples/custom_allocator/heap-buf.c +++ b/examples/default_custom_allocator/default-custom-alloc.c @@ -26,6 +26,7 @@ //******************************************************************************************************** static void* allocator(struct strbuf_allocator_t* this_allocator, void* ptr_to_free, size_t size); + strbuf_allocator_t strbuf_default_allocator = {.allocator = allocator}; //******************************************************************************************************** // Public functions @@ -33,12 +34,10 @@ int main(int argc, const char* argv[]) { - - strbuf_allocator_t str_allocator = {.allocator = allocator}; strbuf_t* buf; DBG("Creating buffer with initial capacity of %i", INITIAL_BUF_CAPACITY); - buf = strbuf_create(INITIAL_BUF_CAPACITY, &str_allocator); + buf = strbuf_create(INITIAL_BUF_CAPACITY, NULL); strbuf_append(&buf, cstr("This ")); strbuf_append(&buf, cstr("buffer ")); @@ -64,8 +63,12 @@ int main(int argc, const char* argv[]) static void* allocator(struct strbuf_allocator_t* this_allocator, void* ptr_to_free, size_t size) { (void)this_allocator; - void* result; - result = realloc(ptr_to_free, size); + void* result = NULL; + DBG("(Custom allocator called)"); + if(size == 0) + free(ptr_to_free); + else + result = realloc(ptr_to_free, size); assert(size==0 || result); // You need to catch a failed allocation here. return result; } diff --git a/examples/default_stdlib_allocator/Makefile b/examples/default_stdlib_allocator/Makefile new file mode 100755 index 0000000..5f86edb --- /dev/null +++ b/examples/default_stdlib_allocator/Makefile @@ -0,0 +1,144 @@ +#---------------------------------------------------------------------------- +# + +# Target file name (without extension). +TARGET = default-stdlib-alloc + +# List C source files here. (C dependencies are automatically generated.) +# To exclude certain files in a folder remove the $(wildcard) and +# list them seperated by spaces, ie src/main.c src/util.c +SRC = default-stdlib-alloc.c ../../strbuf.c ../../strview.c + +# List any extra directories to look for include files here. +# Each directory must be seperated by a space. +# Use forward slashes for directory separators. +# For a directory that has spaces, enclose it in quotes. +EXTRAINCDIRS = . ../.. + +# Object and list files directory +# To put .o and .lst files alongside .c files use a dot (.), do NOT make +# this an empty or blank macro! +# If source files are in sub directories, matching subdirectories must exist under this folder for the .o files +# This is a pain, if you can fix this, please do and share. +OBJLSTDIR = . + +# Compiler flag to set the C Standard level. +# c89 = "ANSI" C +# gnu89 = c89 plus GCC extensions +# c99 = ISO C99 standard (not yet fully implemented) +# gnu99 = c99 plus GCC extensions +CSTANDARD = -std=gnu99 + +# Place -D or -U options here for C sources +CDEFS = -DPLATFORM_PC +CDEFS += -DSTRBUF_DEFAULT_ALLOCATOR_STDLIB +CDEFS += -DSTRBUF_ASSERT_DEFAULT_ALLOCATOR_STDLIB + +#---------------- Compiler Options C ---------------- +# -g debug information +# -f...: tuning, see GCC manual and avr-libc documentation +# -Wall...: warning level +CFLAGS += $(CDEFS) +CFLAGS += -Wall +CFLAGS += -Wno-unused-function +CFLAGS += -Wno-unused-but-set-variable +CFLAGS += $(CSTANDARD) +CFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS)) + +# List any extra directories to look for libraries here. +# Each directory must be seperated by a space. +# Use forward slashes for directory separators. +# For a directory that has spaces, enclose it in quotes. +EXTRALIBDIRS = . +EXTRALIBS = -lm + +#---------------- Linker Options ---------------- + +LDFLAGS = $(patsubst %,-L%,$(EXTRALIBDIRS)) +LDFLAGS += $(EXTRALIBS) + +#============================================================================ + +# Define programs and commands. +SHELL = sh +CC = gcc +REMOVE = rm -f +REMOVEDIR = rm -rf +COPY = cp + +# Define Messages +# English +MSG_ERRORS_NONE = Errors: none +MSG_BEGIN = -------- begin -------- +MSG_END = -------- end -------- +MSG_LINKING = Linking: +MSG_COMPILING = Compiling C: +MSG_CLEANING = Cleaning project: + +# Define all object files. +OBJ = $(SRC:%.c=$(OBJLSTDIR)/%.o) + +# Compiler flags to generate dependency files. +GENDEPFLAGS = -MMD -MP -MF .dep/$(@F).d + +# Combine all necessary flags and optional flags. +# Add target processor to flags. +ALL_CFLAGS = -I. $(CFLAGS) $(GENDEPFLAGS) + +# Default target. +all: begin gccversion build end + + +build: tgt + +tgt: $(TARGET) + +# Eye candy. +# the following magic strings to be generated by the compile job. +begin: + @echo + @echo $(MSG_BEGIN) + +end: + @echo $(MSG_END) + @echo + +# Display compiler version information. +gccversion : + @$(CC) --version + + +# Link: create output file from object files. +.SECONDARY : $(TARGET) +.PRECIOUS : $(OBJ) +$(TARGET): $(OBJ) + @echo + @echo $(MSG_LINKING) $@ + $(CC) $(ALL_CFLAGS) $^ --output $@ $(LDFLAGS) + +# Compile: create object files from C source files. +$(OBJLSTDIR)/%.o : %.c + @echo + @echo $(MSG_COMPILING) $< + $(CC) -c $(ALL_CFLAGS) $< -o $@ + +# Target: clean project. +clean: begin clean_list end + +clean_list : + @echo + @echo $(MSG_CLEANING) + $(REMOVE) $(SRC:%.c=$(OBJLSTDIR)/%.o) + $(REMOVE) $(SRC:%.c=$(OBJLSTDIR)/%.lst) + $(REMOVE) $(TARGET) + $(REMOVEDIR) .dep + + +# Create object files directory +$(shell mkdir $(OBJLSTDIR) 2>/dev/null) + +# Include the dependency files. +-include $(shell mkdir .dep 2>/dev/null) $(wildcard .dep/*) + +# Listing of phony targets. +.PHONY : all begin end gccversion build tgt clean clean_list diff --git a/examples/default_stdlib_allocator/default-stdlib-alloc.c b/examples/default_stdlib_allocator/default-stdlib-alloc.c new file mode 100644 index 0000000..ac79549 --- /dev/null +++ b/examples/default_stdlib_allocator/default-stdlib-alloc.c @@ -0,0 +1,56 @@ +/* +*/ + + #include + #include + #include + #include + + #include "strbuf.h" + #include "strview.h" + +//******************************************************************************************************** +// Configurable defines +//******************************************************************************************************** + + #define INITIAL_BUF_CAPACITY 16 + +//******************************************************************************************************** +// Local defines +//******************************************************************************************************** + + #define DBG(_fmtarg, ...) printf("%s:%.4i - "_fmtarg"\n" , __FILE__, __LINE__ ,##__VA_ARGS__) + +//******************************************************************************************************** +// Private prototypes +//******************************************************************************************************** + + +//******************************************************************************************************** +// Public functions +//******************************************************************************************************** + +int main(int argc, const char* argv[]) +{ + strbuf_t* buf; + + DBG("Creating buffer with initial capacity of %i", INITIAL_BUF_CAPACITY); + buf = strbuf_create(INITIAL_BUF_CAPACITY, NULL); + + strbuf_append(&buf, cstr("This ")); + strbuf_append(&buf, cstr("buffer ")); + strbuf_append(&buf, cstr("lives ")); + strbuf_append(&buf, cstr("on ")); + strbuf_append(&buf, cstr("the ")); + strbuf_append(&buf, cstr("heap. ")); + + // This can only be done with dynamic allocation, as it creates a temporary buffer + strbuf_cat(&buf, cstr("Let's say this twice {"), strbuf_view(&buf), strbuf_view(&buf), cstr("}")); + + DBG("%s", buf->cstr); + + strbuf_destroy(&buf); + + return 0; +} + diff --git a/examples/parse-uri/Makefile b/examples/parse-uri/Makefile index 1f7104b..4d8a964 100755 --- a/examples/parse-uri/Makefile +++ b/examples/parse-uri/Makefile @@ -91,7 +91,7 @@ GENDEPFLAGS = -MMD -MP -MF .dep/$(@F).d ALL_CFLAGS = -I. $(CFLAGS) $(GENDEPFLAGS) # Default target. -all: begin gccversion buildinfo build end +all: begin gccversion build end build: tgt @@ -105,18 +105,9 @@ begin: @echo $(MSG_BEGIN) end: - @cat build_date.inc @echo $(MSG_END) @echo -# Gather information about build -buildinfo: - @$(CC) --version | grep gcc | awk '{print "\x22" $$0 "\x22"}' > gcc_version.inc - @date --iso-8601=seconds -u | awk '{print "\x22" $$0 "\x22"}' > build_date.inc - @read LASTNUM < build_number.inc; \ - NEWNUM=$$(($$LASTNUM + 1)); \ - echo "$$NEWNUM" > build_number.inc - @touch version.c # Display compiler version information. gccversion : @@ -145,6 +136,7 @@ clean_list : @echo $(MSG_CLEANING) $(REMOVE) $(SRC:%.c=$(OBJLSTDIR)/%.o) $(REMOVE) $(SRC:%.c=$(OBJLSTDIR)/%.lst) + $(REMOVE) $(TARGET) $(REMOVEDIR) .dep # Create object files directory @@ -154,4 +146,4 @@ $(shell mkdir $(OBJLSTDIR) 2>/dev/null) -include $(shell mkdir .dep 2>/dev/null) $(wildcard .dep/*) # Listing of phony targets. -.PHONY : all begin end buildinfo gccversion build tgt clean clean_list +.PHONY : all begin end gccversion build tgt clean clean_list diff --git a/examples/parse-uri/build_date.inc b/examples/parse-uri/build_date.inc deleted file mode 100644 index 71780a7..0000000 --- a/examples/parse-uri/build_date.inc +++ /dev/null @@ -1 +0,0 @@ -"2024-03-21T12:02:47+00:00" diff --git a/examples/parse-uri/build_number.inc b/examples/parse-uri/build_number.inc deleted file mode 100644 index aabe6ec..0000000 --- a/examples/parse-uri/build_number.inc +++ /dev/null @@ -1 +0,0 @@ -21 diff --git a/examples/parse-uri/gcc_version.inc b/examples/parse-uri/gcc_version.inc deleted file mode 100644 index c14759c..0000000 --- a/examples/parse-uri/gcc_version.inc +++ /dev/null @@ -1 +0,0 @@ -"gcc (Debian 8.3.0-6) 8.3.0" diff --git a/examples/parse-uri/version.c b/examples/parse-uri/version.c deleted file mode 100644 index a951497..0000000 --- a/examples/parse-uri/version.c +++ /dev/null @@ -1,26 +0,0 @@ -/* -*/ - - #include - #include "version.h" - -//******************************************************************************************************** -// Public variables -//******************************************************************************************************** - - const char version_name[] = "parse-uri"; - - const uint32_t version_build_number = - #include "build_number.inc" - ; - - const char version_build_date[] = - #include "build_date.inc" - ; - - const char version_gcc_version[] = - #include "gcc_version.inc" - ; - - const uint8_t version_numbers[3] = {0,0,0}; // 0:major 1:minor 2:patch - const char version_string[] = "0,0,0"; diff --git a/examples/parse-uri/version.h b/examples/parse-uri/version.h deleted file mode 100644 index f023478..0000000 --- a/examples/parse-uri/version.h +++ /dev/null @@ -1,21 +0,0 @@ - -//******************************************************************************************************** -// Public defines -//******************************************************************************************************** - - #define VERSION_NAME "ML-BASE" - #define VERSION_NUMBERS 6,0,0 - #define VERSION_STRING "V6.0.0b" - -//******************************************************************************************************** -// Public variables -//******************************************************************************************************** - -// String will be in PROGMEM for AVR platform - extern const char version_name[]; // "Name-of-product" - extern const uint32_t version_build_number; // 1-N - extern const char version_build_date[]; // "2019-06-02T01:38:59+00:00" - extern const uint8_t version_numbers[3]; // 0:major 1:minor 2:patch - extern const char version_string[]; // "V0.0.0" - extern const char version_gcc_version[]; // "gcc (Debian 6.3.0-18+deb9u1) 6.3.0 20170516" (first line of cc --version) - diff --git a/examples/stack_buffer/Makefile b/examples/stack_buffer/Makefile index 9075650..3d3ef3c 100755 --- a/examples/stack_buffer/Makefile +++ b/examples/stack_buffer/Makefile @@ -1,5 +1,4 @@ #---------------------------------------------------------------------------- -# BEWARE: Messed up by makefile NOOB Michael Clift for Command line applications # # Target file name (without extension). @@ -85,7 +84,7 @@ GENDEPFLAGS = -MMD -MP -MF .dep/$(@F).d ALL_CFLAGS = -I. $(CFLAGS) $(GENDEPFLAGS) # Default target. -all: begin gccversion buildinfo build end +all: begin gccversion build end build: tgt @@ -99,19 +98,9 @@ begin: @echo $(MSG_BEGIN) end: - @cat build_date.inc @echo $(MSG_END) @echo -# Gather information about build -buildinfo: - @$(CC) --version | grep gcc | awk '{print "\x22" $$0 "\x22"}' > gcc_version.inc - @date --iso-8601=seconds -u | awk '{print "\x22" $$0 "\x22"}' > build_date.inc - @read LASTNUM < build_number.inc; \ - NEWNUM=$$(($$LASTNUM + 1)); \ - echo "$$NEWNUM" > build_number.inc - @touch version.c - # Display compiler version information. gccversion : @$(CC) --version @@ -137,6 +126,7 @@ clean: begin clean_list end clean_list : @echo @echo $(MSG_CLEANING) + $(REMOVE) $(TARGET) $(REMOVE) $(SRC:%.c=$(OBJLSTDIR)/%.o) $(REMOVE) $(SRC:%.c=$(OBJLSTDIR)/%.lst) $(REMOVEDIR) .dep @@ -148,4 +138,4 @@ $(shell mkdir $(OBJLSTDIR) 2>/dev/null) -include $(shell mkdir .dep 2>/dev/null) $(wildcard .dep/*) # Listing of phony targets. -.PHONY : all begin end buildinfo gccversion build tgt clean clean_list +.PHONY : all begin end gccversion build tgt clean clean_list diff --git a/examples/stack_buffer/build_date.inc b/examples/stack_buffer/build_date.inc deleted file mode 100644 index 113a350..0000000 --- a/examples/stack_buffer/build_date.inc +++ /dev/null @@ -1 +0,0 @@ -"2023-04-01T06:51:23+00:00" diff --git a/examples/stack_buffer/build_number.inc b/examples/stack_buffer/build_number.inc deleted file mode 100644 index 4699eb3..0000000 --- a/examples/stack_buffer/build_number.inc +++ /dev/null @@ -1 +0,0 @@ -116 diff --git a/examples/stack_buffer/gcc_version.inc b/examples/stack_buffer/gcc_version.inc deleted file mode 100644 index c14759c..0000000 --- a/examples/stack_buffer/gcc_version.inc +++ /dev/null @@ -1 +0,0 @@ -"gcc (Debian 8.3.0-6) 8.3.0" diff --git a/examples/stack_buffer/version.c b/examples/stack_buffer/version.c deleted file mode 100644 index f81ed18..0000000 --- a/examples/stack_buffer/version.c +++ /dev/null @@ -1,26 +0,0 @@ -/* -*/ - - #include - #include "version.h" - -//******************************************************************************************************** -// Public variables -//******************************************************************************************************** - - const char version_name[] = VERSION_NAME; - - const uint32_t version_build_number = - #include "build_number.inc" - ; - - const char version_build_date[] = - #include "build_date.inc" - ; - - const char version_gcc_version[] = - #include "gcc_version.inc" - ; - - const uint8_t version_numbers[3] = {0,0,0}; // 0:major 1:minor 2:patch - const char version_string[] = "0,0,0"; diff --git a/examples/stack_buffer/version.h b/examples/stack_buffer/version.h deleted file mode 100644 index 3fd4979..0000000 --- a/examples/stack_buffer/version.h +++ /dev/null @@ -1,20 +0,0 @@ - -//******************************************************************************************************** -// Public defines -//******************************************************************************************************** - - #define VERSION_NAME "stack-buf example" - #define VERSION_NUMBERS 0,0,0 - #define VERSION_STRING "V0.0.0" - -//******************************************************************************************************** -// Public variables -//******************************************************************************************************** - - extern const char version_name[]; // "Name-of-product" - extern const uint32_t version_build_number; // 1-N - extern const char version_build_date[]; // "2019-06-02T01:38:59+00:00" - extern const uint8_t version_numbers[3]; // 0:major 1:minor 2:patch - extern const char version_string[]; // "V0.0.0" - extern const char version_gcc_version[]; // "gcc (Debian 6.3.0-18+deb9u1) 6.3.0 20170516" (first line of cc --version) - diff --git a/examples/static_buffer/Makefile b/examples/static_buffer/Makefile index 8324472..05941db 100755 --- a/examples/static_buffer/Makefile +++ b/examples/static_buffer/Makefile @@ -1,5 +1,4 @@ #---------------------------------------------------------------------------- -# BEWARE: Messed up by makefile NOOB Michael Clift for Command line applications # # Target file name (without extension). @@ -85,7 +84,7 @@ GENDEPFLAGS = -MMD -MP -MF .dep/$(@F).d ALL_CFLAGS = -I. $(CFLAGS) $(GENDEPFLAGS) # Default target. -all: begin gccversion buildinfo build end +all: begin gccversion build end build: tgt @@ -99,19 +98,9 @@ begin: @echo $(MSG_BEGIN) end: - @cat build_date.inc @echo $(MSG_END) @echo -# Gather information about build -buildinfo: - @$(CC) --version | grep gcc | awk '{print "\x22" $$0 "\x22"}' > gcc_version.inc - @date --iso-8601=seconds -u | awk '{print "\x22" $$0 "\x22"}' > build_date.inc - @read LASTNUM < build_number.inc; \ - NEWNUM=$$(($$LASTNUM + 1)); \ - echo "$$NEWNUM" > build_number.inc - @touch version.c - # Display compiler version information. gccversion : @$(CC) --version @@ -137,6 +126,7 @@ clean: begin clean_list end clean_list : @echo @echo $(MSG_CLEANING) + $(REMOVE) $(TARGET) $(REMOVE) $(SRC:%.c=$(OBJLSTDIR)/%.o) $(REMOVE) $(SRC:%.c=$(OBJLSTDIR)/%.lst) $(REMOVEDIR) .dep @@ -148,4 +138,4 @@ $(shell mkdir $(OBJLSTDIR) 2>/dev/null) -include $(shell mkdir .dep 2>/dev/null) $(wildcard .dep/*) # Listing of phony targets. -.PHONY : all begin end buildinfo gccversion build tgt clean clean_list +.PHONY : all begin end gccversion build tgt clean clean_list diff --git a/examples/static_buffer/build_date.inc b/examples/static_buffer/build_date.inc deleted file mode 100644 index 16de631..0000000 --- a/examples/static_buffer/build_date.inc +++ /dev/null @@ -1 +0,0 @@ -"2023-04-01T06:51:03+00:00" diff --git a/examples/static_buffer/build_number.inc b/examples/static_buffer/build_number.inc deleted file mode 100644 index c8b255f..0000000 --- a/examples/static_buffer/build_number.inc +++ /dev/null @@ -1 +0,0 @@ -135 diff --git a/examples/static_buffer/gcc_version.inc b/examples/static_buffer/gcc_version.inc deleted file mode 100644 index c14759c..0000000 --- a/examples/static_buffer/gcc_version.inc +++ /dev/null @@ -1 +0,0 @@ -"gcc (Debian 8.3.0-6) 8.3.0" diff --git a/examples/static_buffer/version.c b/examples/static_buffer/version.c deleted file mode 100644 index f81ed18..0000000 --- a/examples/static_buffer/version.c +++ /dev/null @@ -1,26 +0,0 @@ -/* -*/ - - #include - #include "version.h" - -//******************************************************************************************************** -// Public variables -//******************************************************************************************************** - - const char version_name[] = VERSION_NAME; - - const uint32_t version_build_number = - #include "build_number.inc" - ; - - const char version_build_date[] = - #include "build_date.inc" - ; - - const char version_gcc_version[] = - #include "gcc_version.inc" - ; - - const uint8_t version_numbers[3] = {0,0,0}; // 0:major 1:minor 2:patch - const char version_string[] = "0,0,0"; diff --git a/examples/static_buffer/version.h b/examples/static_buffer/version.h deleted file mode 100644 index 3ed7f20..0000000 --- a/examples/static_buffer/version.h +++ /dev/null @@ -1,20 +0,0 @@ - -//******************************************************************************************************** -// Public defines -//******************************************************************************************************** - - #define VERSION_NAME "static-buf example" - #define VERSION_NUMBERS 0,0,0 - #define VERSION_STRING "V0.0.0" - -//******************************************************************************************************** -// Public variables -//******************************************************************************************************** - - extern const char version_name[]; // "Name-of-product" - extern const uint32_t version_build_number; // 1-N - extern const char version_build_date[]; // "2019-06-02T01:38:59+00:00" - extern const uint8_t version_numbers[3]; // 0:major 1:minor 2:patch - extern const char version_string[]; // "V0.0.0" - extern const char version_gcc_version[]; // "gcc (Debian 6.3.0-18+deb9u1) 6.3.0 20170516" (first line of cc --version) - diff --git a/examples/testnum/.vscode/launch.json b/examples/testnum/.vscode/launch.json deleted file mode 100644 index 0fa7ef7..0000000 --- a/examples/testnum/.vscode/launch.json +++ /dev/null @@ -1,9 +0,0 @@ -{ - "version": "0.2.0", - "configurations": [ - - { - "preLaunchTask": "clear-editor-history" - } - ] -} \ No newline at end of file diff --git a/examples/testnum/.vscode/tasks.json b/examples/testnum/.vscode/tasks.json deleted file mode 100644 index a4aee70..0000000 --- a/examples/testnum/.vscode/tasks.json +++ /dev/null @@ -1,41 +0,0 @@ -{ - "version": "2.0.0", - "tasks": [ - { - "label": "make", - "type": "shell", - "command": "make", - "group": { - "kind": "build", - "isDefault": true - }, - "presentation": { - "reveal": "always", - "panel": "new" - }, - "problemMatcher": [ - "$gcc" - ] - }, - { - "label": "burn", - "type": "shell", - "command": "./burn.sh", - "group": { - "kind" : "test", - "isDefault": true - }, - "presentation": { - "reveal": "always", - "panel": "new" - }, - "problemMatcher": [] - }, - { - "label": "clear-editor-history", - "command": "${command:workbench.action.clearEditorHistory}" - } - ] - } - - diff --git a/examples/testnum/Makefile b/examples/testnum/Makefile index 4eeb12c..bae155a 100755 --- a/examples/testnum/Makefile +++ b/examples/testnum/Makefile @@ -1,5 +1,4 @@ #---------------------------------------------------------------------------- -# BEWARE: Messed up by makefile NOOB Michael Clift for Command line applications # # Target file name (without extension). @@ -91,7 +90,7 @@ GENDEPFLAGS = -MMD -MP -MF .dep/$(@F).d ALL_CFLAGS = -I. $(CFLAGS) $(GENDEPFLAGS) # Default target. -all: begin gccversion buildinfo build end +all: begin gccversion build end build: tgt @@ -105,24 +104,13 @@ begin: @echo $(MSG_BEGIN) end: - @cat build_date.inc @echo $(MSG_END) @echo -# Gather information about build -buildinfo: - @$(CC) --version | grep gcc | awk '{print "\x22" $$0 "\x22"}' > gcc_version.inc - @date --iso-8601=seconds -u | awk '{print "\x22" $$0 "\x22"}' > build_date.inc - @read LASTNUM < build_number.inc; \ - NEWNUM=$$(($$LASTNUM + 1)); \ - echo "$$NEWNUM" > build_number.inc -# @touch version.c - # Display compiler version information. gccversion : @$(CC) --version - # Link: create output file from object files. .SECONDARY : $(TARGET) .PRECIOUS : $(OBJ) @@ -145,6 +133,7 @@ clean_list : @echo $(MSG_CLEANING) $(REMOVE) $(SRC:%.c=$(OBJLSTDIR)/%.o) $(REMOVE) $(SRC:%.c=$(OBJLSTDIR)/%.lst) + $(REMOVE) $(TARGET) $(REMOVEDIR) .dep # Create object files directory @@ -154,4 +143,4 @@ $(shell mkdir $(OBJLSTDIR) 2>/dev/null) -include $(shell mkdir .dep 2>/dev/null) $(wildcard .dep/*) # Listing of phony targets. -.PHONY : all begin end buildinfo gccversion build tgt clean clean_list +.PHONY : all begin end gccversion build tgt clean clean_list diff --git a/examples/testnum/build_date.inc b/examples/testnum/build_date.inc deleted file mode 100644 index 2914a31..0000000 --- a/examples/testnum/build_date.inc +++ /dev/null @@ -1 +0,0 @@ -"2023-04-01T06:56:07+00:00" diff --git a/examples/testnum/build_number.inc b/examples/testnum/build_number.inc deleted file mode 100644 index bb7a7c1..0000000 --- a/examples/testnum/build_number.inc +++ /dev/null @@ -1 +0,0 @@ -576 diff --git a/examples/testnum/gcc_version.inc b/examples/testnum/gcc_version.inc deleted file mode 100644 index c14759c..0000000 --- a/examples/testnum/gcc_version.inc +++ /dev/null @@ -1 +0,0 @@ -"gcc (Debian 8.3.0-6) 8.3.0" diff --git a/strbuf.c b/strbuf.c index bd197f7..48ab526 100644 --- a/strbuf.c +++ b/strbuf.c @@ -64,8 +64,11 @@ static void* allocfunc_stdlib(struct strbuf_allocator_t* this_allocator, void* ptr_to_free, size_t size) { (void)this_allocator; - void* result; - result = realloc(ptr_to_free, size); + void* result = NULL; + if(size == 0) + free(ptr_to_free); + else + result = realloc(ptr_to_free, size); #ifdef STRBUF_ASSERT_DEFAULT_ALLOCATOR_STDLIB assert(size==0 || result); #endif @@ -73,8 +76,8 @@ static void* allocfunc_stdlib(struct strbuf_allocator_t* this_allocator, void* p } strbuf_allocator_t strbuf_default_allocator = (strbuf_allocator_t){.allocator=allocfunc_stdlib, .app_data=NULL}; #else - strbuf_allocator_t weak_default_allocator = (strbuf_allocator_t){.allocator=NULL, .app_data=NULL}; - #pragma weak strbuf_default_allocator = weak_default_allocator + #pragma weak strbuf_default_allocator + strbuf_allocator_t strbuf_default_allocator = (strbuf_allocator_t){.allocator=NULL, .app_data=NULL}; #endif strbuf_t* strbuf_create_empty(size_t initial_capacity, strbuf_allocator_t* allocator)