matrefeytontias wrote:
Hey come on, I don't have the level you're thinking about >_< there are things I can't do, even knowing how. Moreover, I need to get the input working faultlessly (handle backspace, erase the whole line, handles arguments etc) before taking care of the filesystem.
True, true, I can understand that. The filesystem I'm proposing would also require some sort of heap allocator. Although, I should say that your issue of having a good input buffer as well as arguments for your commands would be helped by a memory heap. And fortunately for you, I wrote one (also in C, but you can glean the idea):
Code: // The linked list node structure for keeping track of heap data
// In your case, all of these pointers would be filesyst_pointer structures, NOT actual * pointers.
struct my_heapNode{
unsigned int size;
void* position;
struct my_heapNode* next;
};
typedef unsigned char bool;
#define false 0
#define true 1
// Initializes the memory allocator
void init_my_malloc(int bufferSize);
// Deinitializes the memory allocator
void deinit_my_malloc();
// Allocate size bytes of data, returns pointer to start of data
void* my_malloc(unsigned int size);
// NOT FOR USER CODE: Determines if a heapTreeNode contains enough data
bool isBigEnough(struct my_heapNode* node, unsigned int size);
// NOT FOR USER CODE: Go to the next node in the tree
void shiftToNextNode();
// Frees the pointer back to the memory allocator
void my_free(void* item);
// You would want to create two copies of these variables for the RAM and ARCHIVE sections - one for quick access, the other for the permanent filesystem.
static struct my_heapNode my_heapFront;
static struct my_heapNode* my_currentNode;
// You wouldn't need the next two variables in your case - these were just to make sure it worked
static void* memArea;
static int bufferSize;
void init_my_malloc(int bufferSize)
{
/* In your case, this would only be called when you reset the OS. If RAM was reset,
* you would allocate one RAM node for each page of RAM you have access to,
* in a static portion of RAM you know you have control over.
* If you reset ARCHIVE, you would reset RAM (creating new RAM areas)
* and then use that to create ARCHIVE nodes for each page you have access to.
*/
memArea = malloc(bufferSize);
my_heapFront.size = bufferSize;
my_heapFront.position = memArea;
my_currentNode = &my_heapFront;
printf("Buffer initialized.\n");
}
void deinit_my_malloc()
{
// I kept this here for general purposes, but you'd never call this.
free(memArea);
printf("Buffer freed.\n");
}
void* my_malloc(unsigned int size)
{
unsigned int trueSize = size + sizeof(unsigned int);
struct my_heapNode* start = my_currentNode;
void* pointer = NULL;
do
{
if (isBigEnough(my_currentNode, trueSize))
{
my_currentNode->size -= trueSize;
pointer = my_currentNode->position + my_currentNode->size;
(*(unsigned int*)(pointer)) = size;
if (my_currentNode->size == 0 && my_currentNode != &my_heapFront)
{
struct my_heapNode* deadNode = my_currentNode;
while(my_currentNode->next != deadNode)
shiftToNextNode();
my_currentNode->next = my_currentNode->next->next;
my_free(deadNode);
}
return pointer + sizeof(unsigned int);
}
else
{
shiftToNextNode();
}
} while (start != my_currentNode);
return NULL; // not enough memory, so abort
}
bool isBigEnough(struct my_heapNode* node, unsigned int size)
{
return (node != NULL && node->size >= size);
}
void shiftToNextNode()
{
my_currentNode = my_currentNode->next;
if (my_currentNode == NULL)
my_currentNode = &my_heapFront;
}
void my_free(void* item)
{
void* truePosition = item - sizeof(unsigned int);
unsigned int itemSize = *(unsigned int*)(truePosition);
unsigned int trueSize = itemSize + sizeof(unsigned int);
struct my_heapNode* start = my_currentNode;
do
{
if (my_currentNode->position + my_currentNode->size == truePosition)
{
my_currentNode->size += trueSize;
return;
}
else if (truePosition + trueSize == my_currentNode->position)
{
my_currentNode->position -= trueSize;
my_currentNode->size += trueSize;
return;
}
else
{
shiftToNextNode();
}
} while (start != my_currentNode);
// No adjacent memory areas, so create a new node
struct my_heapNode* newNode = my_malloc(sizeof(struct my_heapNode));
newNode->next = my_currentNode->next;
my_currentNode->next = newNode;
}