Skip to content

Commit

Permalink
House keeping
Browse files Browse the repository at this point in the history
  • Loading branch information
simondevenish committed Nov 22, 2024
1 parent c740f37 commit 0d0986f
Show file tree
Hide file tree
Showing 17 changed files with 182 additions and 58 deletions.
104 changes: 50 additions & 54 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,71 +1,67 @@
# Minimum CMake version required
cmake_minimum_required(VERSION 3.15)
# Minimum CMake version
cmake_minimum_required(VERSION 3.16)

# Project name and description
# Project definition
project(AllocX
VERSION 1.0
DESCRIPTION "A high-performance, cross-platform memory management library designed for low-latency, high-frequency systems."
LANGUAGES C CXX
VERSION 1.0.0
DESCRIPTION "High-performance, cross-platform memory management library for low-latency systems"
LANGUAGES C
)

# Set the C++ standard
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
# Set C standard
set(CMAKE_C_STANDARD 11)
set(CMAKE_C_STANDARD_REQUIRED ON)
set(CMAKE_C_EXTENSIONS OFF)

# Enable testing
enable_testing()
include(CTest)

# Options
option(ALLOCX_BUILD_TESTS "Build unit tests" ON)
option(ALLOCX_BUILD_EXAMPLES "Build examples" ON)

# Define output directories
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
# Enable compile warnings
if(CMAKE_C_COMPILER_ID MATCHES "GNU|Clang")
add_compile_options(-Wall -Wextra -Wpedantic -Werror)
endif()

# Include source directories
add_subdirectory(src)
add_subdirectory(include)
# Options
option(ALLOCX_BUILD_TESTS "Build tests for AllocX" ON)
option(ALLOCX_ENABLE_SANITIZERS "Enable sanitizers for debugging" OFF)

# Add tests if enabled
if(ALLOCX_BUILD_TESTS)
add_subdirectory(tests)
# Sanitizers (if enabled)
if(ALLOCX_ENABLE_SANITIZERS AND (CMAKE_C_COMPILER_ID MATCHES "GNU|Clang"))
add_compile_options(-fsanitize=address -fsanitize=undefined)
add_link_options(-fsanitize=address -fsanitize=undefined)
endif()

# Add examples if enabled
if(ALLOCX_BUILD_EXAMPLES)
add_subdirectory(examples)
endif()
# Define project directories
set(ALLOCX_INCLUDE_DIR ${PROJECT_SOURCE_DIR}/include)
set(ALLOCX_SOURCE_DIR ${PROJECT_SOURCE_DIR}/src)

# Documentation target
find_package(Doxygen)
if(Doxygen_FOUND)
add_subdirectory(docs)
else()
message(STATUS "Doxygen not found. Documentation will not be generated.")
endif()
# Create the AllocX library target
add_library(allocx STATIC
${ALLOCX_SOURCE_DIR}/memory_pool.c
${ALLOCX_SOURCE_DIR}/memory_buffer.c
)

# Include directories
target_include_directories(allocx
PUBLIC ${ALLOCX_INCLUDE_DIR}
)

