Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patch 1 #11

Merged
merged 5 commits into from
Jun 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions include/memstack.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@

// Header file

#ifndef MC_MEMSTACK_H
#define MC_MEMSTACK_H

#include <stdlib.h>
#include <stdio.h>

#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
Expand All @@ -23,15 +22,15 @@
// 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
} memstack_chain_ptr;

// 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;
Expand All @@ -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_ */
34 changes: 23 additions & 11 deletions src/memstack.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

#include <memstack.h>

#include <stdlib.h>
#include <stdio.h>

// 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
Expand Down Expand Up @@ -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 */
Expand All @@ -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.
Expand Down