Skip to content

Commit

Permalink
Merge branch 'release/1.1.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
Leonardo committed Feb 21, 2024
2 parents 7bc2645 + 9e1df22 commit be70313
Show file tree
Hide file tree
Showing 11 changed files with 227 additions and 104 deletions.
140 changes: 129 additions & 11 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,20 +1,138 @@
cmake_minimum_required(VERSION 3.26)
project(naughty-buffers LANGUAGES C VERSION 1.1.0)
include(CTest)
project(naughty-buffers LANGUAGES C VERSION 1.1.1)

set(CMAKE_C_STANDARD 99)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(CMAKE_C_EXTENSIONS OFF)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_C_VISIBILITY_PRESET hidden)
set(CMAKE_CXX_VISIBILITY_PRESET hidden)
set(CMAKE_VISIBILITY_INLINES_HIDDEN YES)
include(GNUInstallDirs)
include(GenerateExportHeader)
include(CMakePackageConfigHelpers)

add_subdirectory(src)
set(
CMAKE_C_STANDARD 99
CMAKE_POSITION_INDEPENDENT_CODE ON
CMAKE_C_EXTENSIONS OFF
CMAKE_CXX_EXTENSIONS OFF
CMAKE_C_VISIBILITY_PRESET hidden
CMAKE_CXX_VISIBILITY_PRESET hidden
)

set(NAUGHTY_BUFFERS_PUBLIC_HEADERS
include/naughty-buffers/buffer.h
include/naughty-buffers/array-generator.h
${CMAKE_CURRENT_BINARY_DIR}/naughty-buffers/naughty-buffers-export.h
)

add_library(naughty-buffers-objects OBJECT
src/naughty-buffers/buffer.c
src/naughty-buffers/memory.h
src/naughty-buffers/memory.c
${NAUGHTY_BUFFERS_PUBLIC_HEADERS}
)

target_include_directories(naughty-buffers-objects PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>
$<INSTALL_INTERFACE:include>
)

set_property(TARGET naughty-buffers-objects PROPERTY POSITION_INDEPENDENT_CODE 1)

# shared version
add_library(naughty-buffers SHARED $<TARGET_OBJECTS:naughty-buffers-objects>)
add_library(naughty-buffers::naughty-buffers ALIAS naughty-buffers)

generate_export_header(naughty-buffers
BASE_NAME naughty-buffers
EXPORT_FILE_NAME naughty-buffers/naughty-buffers-export.h
)

set_target_properties(naughty-buffers PROPERTIES
VERSION ${naughty-buffers_VERSION}
SO_VERSION ${naughty-buffers_VERSION_MAJOR}
PUBLIC_HEADER "${NAUGHTY_BUFFERS_PUBLIC_HEADERS}"
OUTPUT_NAME "naughty-buffers"
RUNTIME_OUTPUT_NAME naughty-buffers-${naughty-buffers_VERSION}
)

target_include_directories(naughty-buffers PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>
$<INSTALL_INTERFACE:include>
)

# static vertsion

add_library(naughty-buffers-static STATIC $<TARGET_OBJECTS:naughty-buffers-objects>)
add_library(naughty-buffers::naughty-buffers-static ALIAS naughty-buffers-static)

set_target_properties(naughty-buffers-static PROPERTIES
VERSION ${naughty-buffers_VERSION}
SO_VERSION ${naughty-buffers_VERSION_MAJOR}
PUBLIC_HEADER "${NAUGHTY_BUFFERS_PUBLIC_HEADERS}"
OUTPUT_NAME "naughty-buffers-static"
RUNTIME_OUTPUT_NAME naughty-buffers-${naughty-buffers_VERSION}-static
)

target_include_directories(naughty-buffers-static PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}>
$<INSTALL_INTERFACE:include>
)

if (NOT CMAKE_BUILD_TYPE MATCHES "Release")
target_compile_options(naughty-buffers-objects PRIVATE
$<$<OR:$<C_COMPILER_ID:Clang>,$<C_COMPILER_ID:GNU>>:-Wall -Wextra -pedantic -fvisibility=hidden -Werror>
$<$<C_COMPILER_ID:MSVC>:/W3 /WX /wd4820 /wd4668 /wd4204>
)
endif ()

