Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
MaJerle committed Oct 12, 2024
2 parents c1463a4 + c36034a commit 0f79742
Show file tree
Hide file tree
Showing 55 changed files with 1,074 additions and 622 deletions.
4 changes: 2 additions & 2 deletions .clang-format
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ AlignConsecutiveBitFields:
AlignConsecutiveDeclarations: None
AlignEscapedNewlines: Right
AlignOperands: Align
SortIncludes: false
SortIncludes: true
InsertBraces: true # Control statements must have curly brackets
AlignTrailingComments: true
AllowAllArgumentsOnNextLine: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortEnumsOnASingleLine: true
AllowShortBlocksOnASingleLine: Empty
AllowShortCaseLabelsOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: true
AllowShortFunctionsOnASingleLine: All
AllowShortLambdasOnASingleLine: All
AllowShortIfStatementsOnASingleLine: Never
Expand Down
19 changes: 19 additions & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
Checks: "*,
-abseil-*,
-altera-*,
-android-*,
-fuchsia-*,
-google-*,
-llvm*,
-modernize-use-trailing-return-type,
-zircon-*,
-readability-else-after-return,
-readability-static-accessed-through-instance,
-readability-avoid-const-params-in-decls,
-cppcoreguidelines-non-private-member-variables-in-classes,
-misc-non-private-member-variables-in-classes,
"
WarningsAsErrors: ''
HeaderFilterRegex: ''
FormatStyle: none
18 changes: 18 additions & 0 deletions .readthedocs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
version: 2
build:
os: ubuntu-22.04
tools:
python: "3.11"

# Build documentation in the docs/ directory with Sphinx
sphinx:
configuration: docs/conf.py

# Python configuration
python:
install:
- requirements: docs/requirements.txt

