Skip to content

Commit

Permalink
cleaned up examples
Browse files Browse the repository at this point in the history
  • Loading branch information
mickjc750 committed Aug 23, 2024
1 parent aab5067 commit 281637e
Show file tree
Hide file tree
Showing 37 changed files with 450 additions and 327 deletions.
2 changes: 1 addition & 1 deletion docs/strbuf-api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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__

Expand Down
20 changes: 5 additions & 15 deletions examples/custom_allocator/Makefile
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
1 change: 0 additions & 1 deletion examples/custom_allocator/build_date.inc

This file was deleted.

1 change: 0 additions & 1 deletion examples/custom_allocator/build_number.inc

This file was deleted.

75 changes: 75 additions & 0 deletions examples/custom_allocator/custom-alloc.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
*/

#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include <stdbool.h>

#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;
}
1 change: 0 additions & 1 deletion examples/custom_allocator/gcc_version.inc

This file was deleted.

26 changes: 0 additions & 26 deletions examples/custom_allocator/version.c

This file was deleted.

21 changes: 0 additions & 21 deletions examples/custom_allocator/version.h

This file was deleted.

142 changes: 142 additions & 0 deletions examples/default_custom_allocator/Makefile
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,18 @@
//********************************************************************************************************

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
//********************************************************************************************************

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 "));
Expand All @@ -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;
}
Loading

0 comments on commit 281637e

Please sign in to comment.