install(TARGETS naughty-buffers naughty-buffers-static
EXPORT naughty-buffers
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
COMPONENT "Naughty Buffers Runtime"
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
COMPONENT "Naughty Buffers Runtime"
NAMELINK_COMPONENT "Naughty Buffers Development"
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
COMPONENT "Naughty Buffers Development"
PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/naughty-buffers
COMPONENT "Naughty Buffers Development"
)

export(
TARGETS naughty-buffers naughty-buffers-static
NAMESPACE naughty-buffers::
FILE "${CMAKE_CURRENT_BINARY_DIR}/naughty-buffers-config.cmake"
)

install(EXPORT naughty-buffers
FILE naughty-buffers-config.cmake
NAMESPACE naughty-buffers::
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/naughty-buffers
)

write_basic_package_version_file(
"${CMAKE_CURRENT_BINARY_DIR}/naughty-buffers-config-version.cmake"
VERSION ${naughty-buffers_VERSION}
COMPATIBILITY SameMajorVersion
)

install(
FILES
"${CMAKE_CURRENT_BINARY_DIR}/naughty-buffers-config-version.cmake"
DESTINATION ${CMAKE_INSTALL_LIBDIR}/cmake/naughty-buffers
COMPONENT "Naughty Buffers Development"
)

if (CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME AND BUILD_TESTING)
include(CTest)
add_subdirectory(src/tests)
endif ()

if (CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME AND BUILD_EXAMPLES)
add_subdirectory(src/examples)
endif ()

if (BUILD_DOCUMENTATION)
add_subdirectory(doc)
endif()

include(cmake/setup-exports.cmake)
include(cmake/naughty-buffers-cpack.cmake)
1 change: 0 additions & 1 deletion cmake/naughty-buffers-config.cmake

This file was deleted.

6 changes: 3 additions & 3 deletions cmake/naughty-buffers-cpack.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ endif (MSVC)

set(CPACK_PACKAGE_VENDOR "Leonardo G. L. de Freitas")
set(CPACK_PACKAGE_DESCRIPTION
"Font Chef is a cross-platform C99 library providing dynamic generic buffers")
"Naughty Buffers is a cross-platform C99 library providing dynamic generic buffers")
set(CPACK_PACKAGE_DESCRIPTION_SUMMARY "${CMAKE_PROJECT_DESCRIPTION}")
set(CPACK_PACKAGE_FILE_NAME
"${PROJECT_NAME}-${PROJECT_VERSION}-${NAUGHTY_BUFFERS_SYSTEM_NAME}-${NAUGHTY_BUFFERS_ARCH}-${NAUGHTY_BUFFERS_COMPILER_TARGET}")
set(CPACK_SOURCE_PACKAGE_FILE_NAME
"${PROJECT_NAME}-${PROJECT_VERSION}")
set(CPACK_PACKAGE_NAME KiWi)
set(CPACK_PACKAGE_NAME naughty-buffers)

set(CPACK_SOURCE_IGNORE_FILES .*build.*/ .idea/ .git/)

Expand All @@ -30,4 +30,4 @@ else ()
set(CPACK_GENERATOR "TGZ")
endif ()

include(CPack)
include(CPack)
24 changes: 0 additions & 24 deletions cmake/setup-exports.cmake

This file was deleted.

2 changes: 1 addition & 1 deletion doc/Doxyfile.in
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ PROJECT_NUMBER = @naughty-buffers_VERSION@
# for a project that appears at the top of each page and should give viewer a
# quick idea about the purpose of the project. Keep the description short.

PROJECT_BRIEF = "A font cooking library"
PROJECT_BRIEF = "A dynamic buffer library for C"

