diff --git a/include/memstack.h b/include/memstack.h index a277fe1..f0190fe 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 @@ -23,7 +22,7 @@ // 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 +30,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; @@ -44,7 +43,9 @@ 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 -#endif +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 fd2d271..6ac0b32 100644 --- a/src/memstack.c +++ b/src/memstack.c @@ -6,6 +6,9 @@ #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 @@ -213,23 +216,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 msfprint(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); - } + + msfprint(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 */ @@ -240,11 +242,21 @@ 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); } } +// 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.