Skip to content

Commit

Permalink
rust support - Native bstrings (#160)
Browse files Browse the repository at this point in the history
Backport changes from pelikan to twitter/ccommon#186

Implement proper allocation/free for BString objects

* All allocation of BStrings should happen from libc malloc() that way
  we don't mistakenly free something allocated from rust's allocator.

* Drop should call libc's free() with the pointer, doing the correct
  thing.

* This philosophy allows us to pass bstring pointers back and forth and
  not have to track which heap they're allocated on

* Provide an implementation of a zero-cost reference object, much like
  String -> &str, we have BString -> &BStr.
  • Loading branch information
slyphon authored Jul 19, 2018
1 parent b3de2ee commit 1fe907e
Show file tree
Hide file tree
Showing 17 changed files with 1,159 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,5 @@ tags
# cscope
cscope.*
lcov

CMAKE_BINARY_DIR
8 changes: 7 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ option(HAVE_ASSERT_PANIC "assert_panic disabled by default" OFF)
option(HAVE_LOGGING "logging enabled by default" ON)
option(HAVE_STATS "stats enabled by default" ON)
option(HAVE_DEBUG_MM "debugging oriented memory management disabled by default" OFF)
option(HAVE_RUST "build rust components" OFF)
option(COVERAGE "code coverage" OFF)
option(HAVE_RUST "rust bindings not built by default" OFF)

if(BUILD_AND_INSTALL_CHECK)
# (simms) What follows is a crime against build systems as we run the build/install
Expand Down Expand Up @@ -123,6 +123,10 @@ set(CFLAGS_LIST
string(REPLACE "" "" CFLAGS ${CFLAGS_LIST})
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${CFLAGS}")

if(CMAKE_COMPILER_IS_GNUCC)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -Wl,--no-as-needed -ldl -pthread -fPIC")
endif()

if (COVERAGE)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -g -O0 -Wall -W -fprofile-arcs -ftest-coverage")
endif(COVERAGE)
Expand All @@ -147,8 +151,10 @@ find_package(Threads)
if(HAVE_RUST)
enable_language(Rust)
include(CMakeCargo)
add_subdirectory(rust)
endif()


# where to find include files
include_directories(${include_directories}
"${PROJECT_BINARY_DIR}"
Expand Down
3 changes: 3 additions & 0 deletions include/cc_bstring.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ rstatus_i bstring_duplicate(struct bstring *dst, const struct bstring *src);
rstatus_i bstring_copy(struct bstring *dst, const char *src, uint32_t srclen);
int bstring_compare(const struct bstring *s1, const struct bstring *s2);

struct bstring *bstring_alloc(uint32_t size);
void bstring_free(struct bstring **bstring);

/* TODO(yao): is this endian thing really useful? */
/* efficient implementation of string comparion of short strings */
#define str2cmp(m, c0, c1) \
Expand Down
5 changes: 5 additions & 0 deletions rust/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
file(WRITE CMAKE_BINARY_DIR "${CMAKE_BINARY_DIR}\n")

if(HAVE_RUST)
add_subdirectory(ccommon_rs)
endif()
Loading

0 comments on commit 1fe907e

Please sign in to comment.