Skip to content

Commit

Permalink
Fix compilation on mac by conditionally use std::aling_alloc
Browse files Browse the repository at this point in the history
  • Loading branch information
murillo128 committed Jan 30, 2019
1 parent 50f6fbe commit 195aace
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 24 deletions.
17 changes: 16 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
cmake_minimum_required (VERSION 2.8.11)
project (datachannels)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)


find_package(Crc32c REQUIRED)

INCLUDE(CheckCXXSourceCompiles)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17")
check_cxx_source_compiles("
#include <cstdlib>
int main() { int* p = static_cast<int*>(std::aligned_alloc(1024, 1024*sizeof *p)); std::free(p); return 0; }
"
DHAVE_STD_ALIGNED_ALLOC)
if(DHAVE_STD_ALIGNED_ALLOC)
add_compile_options("-DHAVE_STD_ALIGNED_ALLOC")
endif(DHAVE_STD_ALIGNED_ALLOC)

add_library(libdatachannels "")
set_property(TARGET libdatachannels PROPERTY CXX_STANDARD 17)
target_include_directories (libdatachannels PUBLIC ${PROJECT_SOURCE_DIR}/include)

add_subdirectory (src)
add_subdirectory (gtests)

#add_subdirectory (gtests)
1 change: 1 addition & 0 deletions src/Datachannels.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@
#include "sctp/chunks/ShutdownAssociationChunk.cpp"
#include "sctp/chunks/ForwardCumulativeTSNChunk.cpp"
#include "sctp/chunks/UnknownChunk.cpp"
#include "sctp/chunks/PaddingChunk.cpp"


50 changes: 29 additions & 21 deletions src/internal/Buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,35 @@ inline int RoundUp(size_t alignment, size_t size)
class Buffer
{
public:
Buffer(const uint8_t* data, const size_t size)
{
//Set buffer size to a
capacity = RoundUp(64,size);
//Allocate memory
buffer = (uint8_t*) std::aligned_alloc(64, this->capacity);
//Copy
memcpy(buffer,data,size);
//Reset size
this->size = size;
}

Buffer(size_t capacity = 0)
{
//Set buffer size
this->capacity = capacity ? RoundUp(64,capacity) : 0;
//Allocate memory
buffer = capacity ? (uint8_t*) std::aligned_alloc(64, this->capacity) : nullptr;
//NO size
this->size = 0;
}
Buffer(const uint8_t* data, const size_t size)
{
//Set buffer size to a
capacity = RoundUp(64,size);
//Allocate memory
#ifdef HAVE_STD_ALIGNED_ALLOC
buffer = (uint8_t*) std::aligned_alloc(64, this->capacity);
#else
buffer = (uint8_t*) std::malloc(this->capacity);
#endif
//Copy
memcpy(buffer,data,size);
//Reset size
this->size = size;
}

Buffer(size_t capacity = 0)
{
//Set buffer size
this->capacity = capacity ? RoundUp(64,capacity) : 0;
#ifdef HAVE_STD_ALIGNED_ALLOC
//Allocate memory
buffer = capacity ? (uint8_t*) std::aligned_alloc(64, this->capacity) : nullptr;
#else
buffer = capacity ? (uint8_t*) std::malloc(this->capacity) : nullptr;
#endif
//NO size
this->size = 0;
}

Buffer(Buffer &&other)
{
Expand Down
4 changes: 2 additions & 2 deletions src/internal/BufferWritter.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class BufferWritter


template<std::size_t N>
inline size_t Set(size_t i, const std::array<uint8_t,N>& array)
inline void Set(size_t i, const std::array<uint8_t,N>& array)
{
memcpy(data+i,array.data(),N);
}
Expand All @@ -49,7 +49,7 @@ class BufferWritter
memcpy(data+i,buffer.GetData(),buffer.GetSize());
}

inline size_t Set(size_t i, const std::string& string)
inline void Set(size_t i, const std::string& string)
{
memcpy(data+i,string.data(),string.length());
}
Expand Down

0 comments on commit 195aace

Please sign in to comment.