# Installation rules
include(GNUInstallDirs)
install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
install(TARGETS AllocX
EXPORT AllocXTargets
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
# Library properties
set_target_properties(allocx PROPERTIES
OUTPUT_NAME allocx
POSITION_INDEPENDENT_CODE ON
)

# Export targets for downstream projects
install(EXPORT AllocXTargets
FILE AllocXConfig.cmake
NAMESPACE AllocX::
DESTINATION ${CMAKE_INSTALL_DATADIR}/AllocX
# Install AllocX
install(TARGETS allocx
ARCHIVE DESTINATION lib
LIBRARY DESTINATION lib
RUNTIME DESTINATION bin
)
install(DIRECTORY ${ALLOCX_INCLUDE_DIR}/allocx DESTINATION include)

# Display configuration summary
message(STATUS "Project: ${PROJECT_NAME}")
message(STATUS "Version: ${PROJECT_VERSION}")
message(STATUS "Description: ${PROJECT_DESCRIPTION}")
message(STATUS "C++ Standard: ${CMAKE_CXX_STANDARD}")
# Tests (if enabled)
#if(ALLOCX_BUILD_TESTS)
# enable_testing()
# add_subdirectory(tests)
#endif()
Empty file added examples/CMakeLists.txt
Empty file.
28 changes: 28 additions & 0 deletions include/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# CMakeLists.txt for the include directory of AllocX

# Specify the minimum required version of CMake
cmake_minimum_required(VERSION 3.15)

# Define the project name and language
project(AllocXHeaders LANGUAGES C)

# Set the base directory for headers
set(ALLOCX_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR})

# List all public header files
set(ALLOCX_PUBLIC_HEADERS
${ALLOCX_INCLUDE_DIR}/allocx.h
${ALLOCX_INCLUDE_DIR}/allocx_atomic.h
${ALLOCX_INCLUDE_DIR}/allocx_core_types.h
${ALLOCX_INCLUDE_DIR}/allocx_errors.h
${ALLOCX_INCLUDE_DIR}/memory_pool.h
)

# Create an interface library to hold the headers
add_library(allocx_headers INTERFACE)

# Specify the include directories for the interface library
target_include_directories(allocx_headers INTERFACE ${ALLOCX_INCLUDE_DIR})

# Install the headers to the appropriate destination
install(FILES ${ALLOCX_PUBLIC_HEADERS} DESTINATION include/allocx)
16 changes: 16 additions & 0 deletions include/allocx/allocx.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,22 @@
#ifndef ALLOCX_H
#define ALLOCX_H

#ifdef __cplusplus
extern "C" {
#endif

#include "allocx_core_types.h"
#include "allocx_alignment.h"
#include "allocx_assertions.h"
#include "allocx_atomic.h"
#include "allocx_bitwise.h"
#include "allocx_debug.h"
#include "allocx_utilities.h"
#include "memory_pool.h"
#include "memory_buffer.h"

#ifdef __cplusplus
}
#endif

#endif // ALLOCX_H
8 changes: 8 additions & 0 deletions include/allocx/allocx_alignment.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#ifndef ALLOCX_ALIGNMENT_H
#define ALLOCX_ALIGNMENT_H

#ifdef __cplusplus
extern "C" {
#endif

#include "allocx_core_types.h"

// Alignment macros
Expand All @@ -14,4 +18,8 @@ typedef u16 align16_t __attribute__((aligned(16)));
typedef u32 align32_t __attribute__((aligned(32)));
typedef u64 align64_t __attribute__((aligned(64)));

#ifdef __cplusplus
}
#endif

#endif // ALLOCX_ALIGNMENT_H
8 changes: 8 additions & 0 deletions include/allocx/allocx_assertions.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#ifndef ALLOCX_ASSERTIONS_H
#define ALLOCX_ASSERTIONS_H

#ifdef __cplusplus
extern "C" {
#endif

#include <assert.h>
#include <stdio.h>

Expand All @@ -12,4 +16,8 @@
} \
} while (0)

#ifdef __cplusplus
}
#endif

#endif // ALLOCX_ASSERTIONS_H
10 changes: 9 additions & 1 deletion include/allocx/allocx_atomic.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#ifndef ALLOCX_ATOMIC_H
#define ALLOCX_ATOMIC_H

#ifdef __cplusplus
extern "C" {
#endif

#include <stdatomic.h>

// Memory order macros
Expand All @@ -27,4 +31,8 @@
#define ALLOCX_FETCH_AND(ptr, value) __atomic_fetch_and(ptr, value, __ATOMIC_SEQ_CST)
#define ALLOCX_FETCH_OR(ptr, value) __atomic_fetch_or(ptr, value, __ATOMIC_SEQ_CST)

#endif // ALLOCX_ATOMIC_H
#ifdef __cplusplus
}
#endif

#endif // ALLOCX_ATOMIC_H
8 changes: 8 additions & 0 deletions include/allocx/allocx_bitwise.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#ifndef ALLOCX_BITWISE_UTILS_H
#define ALLOCX_BITWISE_UTILS_H

#ifdef __cplusplus
extern "C" {
#endif

#include "allocx_core_types.h"

// Get the Nth bit of a value
Expand Down Expand Up @@ -48,4 +52,8 @@
// Find the position of the lowest set bit (zero-based)
#define ALLOCX_LOWEST_BIT_POSITION(value) (__builtin_ctzll(value))

#ifdef __cplusplus
}
#endif

