Compare commits
46
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8902a752f5 | ||
|
|
3445da13f3 | ||
|
|
127530abaa | ||
|
|
ed0f538fb7 | ||
|
|
76ef061dec | ||
|
|
c98786a3b3 | ||
|
|
422f3c2c04 | ||
|
|
5ceb34d980 | ||
|
|
73e3d1d1bd | ||
|
|
044a085fb6 | ||
|
|
5344423ad9 | ||
|
|
76ea989865 | ||
|
|
29c4ba3dcc | ||
|
|
42184f69db | ||
|
|
fb8bf65233 | ||
|
|
1d86e51f4a | ||
|
|
53c35a22d7 | ||
|
|
722737d60c | ||
|
|
f951a947ec | ||
|
|
d9401371a6 | ||
|
|
690fd72c8b | ||
|
|
d318493c6c | ||
|
|
fecece19cb | ||
|
|
ea640482a7 | ||
|
|
3e558af559 | ||
|
|
91a602f774 | ||
|
|
6a5c136c72 | ||
|
|
e50fdc82ef | ||
|
|
ec0a082701 | ||
|
|
7bf3ea3a87 | ||
|
|
15492394ad | ||
|
|
09f40b5695 | ||
|
|
eabfcd1d49 | ||
|
|
00aeafc616 | ||
|
|
f512051807 | ||
|
|
4e8b929da9 | ||
|
|
5841f936e6 | ||
|
|
2ee40339b0 | ||
|
|
0c8eca893d | ||
|
|
b16048a55b | ||
|
|
9dc582c6bc | ||
|
|
1df8bf84fb | ||
|
|
e017a90e5c | ||
|
|
58b317b759 | ||
|
|
3a057fc319 | ||
|
|
703ab6ff9c |
No files matched your search
+16
-13
@@ -10,7 +10,7 @@ jobs:
|
||||
runs-on: ${{ matrix.platform }}
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: actions/checkout@v4
|
||||
- name: CPU Information
|
||||
run: lscpu || sysctl -a|grep cpu || echo "CPU information not available"
|
||||
- name: make check C99
|
||||
@@ -26,7 +26,7 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: actions/checkout@v4
|
||||
- name: update package list
|
||||
run: sudo apt-get update
|
||||
- name: install multilib
|
||||
@@ -50,7 +50,7 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: actions/checkout@v4
|
||||
- name: CPU Information
|
||||
run: lscpu || echo "CPU information not available"
|
||||
- name: Clone TCC
|
||||
@@ -64,13 +64,13 @@ jobs:
|
||||
run: make depend
|
||||
- name: make check
|
||||
shell: bash
|
||||
run: make CC="/tmp/tcc/bin/tcc -std=c99" check
|
||||
run: make CC="/tmp/tcc/bin/tcc -std=c99" check -j4 || make CC="/tmp/tcc/bin/tcc -std=c99" check -j2 || make CC="/tmp/tcc/bin/tcc -std=c99" check
|
||||
|
||||
build_linux_sanitize:
|
||||
runs-on: ubuntu-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: actions/checkout@v4
|
||||
- name: make sanitize
|
||||
working-directory: ./tests
|
||||
run: make sanitize -j 2
|
||||
@@ -79,7 +79,7 @@ jobs:
|
||||
runs-on: windows-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: actions/checkout@v4
|
||||
- name: make check
|
||||
shell: bash
|
||||
run: mingw32-make -j 2 CC="gcc -std=c99 -D__USE_MINGW_ANSI_STDIO=1" check
|
||||
@@ -90,7 +90,7 @@ jobs:
|
||||
run:
|
||||
shell: msys2 {0}
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: actions/checkout@v4
|
||||
- uses: msys2/setup-msys2@v2
|
||||
with:
|
||||
msystem: MINGW64
|
||||
@@ -103,7 +103,7 @@ jobs:
|
||||
runs-on: windows-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: actions/checkout@v4
|
||||
- name: make check
|
||||
shell: cmd
|
||||
working-directory: ./tests
|
||||
@@ -113,19 +113,21 @@ jobs:
|
||||
runs-on: windows-latest
|
||||
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: actions/checkout@v4
|
||||
- name: make check
|
||||
shell: cmd
|
||||
working-directory: ./tests
|
||||
run: Make-check-cl.bat clang-cl
|
||||
|
||||
# The host should always be linux
|
||||
build_freebsd:
|
||||
runs-on: macos-12
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: actions/checkout@v4
|
||||
- name: Test in FreeBSD
|
||||
id: test
|
||||
uses: vmactions/freebsd-vm@v0
|
||||
uses: vmactions/freebsd-vm@v1
|
||||
timeout-minutes: 60
|
||||
with:
|
||||
usesh: true
|
||||
run: |
|
||||
@@ -138,10 +140,11 @@ jobs:
|
||||
runs-on: ubuntu-latest
|
||||
name: Build on aarch64
|
||||
steps:
|
||||
- uses: actions/checkout@v3
|
||||
- uses: actions/checkout@v4
|
||||
- uses: uraimo/run-on-arch-action@v2.5.0
|
||||
name: Run commands on aarch64
|
||||
id: make
|
||||
timeout-minutes: 60
|
||||
with:
|
||||
arch: aarch64
|
||||
distro: bullseye
|
||||
|
||||
+1
-1
@@ -38,7 +38,7 @@ depend
|
||||
other/
|
||||
a*.dat
|
||||
config
|
||||
config.link
|
||||
config.*
|
||||
*.log
|
||||
.vscode
|
||||
.vs
|
||||
|
||||
@@ -34,14 +34,14 @@ DESTDIR=
|
||||
|
||||
# Package name & version.
|
||||
PACKAGE=m_lib-$(VERSION)
|
||||
VERSION=0.7.1
|
||||
VERSION=0.7.2
|
||||
|
||||
# Define the contain of the distribution tarball
|
||||
HEADER=m-algo.h m-array.h m-atomic.h m-bitset.h m-bptree.h m-buffer.h m-c-mempool.h m-concurrent.h m-core.h m-deque.h m-dict.h m-funcobj.h m-genint.h m-i-list.h m-i-shared.h m-list.h m-mempool.h m-thread.h m-mutex.h m-prioqueue.h m-rbtree.h m-serial-bin.h m-serial-json.h m-shared.h m-snapshot.h m-string.h m-tree.h m-try.h m-tuple.h m-variant.h m-worker.h
|
||||
HEADER=m-algo.h m-array.h m-atomic.h m-bitset.h m-bptree.h m-buffer.h m-c-mempool.h m-concurrent.h m-core.h m-deque.h m-dict.h m-funcobj.h m-generic.h m-genint.h m-i-list.h m-i-shared.h m-list.h m-mempool.h m-thread.h m-mutex.h m-prioqueue.h m-rbtree.h m-serial-bin.h m-serial-json.h m-shared.h m-snapshot.h m-string.h m-tree.h m-try.h m-tuple.h m-variant.h m-worker.h
|
||||
DOC1=LICENSE README.md
|
||||
DOC2=doc/API.txt doc/Container.html doc/Container.ods doc/depend.png doc/DEV.md doc/ISSUES.org doc/oplist.odp doc/oplist.png
|
||||
EXAMPLE=example/ex11-algo01.c example/ex11-algo02.c example/ex11-json01.c example/ex11-section.c example/ex-algo02.c example/ex-algo03.c example/ex-algo04.c example/ex-array00.c example/ex-array01.c example/ex-array02.c example/ex-array03.c example/ex-array04.c example/ex-array05.c example/ex-bptree01.c example/ex-buffer01.c example/ex-dict01.c example/ex-dict02.c example/ex-dict03.c example/ex-dict04.c example/ex-grep01.c example/ex-list01.c example/ex-mph.c example/ex-multi01.c example/ex11-multi02.c example/ex-multi03.c example/ex-multi04.c example/ex-multi05.c example/ex-rbtree01.c example/ex11-algo02.json example/ex11-json01.json example/Makefile example/ex-defer01.c example/ex-string01.c example/ex-string02.c example/ex-astar.c example/ex-string03.c example/ex11-tstc.c example/ex-no-stdio.c example/ex11-count-lines.c example/ex11-json-net.c example/ex11-rbtree02.c
|
||||
TEST=tests/test-malgo.c tests/test-marray.c tests/test-mbitset.c tests/test-mbptree.c tests/test-mbuffer.c tests/test-mcmempool.c tests/test-mconcurrent.c tests/test-mcore.c tests/test-mdeque.c tests/test-mdict.c tests/test-mfuncobj.c tests/test-mgenint.c tests/test-milist.c tests/test-mlist.c tests/test-mmempool.c tests/test-mmutex.c tests/test-mprioqueue.c tests/test-mrbtree.c tests/test-mserial-bin.c tests/test-mserial-json.c tests/test-mshared.c tests/test-msnapshot.c tests/test-mstring.c tests/test-mtuple.c tests/test-mvariant.c tests/test-mworker.c tests/tgen-bitset.c tests/tgen-marray.c tests/tgen-mdict.c tests/tgen-mlist.c tests/tgen-mstring.c tests/tgen-openmp.c tests/tgen-queue.c tests/tgen-shared.c tests/tgen-mserial.c tests/Makefile tests/coverage.h tests/test-obj.h tests/dict.txt tests/fail-chain-oplist.c tests/fail-incompatible.c tests/fail-no-oplist.c tests/test-mishared.c tests/check-array.cpp tests/check-deque.cpp tests/check-dplist.cpp tests/check-list.cpp tests/check-rbtree.cpp tests/check-uset.cpp tests/check-generic.hpp tests/test-mtree.c tests/check-bptree-map.cpp tests/check-bptree-set.cpp tests/check-prioqueue.cpp tests/check-umap.cpp
|
||||
DOC2=doc/API.txt doc/Container.html doc/Container.ods doc/depend.png doc/DEV.md doc/ISSUES.org doc/oplist.odp doc/oplist.png doc/bench-array-log.png doc/bench-array.png doc/bench-list-log.png doc/bench-list.png doc/bench-oset-log.png doc/bench-oset.png doc/bench-umap-log.png doc/bench-umap.png doc/cc.sh
|
||||
EXAMPLE=example/ex11-algo01.c example/ex11-algo02.c example/ex11-json01.c example/ex11-section.c example/ex-algo02.c example/ex-algo03.c example/ex-algo04.c example/ex-array00.c example/ex-array01.c example/ex-array02.c example/ex-array03.c example/ex-array04.c example/ex-array05.c example/ex-bptree01.c example/ex-buffer01.c example/ex-dict01.c example/ex-dict02.c example/ex-dict03.c example/ex-dict04.c example/ex-grep01.c example/ex-list01.c example/ex-mph.c example/ex-multi01.c example/ex11-multi02.c example/ex-multi03.c example/ex-multi04.c example/ex-multi05.c example/ex-rbtree01.c example/ex11-algo02.json example/ex11-json01.json example/Makefile example/ex-defer01.c example/ex-string01.c example/ex-string02.c example/ex-astar.c example/ex-string03.c example/ex11-tstc.c example/ex-no-stdio.c example/ex11-count-lines.c example/ex11-json-net.c example/ex11-rbtree02.c example/ex-curl.c example/ex-dict05.c example/ex-dict06.c example/exm-lib.c example/exm-main.c example/exn-lib.c example/exn-main.c example/exm-header.h example/exn-header.h example/ex-alloc1.c example/ex-alloc2.c example/ex-try01.c example/ex11-generic01.c
|
||||
TEST=tests/test-malgo.c tests/test-marray.c tests/test-mbitset.c tests/test-mbptree.c tests/test-mbuffer.c tests/test-mcmempool.c tests/test-mconcurrent.c tests/test-mcore.c tests/test-mdeque.c tests/test-mdict.c tests/test-mfuncobj.c tests/test-mgenint.c tests/test-milist.c tests/test-mlist.c tests/test-mmempool.c tests/test-mmutex.c tests/test-mprioqueue.c tests/test-mrbtree.c tests/test-mserial-bin.c tests/test-mserial-json.c tests/test-mshared.c tests/test-msnapshot.c tests/test-mstring.c tests/test-mtuple.c tests/test-mvariant.c tests/test-mworker.c tests/tgen-bitset.c tests/tgen-marray.c tests/tgen-mdict.c tests/tgen-mlist.c tests/tgen-mstring.c tests/tgen-openmp.c tests/tgen-queue.c tests/tgen-shared.c tests/tgen-mserial.c tests/Makefile tests/coverage.h tests/test-obj.h tests/dict.txt tests/fail-chain-oplist.c tests/fail-incompatible.c tests/fail-no-oplist.c tests/test-mishared.c tests/check-array.cpp tests/check-deque.cpp tests/check-dplist.cpp tests/check-list.cpp tests/check-rbtree.cpp tests/check-uset.cpp tests/check-generic.hpp tests/test-mtree.c tests/check-bptree-map.cpp tests/check-bptree-set.cpp tests/check-prioqueue.cpp tests/check-umap.cpp tests/test-mtry.c tests/tgen-mmap.c tests/tgen-try.c tests/tgen-tuple.c tests/test-mgeneric.c
|
||||
|
||||
.PHONY: all test check doc clean distclean depend install uninstall dist
|
||||
|
||||
|
||||
@@ -41,6 +41,7 @@ M\*LIB: Generic type-safe Container Library for C language
|
||||
27. [Memory pool](#m-mempool)
|
||||
28. [JSON Serialization](#m-serial-json)
|
||||
29. [Binary Serialization](#m-serial-bin)
|
||||
30. [Generic interface](#m-generic)
|
||||
12. [Global User Customization](#global-user-customization)
|
||||
13. [License](#license)
|
||||
|
||||
@@ -179,6 +180,7 @@ Other headers offering other functionality are:
|
||||
* [m-worker.h](#m-worker): header for providing an easy pool of workers on separated threads to handle work orders (used for parallel tasks),
|
||||
* [m-serial-json.h](#m-serial-json): header for importing / exporting the containers in [JSON format](https://en.wikipedia.org/wiki/JSON),
|
||||
* [m-serial-bin.h](#m-serial-bin): header for importing / exporting the containers in an adhoc fast binary format,
|
||||
* [m-generic.h](#m-generic): header for using a common interface for all registered types,
|
||||
* m-genint.h: internal header for generating unique integers in a concurrent context,
|
||||
* [m-core.h](#m-core): header for meta-programming with the C preprocessor (used by all other headers).
|
||||
|
||||
@@ -492,15 +494,17 @@ of the library.
|
||||
The final goal of the library is to be able to write code like this in pure C while keeping type safety and compile time name resolution:
|
||||
|
||||
```C
|
||||
let(list, list_uint_t) {
|
||||
M_LET(list, list_uint_t) {
|
||||
push(list, 42);
|
||||
push(list, 17);
|
||||
for each (item, list) {
|
||||
print(item, "\n");
|
||||
M_PRINT(*item, "\n");
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
See #M-GENERIC header for details.
|
||||
|
||||
## Performance
|
||||
|
||||
M\*LIB performance is compared to the one of GNU C++ STL (v10.2) in the following graphs.
|
||||
@@ -4434,6 +4438,18 @@ Initialize the shared pointer 'shared' to the same object than the one
|
||||
pointed by 'src' (sharing ownership).
|
||||
This function is thread safe from 'src' point of view.
|
||||
|
||||
##### void name\_init\_with\[suffix\](shared\_t shared, args...)
|
||||
|
||||
Initialize the shared pointer 'shared' to the new object initilized with args.
|
||||
The provided arguments shall therefore match one of the constructor provided
|
||||
by the EMPLACE\_TYPE operator.
|
||||
There is one generated method per suffix defined in the EMPLACE\_TYPE operator,
|
||||
and the 'suffix' in the generated method name corresponds to the suffix defined in
|
||||
in the EMPLACE\_TYPE operator (it can be empty).
|
||||
This method is created only if the EMPLACE\_TYPE operator is provided.
|
||||
See emplace chapter for more details.
|
||||
It is equivalent to the C++ make\_shared.
|
||||
|
||||
##### bool name\_NULL\_p(const shared\_t shared)
|
||||
|
||||
Return true if shared doesn't reference any object (i.e. is the NULL pointer).
|
||||
@@ -6364,20 +6380,15 @@ Theses macros are only valid if the program is built in C11 mode:
|
||||
Return the printf format associated to the type of 'x'.
|
||||
'x' shall be a basic C variable, printable with printf.
|
||||
|
||||
##### M\_PRINT\_ARG(x)
|
||||
##### M\_FPRINT\_ARG(file, x)
|
||||
|
||||
Print using printf the variable 'x'.
|
||||
Print into a file 'file' using fprintf the variable 'x'.
|
||||
|
||||
The format of 'x' is deduced provided that it is a standard numerical C type.
|
||||
If m-string is included, it supports also the type 'string\_t' natively.
|
||||
If the argument is extended (i.e. in the format '(var, optype)' with optype being either an oplist or a type
|
||||
with a globally registered oplist), then it will call the OUT\_STR method of the oplist on the variable 'var'.
|
||||
|
||||
##### M\_FPRINT\_ARG(file, x)
|
||||
|
||||
Print into a file 'file' using fprintf the variable 'x'.
|
||||
See M\_PRINT\_ARG for details on the supported variable.
|
||||
|
||||
##### M\_GET\_STRING\_ARG(string,x,append)
|
||||
|
||||
Print into the string\_t 'string' the variable 'x'.
|
||||
@@ -7996,7 +8007,8 @@ Example:
|
||||
{
|
||||
sumint_t s;
|
||||
sumint_init_with(s, 16);
|
||||
int n = f(sumint_as_interface(s));
|
||||
interface_t interface = sumint_as_interface(s);
|
||||
int n = f(interface);
|
||||
printf("sum=%d\n", n);
|
||||
sumint_clear(s);
|
||||
}
|
||||
@@ -8025,6 +8037,7 @@ It will define the following type and functions:
|
||||
|
||||
Type representing an interface to such function object.
|
||||
There is only one method for such type (see below).
|
||||
It is a pointer to an hidded structure and can be assigned.
|
||||
|
||||
##### retcode\_type name\_call(name\_t interface, type\_of\_param1, type\_of\_param 2, ...)
|
||||
|
||||
@@ -8402,6 +8415,171 @@ otherwise the behavior of the object is undefined.
|
||||
|
||||
Clear the serialization object 'serial'.
|
||||
|
||||
### M-GENERIC
|
||||
|
||||
This header is for registering and using a generic interface, regardless of the real type.
|
||||
More precisely it provides way of registering the oplist of a type. Then a variable of this type can be used in macro-like functions (init, clear, push, ...) and the associated method of this oplist will be used to handle the variable.
|
||||
If no type is associated to this variable, an error is reported by the compiler.
|
||||
|
||||
This header needs a C23 compliant compiler or a C11 compiler providing the typeof extension (like GCC, CLANG, MSVC).
|
||||
NOTE: TCC is not supported yet due to some bugs in the compiler.
|
||||
|
||||
This provide the same level of flexibility of the C++. However there is some drawbacks of using this generic interface:
|
||||
|
||||
* error messages can be more complex,
|
||||
* compilation time increase a lot (on pair with C++)
|
||||
|
||||
This header is still a WIP and is currently more a demo (Not ready for production).
|
||||
|
||||
#### Example
|
||||
|
||||
A concrete example of the generic header is the following.
|
||||
It reuses the basic example of the introduction of the library.
|
||||
|
||||
```C
|
||||
#include "m-list.h"
|
||||
#include "m-generic.h"
|
||||
|
||||
LIST_DEF(list_uint, unsigned int)
|
||||
|
||||
// Register oplist for M_LET & M_EACH :
|
||||
#define M_OPL_list_uint_t() LIST_OPLIST(list_uint, M_BASIC_OPLIST)
|
||||
|
||||
// Register for Generic (Organization, Component & Oplist)
|
||||
#define M_GENERIC_ORG_1() (USER)
|
||||
#define M_GENERIC_ORG_USER_COMP_1() (CORE)
|
||||
#define M_GENERIC_ORG_USER_COMP_CORE_OPLIST_1() M_OPL_list_uint_t()
|
||||
|
||||
int main(void)
|
||||
{
|
||||
M_LET(list, list_uint_t) {
|
||||
push(list, 42);
|
||||
push(list, 17);
|
||||
for each(item, list) {
|
||||
M_PRINT("ITEM=", *item, "\n");
|
||||
}
|
||||
M_PRINT("List = ", list, "\n");
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
|
||||
#### Registration
|
||||
|
||||
To register an oplist you need to register three things:
|
||||
|
||||
* the *integration user* (user responsible for the integration of the whole program) registers all the *organizations* by mapping an integer from 1 to 50 to the short name of the organization.
|
||||
* the *organization user* (user responsible for the integration produced by an organization, typically a library or the program without its libraries) chooses the short name of the organization and registers all the *components* of this organization by mapping an integer from 1 to 50 to the short name of the component.
|
||||
* the *component user* (user responsible for a component of a library or a program) registers all the *oplists* of this component by mapping an integer from 1 to 50 to the oplist.
|
||||
|
||||
This 3 levels registration is used to enable support for complex integration in real project. So, it maps the basic structure of a program so that each independent layer can register its own oplists independently of the others, and only the final integrator maps everything together.
|
||||
|
||||
To register an organization the *integration user* shall define a macro like this for all organizations:
|
||||
|
||||
```C
|
||||
#define M_GENERIC_ORG_<A_NUM>() (<ORGA_NAME>)
|
||||
```
|
||||
with
|
||||
|
||||
* <A_NUM> replaced by a unique integer in the range 1 to 50
|
||||
* <ORGA_NAME> replaced by the short name of the organization.
|
||||
|
||||
|
||||
To register a component the *organization user* shall define a macro like this for all components:
|
||||
|
||||
```C
|
||||
#define M_GENERIC_ORG_<ORGA_NAME>_COMP_<A_NUM>() (<COMP_NAME>)
|
||||
```
|
||||
with
|
||||
|
||||
* <A_NUM> replaced by a unique integer in the range 1 to 50
|
||||
* <ORGA_NAME> replaced by the short name of the organization.
|
||||
* <COMP_NAME> replaced by the short name of the component.
|
||||
|
||||
|
||||
To register an oplist the *component user* shall define a macro like this for all oplists:
|
||||
|
||||
```C
|
||||
#define M_GENERIC_ORG_<ORGA_NAME>_COMP_<COMP_NAME>_OPLIST_<A_NUM>() <OP_LIST>
|
||||
```
|
||||
with
|
||||
|
||||
* <A_NUM> replaced by a unique integer in the range 1 to 50
|
||||
* <ORGA_NAME> replaced by the short name of the organization.
|
||||
* <COMP_NAME> replaced by the short name of the component.
|
||||
* <OP_LIST> replaced by the oplist (no need for extra parenthesis).
|
||||
|
||||
|
||||
Theses macros should be defined in the header associated to their responsibility.
|
||||
To register something, you don't need to include the m-register header file.
|
||||
|
||||
|
||||
#### Usage
|
||||
|
||||
The following macro functions are defined. They have the same behavior as their corresponding OPERATOR of the oplists:
|
||||
|
||||
* init(x)
|
||||
* init_set(x, y)
|
||||
* init_move(x, y)
|
||||
* move(x, y)
|
||||
* set(x, y)
|
||||
* clear(x)
|
||||
* swap(x, y)
|
||||
* hash(x)
|
||||
* equal(x, y)
|
||||
* cmp(x, y)
|
||||
* sort(x)
|
||||
* splice_back(d, s, i)
|
||||
* splice_at(d, id, s, is)
|
||||
* it_first(x, y)
|
||||
* it_last(x, y)
|
||||
* it_end(x, y)
|
||||
* it_set(d, s)
|
||||
* it_end_p(i)
|
||||
* it_last_p(i)
|
||||
* it_next(i)
|
||||
* it_previous(i)
|
||||
* it_ref(i)
|
||||
* it_cref(i)
|
||||
* it_equal(x, y)
|
||||
* it_insert(c, i, o)
|
||||
* it_remove(c, i)
|
||||
* empty_p(x)
|
||||
* add(x, y)
|
||||
* sub(x, y)
|
||||
* mul(x, y)
|
||||
* div(x, y)
|
||||
* reset(x)
|
||||
* get(x, y)
|
||||
* set_at(x, y, z)
|
||||
* safe_get(x, y)
|
||||
* erase(x, y)
|
||||
* get_size(x)
|
||||
* push(x, y)
|
||||
* pop(x, y)
|
||||
* push_move(x, y)
|
||||
* pop_move(x, y)
|
||||
* reverse(x)
|
||||
* get_str(s, c, b)
|
||||
* parse_str(c, s, e)
|
||||
* out_str(x, y)
|
||||
* in_str(x, y)
|
||||
* out_serial(x, y)
|
||||
* in_serial(x, y)
|
||||
|
||||
The following macro are defined and represents a type associated to the variable. They have the same behavior as their corresponding OPERATOR of the oplists and use the typeof keyword:
|
||||
|
||||
* it_type(x)
|
||||
* sub_type(x)
|
||||
* key_type(x)
|
||||
* value_type(x)
|
||||
|
||||
The following macro is defined and enabled to iterate over a container (See example):
|
||||
|
||||
* each(item, container)
|
||||
|
||||
Note that only short names are defined in this header which may be incompatible with your own namespace (This header is still a WIP)
|
||||
|
||||
|
||||
## Global User Customization
|
||||
|
||||
@@ -8464,6 +8642,7 @@ Define the thread backend to use by m-thread.h:
|
||||
* 1: for C11 header threads.h
|
||||
* 2: for WINDOWS header windows.h
|
||||
* 3: for POSIX THREAD header pthread.h
|
||||
* 4: for FreeRTOS
|
||||
|
||||
Default value: auto-detect in function of the running system.
|
||||
|
||||
@@ -8562,6 +8741,9 @@ M\_USE\_DECL / M\_USE\_DEF shall be defined without effective value.
|
||||
|
||||
See M\_USE\_DECL for more details.
|
||||
|
||||
#### M\_USE\_PRINT\_OPLIST
|
||||
|
||||
If defined, it displays the oplist associated to the static assert.
|
||||
|
||||
#### M\_MEMORY\_ALLOC
|
||||
|
||||
|
||||
+14
-2
@@ -322,8 +322,11 @@ bench-xxhash0:
|
||||
bench-boost:
|
||||
@if test -n "$${BOOST}" ; then $(MAKE) bench-boost0 ; else echo "Nothing to be done for BOOST." ; fi
|
||||
bench-boost0:
|
||||
$(CXX) $(CFLAGS) $(XCFLAGS) $(CPPFLAGS) -I "$${BOOST}" bench-boost.cpp common.c -DMULTI_THREAD_MEASURE -o bench-boost.exe -lpthread
|
||||
@./bench-boost.exe 60
|
||||
$(CXX) $(CFLAGS) $(XCFLAGS) $(CPPFLAGS) -I "$${BOOST}" bench-boost.cpp common.c -DMULTI_THREAD_MEASURE -o bench-boost-thread.exe -lpthread
|
||||
$(CXX) $(CFLAGS) $(XCFLAGS) $(CPPFLAGS) -I "$${BOOST}" bench-boost.cpp common.c -o bench-boost.exe -lpthread
|
||||
@./bench-boost.exe 42
|
||||
@./bench-boost.exe 41
|
||||
@./bench-boost-thread.exe 60
|
||||
|
||||
# NEDTRIES shall point to the header location.
|
||||
bench-nedtries:
|
||||
@@ -399,6 +402,15 @@ bench-CC0:
|
||||
@./bench-CC.exe 40
|
||||
@./bench-CC.exe 41
|
||||
|
||||
# VERSTABLE shall point to the source directory of VERSTABLE (where the headers are)
|
||||
bench-verstable:
|
||||
@if test -n "$${VERSTABLE}" ; then $(MAKE) bench-verstable0 ; else echo "Nothing to be done for VERSTABLE." ; fi
|
||||
|
||||
bench-verstable0:
|
||||
$(CC) $(CFLAGS) $(XCFLAGS) $(CPPFLAGS) -I $${VERSTABLE}/ bench-verstable.c common.c -o bench-verstable.exe
|
||||
@./bench-verstable.exe 40
|
||||
@./bench-verstable.exe 41
|
||||
|
||||
############################################################################
|
||||
|
||||
bench-mlib-json: bench-mlib
|
||||
|
||||
+105
-1
@@ -3,11 +3,111 @@
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
#include "m-thread.h"
|
||||
|
||||
#include <boost/unordered/unordered_flat_map.hpp>
|
||||
#include <boost/lockfree/queue.hpp>
|
||||
|
||||
#include "common.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
/********************************************************************************************/
|
||||
|
||||
static void
|
||||
test_dict2(size_t n)
|
||||
{
|
||||
boost::unordered_flat_map<unsigned long, unsigned long> dict;
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
dict[rand_get()] = rand_get();
|
||||
}
|
||||
rand_init();
|
||||
unsigned int s = 0;
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
boost::unordered_flat_map<unsigned long, unsigned long>::iterator it = dict.find(rand_get());
|
||||
if (it != dict.end())
|
||||
s += it->second;
|
||||
}
|
||||
g_result = s;
|
||||
}
|
||||
|
||||
static void
|
||||
test_dict2_linear(size_t n)
|
||||
{
|
||||
boost::unordered_flat_map<unsigned long, unsigned long> dict;
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
dict[i] = rand_get();
|
||||
}
|
||||
rand_init();
|
||||
unsigned int s = 0;
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
boost::unordered_flat_map<unsigned long, unsigned long>::iterator it = dict.find(i);
|
||||
if (it != dict.end())
|
||||
s += it->second;
|
||||
}
|
||||
g_result = s;
|
||||
}
|
||||
|
||||
/********************************************************************************************/
|
||||
struct char_array_s {
|
||||
char a[256];
|
||||
char_array_s () { a[0] = 0 ; }
|
||||
char_array_s ( const char_array_s & other) { strcpy(a, other.a); }
|
||||
bool operator==(const char_array_s &other) const { return strcmp(a, other.a) == 0; }
|
||||
};
|
||||
|
||||
inline std::size_t hash_value(const char_array_s &k) {
|
||||
size_t hash = 0;
|
||||
const char *s = k.a;
|
||||
while (*s) hash = hash * 31421 + (*s++) + 6927;
|
||||
return hash;
|
||||
}
|
||||
|
||||
static void
|
||||
test_dict_big(size_t n)
|
||||
{
|
||||
boost::unordered_flat_map<char_array_s, char_array_s> dict;
|
||||
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
char_array_s s1, s2;
|
||||
sprintf(s1.a, "%u", rand_get());
|
||||
sprintf(s2.a, "%u", rand_get());
|
||||
dict[s1] = s2;
|
||||
}
|
||||
rand_init();
|
||||
unsigned int s = 0;
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
char_array_s s1;
|
||||
sprintf(s1.a, "%u", rand_get());
|
||||
boost::unordered_flat_map<char_array_s, char_array_s>::iterator it = dict.find(s1);
|
||||
if (it != dict.end())
|
||||
s ++;
|
||||
}
|
||||
g_result = s;
|
||||
}
|
||||
|
||||
/********************************************************************************************/
|
||||
static void
|
||||
test_dict_str(size_t n)
|
||||
{
|
||||
boost::unordered_flat_map<string, string> dict;
|
||||
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
string s1 = static_cast<ostringstream*>( &(ostringstream() << rand_get()) )->str();
|
||||
string s2 = static_cast<ostringstream*>( &(ostringstream() << rand_get()) )->str();
|
||||
dict[s1] = s2;
|
||||
}
|
||||
rand_init();
|
||||
unsigned int s = 0;
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
string s1 = static_cast<ostringstream*>( &(ostringstream() << rand_get()) )->str();
|
||||
boost::unordered_flat_map<string, string>::iterator it = dict.find(s1);
|
||||
if (it != dict.end())
|
||||
s ++;
|
||||
}
|
||||
g_result = s;
|
||||
}
|
||||
|
||||
/********************************************************************************************/
|
||||
boost::lockfree::queue<unsigned int, boost::lockfree::capacity<4*64>> g_buff;
|
||||
boost::lockfree::queue<unsigned long long, boost::lockfree::capacity<4*64>> g_final;
|
||||
@@ -86,11 +186,15 @@ static void test_queue(size_t n)
|
||||
/********************************************************************************************/
|
||||
|
||||
const config_func_t table[] = {
|
||||
{ 41, "dictBig", 1000000, 0, test_dict_big, 0},
|
||||
{ 42, "dict", 1000000, 0, test_dict2, 0},
|
||||
{ 43, "DictStr", 1000000, 0, test_dict_str, 0},
|
||||
{ 46, "DictLinear", 1000000, 0, test_dict2_linear, 0},
|
||||
{ 60, "Queue MPMC", 1000000, 0, test_queue, 0}
|
||||
};
|
||||
|
||||
int main(int argc, const char *argv[])
|
||||
{
|
||||
test("BOOST-LOCKFREE", numberof(table), table, argc, argv);
|
||||
test("BOOST", numberof(table), table, argc, argv);
|
||||
exit(0);
|
||||
}
|
||||
@@ -0,0 +1,94 @@
|
||||
#define NDEBUG
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "m-core.h"
|
||||
#include "common.h"
|
||||
|
||||
#define NAME umap_ulong
|
||||
#define KEY_TY unsigned long
|
||||
#define VAL_TY unsigned long
|
||||
#include "verstable.h"
|
||||
|
||||
static void
|
||||
test_dict(size_t n)
|
||||
{
|
||||
umap_ulong dict;
|
||||
umap_ulong_init(&dict);
|
||||
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
unsigned long value = rand_get();
|
||||
unsigned long key = rand_get();
|
||||
umap_ulong_insert(&dict, key, value);
|
||||
}
|
||||
rand_init();
|
||||
unsigned int s = 0;
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
umap_ulong_itr it = umap_ulong_get(&dict, rand_get());
|
||||
if (!umap_ulong_is_end(it) )
|
||||
s += it.data->val;
|
||||
}
|
||||
g_result = s;
|
||||
|
||||
umap_ulong_cleanup(&dict);
|
||||
}
|
||||
|
||||
/********************************************************************************************/
|
||||
|
||||
#ifndef CHAR_ARRAY_SIZE
|
||||
#define CHAR_ARRAY_SIZE 256
|
||||
#endif
|
||||
typedef char char_array_t[CHAR_ARRAY_SIZE];
|
||||
typedef struct { char_array_t a; } char_encap;// Cannot use char_array_t directly
|
||||
static bool char_equal_p (const char_encap a, const char_encap b) { return strcmp(a.a,b.a)==0; }
|
||||
static size_t char_hash(const char_encap a) { return m_core_hash (a.a, strlen(a.a)); }
|
||||
|
||||
#define NAME umap_char_array
|
||||
#define KEY_TY char_encap
|
||||
#define VAL_TY char_encap
|
||||
#define HASH_FN char_hash
|
||||
#define CMPR_FN char_equal_p
|
||||
#include "verstable.h"
|
||||
|
||||
static void
|
||||
test_dict_big(size_t n)
|
||||
{
|
||||
umap_char_array dict;
|
||||
umap_char_array_init(&dict);
|
||||
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
char_encap s1, s2;
|
||||
|
||||
sprintf(s1.a, "%u", rand_get());
|
||||
sprintf(s2.a, "%u", rand_get());
|
||||
umap_char_array_insert(&dict, s1, s2);
|
||||
}
|
||||
rand_init();
|
||||
unsigned int s = 0;
|
||||
for (size_t i = 0; i < n; i++) {
|
||||
char_encap s1;
|
||||
sprintf(s1.a, "%u", rand_get());
|
||||
umap_char_array_itr it = umap_char_array_get(&dict, s1);
|
||||
if (!umap_char_array_is_end(it) )
|
||||
s ++;
|
||||
}
|
||||
g_result = s;
|
||||
|
||||
umap_char_array_cleanup(&dict);
|
||||
}
|
||||
|
||||
/********************************************************************************************/
|
||||
|
||||
const config_func_t table[] = {
|
||||
{ 40, "dict", 1000000, 0, test_dict, 0},
|
||||
{ 41, "dictBig", 1000000, 0, test_dict_big, 0},
|
||||
};
|
||||
|
||||
int main(int argc, const char *argv[])
|
||||
{
|
||||
test("CTL", numberof(table), table, argc, argv);
|
||||
exit(0);
|
||||
}
|
||||
|
||||
+24
-1
@@ -41,8 +41,28 @@ of the GAIA by adding support for identification of specialization like this:
|
||||
|
||||
** Open points:
|
||||
|
||||
- How to get access to the specialization field of the container? ==> Define the "self" name as the name of the container for all methods? What to do when there are several containers for a method?
|
||||
- How to get access to the specialization field of the container?
|
||||
==> Define the "self" name as the name of the container for all methods?
|
||||
- What to do when there are several containers for a method?
|
||||
==> self shall be the destination?
|
||||
|
||||
- How to initialize the specialization field of the container? provide custom initialization functions? It seems to be the hard point.
|
||||
* INIT: Force a default to NULL ?
|
||||
* INIT_SET: Force the same value than the src? We may want another pool!
|
||||
* INIT_MOVE: Force the same value than the src. Mandatory
|
||||
* MOVE: Assume the same value. Mandatory
|
||||
* SET: Use the already define pool
|
||||
* INIT_WITH: Force a default to NULL?
|
||||
==> init_emplace ?
|
||||
-- How can we handle containers that allocate on initialization? It cannot, so this solution seems broken
|
||||
==> Modify constructor prototype to add allocators (except for MOVE/INIT_MOVE)
|
||||
* INIT(dest) ==> INIT(dest, context)
|
||||
* INIT(dest, src) ==> INIT_SET(dest, context, src)
|
||||
* INIT_WITH(dest, ...) ==> INIT_WITH(dest, context, ...)
|
||||
* INIT emplace(dest [custom]) ==> INIT_emplace(dest, context, [custom])
|
||||
==> How to pass context? Inherited from master container? A CONTEXT may be present in the parent container but the child may not accept CONTEXT... Same than the Heap Allocator is not the same between different types... The allocator of the parent may be different from the one of the child... ==> Force context to be the same ?
|
||||
==> If a type A uses CONTEXT, all containers constructed from A shall use CONTEXT too
|
||||
-- Need to use an oplist for context or force basic type?
|
||||
|
||||
* TODO #31 : Uniformize parametrisation options of containers :ENHANCEMENT:
|
||||
|
||||
@@ -281,6 +301,9 @@ unsigned get_small_hash(int64_t x) {
|
||||
}
|
||||
#+END_SRC
|
||||
|
||||
Can also (should?) use SIMD to test for several hash entries at the same times
|
||||
In which case a complete new implementation will be needed
|
||||
|
||||
* TODO #25 : Support of error return model for error handling. :ENHANCEMENT:
|
||||
|
||||
Find a way to support error return code for the API in case of allocation
|
||||
|
||||
Binary file not shown.
|
Before Width: | Height: | Size: 113 KiB After Width: | Height: | Size: 128 KiB |
+4
-4
@@ -31,7 +31,7 @@ RM=rm -f
|
||||
.SUFFIXES: .c .exe
|
||||
|
||||
.c.exe:
|
||||
$(CC) $(CFLAGS) $(CPPFLAGS) $< -o $@ `cat config` $(LDFLAGS)
|
||||
$(CC) $(CFLAGS) $(CPPFLAGS) $< -o $@ `cat config` `cat config.curl` $(LDFLAGS)
|
||||
|
||||
.c.o:
|
||||
@$(MAKE) config
|
||||
@@ -53,11 +53,11 @@ config:
|
||||
$(CC) $(CFLAGS) $(CPPFLAGS) -I$(GMP_DIR)/include -L$(GMP_DIR)/lib config-test.c -lgmp -o config-test.exe $(LDFLAGS) 2> /dev/null && echo "-DHAVE_GMP=1 -DHAVE_MPFR=0 -I$(GMP_DIR)/include -L$(GMP_DIR)/lib -lgmp" > config || echo "-DHAVE_GMP=0 -DHAVE_MPFR=0 " > config
|
||||
printf "#include <mpfr.h>\nint main(void){mpfr_t z; mpfr_init(z); return 0;}\n" > config-test.c
|
||||
$(CC) $(CFLAGS) $(CPPFLAGS) -I$(GMP_DIR)/include -I$(MPFR_DIR)/include -L$(GMP_DIR)/lib -L$(MPFR_DIR)/lib config-test.c -lmpfr -lgmp -o config-test.exe $(LDFLAGS) 2> /dev/null && echo "-DHAVE_GMP=1 -DHAVE_MPFR=1 -I$(GMP_DIR)/include -L$(GMP_DIR)/lib -I$(MPFR_DIR)/include -L$(MPFR_DIR)/lib -lmpfr -lgmp" > config || echo "MPFR not available"
|
||||
printf "#include <curl/curl.h>\nint main(void){curl_global_init(CURL_GLOBAL_DEFAULT);}" > config-test.c
|
||||
$(CC) $(CFLAGS) $(CPPFLAGS) config-test.c `curl-config --cflags --libs` -o config-test.exe 2> /dev/null && echo "-DHAVE_CURL=1 `curl-config --cflags --libs`" > config.curl || echo "-DHAVE_CURL=0 " > config.curl
|
||||
printf "#include <stdio.h>\nint main(void){printf(\"Ok\"); return 0;}\n" > config-test.c
|
||||
$(CC) $(CFLAGS) $(CPPFLAGS) config-test.c -ffunction-sections -fdata-sections -Wl,--gc-sections 2> /dev/null && echo "-ffunction-sections -fdata-sections -Wl,--gc-sections" > config.link || echo "" > config.link
|
||||
$(RM) config-test.c config-test.exe
|
||||
|
||||
clean:
|
||||
$(RM) *.exe *.s *~ *.o ./a.dat config config.link
|
||||
|
||||
|
||||
$(RM) *.exe *.s *~ *.o ./a.dat config config.link config.curl
|
||||
@@ -0,0 +1,109 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
|
||||
static size_t allocated_size = 0;
|
||||
|
||||
static void* my_alloc(size_t s)
|
||||
{
|
||||
printf("Alloc called s=%lu!\n", s);
|
||||
allocated_size += s;
|
||||
return malloc(s);
|
||||
}
|
||||
|
||||
static void my_free(void *ptr, size_t s)
|
||||
{
|
||||
printf("Free called s=%lu!\n", s);
|
||||
allocated_size -= s;
|
||||
free(ptr);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
|
||||
static void *last_alloc;
|
||||
|
||||
static void* my_array_alloc(size_t s, void *org, size_t n)
|
||||
{
|
||||
printf("Array Alloc called org=%p of n=%zu elements of size s=%lu!\n", org, n, s);
|
||||
last_alloc = realloc(org, s*n);
|
||||
return last_alloc;
|
||||
}
|
||||
|
||||
static void my_array_free(void *ptr)
|
||||
{
|
||||
printf("Array Free called ptr=%p last_alloc=%p!\n", ptr, last_alloc);
|
||||
if (ptr != last_alloc) {
|
||||
printf("Somthing went wrong.\n");
|
||||
exit(4);
|
||||
}
|
||||
free(ptr);
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
|
||||
// Allocate for single object
|
||||
# define M_MEMORY_ALLOC(type) my_alloc (sizeof (type))
|
||||
# define M_MEMORY_DEL(ptr) my_free(ptr, sizeof *ptr)
|
||||
|
||||
// Allocate for array of objects
|
||||
# define M_MEMORY_REALLOC(type, ptr, n) my_array_alloc (sizeof (type), ptr, n)
|
||||
# define M_MEMORY_FREE(ptr) my_array_free(ptr)
|
||||
|
||||
#include "m-list.h"
|
||||
#include "m-array.h"
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
|
||||
LIST_DEF(list_int, int)
|
||||
|
||||
static void test_list(void)
|
||||
{
|
||||
printf("List demo\n");
|
||||
|
||||
list_int_t list;
|
||||
list_int_init(list);
|
||||
|
||||
list_int_push_back(list, 34);
|
||||
list_int_push_back(list, 34);
|
||||
|
||||
if (allocated_size == 0) {
|
||||
printf("Nothing was allocated... Very strange...\n");
|
||||
exit(1);
|
||||
}
|
||||
list_int_clear(list);
|
||||
|
||||
if (allocated_size != 0) {
|
||||
printf("Nothing was freed (%lu)... Very strange...\n", allocated_size);
|
||||
exit(2);
|
||||
}
|
||||
printf("Global custom allocator for list works!\n");
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
|
||||
ARRAY_DEF(array_int, int)
|
||||
|
||||
static void test_array(void)
|
||||
{
|
||||
printf("Array demo\n");
|
||||
|
||||
array_int_t array;
|
||||
array_int_init(array);
|
||||
|
||||
array_int_push_back(array, 34);
|
||||
array_int_push_back(array, 34);
|
||||
|
||||
array_int_clear(array);
|
||||
|
||||
printf("Global custom allocator for array works!\n");
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
|
||||
int main(void)
|
||||
{
|
||||
test_list();
|
||||
test_array();
|
||||
exit(0);
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "m-list.h"
|
||||
#include "m-array.h"
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
|
||||
static size_t allocated_size = 0;
|
||||
|
||||
static void* my_alloc(size_t s)
|
||||
{
|
||||
printf("Alloc called s=%lu!\n", s);
|
||||
allocated_size += s;
|
||||
return malloc(s);
|
||||
}
|
||||
|
||||
static void my_free(void *ptr, size_t s)
|
||||
{
|
||||
printf("Free called s=%lu!\n", s);
|
||||
allocated_size -= s;
|
||||
free(ptr);
|
||||
}
|
||||
|
||||
// Allocate for single object
|
||||
# define MY_MEMORY_ALLOC(type) my_alloc (sizeof (type))
|
||||
# define MY_MEMORY_DEL(ptr) my_free(ptr, sizeof *ptr)
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
|
||||
// Override only for this container the memory allocation function
|
||||
LIST_DEF(list_int, int, M_OPEXTEND(M_BASIC_OPLIST, NEW(MY_MEMORY_ALLOC), DEL(MY_MEMORY_DEL)))
|
||||
|
||||
static void test_list(void)
|
||||
{
|
||||
printf("List demo\n");
|
||||
|
||||
list_int_t list;
|
||||
list_int_init(list);
|
||||
|
||||
list_int_push_back(list, 34);
|
||||
list_int_push_back(list, 34);
|
||||
|
||||
if (allocated_size == 0) {
|
||||
printf("Nothing was allocated... Very strange...\n");
|
||||
exit(1);
|
||||
}
|
||||
list_int_clear(list);
|
||||
|
||||
if (allocated_size != 0) {
|
||||
printf("Nothing was freed (%lu)... Very strange...\n", allocated_size);
|
||||
exit(2);
|
||||
}
|
||||
printf("Global custom allocator for list works!\n");
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
|
||||
static void *last_alloc;
|
||||
|
||||
static void* my_array_alloc(size_t s, void *org, size_t n)
|
||||
{
|
||||
printf("Array Alloc called org=%p of n=%zu elements of size s=%lu!\n", org, n, s);
|
||||
last_alloc = realloc(org, s*n);
|
||||
return last_alloc;
|
||||
}
|
||||
|
||||
static void my_array_free(void *ptr)
|
||||
{
|
||||
printf("Array Free called ptr=%p last_alloc=%p!\n", ptr, last_alloc);
|
||||
if (ptr != last_alloc) {
|
||||
printf("Somthing went wrong.\n");
|
||||
exit(4);
|
||||
}
|
||||
free(ptr);
|
||||
}
|
||||
|
||||
// Allocate for array of objects
|
||||
# define MY_MEMORY_REALLOC(type, ptr, n) my_array_alloc (sizeof (type), ptr, n)
|
||||
# define MY_MEMORY_FREE(ptr) my_array_free(ptr)
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
|
||||
// Override only for this container the memory allocation function
|
||||
ARRAY_DEF(array_int, int, M_OPEXTEND(M_BASIC_OPLIST, REALLOC(MY_MEMORY_REALLOC), FREE(MY_MEMORY_FREE)))
|
||||
|
||||
static void test_array(void)
|
||||
{
|
||||
printf("Array demo\n");
|
||||
|
||||
array_int_t array;
|
||||
array_int_init(array);
|
||||
|
||||
array_int_push_back(array, 34);
|
||||
array_int_push_back(array, 34);
|
||||
|
||||
array_int_clear(array);
|
||||
|
||||
printf("Global custom allocator for array works!\n");
|
||||
}
|
||||
|
||||
////////////////////////////////////////////////////////////
|
||||
|
||||
int main(void)
|
||||
{
|
||||
test_list();
|
||||
test_array();
|
||||
exit(0);
|
||||
}
|
||||
@@ -59,10 +59,12 @@ int main(void)
|
||||
}
|
||||
|
||||
// Print
|
||||
printf("\nElisabeth has id = %d\n", *map_employee_name_get(Employees_r, STRING_CTE("Elisabeth")));
|
||||
printf("\nElisabeth has id = %d\n", *map_employee_name_get(Employees_r, STRING_CTE("Elisabeth")));
|
||||
// Print using emplace (we give a const char [] isntead of a const string_t, avoiding macro STRING_CTE)
|
||||
printf("\nElisabeth has id = %d\n", *map_employee_name_get_emplace(Employees_r, "Elisabeth"));
|
||||
} /* Employees_r is cleared beyond this point */
|
||||
|
||||
// But employee is not
|
||||
// But employee is not since we don't use M_LET
|
||||
map_employee_id_clear(Employees);
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,160 @@
|
||||
#if HAVE_CURL
|
||||
|
||||
#include <curl/curl.h>
|
||||
#include "m-dict.h"
|
||||
#include "m-tuple.h"
|
||||
#include "m-string.h"
|
||||
#include "m-list.h"
|
||||
#include "m-serial-json.h"
|
||||
|
||||
/////////////////////////////////////////
|
||||
|
||||
// Define container types
|
||||
// First a generic map string to string
|
||||
DICT_DEF2(map_str, string_t, string_t)
|
||||
#define M_OPL_map_str_t() DICT_OPLIST(map_str, STRING_OPLIST, STRING_OPLIST)
|
||||
|
||||
// This structure matches the JSON structure returns by the used WEB server
|
||||
// for the GET command.
|
||||
// It can be whatever you want provided it matches what is expected by the WEB server
|
||||
TUPLE_DEF2(web_data,
|
||||
(args, map_str_t),
|
||||
(headers, map_str_t),
|
||||
(origin, string_t),
|
||||
(url, string_t))
|
||||
#define M_OPL_web_data_t() TUPLE_OPLIST(web_data, M_OPL_map_str_t(), M_OPL_map_str_t(), STRING_T, STRING_T)
|
||||
|
||||
/////////////////////////////////////////
|
||||
|
||||
|
||||
static size_t
|
||||
write_data(void *buffer, size_t size, size_t nmemb, void *userp)
|
||||
{
|
||||
(void) size; // Parameter not used
|
||||
char *src = buffer;
|
||||
string_ptr str = userp;
|
||||
for(size_t i = 0; i < nmemb; i++) {
|
||||
string_push_back(str, src[i]);
|
||||
}
|
||||
return nmemb;
|
||||
}
|
||||
|
||||
static bool get_data(web_data_t data, const char url[])
|
||||
{
|
||||
m_serial_return_code_t r = M_SERIAL_FAIL;
|
||||
CURLcode res;
|
||||
string_t str;
|
||||
CURL *handle = curl_easy_init();
|
||||
if (handle) {
|
||||
string_init(str);
|
||||
// Perform an http GET request and fill in str with the JSON answer
|
||||
curl_easy_setopt(handle, CURLOPT_URL, url);
|
||||
curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_data);
|
||||
curl_easy_setopt(handle, CURLOPT_WRITEDATA, str);
|
||||
res = curl_easy_perform(handle);
|
||||
curl_easy_cleanup(handle);
|
||||
if (res == CURLE_OK) {
|
||||
// Parse the string and fill in the object
|
||||
m_serial_str_json_read_t serial;
|
||||
m_serial_str_json_read_init(serial, string_get_cstr(str));
|
||||
r = web_data_in_serial(data, serial);
|
||||
m_serial_str_json_read_clear(serial);
|
||||
}
|
||||
string_clear(str);
|
||||
}
|
||||
return r == M_SERIAL_OK_DONE;
|
||||
}
|
||||
|
||||
/////////////////////////////////////////
|
||||
|
||||
static size_t
|
||||
ignore_data(void *buffer, size_t size, size_t nmemb, void *userp)
|
||||
{
|
||||
(void) buffer; // Parameter not used
|
||||
(void) size; // Parameter not used
|
||||
(void) userp; // Parameter not used
|
||||
return nmemb;
|
||||
}
|
||||
|
||||
static bool post_data(web_data_t data, const char url[], bool display_answer)
|
||||
{
|
||||
m_serial_return_code_t r = M_SERIAL_FAIL;
|
||||
CURLcode res = 0;
|
||||
string_t str;
|
||||
CURL *handle = curl_easy_init();
|
||||
if (handle) {
|
||||
string_init(str);
|
||||
// Get a JSON string
|
||||
m_serial_str_json_write_t serial;
|
||||
m_serial_str_json_write_init(serial, str);
|
||||
r = web_data_out_serial(serial, data);
|
||||
m_serial_str_json_write_clear(serial);
|
||||
if (r == M_SERIAL_OK_DONE) {
|
||||
// Post JSON string
|
||||
struct curl_slist *headers=NULL;
|
||||
headers = curl_slist_append(headers, "Content-Type: text/json");
|
||||
if (headers != NULL) {
|
||||
curl_easy_setopt(handle, CURLOPT_POSTFIELDS, string_get_cstr(str));
|
||||
curl_easy_setopt(handle, CURLOPT_URL, url);
|
||||
curl_easy_setopt(handle, CURLOPT_HTTPHEADER, headers);
|
||||
if (display_answer == false) {
|
||||
curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, ignore_data);
|
||||
curl_easy_setopt(handle, CURLOPT_WRITEDATA, NULL);
|
||||
}
|
||||
res = curl_easy_perform(handle);
|
||||
curl_slist_free_all(headers);
|
||||
}
|
||||
}
|
||||
curl_easy_cleanup(handle);
|
||||
string_clear(str);
|
||||
}
|
||||
return (r == M_SERIAL_OK_DONE) && (res == CURLE_OK);
|
||||
}
|
||||
|
||||
/////////////////////////////////////////
|
||||
|
||||
// Remote server URL
|
||||
#define URL_GET "https://httpbin.org/get"
|
||||
#define URL_POST "https://httpbin.org/post"
|
||||
|
||||
int main(void)
|
||||
{
|
||||
CURLcode ret = curl_global_init(CURL_GLOBAL_DEFAULT);
|
||||
if (ret != CURLE_OK) abort();
|
||||
|
||||
web_data_t data;
|
||||
web_data_init(data);
|
||||
|
||||
// Test http GET
|
||||
bool b = get_data(data, URL_GET);
|
||||
if (b) {
|
||||
// We success receiving the JSON object and fill in the C object web_data with it.
|
||||
// Let's display the object content:
|
||||
printf("SUCCESS. Receive data =\n");
|
||||
web_data_out_str(stdout, data);
|
||||
printf("\n");
|
||||
} else {
|
||||
printf("ERROR: Cannot GET data or get data are not in expected format\n");
|
||||
}
|
||||
|
||||
// Test http POST
|
||||
web_data_emplace_origin(data, "This is the origin");
|
||||
web_data_emplace_url(data, "This is the URL");
|
||||
//web_data_set_origin(data, STRING_CTE("This is the origin"));
|
||||
//web_data_set_url(data, STRING_CTE("This is the URL"));
|
||||
b = post_data(data, URL_POST, false);
|
||||
if (b) {
|
||||
printf("SUCCESS: Post\n");
|
||||
} else {
|
||||
printf("ERROR post\n");
|
||||
}
|
||||
|
||||
web_data_clear(data);
|
||||
|
||||
curl_global_cleanup();
|
||||
return 0;
|
||||
}
|
||||
|
||||
#else
|
||||
int main(void) { return 0; }
|
||||
#endif
|
||||
+1
-1
@@ -10,7 +10,7 @@
|
||||
*/
|
||||
static const char empty_str[] = "EMPTY";
|
||||
static const char deleted_str[] = "DELETED";
|
||||
static const char *oor_table[] = { empty_str, deleted_str };
|
||||
static const char *const oor_table[] = { empty_str, deleted_str };
|
||||
|
||||
static inline bool oor_equal_p(const char *k, int n)
|
||||
{
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
#include "m-try.h"
|
||||
#include "m-array.h"
|
||||
|
||||
// Define & register an array of int
|
||||
ARRAY_DEF(array, int)
|
||||
#define M_OPL_array_t() ARRAY_OPLIST(array, M_BASIC_OPLIST)
|
||||
|
||||
// This shall be defined once per program to define the global variables needed
|
||||
// to handle the exceptions.
|
||||
M_TRY_DEF_ONCE()
|
||||
|
||||
static void f(void)
|
||||
{
|
||||
M_TRY(f_exception) {
|
||||
// Let's define a variable and do stuff:
|
||||
// Using M_LET allows the register of destructors for the exception handler.
|
||||
M_LET(a, array_t) {
|
||||
array_push_back(a, 5);
|
||||
array_push_back(a, 4);
|
||||
array_push_back(a, 3);
|
||||
array_push_back(a, 2);
|
||||
array_push_back(a, 1);
|
||||
// Throw a M_ERROR_MEMORY exception
|
||||
M_THROW(M_ERROR_MEMORY);
|
||||
printf("This should never be printed!\n");
|
||||
}
|
||||
} M_CATCH(f_exception, M_ERROR_MEMORY) {
|
||||
// Exception handler than catches the M_ERROR_MEMORY errors.
|
||||
// Here the destructor of variable a has been called to free memory
|
||||
printf("Memory exception %d received in f function, thrown by %s:%d\n",
|
||||
f_exception->error_code, f_exception->filename, f_exception->line);
|
||||
}
|
||||
}
|
||||
|
||||
static void g(void)
|
||||
{
|
||||
M_TRY(f_exception) {
|
||||
// Let's define a variable and do stuff:
|
||||
// Using M_LET allows the register of destructors for the exception handler.
|
||||
M_LET(a, array_t) {
|
||||
array_push_back(a, 5);
|
||||
array_push_back(a, 4);
|
||||
array_push_back(a, 3);
|
||||
array_push_back(a, 2);
|
||||
array_push_back(a, 1);
|
||||
// Throw an unkown exception
|
||||
M_THROW(42);
|
||||
printf("This should never be printed!\n");
|
||||
}
|
||||
} M_CATCH(f_exception, M_ERROR_MEMORY) {
|
||||
printf("Memory exception %d received in f function, thrown by %s:%d\n",
|
||||
f_exception->error_code, f_exception->filename, f_exception->line);
|
||||
printf("This should never be printed!\n");
|
||||
}
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
// Let's do a global exception handler
|
||||
M_TRY(main_exception) {
|
||||
f();
|
||||
g();
|
||||
} M_CATCH(main_exception, 0) {
|
||||
// Exception handler than catches all errors.
|
||||
// Here the destructor of variable a has been called to free memory
|
||||
printf("Exception %d received in main function, thrown by %s:%d\n",
|
||||
main_exception->error_code, main_exception->filename, main_exception->line);
|
||||
}
|
||||
printf("You should run it under valgrind to check that all memory allocations are freed.\n");
|
||||
exit(0);
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
#include "m-list.h"
|
||||
#include "m-generic.h"
|
||||
|
||||
LIST_DEF(list_uint, unsigned int)
|
||||
// Register for M_LET & M_EACH
|
||||
#define M_OPL_list_uint_t() LIST_OPLIST(list_uint, M_BASIC_OPLIST)
|
||||
// Register for Generic:
|
||||
#define M_GENERIC_ORG_1() (USER)
|
||||
#define M_GENERIC_ORG_USER_COMP_1() (CORE)
|
||||
#define M_GENERIC_ORG_USER_COMP_CORE_OPLIST_1() M_OPL_list_uint_t()
|
||||
|
||||
int main(void)
|
||||
{
|
||||
M_LET(list, list_uint_t) {
|
||||
// We can push in the list. It will call the registered method.
|
||||
push(list, 42);
|
||||
push(list, 17);
|
||||
// We can iterate over the list
|
||||
for each(item, list) {
|
||||
M_PRINT("ITEM=", *item, "\n");
|
||||
}
|
||||
// We can print directly the list. It will use the registered method
|
||||
M_PRINT("List = ", list, "\n");
|
||||
}
|
||||
}
|
||||
@@ -89,7 +89,7 @@
|
||||
,INIT_MOVE(M_F(name, _init_move)) \
|
||||
,MOVE(M_F(name, _move)) \
|
||||
,SWAP(M_F(name, _swap)) \
|
||||
,TYPE(M_F(name,_ct)) \
|
||||
,TYPE(M_F(name,_ct)) , GENTYPE(struct M_F(name,_s)*) \
|
||||
,NAME(name) \
|
||||
,SUBTYPE(M_F(name, _subtype_ct)) \
|
||||
,EMPTY_P(M_F(name,_empty_p)) \
|
||||
|
||||
@@ -332,4 +332,11 @@ M_END_PROTECTED_CODE
|
||||
|
||||
#endif
|
||||
|
||||
// C17 deprecated ATOMIC_VAR_INIT
|
||||
#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201710L
|
||||
# define M_ATOMIC_VAR_INIT(x) (x)
|
||||
#else
|
||||
# define M_ATOMIC_VAR_INIT(x) ATOMIC_VAR_INIT(x)
|
||||
#endif
|
||||
|
||||
#endif
|
||||
+2
-2
@@ -166,7 +166,7 @@
|
||||
MOVE(M_F(name, _move)), \
|
||||
SWAP(M_F(name, _swap)), \
|
||||
NAME(name), \
|
||||
TYPE(M_F(name,_ct)), \
|
||||
TYPE(M_F(name,_ct)), GENTYPE(struct M_F(name,_s)*), \
|
||||
SUBTYPE(M_F(name, _subtype_ct)), \
|
||||
IT_TYPE(M_F(name, _it_ct)), \
|
||||
IT_FIRST(M_F(name,_it)), \
|
||||
@@ -215,7 +215,7 @@
|
||||
MOVE(M_F(name, _move)), \
|
||||
SWAP(M_F(name, _swap)), \
|
||||
NAME(name), \
|
||||
TYPE(M_F(name,_ct)), \
|
||||
TYPE(M_F(name,_ct)), GENTYPE(struct M_F(name,_s)*), \
|
||||
SUBTYPE(M_F(name, _subtype_ct)), \
|
||||
EMPTY_P(M_F(name,_empty_p)), \
|
||||
IT_TYPE(M_F(name, _it_ct)), \
|
||||
|
||||
+1
-1
@@ -1308,7 +1308,7 @@ M_F(name, _init)(buffer_t v, size_t size) \
|
||||
,SET(M_F(name, _set)) \
|
||||
,CLEAR(M_F(name, _clear)) \
|
||||
,NAME(name) \
|
||||
,TYPE(M_F(name,_ct)) \
|
||||
,TYPE(M_F(name,_ct)), GENTYPE(struct M_F(name,_s)*) \
|
||||
,SUBTYPE(M_F(name, _subtype_ct)) \
|
||||
,RESET(M_F(name,_reset)) \
|
||||
,PUSH(M_F(name,_push)) \
|
||||
|
||||
+1
-1
@@ -106,7 +106,7 @@
|
||||
,M_IF_METHOD(MOVE, oplist)(MOVE(M_F(name, _move)),) \
|
||||
,M_IF_METHOD(SWAP,oplist)(SWAP(M_F(name, _swap)),) \
|
||||
,NAME(name) \
|
||||
,TYPE(M_F(name,_ct)) \
|
||||
,TYPE(M_F(name,_ct)), GENTYPE(struct M_F(name,_s)*) \
|
||||
,SUBTYPE(M_F(name, _subtype_ct)) \
|
||||
,OPLIST(oplist) \
|
||||
,M_IF_METHOD(EMPTY_P, oplist)(EMPTY_P(M_F(name,_empty_p)),) \
|
||||
|
||||
@@ -71,7 +71,7 @@
|
||||
/* Define M*LIB version */
|
||||
#define M_CORE_VERSION_MAJOR 0
|
||||
#define M_CORE_VERSION_MINOR 7
|
||||
#define M_CORE_VERSION_PATCHLEVEL 1
|
||||
#define M_CORE_VERSION_PATCHLEVEL 2
|
||||
|
||||
/* M_ASSUME is equivalent to M_ASSERT, but gives hints to compiler
|
||||
about how to optimize the code if NDEBUG is defined.
|
||||
@@ -450,14 +450,13 @@ M_BEGIN_PROTECTED_CODE
|
||||
/* Terminate the compilation of the current unit with an error message.
|
||||
The error parameter is a C token which classifies the error
|
||||
with an optional message detailling the error.
|
||||
Either use C11 to get a proper message, or at least a good hint in C99.
|
||||
error shall be a C name, msg a string.
|
||||
It is not an expression and shall not be used in an expression.
|
||||
It shall be used outside of a function, therefore it is not an expression.
|
||||
In C99, it uses a bitfield to be compatible with most compilers
|
||||
(so that it properly displays 'error' on the command line
|
||||
Quite usefull to terminate with a proper error message rather than
|
||||
a garbage of error due to incorrect code generation in the methods
|
||||
expansion.
|
||||
NOTE: Some implementation claims to be C11 (default mode) but fails
|
||||
to deliver a working assert.h with static_assert so we test for the
|
||||
explicity precense of the macro static_assert.
|
||||
*/
|
||||
#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L && defined(static_assert)) || defined(__cplusplus)
|
||||
# define M_STATIC_FAILURE(error, msg) static_assert(false, #error ": " msg);
|
||||
@@ -469,19 +468,22 @@ M_BEGIN_PROTECTED_CODE
|
||||
/* Test at compile time if the given condition is true.
|
||||
The error parameter is a C token which classifies the error
|
||||
with an optional message detailling the error.
|
||||
NOTE: Use bitfield to be compatible with most compilers
|
||||
In C99, it uses a bitfield to be compatible with most compilers
|
||||
(so that it properly displays 'error' on the command line
|
||||
It cannot use C11 Static Assert as is not usable in expression.
|
||||
NOTE: In C++, use of lambda to encapsulate static_assert in
|
||||
an expression.
|
||||
NOTE: For GNU compatible compilers, uses of a GNU extension.
|
||||
C11 static Assert is not usable in expression,
|
||||
but is usable in struct declaration. Therefore we create
|
||||
a dummy struct, define the static assert within it
|
||||
compute its size and cast it to void.
|
||||
NOTE: Some implementation claims to be C11 (default mode) but fails
|
||||
to deliver a working assert.h with static_assert so we test for the
|
||||
explicity precense of the macro static_assert.
|
||||
*/
|
||||
#if defined(__cplusplus)
|
||||
#ifdef __cplusplus
|
||||
# define M_STATIC_ASSERT(cond, error, msg) \
|
||||
([] { static_assert(cond, #error ": " msg); } ())
|
||||
#elif defined(__GNUC__) && defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L && defined(static_assert)
|
||||
#elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L && defined(static_assert)
|
||||
# define M_STATIC_ASSERT(cond, error, msg) \
|
||||
M_ATTR_EXTENSION ({ static_assert(cond, #error ": " msg); })
|
||||
((void) sizeof(struct { int error; static_assert(cond, #error ": " msg);}))
|
||||
#else
|
||||
# define M_STATIC_ASSERT(cond, error, msg) \
|
||||
((void) sizeof(struct { int error : !!(cond);}))
|
||||
@@ -1156,6 +1158,12 @@ M_BEGIN_PROTECTED_CODE
|
||||
#define M_TRIPLE_2(a,b,c) b
|
||||
#define M_TRIPLE_3(a,b,c) c
|
||||
|
||||
/* Same for quad */
|
||||
#define M_QUAD_1(a,b,c,d) a
|
||||
#define M_QUAD_2(a,b,c,d) b
|
||||
#define M_QUAD_3(a,b,c,d) c
|
||||
#define M_QUAD_4(a,b,c,d) d
|
||||
|
||||
/* Skip the Nth first arguments of a VA_ARGS
|
||||
Generated by:
|
||||
for i in $(seq 0 52) ; do printf "#define M_SKIPI_%d(" $i ; for j in $(seq 1 $i) ; do printf "_%d, " $j ; done ; printf "...) __VA_ARGS__\n"; done
|
||||
@@ -1450,7 +1458,10 @@ M_BEGIN_PROTECTED_CODE
|
||||
#define M_PATTERN_type_type ,
|
||||
#define M_PATTERN_TYPE_TYPE ,
|
||||
#define M_PATTERN_SUBTYPE_SUBTYPE ,
|
||||
#define M_PATTERN_SUBTYPE_PTR_SUBTYPE_PTR ,
|
||||
#define M_PATTERN_IT_TYPE_IT_TYPE ,
|
||||
#define M_PATTERN_KEY_TYPE_KEY_TYPE ,
|
||||
#define M_PATTERN_VALUE_TYPE_VALUE_TYPE ,
|
||||
#define M_PATTERN_M_UNDERFLOW_M_UNDERFLOW ,
|
||||
#define M_PATTERN_M_OVERFLOW_M_OVERFLOW ,
|
||||
#define M_PATTERN_M_OVERFLOW_M_OVERFLOW ,
|
||||
@@ -2513,6 +2524,14 @@ done
|
||||
#define M_AS_STR(x) M_AS_STRI(x)
|
||||
#define M_AS_STRI(x) #x
|
||||
|
||||
/* Convert an oplist into a string. It may generate a very long string,
|
||||
which generates warning (string length greater than 4095).
|
||||
So the conversion is optional */
|
||||
#ifdef M_USE_PRINT_OPLIST
|
||||
# define M_OPL_AS_STR(x) M_AS_STR(x)
|
||||
#else
|
||||
# define M_OPL_AS_STR(x)
|
||||
#endif
|
||||
|
||||
/* C11 MACROS */
|
||||
|
||||
@@ -2619,15 +2638,6 @@ m_core_fopen(const char filename[], const char opt[])
|
||||
/* Can be overloaded by m-string to support string output too */
|
||||
#define M_CORE_PRINTF_ARG(x) x
|
||||
|
||||
/* Print a C variable if it is a standard type (or extension) to stdout
|
||||
If a variable is extended (i.e. like (x, type) ) it will use the
|
||||
method associated to the OUT_STR operator.
|
||||
*/
|
||||
#define M_PRINT_ARG(x) \
|
||||
M_IF(M_PARENTHESIS_P(x)) \
|
||||
( M_FPRINT_ARG_OUT_STR(stdout, M_PAIR_2 x, M_PAIR_1 x), \
|
||||
printf(M_PRINTF_FORMAT(x), M_CORE_PRINTF_ARG(x) ) )
|
||||
|
||||
/* Print a C variable if it is a standard type to the given file 'f'.
|
||||
If a variable is extended (i.e. like (x, type) ) it will use the
|
||||
method associated to the OUT_STR operator.
|
||||
@@ -2802,7 +2812,7 @@ M_PARSE_DEFAULT_TYPE_DEF(m_core_parse_ldouble, long double, strtold, )
|
||||
|
||||
/* Generic PRINT macro: print all its inputs regardless of the type
|
||||
provided it is a generic "non-struct" type. */
|
||||
#define M_PRINT(...) do { M_REDUCE(M_PRINT_ARG, M_SEPARATE_PER_SEMICOLON, __VA_ARGS__); } while (0)
|
||||
#define M_PRINT(...) do { M_REDUCE2(M_FPRINT_ARG, M_SEPARATE_PER_SEMICOLON, stdout, __VA_ARGS__); } while (0)
|
||||
|
||||
/* Generic FPRINT macro: print all its inputs regardless of the type
|
||||
provided it is a generic "non-struct" type into the file 'f'. */
|
||||
@@ -3360,6 +3370,7 @@ M_INLINE size_t m_core_cstr_hash(const char str[])
|
||||
#define M_TYPE_TYPE(a) ,a,
|
||||
#define M_SUBTYPE_SUBTYPE(a) ,a,
|
||||
#define M_GENTYPE_GENTYPE(a) ,a,
|
||||
#define M_SUBTYPE_PTR_SUBTYPE_PTR(a) ,a,
|
||||
#define M_NAME_NAME(a) ,a,
|
||||
#define M_OPLIST_OPLIST(a) ,a,
|
||||
#define M_SORT_SORT(a) ,a,
|
||||
@@ -3449,6 +3460,7 @@ M_INLINE size_t m_core_cstr_hash(const char str[])
|
||||
#define M_GET_CMP(...) M_GET_METHOD(CMP, M_CMP_DEFAULT, __VA_ARGS__)
|
||||
#define M_GET_TYPE(...) M_GET_METHOD(TYPE, M_NO_DEF_TYPE, __VA_ARGS__)
|
||||
#define M_GET_SUBTYPE(...) M_GET_METHOD(SUBTYPE, M_NO_DEF_TYPE, __VA_ARGS__)
|
||||
#define M_GET_SUBTYPE_PTR(...) M_GET_METHOD(SUBTYPE_PTR, M_NO_DEF_TYPE, __VA_ARGS__)
|
||||
#define M_GET_GENTYPE(...) M_GET_METHOD(GENTYPE, M_NO_DEF_TYPE, __VA_ARGS__)
|
||||
#define M_GET_NAME(...) M_GET_METHOD(NAME, M_NO_DEF_TYPE, __VA_ARGS__)
|
||||
#define M_GET_OPLIST(...) M_GET_METHOD(OPLIST, (), __VA_ARGS__)
|
||||
@@ -3530,6 +3542,7 @@ M_INLINE size_t m_core_cstr_hash(const char str[])
|
||||
#define M_CALL_CMP(oplist, ...) M_APPLY_API(M_GET_CMP oplist, oplist, __VA_ARGS__)
|
||||
//#define M_CALL_TYPE(oplist, ...) M_APPLY_API(M_GET_TYPE oplist, oplist, __VA_ARGS__)
|
||||
//#define M_CALL_SUBTYPE(oplist, ...) M_APPLY_API(M_GET_SUBTYPE oplist, oplist, __VA_ARGS__)
|
||||
//#define M_CALL_SUBTYPE_PTR(oplist, ...) M_APPLY_API(M_GET_SUBTYPE_PTR oplist, oplist, __VA_ARGS__)
|
||||
//#define M_CALL_GENTYPE(oplist, ...) M_APPLY_API(M_GET_GENTYPE oplist, oplist, __VA_ARGS__)
|
||||
//#define M_CALL_NAME(oplist, ...) M_APPLY_API(M_GET_NAME oplist, oplist, __VA_ARGS__)
|
||||
//#define M_CALL_OPLIST(oplist, ...) M_APPLY_API(M_GET_OPLIST oplist, oplist, __VA_ARGS__)
|
||||
@@ -3610,7 +3623,7 @@ M_INLINE size_t m_core_cstr_hash(const char str[])
|
||||
|
||||
NOTE: It starts by a comma, to ack the success of the transformation.
|
||||
*/
|
||||
#define M_OPLAPI_ERROR(method, oplist, ...) , M_STATIC_FAILURE(M_LIB_DISABLED_METHOD, "The method has been explictly disabled for the requested operator, yet it has been called by the container")
|
||||
#define M_OPLAPI_ERROR(method, oplist, ...) , M_STATIC_ASSERT(false, M_LIB_DISABLED_METHOD, "The method has been explictly disabled for the requested operator, yet it has been called by the container")
|
||||
#define M_OPLAPI_0(method, oplist, ...) , M_DELAY3(method)(__VA_ARGS__)
|
||||
#define M_OPLAPI_1(method, oplist, ...) , M_DELAY3(method)(oplist, __VA_ARGS__)
|
||||
#define M_OPLAPI_2(method, oplist, ...) , M_DELAY3(method)(& __VA_ARGS__)
|
||||
@@ -3730,11 +3743,11 @@ M_INLINE size_t m_core_cstr_hash(const char str[])
|
||||
if the method is expanded.
|
||||
*/
|
||||
#define M_NO_DEFAULT(...) \
|
||||
M_STATIC_FAILURE(M_LIB_MISSING_METHOD, \
|
||||
M_STATIC_ASSERT(false, M_LIB_MISSING_METHOD, \
|
||||
"The requested operator has no method registered in the given OPLIST. ")
|
||||
|
||||
#define M_NO_DEF_TYPE \
|
||||
M_STATIC_FAILURE(M_LIB_MISSING_METHOD, \
|
||||
M_STATIC_ASSERT(false, M_LIB_MISSING_METHOD, \
|
||||
"The requested operator has no type/subtype/suboplist registered in the given OPLIST. ")
|
||||
|
||||
/* Test if the given variable is a basic C variable:
|
||||
@@ -3800,7 +3813,7 @@ M_INLINE size_t m_core_cstr_hash(const char str[])
|
||||
M_LIB_TYPE_MISTMACH, \
|
||||
"The given type " M_AS_STR(type) \
|
||||
" and the type of the oplist does not match: " \
|
||||
M_AS_STR(oplist) ); \
|
||||
M_OPL_AS_STR(oplist) ); \
|
||||
} \
|
||||
, /* End of TYPE */)
|
||||
#else
|
||||
@@ -3814,7 +3827,7 @@ M_INLINE size_t m_core_cstr_hash(const char str[])
|
||||
M_LIB_TYPE_MISTMACH, \
|
||||
"The given type " M_AS_STR(type) \
|
||||
" and the type of the oplist does not match: " \
|
||||
M_AS_STR(oplist) ); \
|
||||
M_OPL_AS_STR(oplist) ); \
|
||||
} \
|
||||
, /* End of TYPE */)
|
||||
#endif
|
||||
@@ -4310,7 +4323,7 @@ m_core_parse2_enum (const char str[], const char **endptr)
|
||||
M_IF_OPLIST(oplist)(M_LETI3, M_LETI2_FAILURE)(cont, oplist, __VA_ARGS__)
|
||||
// Stop with a failure (invalid oplist)
|
||||
#define M_LETI2_FAILURE(cont, oplist, ...) \
|
||||
M_STATIC_FAILURE(M_LIB_NOT_AN_OPLIST, "(M_LET): the given argument is not a valid oplist: " M_AS_STR(oplist))
|
||||
M_STATIC_ASSERT(false, M_LIB_NOT_AN_OPLIST, "(M_LET): the given argument is not a valid oplist: " M_AS_STR(oplist))
|
||||
// 4. Map all variables to their own LET
|
||||
#define M_LETI3(cont, oplist, ...) \
|
||||
for(bool cont = true; cont ; /* unused */) \
|
||||
|
||||
@@ -1104,7 +1104,7 @@
|
||||
,MOVE(M_F(name, _move)) \
|
||||
,SWAP(M_F(name, _swap)) \
|
||||
,NAME(name) \
|
||||
,TYPE(M_F(name,_ct)) \
|
||||
,TYPE(M_F(name,_ct)), GENTYPE(struct M_F(name,_s)*) \
|
||||
,SUBTYPE(M_F(name, _subtype_ct)) \
|
||||
,EMPTY_P(M_F(name,_empty_p)) \
|
||||
,IT_TYPE(M_F(name,_it_ct)) \
|
||||
|
||||
@@ -1071,7 +1071,7 @@
|
||||
SWAP(M_F(name, _swap)), \
|
||||
RESET(M_F(name, _reset)), \
|
||||
NAME(name), \
|
||||
TYPE(M_F(name, _ct)), \
|
||||
TYPE(M_F(name, _ct)), GENTYPE(struct M_F(name,_s)*), \
|
||||
SUBTYPE(M_F(name, _subtype_ct)), \
|
||||
EMPTY_P(M_F(name,_empty_p)), \
|
||||
IT_TYPE(M_F(name, _it_ct)), \
|
||||
@@ -1126,7 +1126,7 @@
|
||||
SWAP(M_F(name, _swap)), \
|
||||
RESET(M_F(name, _reset)), \
|
||||
NAME(name), \
|
||||
TYPE(M_F(name, _ct)), \
|
||||
TYPE(M_F(name, _ct)), GENTYPE(struct M_F(name,_s)*), \
|
||||
SUBTYPE(M_F(name, _subtype_ct)), \
|
||||
EMPTY_P(M_F(name,_empty_p)), \
|
||||
PUSH(M_F(name,_push)), \
|
||||
|
||||
+4
-4
@@ -111,7 +111,7 @@
|
||||
/* Specialization of the OPLIST in function if there is at least one member or not */
|
||||
#define M_FUNC0BJ_INS_NO_ATTR_OPLIST(name) ( \
|
||||
NAME(name), \
|
||||
TYPE(M_F(name, _ct)), \
|
||||
TYPE(M_F(name, _ct)), GENTYPE(struct M_F(name,_s)*), \
|
||||
CLEAR(M_F(name, _clear)), \
|
||||
INIT(M_F(name,_init)) \
|
||||
)
|
||||
@@ -127,7 +127,7 @@
|
||||
/* Define the oplist of the instance */
|
||||
#define M_FUNC0BJ_INS_ATTR_OPLIST_P3(name, ...) ( \
|
||||
NAME(name), \
|
||||
TYPE(M_F(name, _ct)), \
|
||||
TYPE(M_F(name, _ct)), GENTYPE(struct M_F(name,_s)*), \
|
||||
INIT_WITH(M_F(name, _init_with)), \
|
||||
CLEAR(M_F(name, _clear)), \
|
||||
M_IF_METHOD_ALL(INIT, __VA_ARGS__)(INIT(M_F(name,_init)),), \
|
||||
@@ -156,7 +156,7 @@
|
||||
\
|
||||
typedef struct M_F(name, _s) { \
|
||||
M_F(name, _callback_ct) callback; \
|
||||
} interface_t[1]; \
|
||||
} *interface_t; \
|
||||
\
|
||||
/* Internal type for oplist & instance */ \
|
||||
typedef interface_t M_F(name, _ct); \
|
||||
@@ -192,7 +192,7 @@
|
||||
\
|
||||
typedef struct M_F(name, _s) { \
|
||||
M_F(name, _callback_ct) callback; \
|
||||
} interface_t[1]; \
|
||||
} *interface_t; \
|
||||
\
|
||||
/* Internal type for oplist & instance */ \
|
||||
typedef interface_t M_F(name, _ct); \
|
||||
|
||||
+343
@@ -0,0 +1,343 @@
|
||||
/*
|
||||
* M*LIB - Generic module
|
||||
*
|
||||
* Copyright (c) 2017-2023, Patrick Pelissier
|
||||
* All rights reserved.
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
* + Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* + Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
|
||||
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
|
||||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
#ifndef MSTARLIB_GENERIC_H
|
||||
#define MSTARLIB_GENERIC_H
|
||||
|
||||
#include "m-core.h"
|
||||
|
||||
/* Define the generic macro-functions */
|
||||
// TODO: m_ prefix?
|
||||
#define init(x) M_G3N_CALL_1(INIT, x)
|
||||
#define init_set(x, y) M_G3N_CALL_2(INIT_SET, x, TYPE, x, TYPE, y)
|
||||
#define init_move(x, y) M_G3N_CALL_2(INIT_MOVE, x, TYPE, x, TYPE, y)
|
||||
#define move(x, y) M_G3N_CALL_2(MOVE, x, TYPE, x, TYPE, y)
|
||||
#define set(x, y) M_G3N_CALL_2(SET, x, TYPE, x , TYPE, y)
|
||||
#define clear(x) M_G3N_CALL_1(CLEAR, x)
|
||||
#define swap(x, y) M_G3N_CALL_2(SWAP, x, TYPE, x , TYPE, y)
|
||||
#define hash(x) M_G3N_CALL_1(HASH, x)
|
||||
#define equal(x, y) M_G3N_CALL_2(EQUAL, x, TYPE, x , TYPE, y)
|
||||
#define cmp(x, y) M_G3N_CALL_2(CMP, x, TYPE, x , TYPE, y)
|
||||
#define sort(x) M_G3N_CALL_1(SORT, x)
|
||||
#define splice_back(d, s, i) M_G3N_CALL_3(SPLICE_BACK, d, TYPE, d, TYPE, s, IT_TYPE, i)
|
||||
#define splice_at(d, id, s, is) M_G3N_CALL_4(SPLICE_BACK, d, TYPE, d, IT_TYPE, id, TYPE, s, IT_TYPE, is)
|
||||
#define it_type(x) m_typeof(M_G3N_TYPE_1(IT_TYPE, x))
|
||||
#define sub_type(x) m_typeof(M_G3N_TYPE_1(SUBTYPE, x))
|
||||
#define key_type(x) m_typeof(M_G3N_TYPE_1(KEY_TYPE, x))
|
||||
#define value_type(x) m_typeof(M_G3N_TYPE_1(VALUE_TYPE, x))
|
||||
#define it_first(x, y) M_G3N_CALL_2(IT_FIRST, y, IT_TYPE, x, TYPE, y)
|
||||
#define it_last(x, y) M_G3N_CALL_i2(IT_LAST, y, IT_TYPE, x, TYPE, y)
|
||||
#define it_end(x, y) M_G3N_CALL_i2(IT_END, y, IT_TYPE, x, TYPE, y)
|
||||
#define it_set(d, s) M_G3N_CALL_2i(IT_SET, d, IT_TYPE, d, IT_TYPE, s)
|
||||
#define it_end_p(i) M_G3N_CALL_1i(IT_END_P, i)
|
||||
#define it_last_p(i) M_G3N_CALL_1i(IT_LAST_P, i)
|
||||
#define it_next(i) M_G3N_CALL_1i(IT_NEXT, i)
|
||||
#define it_previous(i) M_G3N_CALL_1i(IT_PREVIOUS, i)
|
||||
#define it_ref(i) M_G3N_CALL_1i(IT_REF, i)
|
||||
#define it_cref(i) M_G3N_CALL_1i(IT_CREF, i)
|
||||
#define it_equal(x, y) M_G3N_CALL_2i(IT_EQUAL, x, IT_TYPE, x, IT_TYPE, y)
|
||||
#define it_insert(c, i, o) M_G2N_CALL_3(IT_INSERT, c, TYPE, c, IT_TYPE, i, SUBTYPE, o)
|
||||
#define it_remove(c, i) M_G3N_CALL_2(IT_REMOVE, c, TYPE, c, IT_TYPE, i)
|
||||
#define empty_p(x) M_G3N_CALL_1(EMPTY_P, x)
|
||||
#define add(x, y) M_G3N_CALL_2(ADD, x, TYPE, x, TYPE, y)
|
||||
#define sub(x, y) M_G3N_CALL_2(SUB, x, TYPE, x, TYPE, y)
|
||||
#define mul(x, y) M_G3N_CALL_2(MUL, x, TYPE, x, TYPE, y)
|
||||
#define div(x, y) M_G3N_CALL_2(DIV, x, TYPE, x, TYPE, y)
|
||||
#define reset(x) M_G3N_CALL_1(RESET, x)
|
||||
#define get(x, y) M_G3N_CALL_2(GET_KEY, x, TYPE, x, KEY_TYPE, y)
|
||||
#define set_at(x, y, z) M_G3N_CALL_3(SET_KEY, x, TYPE, x, KEY_TYPE, y, VALUE_TYPE, z)
|
||||
#define safe_get(x, y) M_G3N_CALL_2(SAFE_GET_KEY, x, TYPE, x, KEY_TYPE, y)
|
||||
#define erase(x, y) M_G3N_CALL_2(ERASE, x, TYPE, x, KEY_TYPE, y)
|
||||
#define get_size(x) M_G3N_CALL_1(GET_SIZE, x)
|
||||
#define push(x, y) M_G3N_CALL_2(PUSH, x, TYPE, x, SUBTYPE, y)
|
||||
#define pop(x, y) M_G3N_CALL_2(POP, y, SUBTYPE_PTR, x, TYPE, y)
|
||||
#define push_move(x, y) M_G3N_CALL_2(PUSH_MOVE, x, TYPE, x, SUBTYPE_PTR, y)
|
||||
#define pop_move(x, y) M_G3N_CALL_2(POP_MOVE, y, SUBTYPE_PTR, x, TYPE, y)
|
||||
#define reverse(x) M_G3N_CALL_1(REVERSE, x)
|
||||
#define get_str(s, c, b) M_G3N_CALL_3(GET_STR, c, string_t, s, TYPE, c, bool, b)
|
||||
#define parse_str(c, s, e) M_G3N_CALL_3(PARSE_STR, c, TYPE, c, m_g3n_cstring, s, m_g3n_cstring_end, e)
|
||||
#define out_str(x, y) M_G3N_CALL_2(OUT_STR, y, m_g3n_file, x, TYPE, y)
|
||||
#define in_str(x, y) M_G3N_CALL_2(IN_STR, x, TYPE, x, m_g3n_file, y)
|
||||
#define out_serial(x, y) M_G3N_CALL_2(OUT_SERIAL, y, m_serial_write_t, x, TYPE, y)
|
||||
#define in_serial(x, y) M_G3N_CALL_2(IN_SERIAL, x, TYPE, x, m_serial_read_t, y)
|
||||
|
||||
// for each(item, container)
|
||||
#define each(item, container) \
|
||||
M_G3N_EACHI(item, container, M_C(m_local_it_, __LINE__), M_C(local_pass_, __LINE__))
|
||||
|
||||
/* User code has to register oplists like this:
|
||||
#define M_GENERIC_ORG_2() (USER)
|
||||
#define M_GENERIC_ORG_USER_COMP_1() (CORE)
|
||||
#define M_GENERIC_ORG_USER_COMP_CORE_OPLIST_6() FLT1
|
||||
#define M_GENERIC_ORG_USER_COMP_CORE_OPLIST_7() STR1
|
||||
*/
|
||||
|
||||
|
||||
/*****************************************************************************/
|
||||
/********************************** INTERNAL *********************************/
|
||||
/*****************************************************************************/
|
||||
|
||||
/* Definition of the typeof keyword
|
||||
In C23, we can use the standard keyword.
|
||||
Otherwise we use some extensions of the compilers. */
|
||||
#ifdef _MSC_VER
|
||||
#define m_typeof(x) decltype(x)
|
||||
#elif defined(__GNUC__)
|
||||
#define m_typeof(x) __typeof__(x)
|
||||
#else
|
||||
#define m_typeof(x) typeof(x)
|
||||
#endif
|
||||
|
||||
/* Warnings disabled for CLANG in C mode:
|
||||
Due to the genericity of the _Generic generation,
|
||||
we cannot avoid generating both T and const T in the generic association. */
|
||||
#if defined(__clang__) && __clang_major__ >= 15
|
||||
#define M_G3N_BEGIN_PROTECTED_CODE \
|
||||
_Pragma("clang diagnostic push") \
|
||||
_Pragma("clang diagnostic ignored \"-Wunreachable-code-generic-assoc\"")
|
||||
|
||||
#define M_G3N_END_PROTECTED_CODE \
|
||||
_Pragma("clang diagnostic pop")
|
||||
|
||||
#else
|
||||
|
||||
#define M_G3N_BEGIN_PROTECTED_CODE
|
||||
#define M_G3N_END_PROTECTED_CODE
|
||||
|
||||
#endif
|
||||
|
||||
// Instead of using expensive M_SEQ(1,50), precompute it:
|
||||
#define M_G3N_SEQ_INT \
|
||||
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, \
|
||||
21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, \
|
||||
40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50
|
||||
|
||||
/* Generate a sequence of the oplist that have been registered.
|
||||
|
||||
3 levels of indirection are needed:
|
||||
* One level is the organisation.
|
||||
* One level is the component of the organisation.
|
||||
* One level is the oplist within the component of the organisation
|
||||
Return the list of unevaluated oplist that are registered across all organizations, all components.
|
||||
M*LIB Organisation (MLIB) is the default organisation and is always registered
|
||||
Usage Domain: Nb organizations * Nb components * Nb oplist <= M_MAX_NB_ARGUMENT
|
||||
*/
|
||||
#define M_G3N_REGISTERED_ITEMS() \
|
||||
M_CROSS_MAP( M_G3N_IS_PRESENT3, ( comp M_CROSS_MAP(M_G3N_IS_PRESENT2, (MLIB M_MAP(M_G3N_IS_PRESENT, M_G3N_SEQ_INT)), ( M_G3N_SEQ_INT ) ) ), ( M_G3N_SEQ_INT ) )
|
||||
|
||||
#define M_G3N_IS_PRESENT(num) \
|
||||
M_IF(M_PARENTHESIS_P(M_C(M_GENERIC_ORG_, num)() ))(M_DEFERRED_COMMA M_ID M_C(M_GENERIC_ORG_, num)(), )
|
||||
|
||||
#define M_G3N_IS_PRESENT2(el1, num) \
|
||||
M_IF(M_PARENTHESIS_P(M_C4(M_GENERIC_ORG_, el1, _COMP_, num)()))(M_DEFERRED_COMMA M_APPLY(M_C3, el1, _COMP_, M_ID M_C4(M_GENERIC_ORG_, el1, _COMP_, num)()), )
|
||||
|
||||
#define M_G3N_IS_PRESENT3(el1, num) \
|
||||
M_IF(M_OPLIST_P(M_C4(M_GENERIC_ORG_, el1, _OPLIST_, num)()))(M_DEFERRED_COMMA M_C4(M_GENERIC_ORG_, el1, _OPLIST_, num), )
|
||||
|
||||
|
||||
// Dummy unused structure. Just to fill in default case of the _Generic
|
||||
struct m_g3neric_dummy_s;
|
||||
|
||||
// Synonymous for some basic pointers doesn't work well with preprocessing code
|
||||
typedef FILE *m_g3n_file;
|
||||
typedef const char *m_g3n_cstring;
|
||||
typedef const char **m_g3n_cstring_end;
|
||||
|
||||
// If x is of typex, return y else return (typey){O}
|
||||
#define M_AS_LINKED_TYPE(typex, x, typey, y) _Generic(((void)0,(x)), typex: (y), default: (typey) {0})
|
||||
|
||||
// Return IT_TYPE of oplist promoted to a type suitable for a _Generic statement
|
||||
// i.e. transform array type into corresponding pointer type
|
||||
#define M_G3N_GEN_IT_TYPE(oplist) m_typeof( ((void)0, (M_GET_IT_TYPE oplist()){0}))
|
||||
#define M_G3N_GEN_TYPE(oplist) M_GET_GENTYPE oplist()
|
||||
|
||||
// Translate type' into the container type if type == TYPE, IT_TYPE into its iterator
|
||||
// KEY_TYPE, VALUE_TYPE, SUBTYPE into the associated type in the oplist,
|
||||
// or keep 'type' otherwise
|
||||
// Handle also specially SUBTYPE_PTR
|
||||
#define M_G3N_TYPE(type, oplist) M_G3N_TYPE_B(type, oplist())
|
||||
#define M_G3N_TYPE_B(type, oplist) M_GET_METHOD(type, type, SUBTYPE_PTR(M_GET_SUBTYPE oplist *), M_OPFLAT oplist)
|
||||
|
||||
// Call the OPERATOR call of the oplist registered to the variable 'x'
|
||||
// which takes one argument.
|
||||
#define M_G3N_CALL_1(op, x) \
|
||||
M_G3N_BEGIN_PROTECTED_CODE \
|
||||
_Generic( ((void)0, (x)), \
|
||||
M_MAP2(M_G3N_CALL_1_func, (op, x) M_G3N_REGISTERED_ITEMS() ) \
|
||||
struct m_g3neric_dummy_s *: /* cannot happen */ (void)0) \
|
||||
M_G3N_END_PROTECTED_CODE
|
||||
#define M_G3N_CALL_1_func(x, oplist) \
|
||||
M_G3N_CALL_1_func_test(M_G3N_GEN_TYPE(oplist), M_PAIR_1 x, M_PAIR_2 x, oplist)
|
||||
#define M_G3N_CALL_1_func_test(gentype, op, x, oplist) \
|
||||
M_IF_METHOD(op, oplist())(M_G3N_CALL_1_func_expand, M_EAT)(gentype, op, x, oplist)
|
||||
#define M_G3N_CALL_1_func_expand(gentype, op, x, oplist) \
|
||||
gentype: M_C(M_CALL_, op)(oplist(), M_AS_TYPE(gentype, x)), \
|
||||
const gentype: M_C(M_CALL_, op)(oplist(), M_AS_LINKED_TYPE(const gentype, x, gentype, x)),
|
||||
|
||||
// Call the OPERATOR call of the oplist registered to the variable 'svar'
|
||||
// which takes 2 arguments of the given type
|
||||
#define M_G3N_CALL_2(op, svar, type1, var1, type2, var2) \
|
||||
M_G3N_BEGIN_PROTECTED_CODE \
|
||||
_Generic( ((void)0, (svar)), \
|
||||
M_MAP2(M_G3N_CALL_2_func, (op, svar, type1, var1, type2, var2) M_G3N_REGISTERED_ITEMS() ) \
|
||||
struct m_g3neric_dummy_s *: /* cannot happen */ (void) 0) \
|
||||
M_G3N_END_PROTECTED_CODE
|
||||
#define M_G3N_CALL_2_func(x, oplist) \
|
||||
M_APPLY(M_G3N_CALL_2_func_test, M_G3N_GEN_TYPE(oplist), M_ID x, oplist)
|
||||
#define M_G3N_CALL_2_func_test(gentype, op, svar, type1, var1, type2, var2, oplist) \
|
||||
M_IF_METHOD(op, oplist())(M_G3N_CALL_2_func_expand, M_EAT)(gentype, op, svar, M_G3N_TYPE(type1, oplist), var1, M_G3N_TYPE(type2, oplist), var2, oplist)
|
||||
#define M_G3N_CALL_2_func_expand(gentype, op, svar, type1, var1, type2, var2, oplist) \
|
||||
gentype: M_C(M_CALL_, op)(oplist(), M_AS_LINKED_TYPE(gentype, svar, type1, var1), M_AS_LINKED_TYPE(gentype, svar, type2, var2) ), \
|
||||
const gentype: M_C(M_CALL_, op)(oplist(), M_AS_LINKED_TYPE(const gentype, svar, type1, var1), M_AS_LINKED_TYPE(const gentype, svar, type2, var2) ),
|
||||
|
||||
// Call the OPERATOR call of the oplist registered to the variable 'svar'
|
||||
// which takes 3 arguments of the given type
|
||||
#define M_G3N_CALL_3(op, svar, type1, var1, type2, var2, type3, var3) \
|
||||
M_G3N_BEGIN_PROTECTED_CODE \
|
||||
_Generic( ((void)0, (svar)), \
|
||||
M_MAP2(M_G3N_CALL_3_func, (op, svar, type1, var1, type2, var2, type3, var3) M_G3N_REGISTERED_ITEMS() ) \
|
||||
struct m_g3neric_dummy_s *: /* cannot happen */ (void) 0) \
|
||||
M_G3N_END_PROTECTED_CODE
|
||||
#define M_G3N_CALL_3_func(x, oplist) \
|
||||
M_APPLY(M_G3N_CALL_3_func_test, M_G3N_GEN_TYPE(oplist), M_ID x, oplist)
|
||||
#define M_G3N_CALL_3_func_test(gentype, op, svar, type1, var1, type2, var2, type3, var3, oplist) \
|
||||
M_IF_METHOD(op, oplist())(M_G3N_CALL_3_func_expand, M_EAT)(gentype, op, svar, M_G3N_TYPE(type1, oplist), var1, M_G3N_TYPE(type2, oplist), var2, M_G3N_TYPE(type3, oplist), var3, oplist)
|
||||
#define M_G3N_CALL_3_func_expand(gentype, op, svar, type1, var1, type2, var2, oplist) \
|
||||
gentype: M_C(M_CALL_, op)(oplist(), M_AS_LINKED_TYPE(gentype, svar, type1, var1), M_AS_LINKED_TYPE(gentype, svar, type2, var2) ), \
|
||||
const gentype: M_C(M_CALL_, op)(oplist(), M_AS_LINKED_TYPE(const gentype, svar, type1, var1), M_AS_LINKED_TYPE(const gentype, svar, type2, var2) ),
|
||||
|
||||
// Call the OPERATOR call of the oplist registered to the variable 'svar'
|
||||
// which takes 4 arguments of the given type
|
||||
#define M_G3N_CALL_4(op, svar, type1, var1, type2, var2, type3, var3, type4, var4) \
|
||||
M_G3N_BEGIN_PROTECTED_CODE \
|
||||
_Generic( ((void)0, (svar)), \
|
||||
M_MAP2(M_G3N_CALL_4_func, (op, svar, type1, var1, type2, var2, type3, var3, type4, var4) M_G3N_REGISTERED_ITEMS() ) \
|
||||
struct m_g3neric_dummy_s *: /* cannot happen */ (void) 0) \
|
||||
M_G3N_END_PROTECTED_CODE
|
||||
#define M_G3N_CALL_4_func(x, oplist) \
|
||||
M_APPLY(M_G3N_CALL_4_func_test, M_G3N_GEN_TYPE(oplist), M_ID x, oplist)
|
||||
#define M_G3N_CALL_4_func_test(gentype, op, svar, type1, var1, type2, var2, type3, var3, type4, var4, oplist) \
|
||||
M_IF_METHOD(op, oplist())(M_G3N_CALL_4_func_expand, M_EAT)(gentype, op, svar, M_G3N_TYPE(type1, oplist), var1, M_G3N_TYPE(type2, oplist), var2, M_G3N_TYPE(type3, oplist), var3, M_G3N_TYPE(type4, oplist), var4, oplist)
|
||||
#define M_G3N_CALL_4_func_expand(gentype, op, svar, type1, var1, type2, var2, oplist) \
|
||||
gentype: M_C(M_CALL_, op)(oplist(), M_AS_LINKED_TYPE(gentype, svar, type1, var1), M_AS_LINKED_TYPE(gentype, svar, type2, var2) ), \
|
||||
const gentype: M_C(M_CALL_, op)(oplist(), M_AS_LINKED_TYPE(const gentype, svar, type1, var1), M_AS_LINKED_TYPE(const gentype, svar, type2, var2) ),
|
||||
|
||||
// Call the OPERATOR call of the oplist registered to the variable 'x'
|
||||
// which takes one argument, it_type
|
||||
#define M_G3N_CALL_1i(op, x) \
|
||||
M_G3N_BEGIN_PROTECTED_CODE \
|
||||
_Generic( ((void)0, (x)), \
|
||||
M_MAP2(M_G3N_CALL_1i_func, (op, x) M_G3N_REGISTERED_ITEMS() ) \
|
||||
struct m_g3neric_dummy_s *: /* cannot happen */ (void)0) \
|
||||
M_G3N_END_PROTECTED_CODE
|
||||
#define M_G3N_CALL_1i_func(x, oplist) \
|
||||
M_G3N_CALL_1_func_test(M_G3N_GEN_IT_TYPE(oplist), M_PAIR_1 x, M_PAIR_2 x, oplist)
|
||||
|
||||
// Call the OPERATOR call of the oplist registered to the variable 'svar'
|
||||
// which takes 2 arguments of the given it_type
|
||||
#define M_G3N_CALL_2i(op, svar, type1, var1, type2, var2) \
|
||||
M_G3N_BEGIN_PROTECTED_CODE \
|
||||
_Generic( ((void)0, (svar)), \
|
||||
M_MAP2(M_G3N_CALL_2i_func, (op, svar, type1, var1, type2, var2) M_G3N_REGISTERED_ITEMS() ) \
|
||||
struct m_g3neric_dummy_s *: /* cannot happen */ (void) 0) \
|
||||
M_G3N_END_PROTECTED_CODE
|
||||
#define M_G3N_CALL_2i_func(x, oplist) \
|
||||
M_APPLY(M_G3N_CALL_2_func_test, M_G3N_GEN_IT_TYPE(oplist), M_ID x, oplist)
|
||||
|
||||
// Create a variable of type OPERATOR call of the oplist registered to the variable 'x'
|
||||
#define M_G3N_TYPE_1(op, x) \
|
||||
M_G3N_BEGIN_PROTECTED_CODE \
|
||||
_Generic( ((void)0, (x)), \
|
||||
M_MAP2(M_G3N_TYPE_1_func, (op, x) M_G3N_REGISTERED_ITEMS() ) \
|
||||
struct m_g3neric_dummy_s *: /* cannot happen */ (void)0) \
|
||||
M_G3N_END_PROTECTED_CODE
|
||||
#define M_G3N_TYPE_1_func(x, oplist) \
|
||||
M_G3N_TYPE_1_func_test(M_G3N_GEN_TYPE(oplist), M_PAIR_1 x, M_PAIR_2 x, oplist)
|
||||
#define M_G3N_TYPE_1_func_test(gentype, op, x, oplist) \
|
||||
M_IF_METHOD(op, oplist())(M_G3N_TYPE_1_func_expand, M_EAT)(gentype, op, x, oplist)
|
||||
#define M_G3N_TYPE_1_func_expand(gentype, op, x, oplist) \
|
||||
gentype: (M_G3N_TYPE(op, oplist)){0}, \
|
||||
const gentype: (M_G3N_TYPE(op, oplist)){0},
|
||||
|
||||
|
||||
// for each item in the container
|
||||
// Same as M_EACH except it uses the generic macros
|
||||
#define M_G3N_EACHI(item, container, l_it, l_pass) \
|
||||
(bool l_pass = true; l_pass; l_pass = false) \
|
||||
for(sub_type(container) *item; l_pass ; l_pass = false) \
|
||||
for(it_type(container) l_it; l_pass ; l_pass = false) \
|
||||
for(it_first(l_it, container) ; \
|
||||
!it_end_p(l_it) \
|
||||
&& (item = it_ref(l_it), true) ; \
|
||||
it_next(l_it))
|
||||
|
||||
|
||||
// TODO: init_with ? How to handle the different type of parameters ? Using EMPLACE_TYPE ?
|
||||
// TODO: support of PUSH_EMPLACE ?
|
||||
// TODO: support of "let" directly to avoid using the post-processing registration of M_LET?
|
||||
|
||||
// Overwrite M_FPRINT & M_PRINT to support global oplists registration
|
||||
#define M_FPRINT_ARG_G3N(f, x) \
|
||||
M_G3N_BEGIN_PROTECTED_CODE \
|
||||
_Generic(((void)0,(x)), \
|
||||
char: fprintf(f, "%c", M_AS_TYPE(char,x)), \
|
||||
bool: fprintf(f, "%d", M_AS_TYPE(bool,x)), \
|
||||
signed char: fprintf(f, "%hhd", M_AS_TYPE(signed char,x)), \
|
||||
unsigned char: fprintf(f, "%hhu", M_AS_TYPE(unsigned char,x)), \
|
||||
signed short: fprintf(f, "%hd", M_AS_TYPE(signed short,x)), \
|
||||
unsigned short: fprintf(f, "%hu", M_AS_TYPE(unsigned short,x)), \
|
||||
signed int: fprintf(f, "%d", M_AS_TYPE(signed int,x)), \
|
||||
unsigned int: fprintf(f, "%u", M_AS_TYPE(unsigned int,x)), \
|
||||
long int: fprintf(f, "%ld", M_AS_TYPE(long int,x)), \
|
||||
unsigned long int: fprintf(f, "%lu", M_AS_TYPE(unsigned long int,x)), \
|
||||
long long int: fprintf(f, "%lld", M_AS_TYPE(long long int,x)), \
|
||||
unsigned long long int: fprintf(f, "%llu", M_AS_TYPE(unsigned long long int,x)), \
|
||||
float: fprintf(f, "%f", M_AS_TYPE(float,x)), \
|
||||
double: fprintf(f, "%f", M_AS_TYPE(double,x)), \
|
||||
long double: fprintf(f, "%Lf", M_AS_TYPE(long double,x)), \
|
||||
const char *: fprintf(f, "%s", M_AS_TYPE(const char*,x)), \
|
||||
char *: fprintf(f, "%s", M_AS_TYPE(char*,x)), \
|
||||
const void *: fprintf(f, "%p", M_AS_TYPE(const void *,x)), \
|
||||
void *: fprintf(f, "%p", M_AS_TYPE(void *,x)), \
|
||||
M_MAP2(M_G3N_CALL_2_func, (OUT_STR, x, m_g3n_file, f, TYPE, x) M_G3N_REGISTERED_ITEMS() ) \
|
||||
struct m_g3neric_dummy_s *: /* cannot happen */ (void) 0) \
|
||||
M_G3N_END_PROTECTED_CODE
|
||||
|
||||
#define M_G3N_FPRINT_ARG(f, x) \
|
||||
M_IF(M_PARENTHESIS_P(x)) \
|
||||
( M_FPRINT_ARG_OUT_STR(f, M_PAIR_2 x, M_PAIR_1 x), \
|
||||
M_FPRINT_ARG_G3N(f, x) )
|
||||
|
||||
/* Ovverride M_PRINT */
|
||||
#undef M_PRINT
|
||||
#define M_PRINT(...) do { M_REDUCE2(M_G3N_FPRINT_ARG, M_SEPARATE_PER_SEMICOLON, stdout, __VA_ARGS__); } while (0)
|
||||
|
||||
#undef M_FPRINT
|
||||
#define M_FPRINT(f,...) do { M_REDUCE2(M_G3N_FPRINT_ARG, M_SEPARATE_PER_SEMICOLON, f, __VA_ARGS__); } while (0)
|
||||
|
||||
// Register CORE component of M*LIB
|
||||
#define M_GENERIC_ORG_MLIB_COMP_1() (CORE)
|
||||
|
||||
|
||||
#endif
|
||||
+1
-1
@@ -100,7 +100,7 @@ typedef struct m_il1st_head_s {
|
||||
INIT_MOVE(M_F(name, _init_move)), \
|
||||
MOVE(M_F(name, _move)), \
|
||||
NAME(name), \
|
||||
TYPE(M_F(name,_ct)), \
|
||||
TYPE(M_F(name,_ct)), GENTYPE(struct M_F(name,_s)*), \
|
||||
RESET(M_F(name,_reset)), \
|
||||
SUBTYPE(M_F(name,_subtype_ct)), \
|
||||
EMPTY_P(M_F(name,_empty_p)), \
|
||||
|
||||
+3
-3
@@ -48,11 +48,11 @@ M_BEGIN_PROTECTED_CODE
|
||||
|
||||
/* Value of the interface field for static intialization (Uses C99 designated element). */
|
||||
#define M_ISHARED_PTR_STATIC_DESIGNATED_INIT(name, type) \
|
||||
.M_F(name, _cpt) = ATOMIC_VAR_INIT(0)
|
||||
.M_F(name, _cpt) = M_ATOMIC_VAR_INIT(0)
|
||||
|
||||
/* Value of the interface field for static intialization (Uses C89 designated element). */
|
||||
#define M_ISHARED_PTR_STATIC_INIT(name, type) \
|
||||
ATOMIC_VAR_INIT(0)
|
||||
M_ATOMIC_VAR_INIT(0)
|
||||
|
||||
|
||||
/* Define the intrusive shared pointer type and its M_INLINE functions.
|
||||
@@ -95,7 +95,7 @@ M_BEGIN_PROTECTED_CODE
|
||||
CLEAR(M_F(name, _clear)), \
|
||||
RESET(M_F(name, _reset) M_IPTR), \
|
||||
NAME(name), \
|
||||
TYPE(M_F(name, _ct)), \
|
||||
TYPE(M_F(name, _ct)), GENTYPE(struct M_F(name,_s)*), \
|
||||
OPLIST(oplist), \
|
||||
SUBTYPE(M_F(name, _subtype_ct)) \
|
||||
)
|
||||
|
||||
@@ -109,7 +109,7 @@
|
||||
INIT_MOVE(M_F(name, _init_move)), \
|
||||
SWAP(M_F(name, _swap)), \
|
||||
NAME(name), \
|
||||
TYPE(M_F(name,_ct)), \
|
||||
TYPE(M_F(name,_ct)), GENTYPE(struct M_F(name,_s)**), \
|
||||
SUBTYPE(M_F(name,_subtype_ct)), \
|
||||
EMPTY_P(M_F(name,_empty_p)), \
|
||||
IT_TYPE(M_F(name, _it_ct)), \
|
||||
|
||||
+1
-1
@@ -82,7 +82,7 @@
|
||||
,MOVE(M_F(name, _move)) \
|
||||
,SWAP(M_F(name, _swap)) \
|
||||
,NAME(name) \
|
||||
,TYPE(M_F(name,_ct)) \
|
||||
,TYPE(M_F(name,_ct)), GENTYPE(struct M_F(name,_s)*) \
|
||||
,SUBTYPE(M_F(name, _subtype_ct)) \
|
||||
,RESET(M_F(name,_reset)) \
|
||||
,PUSH(M_F(name,_push)) \
|
||||
|
||||
+1
-1
@@ -81,7 +81,7 @@
|
||||
MOVE(M_F(name, _move)), \
|
||||
SWAP(M_F(name, _swap)), \
|
||||
NAME(name), \
|
||||
TYPE(M_F(name,_ct)), \
|
||||
TYPE(M_F(name,_ct)), GENTYPE(struct M_F(name,_s)*), \
|
||||
SUBTYPE(M_F(name, _subtype_ct)), \
|
||||
EMPTY_P(M_F(name,_empty_p)), \
|
||||
GET_SIZE(M_F(name, _size)), \
|
||||
|
||||
+39
-11
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* M*LIB - SHARED Module
|
||||
* M*LIB - SHARED Pointer Module
|
||||
*
|
||||
* Copyright (c) 2017-2023, Patrick Pelissier
|
||||
* All rights reserved.
|
||||
@@ -118,7 +118,7 @@ M_BEGIN_PROTECTED_CODE
|
||||
MOVE(M_F(name, _move)), \
|
||||
SWAP(M_F(name, _swap)) \
|
||||
,NAME(name) \
|
||||
,TYPE(M_F(name, _ct)) \
|
||||
,TYPE(M_F(name, _ct)), GENTYPE(struct M_F(name,_s)*) \
|
||||
)
|
||||
|
||||
// OPLIST to handle a counter of atomic type
|
||||
@@ -166,6 +166,7 @@ M_INLINE int m_shar3d_integer_cref(int *p) { return *p; }
|
||||
M_SHAR3D_PTR_DEF_TYPE(name, type, oplist, cpt_oplist, shared_t) \
|
||||
M_CHECK_COMPATIBLE_OPLIST(name, 1, type, oplist) \
|
||||
M_SHAR3D_PTR_DEF_CORE(name, type, oplist, cpt_oplist, shared_t) \
|
||||
M_EMPLACE_QUEUE_DEF(name, cpt_oplist, M_F(name, _init_with), oplist, M_SHAR3D_PTR_DEF_EMPLACE)
|
||||
|
||||
/* Define the types */
|
||||
#define M_SHAR3D_PTR_DEF_TYPE(name, type, oplist, cpt_oplist, shared_t) \
|
||||
@@ -180,10 +181,11 @@ M_INLINE int m_shar3d_integer_cref(int *p) { return *p; }
|
||||
\
|
||||
/* Internal type for oplist */ \
|
||||
typedef shared_t M_F(name, _ct); \
|
||||
typedef type M_F(name, _subtype_ct); \
|
||||
\
|
||||
typedef struct M_F(name, combine_s) { \
|
||||
type data; \
|
||||
typedef struct M_F(name, _combine_s) { \
|
||||
struct M_F(name, _s) ptr; \
|
||||
type data; \
|
||||
} M_F(name, combine_ct)[1]; \
|
||||
|
||||
/* Define the core functions */
|
||||
@@ -222,18 +224,18 @@ M_INLINE int m_shar3d_integer_cref(int *p) { return *p; }
|
||||
M_F(name, _init_new)(shared_t shared) \
|
||||
{ \
|
||||
/* NOTE: Alloc 1 struct with both structures. */ \
|
||||
struct M_F(name, combine_s) *p = \
|
||||
M_CALL_NEW(oplist, struct M_F(name, combine_s)); \
|
||||
struct M_F(name, _combine_s) *p = \
|
||||
M_CALL_NEW(oplist, struct M_F(name, _combine_s)); \
|
||||
if (M_UNLIKELY_NOMEM (p == NULL)) { \
|
||||
M_MEMORY_FULL(sizeof(struct M_F(name, combine_s))); \
|
||||
M_MEMORY_FULL(sizeof(struct M_F(name, _combine_s))); \
|
||||
return; \
|
||||
} \
|
||||
struct M_F(name, _s) *ptr = &p->ptr; \
|
||||
ptr->combineAlloc = true; \
|
||||
type *data = &p->data; \
|
||||
M_CALL_INIT( oplist, *data); \
|
||||
ptr->data = data; \
|
||||
M_CALL_INIT_SET(cpt_oplist, &ptr->cpt, 1); \
|
||||
ptr->combineAlloc = true; \
|
||||
*shared = ptr; \
|
||||
M_SHAR3D_CONTRACT(shared, cpt_oplist); \
|
||||
} \
|
||||
@@ -272,9 +274,10 @@ M_INLINE int m_shar3d_integer_cref(int *p) { return *p; }
|
||||
first element, aka data itself. Static analyzer tools don't \
|
||||
seem to detect this and report error. */ \
|
||||
M_CALL_CLEAR(oplist, *(*dest)->data); \
|
||||
M_CALL_DEL(oplist, (*dest)->data); \
|
||||
if (combineAlloc == false) \
|
||||
M_CALL_DEL(oplist, *dest); \
|
||||
if (combineAlloc == false) { \
|
||||
M_CALL_DEL(oplist, (*dest)->data); \
|
||||
} \
|
||||
M_CALL_DEL(oplist, *dest); \
|
||||
} \
|
||||
*dest = NULL; \
|
||||
} \
|
||||
@@ -361,6 +364,30 @@ M_INLINE int m_shar3d_integer_cref(int *p) { return *p; }
|
||||
return data; \
|
||||
} \
|
||||
|
||||
/* Definition of the emplace_back function for arrays */
|
||||
#define M_SHAR3D_PTR_DEF_EMPLACE(name, cpt_oplist, function_name, oplist, init_func, exp_emplace_type) \
|
||||
M_INLINE void \
|
||||
function_name(M_F(name, _ct) shared \
|
||||
M_EMPLACE_LIST_TYPE_VAR(a, exp_emplace_type) ) \
|
||||
{ \
|
||||
/* NOTE: Alloc 1 struct with both structures. */ \
|
||||
struct M_F(name, _combine_s) *p = \
|
||||
M_CALL_NEW(oplist, struct M_F(name, _combine_s)); \
|
||||
if (M_UNLIKELY_NOMEM (p == NULL)) { \
|
||||
M_MEMORY_FULL(sizeof(struct M_F(name, _combine_s))); \
|
||||
return; \
|
||||
} \
|
||||
struct M_F(name, _s) *ptr = &p->ptr; \
|
||||
ptr->combineAlloc = true; \
|
||||
M_F(name, _subtype_ct) *data = &p->data; \
|
||||
M_EMPLACE_CALL_FUNC(a, init_func, oplist, *data, exp_emplace_type); \
|
||||
ptr->data = data; \
|
||||
M_CALL_INIT_SET(cpt_oplist, &ptr->cpt, 1); \
|
||||
*shared = ptr; \
|
||||
M_SHAR3D_CONTRACT(shared, cpt_oplist); \
|
||||
} \
|
||||
|
||||
|
||||
|
||||
/********************************** INTERNAL *********************************/
|
||||
|
||||
@@ -407,6 +434,7 @@ M_INLINE int m_shar3d_integer_cref(int *p) { return *p; }
|
||||
\
|
||||
/* Internal Types for oplist */ \
|
||||
typedef shared_t M_F(name, _ct); \
|
||||
typedef type M_F(name, _subtype_ct); \
|
||||
|
||||
/* Define the core functions */
|
||||
#define M_SHAR3D_RESOURCE_DEF_CORE(name, type, oplist, shared_t, it_t) \
|
||||
|
||||
+1
-1
@@ -112,7 +112,7 @@ M_BEGIN_PROTECTED_CODE
|
||||
,SET(M_F(name, _set)) \
|
||||
,CLEAR(M_F(name, _clear)) \
|
||||
,NAME(name) \
|
||||
,TYPE(M_F(name, _ct)) \
|
||||
,TYPE(M_F(name, _ct)) , GENTYPE(struct M_F(name,_s)*) \
|
||||
,SUBTYPE(M_F(name, _subtype_ct)) \
|
||||
,OPLIST(oplist) \
|
||||
,M_IF_METHOD(INIT_MOVE, oplist)(INIT_MOVE(M_F(name, _init_move)),) \
|
||||
|
||||
+9
-6
@@ -2023,14 +2023,14 @@ m_string_utf8_p(m_string_t str)
|
||||
/* Use of Compound Literals to init a constant string.
|
||||
NOTE: The use of the additional structure layer is to ensure
|
||||
that the pointer to char is properly aligned to an int (this
|
||||
is a needed asumption by m_string_hash).
|
||||
is a needed assumption by m_string_hash).
|
||||
Otherwise it could have been :
|
||||
#define M_STRING_CTE(s) \
|
||||
((const m_string_t){{.size = sizeof(s)-1, .alloc = sizeof(s), \
|
||||
.ptr = s}})
|
||||
which produces faster code.
|
||||
Note: This code doesn't work with C++ (use of c99 feature
|
||||
of recursive struct definition and compound literral).
|
||||
of recursive struct definition and compound literal).
|
||||
As such, there is a separate C++ definition.
|
||||
*/
|
||||
#ifndef __cplusplus
|
||||
@@ -2101,7 +2101,7 @@ namespace m_lib {
|
||||
SWAP(m_string_swap), RESET(m_string_reset), \
|
||||
EMPTY_P(m_string_empty_p), \
|
||||
CLEAR(m_string_clear), HASH(m_string_hash), EQUAL(m_string_equal_p), \
|
||||
CMP(m_string_cmp), TYPE(m_string_t), \
|
||||
CMP(m_string_cmp), TYPE(m_string_t), GENTYPE(struct m_string_s*), \
|
||||
PARSE_STR(m_string_parse_str), GET_STR(m_string_get_str), \
|
||||
OUT_STR(m_string_out_str), IN_STR(m_string_in_str), \
|
||||
OUT_SERIAL(m_string_out_serial), IN_SERIAL(m_string_in_serial), \
|
||||
@@ -2115,13 +2115,16 @@ namespace m_lib {
|
||||
,IT_END_P(m_string_end_p) \
|
||||
,IT_EQUAL_P(m_string_it_equal_p) \
|
||||
,IT_NEXT(m_string_next) \
|
||||
,IT_CREF(m_string_cref) \
|
||||
,IT_CREF(m_string_cref), PUSH(m_string_push_u) \
|
||||
,EMPLACE_TYPE(const char*) \
|
||||
)
|
||||
|
||||
/* Register the OPLIST as a global one */
|
||||
#define M_OPL_m_string_t() M_STRING_OPLIST
|
||||
|
||||
/* Register the string_t oplist as a generic type */
|
||||
#define M_GENERIC_ORG_MLIB_COMP_CORE_OPLIST_1() M_STRING_OPLIST
|
||||
|
||||
|
||||
/***********************************************************************/
|
||||
/* */
|
||||
@@ -2194,7 +2197,7 @@ namespace m_lib {
|
||||
/* Set the string a to the string (or C string) b */
|
||||
#define m_string_set(a,b) M_STR1NG_SELECT2(m_string_set, m_string_set_cstr, a, b)
|
||||
|
||||
/* Concatene the string (or C string) b to the string a */
|
||||
/* Concatenate the string (or C string) b to the string a */
|
||||
#define m_string_cat(a,b) M_STR1NG_SELECT2(m_string_cat, m_string_cat_cstr, a, b)
|
||||
|
||||
/* Compare the string a to the string (or C string) b and return the sort order */
|
||||
@@ -2270,7 +2273,7 @@ namespace m_lib {
|
||||
HASH(M_F(name,_hash)), \
|
||||
EQUAL(M_F(name,_equal_p)), \
|
||||
CMP(M_F(name,_cmp)), \
|
||||
TYPE(M_F(name,_ct)), \
|
||||
TYPE(M_F(name,_ct)), GENTYPE(struct M_F(name,_s)*), \
|
||||
OOR_EQUAL(M_F(name,_oor_equal_p)), \
|
||||
OOR_SET(M_F(name, _oor_set)), \
|
||||
PARSE_STR(M_F(name,_parse_str)), \
|
||||
|
||||
+1
-1
@@ -681,7 +681,7 @@ typedef struct {
|
||||
} m_once_t[1];
|
||||
|
||||
// Initial value for m_once_t
|
||||
#define M_ONCE_INIT_VALUE { { ATOMIC_VAR_INIT(0) } }
|
||||
#define M_ONCE_INIT_VALUE { { M_ATOMIC_VAR_INIT(0) } }
|
||||
|
||||
// Call the function exactly once
|
||||
M_INLINE void m_once_call(m_once_t o, void (*func)(void))
|
||||
|
||||
@@ -134,6 +134,7 @@ namespace m_lib {
|
||||
M_TUPL3_DEFINE_CLEAR(name, __VA_ARGS__) \
|
||||
M_TUPL3_DEFINE_GETTER_FIELD(name, __VA_ARGS__) \
|
||||
M_TUPL3_DEFINE_SETTER_FIELD(name, __VA_ARGS__) \
|
||||
M_TUPL3_DEFINE_EMPLACE_FIELD(name, __VA_ARGS__) \
|
||||
M_TUPL3_IF_ONE(CMP, __VA_ARGS__)(M_TUPL3_DEFINE_CMP(name, __VA_ARGS__),) \
|
||||
M_TUPL3_IF_ALL(CMP, __VA_ARGS__)(M_TUPL3_DEFINE_CMP_ORDER(name, __VA_ARGS__),) \
|
||||
M_TUPL3_DEFINE_CMP_FIELD(name, __VA_ARGS__) \
|
||||
@@ -348,6 +349,25 @@ namespace m_lib {
|
||||
}
|
||||
|
||||
|
||||
/* Define the EMPLACE_field methods for all params. */
|
||||
#define M_TUPL3_DEFINE_EMPLACE_FIELD(name, ...) \
|
||||
M_REDUCE3(M_TUPL3_DEFINE_EMPLACE_FIELD_PROTO, M_TUPL3_DEFINE_EMPLACE_G, name, __VA_ARGS__)
|
||||
|
||||
#define M_TUPL3_DEFINE_EMPLACE_G(a, b) a b
|
||||
|
||||
#define M_TUPL3_DEFINE_EMPLACE_FIELD_PROTO(name, id, a) \
|
||||
M_EMPLACE_QUEUE_DEF(M_TUPL3_GET_FIELD a, M_F(name, _ct), M_C3(name, _emplace_, M_TUPL3_GET_FIELD a), M_TUPL3_GET_OPLIST a, M_TUPL3_EMPLACE_DEF)
|
||||
|
||||
#define M_TUPL3_EMPLACE_DEF(name, name_t, function_name, oplist, init_func, exp_emplace_type) \
|
||||
M_INLINE void \
|
||||
function_name(name_t v \
|
||||
M_EMPLACE_LIST_TYPE_VAR(a, exp_emplace_type) ) \
|
||||
{ \
|
||||
M_CALL_CLEAR(oplist, v->name); \
|
||||
M_EMPLACE_CALL_FUNC(a, init_func, oplist, v->name, exp_emplace_type); \
|
||||
}
|
||||
|
||||
|
||||
/* Define the CMP method by calling CMP methods for all params. */
|
||||
#define M_TUPL3_DEFINE_CMP(name, ...) \
|
||||
M_INLINE int M_F(name, _cmp)(M_F(name,_ct) const e1 , \
|
||||
@@ -730,7 +750,7 @@ namespace m_lib {
|
||||
SET(M_F(name,_set)), \
|
||||
CLEAR(M_F(name, _clear)), \
|
||||
NAME(name), \
|
||||
TYPE(M_F(name,_ct)), \
|
||||
TYPE(M_F(name,_ct)), GENTYPE(struct M_F(name,_s)*), \
|
||||
OPLIST( (__VA_ARGS__) ), \
|
||||
M_IF_METHOD_ALL(CMP, __VA_ARGS__)(CMP(M_F(name, _cmp)),), \
|
||||
M_IF_METHOD_ALL(HASH, __VA_ARGS__)(HASH(M_F(name, _hash)),), \
|
||||
|
||||
+1
-1
@@ -774,7 +774,7 @@ M_INLINE void \
|
||||
CLEAR(M_F(name, _clear)), \
|
||||
RESET(M_F(name, _reset)), \
|
||||
NAME(name), \
|
||||
TYPE(M_F(name,_ct)), \
|
||||
TYPE(M_F(name,_ct)), GENTYPE(struct M_F(name,_s)*), \
|
||||
EMPTY_P(M_F(name,_empty_p)), \
|
||||
M_IF_METHOD_ALL(HASH, __VA_ARGS__)(HASH(M_F(name, _hash)),), \
|
||||
M_IF_METHOD_ALL(EQUAL, __VA_ARGS__)(EQUAL(M_F(name, _equal_p)),), \
|
||||
|
||||
+5
-3
@@ -887,13 +887,15 @@ static void test_M_CSTR(void)
|
||||
#undef M_USE_CSTR_ALLOC
|
||||
#define M_USE_CSTR_ALLOC 8
|
||||
// Disable GNUC warning as we want to test this behavior.
|
||||
// this warning is not supported by CLANG
|
||||
#if defined (__GNUC__) && __GNUC__ >= 7 && !defined(__clang__)
|
||||
// this warning is supported by CLANG
|
||||
#if (defined (__GNUC__) && __GNUC__ >= 7 && !defined(__clang__)) \
|
||||
||(defined(__clang__) && __clang_major__ >= 18)
|
||||
_Pragma("GCC diagnostic push")
|
||||
_Pragma("GCC diagnostic ignored \"-Wformat-truncation\"")
|
||||
#endif
|
||||
r = strcmp(M_CSTR("Hello %s %c", "World", '!'), "Hello W");
|
||||
#if defined(__GNUC__) && __GNUC__ >= 7 && !defined(__clang__)
|
||||
#if (defined (__GNUC__) && __GNUC__ >= 7 && !defined(__clang__)) \
|
||||
||(defined(__clang__) && __clang_major__ >= 18)
|
||||
_Pragma("GCC diagnostic pop")
|
||||
#endif
|
||||
assert (r == 0);
|
||||
|
||||
@@ -85,7 +85,8 @@ static void test_instance11(void)
|
||||
instance11_t cmp;
|
||||
int n = 2;
|
||||
instance11_init_with(cmp, -1, STRING_CTE("INIT") );
|
||||
int x = interface1_call(instance11_as_interface(cmp), 10, &n);
|
||||
interface1_t interface = instance11_as_interface(cmp);
|
||||
int x = interface1_call(interface, 10, &n);
|
||||
assert(x == -8);
|
||||
assert(string_equal_str_p(cmp->c, "CALLED"));
|
||||
instance11_clear(cmp);
|
||||
@@ -111,7 +112,8 @@ static void test_instance21(void)
|
||||
{
|
||||
instance21_t cmp;
|
||||
instance21_init(cmp);
|
||||
int x = interface2_call(instance21_as_interface(cmp));
|
||||
interface2_t interface = instance21_as_interface(cmp);
|
||||
int x = interface2_call(interface);
|
||||
assert(x == 1);
|
||||
instance21_clear(cmp);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,154 @@
|
||||
/*
|
||||
* Copyright (c) 2017-2023, Patrick Pelissier
|
||||
* All rights reserved.
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
* + Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* + Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
|
||||
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
|
||||
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <assert.h>
|
||||
#include "coverage.h"
|
||||
|
||||
#include "m-string.h"
|
||||
#include "m-array.h"
|
||||
#include "m-list.h"
|
||||
#include "m-generic.h"
|
||||
|
||||
// Generic is not supported if not C11
|
||||
// TCC has some issues in the preprocessing.
|
||||
// See https://lists.nongnu.org/archive/html/tinycc-devel/2023-12/msg00004.html
|
||||
#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L && !defined(__TINYC__)
|
||||
|
||||
ARRAY_DEF(array_int, int)
|
||||
#define M_OPL_array_int_t() ARRAY_OPLIST(array_int, M_BASIC_OPLIST)
|
||||
|
||||
LIST_DEF(list_int, int)
|
||||
#define M_OPL_list_int_t() LIST_OPLIST(list_int, M_BASIC_OPLIST)
|
||||
|
||||
static void h(string_t x)
|
||||
{
|
||||
string_set_str(x, "TEST STRING");
|
||||
}
|
||||
|
||||
// Global variable to test genericity on a global variable
|
||||
const string_t gx;
|
||||
|
||||
#define FLT1 (GENTYPE(float), TYPE(float), INIT(M_INIT_BASIC), INIT_SET(M_SET_BASIC), SET(M_SET_BASIC), \
|
||||
CLEAR(M_NOTHING_DEFAULT) )
|
||||
#define INT1 (GENTYPE(int), TYPE(int), INIT(M_INIT_BASIC), INIT_SET(M_SET_BASIC), SET(M_SET_BASIC), \
|
||||
CLEAR(M_NOTHING_DEFAULT) )
|
||||
|
||||
//NOTE: string_t is registered by the CORE component of the MLIB organization
|
||||
|
||||
#define M_GENERIC_ORG_2() (USER)
|
||||
#define M_GENERIC_ORG_USER_COMP_1() (CORE)
|
||||
#define M_GENERIC_ORG_USER_COMP_CORE_OPLIST_6() FLT1
|
||||
#define M_GENERIC_ORG_USER_COMP_CORE_OPLIST_1() INT1
|
||||
|
||||
#define M_GENERIC_ORG_3() (ARRAY)
|
||||
#define M_GENERIC_ORG_ARRAY_COMP_5() (AINT)
|
||||
#define M_GENERIC_ORG_ARRAY_COMP_AINT_OPLIST_6() M_OPL_array_int_t()
|
||||
#define M_GENERIC_ORG_ARRAY_COMP_10() (LINT)
|
||||
#define M_GENERIC_ORG_ARRAY_COMP_LINT_OPLIST_30() M_OPL_list_int_t()
|
||||
|
||||
static bool test_empty(const string_t p)
|
||||
{
|
||||
return empty_p(p);
|
||||
}
|
||||
|
||||
static void test_string(string_t p)
|
||||
{
|
||||
string_t s , d;
|
||||
|
||||
init(s);
|
||||
h(s);
|
||||
init_set(d, s);
|
||||
h(d);
|
||||
init_set(p, s);
|
||||
bool b = test_empty(p);
|
||||
assert(!b);
|
||||
push(p, 'c');
|
||||
out_str(stdout, p);
|
||||
clear(s);
|
||||
clear(d);
|
||||
|
||||
float f;
|
||||
init(f);
|
||||
clear(f);
|
||||
|
||||
clear(p);
|
||||
}
|
||||
|
||||
static void test_array(void)
|
||||
{
|
||||
M_LET(a, array_int_t) {
|
||||
bool b = empty_p(a);
|
||||
assert(b);
|
||||
push(a, 14);
|
||||
b = empty_p(a);
|
||||
assert(!b);
|
||||
push(a, 15);
|
||||
out_str(stdout, a);
|
||||
for each(it, a) {
|
||||
printf("=%d, ", *it);
|
||||
}
|
||||
int z;
|
||||
pop(&z, a);
|
||||
assert(z == 15);
|
||||
}
|
||||
// Just change the type, same code!
|
||||
M_LET(a, list_int_t) {
|
||||
push(a, 14);
|
||||
push(a, 15);
|
||||
out_str(stdout, a);
|
||||
for each(it, a) {
|
||||
printf("=%d, ", *it);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void test_print(void)
|
||||
{
|
||||
int x = 0;
|
||||
M_PRINT("X= ", x, "\n");
|
||||
M_LET( (s, "Hello"), string_t) {
|
||||
M_PRINT("s=", s, "\n");
|
||||
}
|
||||
M_LET( (a, 3,4,5), array_int_t) {
|
||||
M_PRINT("a=", a, "\n");
|
||||
}
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
FILE *f = freopen("atmp-generic.dat", "w", stdout);
|
||||
if (!f) abort();
|
||||
string_t p;
|
||||
test_string(p);
|
||||
test_array();
|
||||
test_print();
|
||||
exit(0);
|
||||
}
|
||||
|
||||
#else
|
||||
int main(void)
|
||||
{
|
||||
exit(0);
|
||||
}
|
||||
#endif
|
||||
@@ -61,7 +61,20 @@ TUPLE_DEF2(my,
|
||||
(vall, double),
|
||||
(valm, long double)
|
||||
)
|
||||
#define M_OPL_my_t() TUPLE_OPLIST(my, M_BASIC_OPLIST, M_BASIC_OPLIST, M_BASIC_OPLIST, STRING_OPLIST, M_OPL_a2_t(), M_OPL_v2_t(), M_OPL_l2_t(), M_OPL_d2_t() )
|
||||
#define M_OPL_my_t() TUPLE_OPLIST(my, \
|
||||
M_BASIC_OPLIST, \
|
||||
M_BASIC_OPLIST, \
|
||||
M_BOOL_OPLIST, \
|
||||
STRING_OPLIST, \
|
||||
M_OPL_a2_t(), \
|
||||
M_OPL_v2_t(), \
|
||||
M_OPL_l2_t(), \
|
||||
M_OPL_d2_t(), \
|
||||
M_BASIC_OPLIST, \
|
||||
M_BASIC_OPLIST, \
|
||||
M_BASIC_OPLIST, \
|
||||
M_BASIC_OPLIST, \
|
||||
M_BASIC_OPLIST )
|
||||
|
||||
TUPLE_DEF2(my2,
|
||||
(activated, bool),
|
||||
|
||||
@@ -135,6 +135,15 @@ static void test2(void)
|
||||
shared_int_clear(p1);
|
||||
}
|
||||
|
||||
static void test_emplace(void)
|
||||
{
|
||||
shared_mpz_t z;
|
||||
|
||||
shared_mpz_init_with_ui(z, 23);
|
||||
assert ( (*shared_mpz_ref(z))->ptr[0] == 23);
|
||||
shared_mpz_clear(z);
|
||||
}
|
||||
|
||||
#define MAX_RESSOURCE 10
|
||||
|
||||
static void test_ressource(int n)
|
||||
@@ -223,6 +232,7 @@ int main(void)
|
||||
{
|
||||
test1();
|
||||
test2();
|
||||
test_emplace();
|
||||
for(int i = 1; i < MAX_RESSOURCE; i++) {
|
||||
test_ressource(i);
|
||||
}
|
||||
|
||||
@@ -104,6 +104,19 @@ static void check_swap(void)
|
||||
pair_str_clear(p2);
|
||||
}
|
||||
|
||||
static void check_emplace(void)
|
||||
{
|
||||
pair_str_t p1;
|
||||
pair_str_init (p1);
|
||||
|
||||
pair_str_emplace_vala(p1, "Hello");
|
||||
pair_str_emplace_valb(p1, "World");
|
||||
assert(string_equal_str_p (p1->vala, "Hello"));
|
||||
assert(string_equal_str_p (p1->valb, "World"));
|
||||
|
||||
pair_str_clear(p1);
|
||||
}
|
||||
|
||||
static void check_reset(void)
|
||||
{
|
||||
single_str_t p1;
|
||||
@@ -286,6 +299,7 @@ int main(void)
|
||||
check_io();
|
||||
check_swap();
|
||||
check_reset();
|
||||
check_emplace();
|
||||
check_chaining_tuple();
|
||||
test_double();
|
||||
testobj_final_check();
|
||||
|
||||
@@ -31,7 +31,7 @@ static int fib(int n);
|
||||
struct fib2_s {
|
||||
int x, n;
|
||||
};
|
||||
atomic_bool resetFunc_called = ATOMIC_VAR_INIT(false);
|
||||
atomic_bool resetFunc_called = M_ATOMIC_VAR_INIT(false);
|
||||
static void resetFunc(void)
|
||||
{
|
||||
atomic_store(&resetFunc_called, true);
|
||||
|
||||
Reference in new issue
Block a user