Skip to content

Commit

Permalink
Add dynamic memory pool (#7)
Browse files Browse the repository at this point in the history
Add dynamic memory pool
  • Loading branch information
bignacio authored Apr 16, 2023
1 parent d2c5a42 commit dcecae9
Show file tree
Hide file tree
Showing 11 changed files with 1,212 additions and 9 deletions.
28 changes: 25 additions & 3 deletions .github/workflows/build_linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ name: Linux build
on: [pull_request]

jobs:
build-linux:
name: build-linux
build-cpp-linux:
name: build-cpp-linux
runs-on: ubuntu-22.04
container:
image: ghcr.io/bignacio/docker-images/ubuntu-cpp-22.10:1
Expand All @@ -14,10 +14,32 @@ jobs:
strategy:
matrix:
compiler: [g++-10, g++-11, g++-12, clang++-13, clang++-14, clang++-15]

steps:
- uses: actions/checkout@v3

- name: Build and test
env:
CXX: ${{matrix.compiler}}
run: ci-scripts/build_and_test.sh
run: ci-scripts/build_and_test.sh

build-simplepool-linux:
name: build-simplepool-linux
runs-on: ubuntu-22.04
container:
image: ghcr.io/bignacio/docker-images/ubuntu-cpp-22.10:1
credentials:
username: bignacio
password: ${{ secrets.GHCR_PACKAGES_TOKEN }}
strategy:
matrix:
compiler: [gcc-12, clang-15]

steps:
- uses: actions/checkout@v3

- name: Build and test
env:
CC: ${{matrix.compiler}}
CXX: clang++-15 # needed to make cmake happy
run: ci-scripts/build_and_test_simplepool.sh
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,5 @@ build
docs
.vscode
dxpool.code-workspace

simple_pool/build
8 changes: 4 additions & 4 deletions Doxyfile
Original file line number Diff line number Diff line change
Expand Up @@ -69,10 +69,10 @@ EXTRACT_ALL = NO
EXTRACT_PRIVATE = NO
EXTRACT_PRIV_VIRTUAL = NO
EXTRACT_PACKAGE = NO
EXTRACT_STATIC = NO
EXTRACT_STATIC = YES
EXTRACT_LOCAL_CLASSES = YES
EXTRACT_LOCAL_METHODS = NO
EXTRACT_ANON_NSPACES = NO
EXTRACT_LOCAL_METHODS = YES
EXTRACT_ANON_NSPACES = YES
RESOLVE_UNNAMED_PARAMS = YES
HIDE_UNDOC_MEMBERS = NO
HIDE_UNDOC_CLASSES = NO
Expand Down Expand Up @@ -118,7 +118,7 @@ WARN_LOGFILE =
#---------------------------------------------------------------------------
# Configuration options related to the input files
#---------------------------------------------------------------------------
INPUT = src README.md CODE-OF-CONDUCT.md CONTRIBUTING.md
INPUT = src README.md CODE-OF-CONDUCT.md CONTRIBUTING.md simple_pool/dyn_mem_pool.h
INPUT_ENCODING = UTF-8
FILE_PATTERNS = *.cpp *.h *.md
RECURSIVE = YES
Expand Down
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ The `StaticPool` statically allocates the pool size and its elements. In this ca

The `RuntimePool` permits developers to specify the size of the pool at runtime but in this case, the type of the objects in the pool must be *move constructible (note the pool size still won't change after its construction).

## The dynamic memory pool

The Simple Pool dynamic memory pool library is a pure C, header only library that allows dynamic allocation of new pool entries. Though all memory blocks in the same pool are of the same size, the number of entries in the pool is not limited, unlike the C++ object pool implementation.
The Simple Pool library is thread-safe.

Including the header [`dyn_mem_pool.h`](simple_pool/dyn_mem_pool.h) is all that is required and the directory [simple_pool](simple_pool) contains examples for how to use the pool;

### Concurrency support

All object pools are thread safe and allow different mechanisms for data safety via what is called an `Indexer`.
Expand Down
4 changes: 2 additions & 2 deletions ci-scripts/build_and_test.sh
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
#!/bin/bash

rm -rf build
set -e

for build_type in Release Debug ; do
for std in 11 14 17 20 ; do
echo "Building type '${build_type}' standard ${std}"
make -B build clean
rm -rf build
cmake -B build \
-DCMAKE_BUILD_TYPE=${build_type} -DCMAKE_CXX_STANDARD=${std} \
-DCMAKE_CXX_STANDARD_REQUIRED=ON -DCMAKE_CXX_EXTENSIONS=OFF
Expand Down
16 changes: 16 additions & 0 deletions ci-scripts/build_and_test_simplepool.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash

set -e
cd simple_pool

rm -rf build
cmake -B build -DCMAKE_BUILD_TYPE=Debug

make -j2 -C build
build/dyn_mem_pool_test
build/dyn_mem_pool_fuzz
build/dyn_mem_pool_fuzz_addr

if [ "$CC" = "clang-15" ]; then
build/dyn_mem_pool_fuzz_mem
fi
215 changes: 215 additions & 0 deletions simple_pool/.clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
---
Language: Cpp
# BasedOnStyle: LLVM
AccessModifierOffset: -2
AlignAfterOpenBracket: Align
AlignArrayOfStructures: None
AlignConsecutiveAssignments:
Enabled: false
AcrossEmptyLines: false
AcrossComments: false
AlignCompound: false
PadOperators: true
AlignConsecutiveBitFields:
Enabled: false
AcrossEmptyLines: false
AcrossComments: false
AlignCompound: false
PadOperators: false
AlignConsecutiveDeclarations:
Enabled: false
AcrossEmptyLines: false
AcrossComments: false
AlignCompound: false
PadOperators: false
AlignConsecutiveMacros:
Enabled: false
AcrossEmptyLines: false
AcrossComments: false
AlignCompound: false
PadOperators: false
AlignEscapedNewlines: Right
AlignOperands: Align
AlignTrailingComments: true
AllowAllArgumentsOnNextLine: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortEnumsOnASingleLine: true
AllowShortBlocksOnASingleLine: Never
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: None
AllowShortLambdasOnASingleLine: All
AllowShortIfStatementsOnASingleLine: Never
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: MultiLine
AttributeMacros:
- __capability
BinPackArguments: true
BinPackParameters: true
BraceWrapping:
AfterCaseLabel: false
AfterClass: false
AfterControlStatement: Never
AfterEnum: false
AfterFunction: false
AfterNamespace: false
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
AfterExternBlock: false
BeforeCatch: false
BeforeElse: false
BeforeLambdaBody: false
BeforeWhile: false
IndentBraces: false
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: true
BreakBeforeBinaryOperators: None
BreakBeforeConceptDeclarations: Always
BreakBeforeBraces: Attach
BreakBeforeInheritanceComma: false
BreakInheritanceList: BeforeColon
BreakBeforeTernaryOperators: true
BreakConstructorInitializersBeforeComma: false
BreakConstructorInitializers: BeforeColon
BreakAfterJavaFieldAnnotations: false
BreakStringLiterals: true
ColumnLimit: 200
CommentPragmas: '^ IWYU pragma:'
QualifierAlignment: Leave
CompactNamespaces: false
ConstructorInitializerIndentWidth: 4
ContinuationIndentWidth: 4
Cpp11BracedListStyle: true
DeriveLineEnding: true
DerivePointerAlignment: false
DisableFormat: false
EmptyLineAfterAccessModifier: Never
EmptyLineBeforeAccessModifier: LogicalBlock
ExperimentalAutoDetectBinPacking: false
PackConstructorInitializers: BinPack
BasedOnStyle: ''
ConstructorInitializerAllOnOneLineOrOnePerLine: false
AllowAllConstructorInitializersOnNextLine: true
FixNamespaceComments: true
ForEachMacros:
- foreach
- Q_FOREACH
- BOOST_FOREACH
IfMacros:
- KJ_IF_MAYBE
IncludeBlocks: Preserve
IncludeCategories:
- Regex: '^"(llvm|llvm-c|clang|clang-c)/'
Priority: 2
SortPriority: 0
CaseSensitive: false
- Regex: '^(<|"(gtest|gmock|isl|json)/)'
Priority: 3
SortPriority: 0
CaseSensitive: false
- Regex: '.*'
Priority: 1
SortPriority: 0
CaseSensitive: false
IncludeIsMainRegex: '(Test)?$'
IncludeIsMainSourceRegex: ''
IndentAccessModifiers: false
IndentCaseLabels: false
IndentCaseBlocks: false
IndentGotoLabels: true
IndentPPDirectives: None
IndentExternBlock: AfterExternBlock
IndentRequiresClause: true
IndentWidth: 4
IndentWrappedFunctionNames: false
InsertBraces: false
InsertTrailingCommas: None
JavaScriptQuotes: Leave
JavaScriptWrapImports: true
KeepEmptyLinesAtTheStartOfBlocks: true
LambdaBodyIndentation: Signature
MacroBlockBegin: ''
MacroBlockEnd: ''
MaxEmptyLinesToKeep: 1
NamespaceIndentation: None
ObjCBinPackProtocolList: Auto
ObjCBlockIndentWidth: 4
ObjCBreakBeforeNestedBlockParam: true
ObjCSpaceAfterProperty: false
ObjCSpaceBeforeProtocolList: true
PenaltyBreakAssignment: 2
PenaltyBreakBeforeFirstCallParameter: 19
PenaltyBreakComment: 300
PenaltyBreakFirstLessLess: 120
PenaltyBreakOpenParenthesis: 0
PenaltyBreakString: 1000
PenaltyBreakTemplateDeclaration: 10
PenaltyExcessCharacter: 1000000
PenaltyReturnTypeOnItsOwnLine: 60
PenaltyIndentedWhitespace: 0
PointerAlignment: Right
PPIndentWidth: -1
ReferenceAlignment: Pointer
ReflowComments: true
RemoveBracesLLVM: false
RequiresClausePosition: OwnLine
SeparateDefinitionBlocks: Leave
ShortNamespaceLines: 1
SortIncludes: CaseSensitive
SortJavaStaticImport: Before
SortUsingDeclarations: true
SpaceAfterCStyleCast: false
SpaceAfterLogicalNot: false
SpaceAfterTemplateKeyword: true
SpaceBeforeAssignmentOperators: true
SpaceBeforeCaseColon: false
SpaceBeforeCpp11BracedList: false
SpaceBeforeCtorInitializerColon: true
SpaceBeforeInheritanceColon: true
SpaceBeforeParens: ControlStatements
SpaceBeforeParensOptions:
AfterControlStatements: true
AfterForeachMacros: true
AfterFunctionDefinitionName: false
AfterFunctionDeclarationName: false
AfterIfMacros: true
AfterOverloadedOperator: false
AfterRequiresInClause: false
AfterRequiresInExpression: false
BeforeNonEmptyParentheses: false
SpaceAroundPointerQualifiers: Default
SpaceBeforeRangeBasedForLoopColon: true
SpaceInEmptyBlock: false
SpaceInEmptyParentheses: false
SpacesBeforeTrailingComments: 1
SpacesInAngles: Never
SpacesInConditionalStatement: false
SpacesInContainerLiterals: true
SpacesInCStyleCastParentheses: false
SpacesInLineCommentPrefix:
Minimum: 1
Maximum: -1
SpacesInParentheses: false
SpacesInSquareBrackets: false
SpaceBeforeSquareBrackets: false
BitFieldColonSpacing: Both
Standard: Latest
StatementAttributeLikeMacros:
- Q_EMIT
StatementMacros:
- Q_UNUSED
- QT_REQUIRE_VERSION
TabWidth: 4
UseCRLF: false
UseTab: Never
WhitespaceSensitiveMacros:
- STRINGIZE
- PP_STRINGIZE
- BOOST_PP_STRINGIZE
- NS_SWIFT_NAME
- CF_SWIFT_NAME
...
42 changes: 42 additions & 0 deletions simple_pool/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
cmake_minimum_required(VERSION 3.21)
project(dyn_mem_pool VERSION 1.0)

set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
#set(CMAKE_C_CLANG_TIDY clang-tidy -checks=-*,bugprone-*,performance-*,clang-*,llvm-*,llvm-libc-*,misc-*,modernize-*,readability-*,cert-*,-llvm-header-guard -header-filter=.* --warnings-as-errors=* -p=build)

add_compile_options(
-Wall
-Wpedantic
-Werror
-Wextra
-Wconversion
-Wsign-conversion
-Walloca
-Wshadow
-Wfloat-equal
-Wswitch-enum
-Wcast-qual
-Wimplicit-fallthrough
-Wundef
-Wfloat-equal
-march=native
-fPIE
-fno-omit-frame-pointer
)

add_executable(dyn_mem_pool_test dyn_mem_pool_test.c)
target_link_libraries(dyn_mem_pool_test pthread)
add_executable(dyn_mem_pool_fuzz dyn_mem_pool_fuzz.c)
target_link_libraries(dyn_mem_pool_fuzz pthread)

set_target_properties(dyn_mem_pool_test PROPERTIES CMAKE_C_CLANG_TIDY clang-tidy -checks=-*,bugprone-*,performance-*,clang-*,llvm-*,llvm-libc-*,misc-*,modernize-*,readability-*,cert-*,-llvm-header-guard -header-filter=.* --warnings-as-errors=* -p=build)

add_executable(dyn_mem_pool_fuzz_addr dyn_mem_pool_fuzz.c)
target_compile_options(dyn_mem_pool_fuzz_addr PRIVATE -fsanitize=address)
target_link_options(dyn_mem_pool_fuzz_addr PRIVATE -fsanitize=address)

if (${CMAKE_C_COMPILER_ID} STREQUAL "Clang")
add_executable(dyn_mem_pool_fuzz_mem dyn_mem_pool_fuzz.c)
target_compile_options(dyn_mem_pool_fuzz_mem PRIVATE -fsanitize=memory -fsanitize-memory-track-origins=2)
target_link_options(dyn_mem_pool_fuzz_mem PRIVATE -fsanitize=memory -fsanitize-memory-track-origins=2)
endif()
Loading

0 comments on commit dcecae9

Please sign in to comment.