parent
8b6ab8f7cb
commit
a1c8b96abd
@ -1 +1,2 @@
|
||||
cow
|
||||
cow-*
|
||||
obj/
|
||||
|
@ -0,0 +1,78 @@
|
||||
# Generic C and C++ Makefile project template
|
||||
# Contains targets for `release', `debug', and `clean'.
|
||||
|
||||
PROJECT=cow
|
||||
AUTHOR=Avril (Flanchan) <flanchan@cumallover.me>
|
||||
|
||||
SRC_C = $(wildcard src/*.c)
|
||||
SRC_CXX = $(wildcard src/*.cpp)
|
||||
|
||||
INCLUDE=include
|
||||
|
||||
COMMON_FLAGS= -W -Wall -pedantic -fno-strict-aliasing $(addprefix -I,$(INCLUDE))
|
||||
|
||||
MARCH?=native
|
||||
OPT_FLAGS?= $(addprefix -march=,$(MARCH)) -fgraphite -fopenmp -floop-parallelize-all -ftree-parallelize-loops=4 \
|
||||
-floop-interchange -ftree-loop-distribution -floop-strip-mine -floop-block \
|
||||
-fno-stack-check
|
||||
|
||||
CXX_OPT_FLAGS?= $(OPT_FLAGS) -felide-constructors
|
||||
|
||||
CFLAGS += $(COMMON_FLAGS) --std=gnu11
|
||||
CXXFLAGS += $(COMMON_FLAGS) --std=gnu++20 -fno-exceptions
|
||||
LDFLAGS +=
|
||||
|
||||
STRIP=strip
|
||||
|
||||
RELEASE_CFLAGS?= -O3 -flto $(OPT_FLAGS)
|
||||
RELEASE_CXXFLAGS?= -O3 -flto $(CXX_OPT_FLAGS)
|
||||
RELEASE_LDFLAGS?= -O3 -flto
|
||||
|
||||
DEBUG_CFLAGS?= -O0 -g -DDEBUG
|
||||
DEBUG_CXXFLAGS?= $(DEBUG_CFLAGS)
|
||||
DEBUG_LDFLAGS?=
|
||||
|
||||
# Objects
|
||||
|
||||
OBJ_C = $(addprefix obj/c/,$(SRC_C:.c=.o))
|
||||
OBJ_CXX = $(addprefix obj/cxx/,$(SRC_CXX:.cpp=.o))
|
||||
OBJ = $(OBJ_C) $(OBJ_CXX)
|
||||
|
||||
# Phonies
|
||||
|
||||
.PHONY: release
|
||||
release: | dirs $(PROJECT)-release
|
||||
|
||||
.PHONY: debug
|
||||
debug: | dirs $(PROJECT)-debug
|
||||
|
||||
# Targets
|
||||
|
||||
dirs:
|
||||
@mkdir -p obj/c{,xx}/src
|
||||
|
||||
obj/c/%.o: %.c
|
||||
$(CC) -c $< $(CFLAGS) -o $@ $(LDFLAGS)
|
||||
|
||||
obj/cxx/%.o: %.cpp
|
||||
$(CXX) -c $< $(CXXFLAGS) -o $@ $(LDFLAGS)
|
||||
|
||||
$(PROJECT)-release: CFLAGS+= $(RELEASE_CFLAGS)
|
||||
$(PROJECT)-release: CXXFLAGS += $(RELEASE_CXXFLAGS)
|
||||
$(PROJECT)-release: LDFLAGS += $(RELEASE_LDFLAGS)
|
||||
$(PROJECT)-release: $(OBJ)
|
||||
$(CXX) $^ $(CXXFLAGS) -o $@ $(LDFLAGS)
|
||||
$(STRIP) $@
|
||||
|
||||
$(PROJECT)-debug: CFLAGS+= $(DEBUG_CFLAGS)
|
||||
$(PROJECT)-debug: CXXFLAGS += $(DEBUG_CXXFLAGS)
|
||||
$(PROJECT)-debug: LDFLAGS += $(DEBUG_LDFLAGS)
|
||||
$(PROJECT)-debug: $(OBJ)
|
||||
$(CXX) $^ $(CXXFLAGS) -o $@ $(LDFLAGS)
|
||||
|
||||
clean-rebuild:
|
||||
rm -rf obj
|
||||
|
||||
clean: clean-rebuild
|
||||
rm -f $(PROJECT)-{release,debug,pgo}
|
||||
|
@ -0,0 +1,19 @@
|
||||
#ifndef _COW_H
|
||||
#define _COW_H
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
// Copy-on-write mapped memory.
|
||||
typedef struct cow cow_t, *cow;
|
||||
|
||||
cow_t* cow_create(size_t size);
|
||||
void cow_free(cow_t* restrict cow);
|
||||
cow_t* cow_clone(const cow_t* cow);
|
||||
|
||||
int cow_is_fake(const cow_t* cow);
|
||||
size_t cow_size(const cow_t* cow);
|
||||
|
||||
#define cow_ptr(v) (*((void**)(v)))
|
||||
#define cow_ptr_of(t, v) (*((t **)(v)))
|
||||
|
||||
#endif /* _COW_H */
|
@ -0,0 +1,24 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include <cow.h>
|
||||
|
||||
#define SIZE 4096
|
||||
|
||||
int main()
|
||||
{
|
||||
cow_t* origin = cow_create(SIZE);
|
||||
|
||||
strcpy(cow_ptr(origin), "Hello world");
|
||||
cow_t* fake = cow_clone(origin);
|
||||
printf("Fake (pre write): %s\n", (const char*)cow_ptr(fake));
|
||||
strcpy(cow_ptr(fake), "Hello fake!");
|
||||
|
||||
printf("Real: %s\n", (const char*)cow_ptr(origin));
|
||||
printf("Fake: %s\n", (const char*)cow_ptr(fake));
|
||||
cow_free(fake);
|
||||
cow_free(origin);
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Reference in new issue