# With the PROJECT_LOGO tag one can specify a logo or an icon that is included
# in the documentation. The maximum height of the logo should not exceed 55
Expand Down
53 changes: 53 additions & 0 deletions include/naughty-buffers/buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,23 @@ struct nb_buffer_memory_context {
void * context;
};

/**
* @brief Structure returned by ::nb_iterator containing enough information to control a for-loop
*
* @sa ::nb_iterator
* @ingroup buffer
*/
struct nb_buffer_iterator {
/** Marks the initial data block */
void * begin;

/** Marks past the final data block. Do not dereference it. */
void * end;

/** The value to add to the block pointer in each iteration */
size_t increment;
};

/**
* @brief a structure holding the buffer data and metadata about the blocks.
*
Expand Down Expand Up @@ -474,6 +491,42 @@ NAUGHTY_BUFFERS_EXPORT void nb_sort(struct nb_buffer * buffer, nb_compare_fn com
*/
NAUGHTY_BUFFERS_EXPORT void nb_release(struct nb_buffer * buffer);

/**
* @brief Creates and returns an iterator that allows for performance-friendly traversal.
*
* If you intend to access a lot of elements in sequence, this is the preferred method to do so as it offers better
* performance in comparison to ::nb_at
*
* @param buffer A pointer to a ::nb_buffer struct
* @returns A `nb_buffer_iterator` struct with values that can be used to control a for-loop.
* @ingroup buffer
*
* **Example**
* @code
int main(void) {
struct nb_buffer buffer;
nb_init(&buffer, sizeof(int));
int value;
value = 0; nb_push(&buffer, &value);
value = 10; nb_push(&buffer, &value);
value = 20; nb_push(&buffer, &value);
struct nb_buffer_iterator itr = nb_iterator(&buffer);
for (uint8_t * block = itr.begin; block != itr.end; block += itr.increment) {
int * data = (int *) block;
printf("data: %d\n", *data);
}
nb_release(&buffer);
return 0;
}
* @endcode
*/
NAUGHTY_BUFFERS_EXPORT struct nb_buffer_iterator nb_iterator(struct nb_buffer * buffer);

#ifdef __cplusplus
};
#endif
Expand Down
11 changes: 0 additions & 11 deletions src/CMakeLists.txt

This file was deleted.

53 changes: 0 additions & 53 deletions src/naughty-buffers/CMakeLists.txt

This file was deleted.

8 changes: 8 additions & 0 deletions src/naughty-buffers/buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,11 @@ void nb_remove_at(struct nb_buffer * buffer, size_t index) {
void nb_sort(struct nb_buffer * buffer, nb_compare_fn compare_fn) {
qsort(buffer->data, buffer->block_count, buffer->block_size, compare_fn);
}

struct nb_buffer_iterator nb_iterator(struct nb_buffer * buffer) {
return (struct nb_buffer_iterator) {
.begin = (uint8_t *) buffer->data,
.end = (uint8_t *) buffer->data + (buffer->block_count * buffer->block_size),
.increment = buffer->block_size
};
}
1 change: 1 addition & 0 deletions src/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ nb_test(test-insert insert.c)
nb_test(test-remove remove.c)
nb_test(test-sort sort.c)
nb_test(test-array-generator array-generator.c)
nb_test(test-iterators iterators.c)
32 changes: 32 additions & 0 deletions src/tests/iterators.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "naughty-buffers/buffer.h"
#include <assert.h>
#include <stdlib.h>

void iterator_works() {
struct nb_buffer buffer;
nb_init(&buffer, sizeof(uint32_t));
int count = rand() % 300;

for (uint32_t i = 0; i < count; i++) {
uint32_t value = rand();
nb_push(&buffer, &value);
}

struct nb_buffer_iterator itr = nb_iterator(&buffer);

size_t i = 0;
for (void * block = itr.begin; block != itr.end; block += itr.increment) {
uint32_t * read_value = block;
uint32_t * expected_value = nb_at(&buffer, i);
assert(read_value == expected_value);
i++;
}

nb_release(&buffer);
}


int main(void) {
iterator_works();
return 0;
}

0 comments on commit be70313

Please sign in to comment.