#endif // ALLOCX_BITWISE_UTILS_H
9 changes: 9 additions & 0 deletions include/allocx/allocx_core_types.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
#ifndef ALLOCX_CORE_TYPES_H
#define ALLOCX_CORE_TYPES_H

#ifdef __cplusplus
extern "C" {
#endif


#include <stddef.h>
#include <stdint.h>

Expand Down Expand Up @@ -31,4 +36,8 @@ typedef void* mem_ptr_t; // Generic memory pointer
typedef usize mem_size_t; // Memory size in bytes
typedef isize mem_offset_t; // Memory offset

#ifdef __cplusplus
}
#endif

#endif // ALLOCX_CORE_TYPES_H
8 changes: 8 additions & 0 deletions include/allocx/allocx_debug.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#ifndef ALLOCX_DEBUG_H
#define ALLOCX_DEBUG_H

#ifdef __cplusplus
extern "C" {
#endif

#include <stdio.h>

// Debug Logging
Expand All @@ -21,4 +25,8 @@
fprintf(stderr, "\n"); \
} while (0)

#ifdef __cplusplus
}
#endif

#endif // ALLOCX_DEBUG_H
10 changes: 9 additions & 1 deletion include/allocx/allocx_errors.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#ifndef ALLOCX_ERRORS_H
#define ALLOCX_ERRORS_H

#ifdef __cplusplus
extern "C" {
#endif

typedef enum {
ALLOCX_SUCCESS = 0,
ALLOCX_ERROR_OUT_OF_MEMORY,
Expand All @@ -10,4 +14,8 @@ typedef enum {
ALLOCX_ERROR_UNKNOWN
} allocx_error_t;

#endif // ALLOCX_ERRORS_H
#ifdef __cplusplus
}
#endif

#endif // ALLOCX_ERRORS_H
8 changes: 8 additions & 0 deletions include/allocx/allocx_utilities.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#ifndef ALLOCX_UTILITIES_H
#define ALLOCX_UTILITIES_H

#ifdef __cplusplus
extern "C" {
#endif

#include "allocx_alignment.h"

// Array size
Expand All @@ -10,4 +14,8 @@
#define IN_RANGE(value, min, max) ((value) >= (min) && (value) <= (max))
#define CLAMP(value, min, max) (allocx_min(allocx_max((value), (min)), (max)))

#ifdef __cplusplus
}
#endif

#endif // ALLOCX_UTILITIES_H
11 changes: 10 additions & 1 deletion include/allocx/memory_buffer.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
#ifndef ALLOCX_BUFFER_H
#define ALLOCX_BUFFER_H

#ifdef __cplusplus
extern "C" {
#endif


#include "allocx_core_types.h"
#include "allocx_atomic.h"
#include "allocx_errors.h"
Expand Down Expand Up @@ -83,4 +88,8 @@ void allocx_buffer_compact(AllocXBuffer *buf);
*/
allocx_error_t allocx_buffer_append(AllocXBuffer *dst, const AllocXBuffer *src);

#endif // ALLOCX_BUFFER_H
#ifdef __cplusplus
}
#endif

#endif // ALLOCX_BUFFER_H
10 changes: 9 additions & 1 deletion include/allocx/memory_pool.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#ifndef ALLOCX_MEMORY_POOL_H
#define ALLOCX_MEMORY_POOL_H

#ifdef __cplusplus
extern "C" {
#endif

#include "allocx_core_types.h" // Core types like usize, u8, etc.
#include "allocx_atomic.h" // Atomic utilities for thread safety
#include "allocx_errors.h" // Error return codes
Expand Down Expand Up @@ -83,4 +87,8 @@ void allocx_memory_pool_free(AllocXMemoryPool* pool, mem_ptr_t block);
*/
void allocx_memory_pool_destroy(AllocXMemoryPool* pool);

#endif // ALLOCX_MEMORY_POOL_H
#ifdef __cplusplus
}
#endif

#endif // ALLOCX_MEMORY_POOL_H
Empty file added src/CMakeLists.txt
Empty file.
1 change: 1 addition & 0 deletions src/memory_buffer.c
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "allocx/memory_buffer.h"
1 change: 1 addition & 0 deletions src/memory_pool.c
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#include "allocx/memory_pool.h"

0 comments on commit 0d0986f

Please sign in to comment.