-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
37 changed files
with
450 additions
and
327 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
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,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; | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,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 |
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
Oops, something went wrong.