formats:
- pdf
- epub
9 changes: 8 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@
"lwevt_type.h": "c",
"lwevt.h": "c",
"string.h": "c",
"lwevt_opt.h": "c"
"lwevt_opt.h": "c",
"streambuf": "c",
"lwmem.h": "c",
"lwmem_opt.h": "c",
"array": "c",
"string_view": "c",
"initializer_list": "c",
"limits.h": "c"
},
"esbonio.sphinx.confDir": ""
}
2 changes: 1 addition & 1 deletion .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"version": "2.1.0",
"version": "2.0.0",
"tasks": [
{
"type": "cppbuild",
Expand Down
4 changes: 4 additions & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Tilen Majerle <[email protected]>
upbeat27 <[email protected]>
Tilen Majerle <[email protected]>
Y <[email protected]>
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

## Develop

## v2.2.0

- Rework library CMake with removed INTERFACE type
- Add `LWMEM_CFG_FULL` to allow control build configuration of the library
- Implement support for simple (no realloc, no free, grow-only malloc) allocation mechanism

## v2.1.0

- Split CMakeLists.txt files between library and executable
Expand Down
29 changes: 7 additions & 22 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,26 @@ project(LwLibPROJECT)
if(NOT PROJECT_IS_TOP_LEVEL)
add_subdirectory(lwmem)
else()
# Set as executable
add_executable(${PROJECT_NAME})

# Add key executable block
target_sources(${PROJECT_NAME} PUBLIC
${CMAKE_CURRENT_LIST_DIR}/dev/main.cpp
${CMAKE_CURRENT_LIST_DIR}/tests/lwmem_test.c
${CMAKE_CURRENT_LIST_DIR}/tests/lwmem_test_simple.c

# win32 port
${CMAKE_CURRENT_LIST_DIR}/lwmem/src/system/lwmem_sys_win32.c
)

# Add key include paths
target_include_directories(${PROJECT_NAME} PUBLIC
${CMAKE_CURRENT_LIST_DIR}
${CMAKE_CURRENT_LIST_DIR}/dev
)

# Compilation definition information
target_compile_definitions(${PROJECT_NAME} PUBLIC
WIN32
_DEBUG
CONSOLE
LWMEM_DEV
)

# Compiler options
target_compile_options(${PROJECT_NAME} PRIVATE
-Wall
-Wextra
-Wpedantic
)

# Add subdir with lwmem and link to the project
set(LWMEM_OPTS_FILE ${CMAKE_CURRENT_LIST_DIR}/dev/lwmem_opts.h)
add_subdirectory(lwmem)
target_link_libraries(${PROJECT_NAME} lwmem)
target_link_libraries(${PROJECT_NAME} lwmem_cpp)
target_link_libraries(${PROJECT_NAME} PUBLIC lwmem_cpp)

# Add compile options to the library, which will propagate options to executable through public link
target_compile_definitions(lwmem PUBLIC WIN32 _DEBUG CONSOLE LWMEM_DEV)
target_compile_options(lwmem PUBLIC -Wall -Wextra -Wpedantic)
endif()
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2023 Tilen MAJERLE
Copyright (c) 2024 Tilen MAJERLE

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

## Features

* Written in ANSI C99, compatible with ``size_t`` for size data types
* Written in C (C11), compatible with ``size_t`` for size data types
* Implements standard C library functions for memory allocation, malloc, calloc, realloc and free
* Uses *first-fit* algorithm to search for free block
* Supports multiple allocation instances to split between memories and/or CPU cores
Expand All @@ -13,16 +13,17 @@
* Supports embedded applications with fragmented memories
* Supports automotive applications
* Supports advanced free/realloc algorithms to optimize memory usage
* **Since v2.2.0** Supports light implementation with allocation only
* Operating system ready, thread-safe API
* C++ wrapper functions
* User friendly MIT license

## Contribute

Fresh contributions are always welcome. Simple instructions to proceed::
Fresh contributions are always welcome. Simple instructions to proceed:

1. Fork Github repository
2. Respect [C style & coding rules](https://github.com/MaJerle/c-code-style) used by the library
2. Follow [C style & coding rules](https://github.com/MaJerle/c-code-style) already used in the project
3. Create a pull request to develop branch with new features or bug fixes

Alternatively you may:
Expand Down
11 changes: 6 additions & 5 deletions dev/lwmem_opts.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
*/

/*
* Copyright (c) 2023 Tilen MAJERLE
* Copyright (c) 2024 Tilen MAJERLE
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
Expand Down Expand Up @@ -41,9 +41,10 @@
* Open "include/lwmem/lwmem_opt.h" and
* copy & replace here settings you want to change values
*/
#define LWMEM_CFG_OS 1
#define LWMEM_CFG_OS_MUTEX_HANDLE HANDLE
#define LWMEM_CFG_ENABLE_STATS 0
#define LWMEM_CFG_CLEAN_MEMORY 1
#define LWMEM_CFG_OS 1
#define LWMEM_CFG_OS_MUTEX_HANDLE HANDLE
#define LWMEM_CFG_ENABLE_STATS 0
#define LWMEM_CFG_CLEAN_MEMORY 1
#define LWMEM_CFG_FULL 0

#endif /* LWMEM_HDR_OPTS_H */
15 changes: 12 additions & 3 deletions dev/main.cpp
Original file line number Diff line number Diff line change
@@ -1,24 +1,33 @@
#include "lwmem/lwmem.h"
#include "lwmem/lwmem.hpp"
#include <iostream>
#include <stdint.h>
#include <string.h>
#include "lwmem/lwmem.h"
#include "lwmem/lwmem.hpp"

extern "C" void lwmem_test_run(void);
extern "C" void lwmem_test_simple_run(void);
extern "C" void lwmem_test_memory_structure(void);

/* Setup manager */
Lwmem::LwmemLight<1024> manager;
static Lwmem::LwmemLight<1024> manager;

int
main(void) {
#if LWMEM_CFG_FULL
lwmem_test_memory_structure();
//lwmem_test_run();
#else
lwmem_test_simple_run();
#endif

#if 1
/* Test C++ code */
void* ret = manager.malloc(123);
std::cout << ret << std::endl;
#if LWMEM_CFG_FULL
manager.free(ret);
#endif /* LWMEM_CFG_FULL */
#endif

return 0;
}
8 changes: 8 additions & 0 deletions docs/authors/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.. _authors:

Authors
=======

List of authors and contributors to the library

.. literalinclude:: ../../AUTHORS
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
# -- Project information -----------------------------------------------------

project = 'LwMEM'
copyright = '2022, Tilen MAJERLE'
copyright = '2023, Tilen MAJERLE'
author = 'Tilen MAJERLE'

# Try to get branch at which this is running
Expand Down
29 changes: 15 additions & 14 deletions docs/examples_src/example_realloc_enlarge_full.c
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
#define ASSERT(x) do { \
if (!(x)) { \
printf("Assert failed with condition (" # x ")\r\n"); \
} else {\
printf("Assert passed with condition (" # x ")\r\n"); \
}\
} while (0)
#define ASSERT(x) \
do { \
if (!(x)) { \
printf("Assert failed with condition (" #x ")\r\n"); \
} else { \
printf("Assert passed with condition (" #x ")\r\n"); \
} \
} while (0)

/* For debug purposes */
lwmem_region_t* regions_used;
size_t regions_count = 1; /* Use only 1 region for debug purposes of non-free areas */
size_t regions_count = 1; /* Use only 1 region for debug purposes of non-free areas */

int
main(void) {
uint8_t* ptr1, *ptr2, *ptr3, *ptr4;
uint8_t* rptr1, *rptr2, *rptr3, *rptr4;
uint8_t *ptr1, *ptr2, *ptr3, *ptr4;
uint8_t *rptr1, *rptr2, *rptr3, *rptr4;

/* Create regions for debug purpose */
if (!lwmem_debug_create_regions(&regions_used, regions_count, 128)) {
Expand All @@ -31,11 +32,11 @@ main(void) {
ptr2 = lwmem_malloc(4);
ptr3 = lwmem_malloc(4);
ptr4 = lwmem_malloc(16);
lwmem_free(ptr1); /* Free but keep value for future comparison */
lwmem_free(ptr3); /* Free but keep value for future comparison */
lwmem_free(ptr1); /* Free but keep value for future comparison */
lwmem_free(ptr3); /* Free but keep value for future comparison */
lwmem_debug_print(1, 1);
printf("Debug above is effectively state 3\r\n");
lwmem_debug_save_state(); /* Every restore operations rewinds here */
lwmem_debug_save_state(); /* Every restore operations rewinds here */

/* We always try to reallocate pointer ptr2 */

Expand All @@ -53,7 +54,7 @@ main(void) {
printf("State 3b\r\n");
rptr2 = lwmem_realloc(ptr2, 20);
lwmem_debug_print(1, 1);
ASSERT(rptr2 == ptr2);
ASSERT(rptr2 == ptr1);

/* Create 3c case */
printf("\r\n------------------------------------------------------------------------\r\n");
Expand Down
2 changes: 1 addition & 1 deletion docs/examples_src/example_realloc_enlarge_full_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ State 3b
| 4 | 0034A548 | 1 | 56 | 48 |Free block |
| 5 | 0034A580 | 0 | 0 | 0 |End of region |
|-------|----------|--------|------|------------------|----------------|
Assert failed with condition (rptr2 == ptr2)
Assert passed with condition (rptr2 == ptr1)

------------------------------------------------------------------------
-- > State restored to last saved!
Expand Down
22 changes: 18 additions & 4 deletions docs/get-started/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,18 @@ Update cloned to latest version
Add library to project
^^^^^^^^^^^^^^^^^^^^^^

At this point it is assumed that you have successfully download library, either cloned it or from releases page.
Next step is to add the library to the project, by means of source files to compiler inputs and header files in search path
At this point it is assumed that you have successfully download library, either with ``git clone`` command or with manual download from the library releases page.
Next step is to add the library to the project, by means of source files to compiler inputs and header files in search path.

*CMake* is the main supported build system. Package comes with the ``CMakeLists.txt`` and ``library.cmake`` files, both located in the ``lwmem`` directory:

* ``library.cmake``: It is a fully configured set of variables and with library definition. User can include this file to the project file with ``include(path/to/library.cmake)`` and then manually use the variables provided by the file, such as list of source files, include paths or necessary compiler definitions. It is up to the user to properly use the this file on its own.
* ``CMakeLists.txt``: It is a wrapper-only file and includes ``library.cmake`` file. It is used for when user wants to include the library to the main project by simply calling *CMake* ``add_subdirectory`` command, followed by ``target_link_libraries`` to link external library to the final project.

.. tip::
Open ``library.cmake`` and analyze the provided information. Among variables, you can also find list of all possible exposed libraries for the user.

If you do not use the *CMake*, you can do the following:

* Copy ``lwmem`` folder to your project, it contains library files
* Add ``lwmem/src/include`` folder to `include path` of your toolchain. This is where `C/C++` compiler can find the files during compilation process. Usually using ``-I`` flag
Expand All @@ -70,15 +80,19 @@ Configuration file
^^^^^^^^^^^^^^^^^^

Configuration file is used to overwrite default settings defined for the essential use case.
Library comes with template config file, which can be modified according to needs.
Library comes with template config file, which can be modified according to the application needs.
and it should be copied (or simply renamed in-place) and named ``lwmem_opts.h``

.. note::
Default configuration template file location: ``lwmem/src/include/lwmem/lwmem_opts_template.h``.
File must be renamed to ``lwmem_opts.h`` first and then copied to the project directory where compiler
include paths have access to it by using ``#include "lwmem_opts.h"``.

List of configuration options are available in the :ref:`api_lwmem_opt` section.
.. tip::
If you are using *CMake* build system, define the variable ``LWMEM_OPTS_FILE`` before adding library's directory to the *CMake* project.
Variable must contain the path to the user options file. If not provided and to avoid build error, one will be generated in the build directory.

Configuration options list is available available in the :ref:`api_lwmem_opt` section.
If any option is about to be modified, it should be done in configuration file

.. literalinclude:: ../../lwmem/src/include/lwmem/lwmem_opts_template.h
Expand Down
Loading

0 comments on commit 0f79742

Please sign in to comment.