diff --git a/Makefile b/Makefile index a411c8b..cf89ba5 100644 --- a/Makefile +++ b/Makefile @@ -21,21 +21,29 @@ OPT_FLAGS?= $(addprefix -march=,$(TARGET_CPU)) -fgraphite -fopenmp -floop-parall -floop-interchange -ftree-loop-distribution -floop-strip-mine -floop-block \ -fno-stack-check -CXX_OPT_FLAGS?= $(OPT_FLAGS) -felide-constructors +CXX_OPT_FLAGS?= $(OPT_FLAGS) CFLAGS += $(COMMON_FLAGS) --std=gnu11 -CXXFLAGS += $(COMMON_FLAGS) --std=gnu++20 #-fno-exceptions +CXXFLAGS += $(COMMON_FLAGS) --std=gnu++20 -felide-constructors LDFLAGS += STRIP=strip -RELEASE_CFLAGS?= -O3 -flto $(OPT_FLAGS) -RELEASE_CXXFLAGS?= -O3 -flto $(CXX_OPT_FLAGS) -RELEASE_LDFLAGS?= -O3 -flto +ifneq ($(TARGET_SPEC_FLAGS),no) + RELEASE_CFLAGS?= -O3 -flto $(OPT_FLAGS) + RELEASE_CXXFLAGS?= -O3 -flto $(CXX_OPT_FLAGS) + RELEASE_LDFLAGS?= -O3 -flto -DEBUG_CFLAGS?= -O0 -g -DDEBUG -DEBUG_CXXFLAGS?=-O0 -g -DDEBUG -DEBUG_LDFLAGS?= + DEBUG_CFLAGS?= -O0 -g + DEBUG_CXXFLAGS?=-O0 -g + DEBUG_LDFLAGS?= +endif + +DEBUG_CFLAGS+=-DDEBUG +DEBUG_CXXFLAGS+=-DDEBUG + +RELEASE_CFLAGS+=-DRELEASE +RELEASE_CXXFLAGS+=-DRELEASE # Objects diff --git a/README.md b/README.md index 9edd796..ddccd50 100644 --- a/README.md +++ b/README.md @@ -3,10 +3,17 @@ Automatic copy-on-write semantic memory slices for use in C (and C++) # Usage See `include/cow.h` for documentation on each function. + +## C API Each function, macro, and type definition in the header will be prefixed with `cow_` or `COW_`. Internal non-prototpyed items use the namespace `_cow_` or `_COW_`. +### C++ wrapper API The C++ interface defines the type `Cow`, a reference-counted wrapper over `cow_t` instances that supports cloning through its subtype, `Cow::Fake`, and automatically ensures the originally created `cow_t` is not destroyed until all its clones are, as well as the namespace `_cow_util` which contains memory accessor helpers `Span` and `Slice` (aka `Span::Slice`). +There are also the following: + * `cow/area.hpp` (namespace `_cow_util`) - The `Area` type is a copy-constructable wrapper around *both* `Cow` and `Cow::Fake`, allowing for implicit cloning. + * `cow/slice.hpp` (namespace `_cow_util`) - Contains the definitions for `Span` and `Slice`. Included automatically by `cow.hpp` (*see above*). + ## Building Run `make` to build to build the `release` (optimised) target of the library. It will create four files: `libcow-release.a`, `libcow-release.so`, `libcow.a`, and `libcow.so`. @@ -18,6 +25,18 @@ It will create two files: `libcow-debug.a` and `libcow-debug.so`. Each target compiles both a static and dynamic library. You may need to run `make clean` before switching build targets. To build both targets, run `make all`. +To disable default target-specific (e.g. optimisation) flags, set `TARGET_SPEC_FLAGS=no` when running `make`. + +Run `sudo make install` to install the libraries (static and dynamic) and header files (C and C++). +Run `sudo make uninstall` to remove the libraries and header files. + +By default, the install target is `/usr/local/`. Set the `PREFIX` variable when running `make install` / `make uninstall` to specify a different path. + +### Full build and installation +```shell +$ make && sudo make install +``` + ### Notes * The `release` target specifies `-march=native` by default. This may be undesirable, if so, run `make MARCH="" release` instead. * Many optimisation flags for the `release` configuration are specific to GCC (with graphite enabled by default), if builds on other compilers (or non-graphite enabled GCC builds) complain, either set the `OPT_FLAGS` env var or remove the problem flags from the Makefile. diff --git a/include/cow/area.hpp b/include/cow/area.hpp index baeebf2..9bc26e4 100644 --- a/include/cow/area.hpp +++ b/include/cow/area.hpp @@ -6,30 +6,32 @@ #include -struct Area { - Area() = delete; - - explicit Area(size_t sz); - Area(const Area& area); - Area(Area&& area); +namespace _cow_util { + struct Area { + Area() = delete; + + explicit Area(size_t sz); + Area(const Area& area); + Area(Area&& area); - Area(Cow&& move); - explicit Area(const Cow& copy); + Area(Cow&& move); + explicit Area(const Cow& copy); - inline const Cow* operator->() const { return _area.get(); } - inline Cow* operator->() { return _area.get(); } + inline const Cow* operator->() const { return _area.get(); } + inline Cow* operator->() { return _area.get(); } - inline const Cow* operator*() const { return _area.get(); } - inline Cow* operator*() { return _area.get(); } + inline const Cow* operator*() const { return _area.get(); } + inline Cow* operator*() { return _area.get(); } - inline operator const Cow&() const { return *_area.get(); } - inline operator Cow&() { return *_area.get(); } + inline operator const Cow&() const { return *_area.get(); } + inline operator Cow&() { return *_area.get(); } - inline bool is_clone() const { return dynamic_cast(_area.get()) != nullptr; } + inline bool is_clone() const { return dynamic_cast(_area.get()) != nullptr; } - inline cow_t* raw() const { return _area->raw(); } + inline cow_t* raw() const { return _area->raw(); } - ~Area(); - private: - const std::unique_ptr _area; -}; + ~Area(); + private: + const std::unique_ptr _area; + }; +} diff --git a/src/area.cpp b/src/area.cpp index 730763c..85cd7a8 100644 --- a/src/area.cpp +++ b/src/area.cpp @@ -1,13 +1,16 @@ #include -Area::Area(size_t sz) : _area(std::make_unique(sz)){} -Area::Area(const Area& copy) : - _area(std::make_unique(*copy._area.get())){} -Area::Area(Area&& move) : - _area(std::move(*const_cast*>(&move._area))){} -Area::~Area(){} +namespace _cow_util { + Area::Area(size_t sz) : _area(std::make_unique(sz)){} + Area::Area(const Area& copy) : + _area(std::make_unique(*copy._area.get())){} + Area::Area(Area&& move) : + _area(std::move(*const_cast*>(&move._area))){} + Area::~Area(){} -Area::Area(Cow&& r) : - _area(std::make_unique(std::move(r))){} -Area::Area(const Cow& r) : - _area(std::make_unique(r.clone())){} + Area::Area(Cow&& r) : + _area(std::make_unique(std::move(r))){} + Area::Area(const Cow& r) : + _area(std::make_unique(r.clone())){} + +}