From 0014338f03c91802b19e198b0f25d824b41f6f33 Mon Sep 17 00:00:00 2001 From: mauro-balades Date: Fri, 18 Nov 2022 11:18:33 +0100 Subject: [PATCH 1/5] improved the header file --- examples/msrollback.c | 1 + include/memstack.h | 17 +++++++---------- src/memstack.c | 4 ++++ 3 files changed, 12 insertions(+), 10 deletions(-) diff --git a/examples/msrollback.c b/examples/msrollback.c index e10536b..88afaa8 100644 --- a/examples/msrollback.c +++ b/examples/msrollback.c @@ -1,5 +1,6 @@ #include #include +#include int main(void) { printf("Running example \"msrollback\"!\n"); diff --git a/include/memstack.h b/include/memstack.h index a277fe1..9b9369b 100644 --- a/include/memstack.h +++ b/include/memstack.h @@ -4,12 +4,11 @@ // Header file -#ifndef MC_MEMSTACK_H -#define MC_MEMSTACK_H - -#include #include +#ifndef __MC_MEMSTACK_H_ +#define __MC_MEMSTACK_H_ + // C macro to improve readability when using the library. // Example: // Before: msfree(NULL); // Does not explicitly show global is being freed @@ -17,13 +16,10 @@ // It isn't necessary to use this however, and using NULL will also work. #define GLOBAL_MEMSTACK ((memstack*)NULL) -#define TRUE (1==1) -#define FALSE (!TRUE) - // Linked list node. // Stores a ptr to the memory allocated and the next element in the linked list. // If *next* is NULL then that node is the last node in the linked list. -typedef struct memstack_chain_ptr_t{ +typedef struct memstack_chain_ptr_t { void* ptr; // Pointer to the memory allocated struct memstack_chain_ptr_t* next; // Pointer to next node struct memstack_chain_ptr_t* previous; // Pointer to previous node @@ -31,7 +27,7 @@ typedef struct memstack_chain_ptr_t{ // Memstack structure. // Stores the linked list for all of our allocations. -typedef struct memstack_t { +typedef struct { memstack_chain_ptr* last; // Stores last element memstack_chain_ptr* first; // Stores first element int length; @@ -47,4 +43,5 @@ extern void* mspop(memstack* storage); // Removes the last node from the memsta extern void msprint(memstack* storage); // Displays all the nodes in the memstack for debugging uses extern void msrollback(memstack* storage, int rollback_count, int destructive); // Removes a number of nodes from a memstack extern void msdealloc(memstack* storage, void* ptr); // Deallocate a piece of memory and remove it from the stack -#endif + +#endif /* __MC_MEMSTACK_H_ */ diff --git a/src/memstack.c b/src/memstack.c index fd2d271..6c42439 100644 --- a/src/memstack.c +++ b/src/memstack.c @@ -6,6 +6,10 @@ #include +#include +#include +#include + // Global memstack can be accessed across the whole of a code base, but is hidden. // The global memstack cannot be accessed directly by the user, and instead they must // use NULL or GLOBAL_MEMSTACK to apply functions such as msalloc and msfree to From 1047253eee03f19c3ae1b4869f47178832d7a554 Mon Sep 17 00:00:00 2001 From: mauro-balades Date: Fri, 18 Nov 2022 11:33:34 +0100 Subject: [PATCH 2/5] msprint can now print to any stream --- .vscode/settings.json | 5 +++++ include/memstack.h | 16 +++++++++++++++- src/memstack.c | 22 +++++++++++----------- 3 files changed, 31 insertions(+), 12 deletions(-) create mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..1428c38 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.associations": { + "random": "cpp" + } +} \ No newline at end of file diff --git a/include/memstack.h b/include/memstack.h index 9b9369b..1da931b 100644 --- a/include/memstack.h +++ b/include/memstack.h @@ -40,8 +40,22 @@ extern void msfree(memstack* storage); // Free all of memstack extern void msclear(memstack* storage); // Frees allocations but doesn't delete the memstack extern void mspush(memstack* storage, void* ptr); // Adds new memory into the memstack even if it hasn't been allocated with msalloc() extern void* mspop(memstack* storage); // Removes the last node from the memstack and returns the user allocated memory -extern void msprint(memstack* storage); // Displays all the nodes in the memstack for debugging uses extern void msrollback(memstack* storage, int rollback_count, int destructive); // Removes a number of nodes from a memstack extern void msdealloc(memstack* storage, void* ptr); // Deallocate a piece of memory and remove it from the stack +extern void _msprint(memstack* storage, FILE* stream); // Displays all the nodes in the memstack for debugging uses + +#define __MS_MSPRINT_C1(a) _msprint(a, stdout); +#define __MS_MSPRINT_C2(a,b) _msprint(a, b); + +#define _ARG2(_0, _1, _2, ...) _2 +#define NARG2(...) _ARG2(__VA_ARGS__, 2, 1, 0) + +#define __MS_ONE_OR_TWO_ARGS_1(a) __MS_MSPRINT_C1(a) +#define __MS_ONE_OR_TWO_ARGS_2(a, b) __MS_MSPRINT_C2(a,b) + +#define __MS_ONE_OR_TWO_ARGS__(N, ...) __MS_ONE_OR_TWO_ARGS_ ## N (__VA_ARGS__) +#define __MS_ONE_OR_TWO_ARGS_(N, ...) __MS_ONE_OR_TWO_ARGS__(N, __VA_ARGS__) + +#define msprint(...) __MS_ONE_OR_TWO_ARGS_(NARG2(__VA_ARGS__), __VA_ARGS__) #endif /* __MC_MEMSTACK_H_ */ diff --git a/src/memstack.c b/src/memstack.c index 6c42439..5905243 100644 --- a/src/memstack.c +++ b/src/memstack.c @@ -217,23 +217,22 @@ void* mspop(memstack* storage) { // This is useful for both maintainers, and users of memstack. // Originally created to help debug, I've made it available to all library users. // For each node, it shows the previous, next, and current memstack_chain_ptr -void msprint(memstack* storage) { +void _msprint(memstack* storage, FILE* stream) { // To handle global memstack if (storage == GLOBAL_MEMSTACK) { - if (global != NULL) { - msprint(global); - } else { + if (global == NULL) msinit(); // Allocate for the user if needs be - msprint(global); - } + + msprint(global, stream); } else { memstack_chain_ptr* current_node = storage->first; int index = 0; - printf("Printing memstack: \n"); + fprintf(stream, "Printing memstack: \n"); while (current_node != NULL) { - printf( - " [%d] %p <--previous-- [%d] Current: %p --next--> [%d] %p\n", + fprintf( + stream, + " [%d] %p <--previous-- [%d] Current: %p --next--> [%d] %p\n", index-1, /* Previous node index */ current_node->previous, /* Previous node pointer */ index, /* Current node index */ @@ -244,8 +243,9 @@ void msprint(memstack* storage) { current_node = current_node->next; ++index; } - printf(" [INFO] Node count: %d\n", index); - printf(" [INFO] Nodes are linked to both the next, and previous node\n"); + fprintf(stream, " [INFO] Node count: %d\n", index); + fprintf(stream, " [INFO] Nodes are linked to both the next, and previous node\n"); + fflush(stream); } } From db0362124515abb12e88832638bb57e7e79dc289 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mauro=20Balad=C3=A9s?= Date: Fri, 18 Nov 2022 11:38:44 +0100 Subject: [PATCH 3/5] removed vscode setings --- .vscode/settings.json | 5 ----- 1 file changed, 5 deletions(-) delete mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index 1428c38..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "files.associations": { - "random": "cpp" - } -} \ No newline at end of file From 85afc82f0ec452cce946ca0588c7de82912b3116 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mauro=20Balad=C3=A9s?= Date: Mon, 12 Jun 2023 19:15:41 +0000 Subject: [PATCH 4/5] removed stdbool and fixed macro hell --- examples/msrollback.c | 4 ++-- include/memstack.h | 17 ++--------------- src/memstack.c | 14 +++++++++++--- 3 files changed, 15 insertions(+), 20 deletions(-) diff --git a/examples/msrollback.c b/examples/msrollback.c index 88afaa8..e6d771f 100644 --- a/examples/msrollback.c +++ b/examples/msrollback.c @@ -18,8 +18,8 @@ int main(void) { // msrollback can be used to remove, and destroy, multiple allocations from the memstack. // Below, we destroy "c" and "d", and remove "a" and "b". - msrollback(example_memstack, 2, TRUE); // Destroy last two nodes - msrollback(example_memstack, 2, FALSE); // Remove last two nodes + msrollback(example_memstack, 2, true); // Destroy last two nodes + msrollback(example_memstack, 2, false); // Remove last two nodes // We have to handle "a", and "b", ourself. free(a); diff --git a/include/memstack.h b/include/memstack.h index 1da931b..7b8c068 100644 --- a/include/memstack.h +++ b/include/memstack.h @@ -42,20 +42,7 @@ extern void mspush(memstack* storage, void* ptr); // Adds new memory into the m extern void* mspop(memstack* storage); // Removes the last node from the memstack and returns the user allocated memory extern void msrollback(memstack* storage, int rollback_count, int destructive); // Removes a number of nodes from a memstack extern void msdealloc(memstack* storage, void* ptr); // Deallocate a piece of memory and remove it from the stack -extern void _msprint(memstack* storage, FILE* stream); // Displays all the nodes in the memstack for debugging uses - -#define __MS_MSPRINT_C1(a) _msprint(a, stdout); -#define __MS_MSPRINT_C2(a,b) _msprint(a, b); - -#define _ARG2(_0, _1, _2, ...) _2 -#define NARG2(...) _ARG2(__VA_ARGS__, 2, 1, 0) - -#define __MS_ONE_OR_TWO_ARGS_1(a) __MS_MSPRINT_C1(a) -#define __MS_ONE_OR_TWO_ARGS_2(a, b) __MS_MSPRINT_C2(a,b) - -#define __MS_ONE_OR_TWO_ARGS__(N, ...) __MS_ONE_OR_TWO_ARGS_ ## N (__VA_ARGS__) -#define __MS_ONE_OR_TWO_ARGS_(N, ...) __MS_ONE_OR_TWO_ARGS__(N, __VA_ARGS__) - -#define msprint(...) __MS_ONE_OR_TWO_ARGS_(NARG2(__VA_ARGS__), __VA_ARGS__) +extern void msfprint(memstack* storage, FILE* stream); // Displays all the nodes in the memstack for debugging uses +extern void msprint(memstack* storage); // Displays all the nodes in the memstack for debugging uses #endif /* __MC_MEMSTACK_H_ */ diff --git a/src/memstack.c b/src/memstack.c index 5905243..6ac0b32 100644 --- a/src/memstack.c +++ b/src/memstack.c @@ -8,7 +8,6 @@ #include #include -#include // Global memstack can be accessed across the whole of a code base, but is hidden. // The global memstack cannot be accessed directly by the user, and instead they must @@ -217,14 +216,14 @@ void* mspop(memstack* storage) { // This is useful for both maintainers, and users of memstack. // Originally created to help debug, I've made it available to all library users. // For each node, it shows the previous, next, and current memstack_chain_ptr -void _msprint(memstack* storage, FILE* stream) { +void msfprint(memstack* storage, FILE* stream) { // To handle global memstack if (storage == GLOBAL_MEMSTACK) { if (global == NULL) msinit(); // Allocate for the user if needs be - msprint(global, stream); + msfprint(global, stream); } else { memstack_chain_ptr* current_node = storage->first; int index = 0; @@ -249,6 +248,15 @@ void _msprint(memstack* storage, FILE* stream) { } } +// msprint loops through the linked list and shows some debug information. +// This is useful for both maintainers, and users of memstack. +// Originally created to help debug, I've made it available to all library users. +// For each node, it shows the previous, next, and current memstack_chain_ptr +extern void msprint(memstack* storage) { + msfprint(storage, stdout); +} + + // msrollback rolls back a memstack by removing a number of memstack_chain_ptr* nodes. // The number of nodes removed is described by rollback_count. // Nodes are removed from the last node added, to the first node added. From eb2bc2f11e3bd946504eccfcbb5ed47ac6a29616 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mauro=20Balad=C3=A9s?= Date: Mon, 12 Jun 2023 19:39:46 +0000 Subject: [PATCH 5/5] Removed usage of the stdbool.h --- examples/msrollback.c | 5 ++--- include/memstack.h | 3 +++ 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/examples/msrollback.c b/examples/msrollback.c index e6d771f..e10536b 100644 --- a/examples/msrollback.c +++ b/examples/msrollback.c @@ -1,6 +1,5 @@ #include #include -#include int main(void) { printf("Running example \"msrollback\"!\n"); @@ -18,8 +17,8 @@ int main(void) { // msrollback can be used to remove, and destroy, multiple allocations from the memstack. // Below, we destroy "c" and "d", and remove "a" and "b". - msrollback(example_memstack, 2, true); // Destroy last two nodes - msrollback(example_memstack, 2, false); // Remove last two nodes + msrollback(example_memstack, 2, TRUE); // Destroy last two nodes + msrollback(example_memstack, 2, FALSE); // Remove last two nodes // We have to handle "a", and "b", ourself. free(a); diff --git a/include/memstack.h b/include/memstack.h index 7b8c068..f0190fe 100644 --- a/include/memstack.h +++ b/include/memstack.h @@ -16,6 +16,9 @@ // It isn't necessary to use this however, and using NULL will also work. #define GLOBAL_MEMSTACK ((memstack*)NULL) +#define TRUE (1==1) +#define FALSE (!TRUE) + // Linked list node. // Stores a ptr to the memory allocated and the next element in the linked list. // If *next* is NULL then that node is the last node in the linked list.