Compare commits
10 Commits
3b24e85bb0
...
fe07cf2106
Author | SHA1 | Date | |
---|---|---|---|
|
fe07cf2106 | ||
|
53e4cb652f | ||
|
9e642f9ed4 | ||
|
6f106988b4 | ||
|
1b860723b8 | ||
|
68a51e7d7e | ||
|
91b20bc366 | ||
|
bf5ad27064 | ||
|
476a01d6d4 | ||
|
d6bb77d112 |
15
array.h
Normal file
15
array.h
Normal file
@ -0,0 +1,15 @@
|
||||
#include <stdlib.h>
|
||||
#include "handle.h"
|
||||
|
||||
/* Generic array macro */
|
||||
#define Array(T,AT) struct AT { \
|
||||
size_t count; \
|
||||
T *v; \
|
||||
}; handle(AT) { \
|
||||
if(state == HANDLE_CREAT) { \
|
||||
self->count = *(size_t*) data; \
|
||||
self->v = (T*) calloc(self->count, sizeof(T)); \
|
||||
} else if (state == HANDLE_DESTR){ \
|
||||
if(self->v) free(self->v); \
|
||||
} \
|
||||
}
|
27
array_handle_gen_test.c
Normal file
27
array_handle_gen_test.c
Normal file
@ -0,0 +1,27 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include "array.h"
|
||||
|
||||
/* Prepare usage for 64 bit arrays - adds "Array_uint64" for creat */
|
||||
Array(uint64_t, Array_uint64);
|
||||
|
||||
int main() {
|
||||
size_t n = 0;
|
||||
printf("Number of elements to sum: ");
|
||||
scanf(" %zu", &n);
|
||||
|
||||
/* Create a 64 bit array of 'n' elements named 'data' */
|
||||
creat(Array_uint64, data, &n);
|
||||
|
||||
for(int i = 0; i < data.count; ++i) {
|
||||
scanf(" %zu", &n);
|
||||
data.v[i] = n;
|
||||
}
|
||||
n = 0; for(int i = 0; i < data.count; ++i) n += data.v[i];
|
||||
printf("Sum: %zu\n", n);
|
||||
|
||||
// RAII releases array here automagicaly!
|
||||
|
||||
return 0;
|
||||
}
|
32
array_handle_test.c
Normal file
32
array_handle_test.c
Normal file
@ -0,0 +1,32 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include "handle.h"
|
||||
|
||||
struct Vektor {
|
||||
int count;
|
||||
int *v;
|
||||
}; handle(Vektor) {
|
||||
if(HANDLE_CREAT == state) {
|
||||
self->count = *(int*) data;
|
||||
self->v = (int*) calloc(self->count, sizeof(int));
|
||||
} else if (HANDLE_DESTR == state){
|
||||
if(self->v) free(self->v);
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
int n;
|
||||
printf("Number of elements to sum: ");
|
||||
scanf(" %d", &n);
|
||||
creat(Vektor, data, &n);
|
||||
// printf("data.count:%d\n", data.count);
|
||||
|
||||
for(int i = 0; i < data.count; ++i) {
|
||||
scanf(" %d", &n);
|
||||
data.v[i] = n;
|
||||
}
|
||||
n = 0; for(int i = 0; i < data.count; ++i) n += data.v[i];
|
||||
printf("Sum: %d\n", n);
|
||||
|
||||
return 0;
|
||||
}
|
48
array_test.c
Normal file
48
array_test.c
Normal file
@ -0,0 +1,48 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
struct Vektor {
|
||||
int count;
|
||||
int *v;
|
||||
};
|
||||
typedef struct Vektor Vektor;
|
||||
|
||||
Vektor create_Vektor(int count) {
|
||||
Vektor self;
|
||||
self.count = count;
|
||||
self.v = (int*) calloc(self.count, sizeof(int));
|
||||
return self;
|
||||
}
|
||||
|
||||
int sum_Vektor(Vektor *self) {
|
||||
int s = 0;
|
||||
for(int i = 0; i < self->count; ++i) {
|
||||
s += self->v[i];
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
void delete_Vektor(Vektor *self) {
|
||||
if(self->v) free(self->v);
|
||||
}
|
||||
|
||||
int main() {
|
||||
int n;
|
||||
printf("Number of elements to sum: ");
|
||||
scanf(" %d", &n);
|
||||
Vektor data = create_Vektor(n);
|
||||
// printf("vektor.count:%d\n", data.count);
|
||||
|
||||
for(int i = 0; i < data.count; ++i) {
|
||||
scanf(" %d", &n);
|
||||
data.v[i] = n;
|
||||
}
|
||||
// n = 0; for(int i = 0; i < data.count; ++i) n += data.v[i];
|
||||
n = sum_Vektor(&data);
|
||||
printf("Sum: %d\n", n);
|
||||
|
||||
// Need to not forget this
|
||||
delete_Vektor(&data);
|
||||
|
||||
return 0;
|
||||
}
|
48
bugsy.c
Normal file
48
bugsy.c
Normal file
@ -0,0 +1,48 @@
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
enum HANDLE_STATE {
|
||||
HANDLE_CREAT,
|
||||
HANDLE_DESTR
|
||||
};
|
||||
|
||||
|
||||
struct Array_uint64 { size_t count; uint64_t *v; };
|
||||
static inline void Array_uint64_lifetime_handler(
|
||||
enum HANDLE_STATE state,
|
||||
struct Array_uint64 *self,
|
||||
void *data) {
|
||||
if(state == HANDLE_CREAT) {
|
||||
self->count = *(size_t*) data;
|
||||
printf("self: %zd, count: %d", self, self->count);
|
||||
self->v = (uint64_t*) calloc(self->count, sizeof(uint64_t));
|
||||
} else if (state == HANDLE_DESTR) {
|
||||
if(self->v) free(self->v);
|
||||
}
|
||||
};
|
||||
|
||||
int main() {
|
||||
// XXX: size_t n = 0;
|
||||
int n; // FIXME: BUG
|
||||
printf("Number of elements to sum: ");
|
||||
// XXX: scanf(" %zu", &n);
|
||||
scanf(" %d", &n); // FIXME: BUG
|
||||
|
||||
|
||||
[[gnu::always_inline]] inline void data_cleanuper(struct Array_uint64 *v) {
|
||||
Array_uint64_lifetime_handler(HANDLE_DESTR, v, ((void *)0) );
|
||||
}
|
||||
|
||||
[[gnu::cleanup(data_cleanuper)]] struct Array_uint64 data;
|
||||
Array_uint64_lifetime_handler(HANDLE_CREAT, & data, &n);;
|
||||
|
||||
for(int i = 0; i < data.count; ++i) {
|
||||
scanf(" %d", &n);
|
||||
data.v[i] = n;
|
||||
}
|
||||
n = 0; for(int i = 0; i < data.count; ++i) n += data.v[i];
|
||||
printf("Sum: %d\n", n);
|
||||
|
||||
return 0;
|
||||
}
|
@ -6,8 +6,8 @@ void __attribute__((destructor)) calledLast();
|
||||
|
||||
int main() {
|
||||
printf("\nI am in main");
|
||||
// return 0;
|
||||
exit(0); // Even called on this!!!
|
||||
return 0;
|
||||
// exit(0); // Even called on this!!!
|
||||
}
|
||||
|
||||
void calledFirst() {
|
||||
|
31
expanded.c
Normal file
31
expanded.c
Normal file
@ -0,0 +1,31 @@
|
||||
#include <stdio.h>
|
||||
|
||||
enum HANDLE_STATE {
|
||||
HANDLE_CREAT,
|
||||
HANDLE_DESTR
|
||||
};
|
||||
|
||||
struct Meaning {
|
||||
int a;
|
||||
int b;
|
||||
};
|
||||
|
||||
static inline void Meaning_lifetime_handler(enum HANDLE_STATE state, struct Meaning *this, void *data) {
|
||||
if(state == HANDLE_CREAT) {
|
||||
this->a = *(int*) data;
|
||||
this->b = 2;
|
||||
} else {
|
||||
printf("%d\n", this->a + this->b);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
int main() {
|
||||
int initializer = 40;
|
||||
[[gnu::always_inline]] inline void F(struct Meaning *v) { Meaning_lifetime_handler(HANDLE_DESTR, v,
|
||||
((void *)0)
|
||||
); } [[gnu::cleanup(F)]] struct Meaning d; Meaning_lifetime_handler(HANDLE_CREAT, & d, &initializer);;
|
||||
|
||||
return 0;
|
||||
}
|
35
expanded.cpp
Normal file
35
expanded.cpp
Normal file
@ -0,0 +1,35 @@
|
||||
#include <stdio.h>
|
||||
|
||||
enum HANDLE_STATE {
|
||||
HANDLE_CREAT,
|
||||
HANDLE_DESTR
|
||||
};
|
||||
|
||||
struct Vektor {
|
||||
int count;
|
||||
int *v;
|
||||
}; struct Vektor_raiicpp; static inline void Vektor_lifetime_handler( enum HANDLE_STATE state, struct Vektor_raiicpp *self, void *data); struct Vektor_raiicpp : public Vektor { inline Vektor_raiicpp(void *data) { Vektor_lifetime_handler(HANDLE_CREAT, this, data); } inline ~Vektor_raiicpp() { Vektor_lifetime_handler(HANDLE_DESTR, this, nullptr); } } static inline void Vektor_lifetime_handler( enum HANDLE_STATE state, struct Vektor_raiicpp *self, void *data) {
|
||||
if(state == HANDLE_CREAT) {
|
||||
self->count = *(int*) data;
|
||||
self->v = calloc(self->count, sizeof(int));
|
||||
} else if (state == HANDLE_DESTR){
|
||||
if(self->v) free(self->v);
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
int n;
|
||||
printf("Number of elements to sum: ");
|
||||
scanf(" %d", &n);
|
||||
Vektor_raiicpp data(&n);;
|
||||
|
||||
|
||||
for(int i = 0; i < data.count; ++i) {
|
||||
scanf(" %d", &n);
|
||||
data.v[i] = n;
|
||||
}
|
||||
n = 0; for(int i = 0; i < data.count; ++i) n += data.v[i];
|
||||
printf("Sum: %d\n", n);
|
||||
|
||||
return 0;
|
||||
}
|
82
handle.h
82
handle.h
@ -1,33 +1,69 @@
|
||||
#ifndef MAG_HANDLE_H
|
||||
#define MAG_HANDLE_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
#error "handle.h does not work with C++ compilers, you must compile these files with GCC directly!"
|
||||
#endif
|
||||
/* Simple single-header library that makes you have RAII in gcc (also works in C++ compilers) */
|
||||
/* Licence: CC-BY */
|
||||
|
||||
/** Tells your constructor/destructor handler function about the state */
|
||||
enum HANDLE_STATE {
|
||||
HANDLE_CREAT,
|
||||
HANDLE_DESTR,
|
||||
HANDLE_DESTR
|
||||
};
|
||||
|
||||
/**
|
||||
* This creates a "handle". Basically a struct with given constructor and destructor + RAII.
|
||||
*
|
||||
* Usage:
|
||||
#ifndef __cplusplus
|
||||
|
||||
#define creat(TNAME, VNAME, VINIT) \
|
||||
[[gnu::always_inline]] inline void VNAME##_cleanuper(struct TNAME *v) { \
|
||||
TNAME##_lifetime_handler(HANDLE_DESTR, v, NULL); \
|
||||
} \
|
||||
[[gnu::cleanup(VNAME##_cleanuper)]] struct TNAME VNAME; \
|
||||
TNAME##_lifetime_handler(HANDLE_CREAT, & VNAME, VINIT);
|
||||
|
||||
#define handle(TNAME) \
|
||||
static inline void TNAME##_lifetime_handler( \
|
||||
enum HANDLE_STATE state, \
|
||||
struct TNAME *self, \
|
||||
void *data)
|
||||
|
||||
#else /* C++ compilers better be able to compile code written with us */
|
||||
|
||||
#define creat(TNAME, VNAME, VINIT) \
|
||||
TNAME##_raiicpp VNAME(VINIT);
|
||||
|
||||
#define handle(TNAME) \
|
||||
struct TNAME##_raiicpp; \
|
||||
static inline void TNAME##_lifetime_handler( \
|
||||
enum HANDLE_STATE state, \
|
||||
struct TNAME##_raiicpp *self, \
|
||||
void *data); \
|
||||
struct TNAME##_raiicpp : public TNAME { \
|
||||
inline TNAME##_raiicpp(void *data) { \
|
||||
TNAME##_lifetime_handler(HANDLE_CREAT, this, data); \
|
||||
} \
|
||||
inline ~TNAME##_raiicpp() { \
|
||||
TNAME##_lifetime_handler(HANDLE_DESTR, this, nullptr); \
|
||||
} \
|
||||
}; \
|
||||
static inline void TNAME##_lifetime_handler( \
|
||||
enum HANDLE_STATE state, \
|
||||
struct TNAME##_raiicpp *self, \
|
||||
void *data)
|
||||
|
||||
#endif /* __cplusplus */
|
||||
/*
|
||||
* EXAMPLE:
|
||||
*
|
||||
* struct Meaning {
|
||||
* int a;
|
||||
* int b;
|
||||
* };
|
||||
* // You can typedef if you want, change code accordingly!
|
||||
* // You should not typedef this EVER!
|
||||
*
|
||||
* handle(struct Meaning) {
|
||||
* handle(Meaning) {
|
||||
* if(state == HANDLE_CREAT) {
|
||||
* this->a = *(int*) data;
|
||||
* this->b = 2
|
||||
* self->a = *(int*) data;
|
||||
* self->b = 2;
|
||||
* } else {
|
||||
* printf("%d\n", this->a + this->b);
|
||||
* printf("%d\n", self->a + self->b);
|
||||
* }
|
||||
* }
|
||||
*
|
||||
@ -35,23 +71,9 @@ enum HANDLE_STATE {
|
||||
* // Should print 42:
|
||||
* int main() {
|
||||
* int initializer = 40;
|
||||
* creat(struct Data, d, &initializer);
|
||||
* creat(Meaning, d, &initializer);
|
||||
* }
|
||||
*
|
||||
* Rem.: Unrelated trick, but you can typedef struct A A; // both typedef and forward declare
|
||||
*/
|
||||
|
||||
#define creat(TNAME, VNAME, VINIT) \
|
||||
[[gnu::always_inline]] inline void F(TNAME *v) { \
|
||||
TNAME##_lifetime_handler(HANDLE_DESTR, v, NULL); \
|
||||
} \
|
||||
[[gnu::cleanup(F)]] TNAME VNAME; \
|
||||
TNAME##_lifetime_handler(HANDLE_CREAT, VNAME, VINIT);
|
||||
|
||||
#define handle(TNAME) \
|
||||
static void [[gnu::always_inline]] inline TNAME##_lifetime_handler( \
|
||||
HANDLE_STATE state, \
|
||||
TNAME *this, \
|
||||
void *data)
|
||||
|
||||
#endif // MAG_HANDLE_H
|
||||
#endif /* MAG_HANDLE_H */
|
||||
|
@ -1,4 +1,29 @@
|
||||
#include <stdio.h>
|
||||
#include "handle.h"
|
||||
|
||||
int main() {
|
||||
struct Meaning {
|
||||
int a;
|
||||
int b;
|
||||
};
|
||||
|
||||
handle(Meaning) {
|
||||
if(state == HANDLE_CREAT) {
|
||||
// Constructor
|
||||
self->a = *(int*) data;
|
||||
self->b = 2;
|
||||
} else {
|
||||
// Destructor
|
||||
printf("%d\n", self->a + self->b);
|
||||
}
|
||||
}
|
||||
|
||||
// Now you can use a handle in your local scopes
|
||||
// Should print 42:
|
||||
int main() {
|
||||
int initializer = 40;
|
||||
creat(Meaning, d, &initializer);
|
||||
initializer = 20;
|
||||
creat(Meaning, d2, &initializer);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
19
innerf.c
19
innerf.c
@ -1,18 +1,27 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main() {
|
||||
int a = 0;
|
||||
int b = 40;
|
||||
int c = 2;
|
||||
auto int a = 0;
|
||||
int b = 20;
|
||||
int c = 1;
|
||||
int d = 0;
|
||||
|
||||
// See: https://gcc.gnu.org/onlinedocs/gcc-4.3.0/gcc/Nested-Functions.html
|
||||
inline void lf(register int* r) {
|
||||
inline auto void lf(register int *r) {
|
||||
a += *r;
|
||||
int s = a + *r;
|
||||
inline void deepfun() {
|
||||
s *= 2;
|
||||
}
|
||||
|
||||
deepfun();
|
||||
d += s;
|
||||
}; // semicolon(;) not needeed!
|
||||
|
||||
/*
|
||||
// Same as above... but need auto storage class if you pre-declare
|
||||
inline auto void lf(register int* r);
|
||||
// ...
|
||||
inline void lf(register int* r) {
|
||||
a += *r;
|
||||
}; // semicolon(;) not needeed!
|
||||
@ -21,6 +30,6 @@ int main() {
|
||||
lf(&b);
|
||||
lf(&c);
|
||||
|
||||
printf("%d\n", a);
|
||||
printf("%d .. %d\n", a, d);
|
||||
}
|
||||
|
||||
|
24
makefile
24
makefile
@ -8,6 +8,27 @@ gcc:
|
||||
gcc raii_test.c -o raii_test
|
||||
g++ raii_test.cpp -o raii_test_cpp
|
||||
gcc handle_test.c -o handle_test
|
||||
gcc array_handle_test.c -o array_handle_test
|
||||
g++ array_handle_test.c -o array_handle_test_cpp
|
||||
gcc array_test.c -o array_test
|
||||
g++ array_test.c -o array_test_cpp
|
||||
gcc array_handle_gen_test.c -o array_handle_gen_test
|
||||
g++ array_handle_gen_test.c -o array_handle_gen_test_cpp
|
||||
debug:
|
||||
gcc -g macroname.c -o macroname
|
||||
gcc -g count.c -o count
|
||||
gcc -g innerf.c -o innerf
|
||||
# g++ innerf.c -o innerf_cpp # does not work :-(
|
||||
gcc -g construct_destruct.c -o construct_destruct
|
||||
gcc -g raii_test.c -o raii_test
|
||||
g++ -g raii_test.cpp -o raii_test_cpp
|
||||
gcc -g handle_test.c -o handle_test
|
||||
gcc -g array_handle_test.c -o array_handle_test
|
||||
g++ -g array_handle_test.c -o array_handle_test_cpp
|
||||
gcc -g array_test.c -o array_test
|
||||
g++ -g array_test.c -o array_test_cpp
|
||||
gcc -g array_handle_gen_test.c -o array_handle_gen_test
|
||||
g++ -g array_handle_gen_test.c -o array_handle_gen_test_cpp
|
||||
clang:
|
||||
clang macroname.c -o macroname
|
||||
clang count.c -o count
|
||||
@ -16,3 +37,6 @@ clang:
|
||||
clang construct_destruct.c -o construct_destruct
|
||||
# clang raii_test.c -o raii_test # should not work
|
||||
clang++ raii_test.cpp -o raii_test_cpp
|
||||
clang++ array_handle_gen_test.c -o array_handle_gen_test_clangpp
|
||||
expand:
|
||||
gcc -E handle_test.c -o handle_test_expanded.c
|
||||
|
@ -17,7 +17,7 @@ int main() {
|
||||
cs += bytes[i];
|
||||
}
|
||||
|
||||
printf("Checksum of found bytes: %d\n", cs);
|
||||
printf("Checksum of found bytes: %d\n", (int) cs);
|
||||
|
||||
// exit(0); // This does not release resources!
|
||||
return 0;
|
||||
|
Loading…
x
Reference in New Issue
Block a user