Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include "part1.h"
- #include <stdbool.h>
- Node * STARTOFHEAP;
- char * endofheap;
- void initialise(void *memory, size_t size){
- STARTOFHEAP = (Node *) memory;
- STARTOFHEAP->free = true;
- STARTOFHEAP->blockSize = size - sizeof(Node);
- STARTOFHEAP->previous = NULL;
- STARTOFHEAP->next = NULL;
- }
- void *allocate(size_t bytes){
- Node * allocatedNode = (Node *) STARTOFHEAP;
- Node * freeNode;
- while(allocatedNode != NULL) {
- if ((allocatedNode->free == true) && (allocatedNode->blockSize >= bytes)) {
- char *tempHead = (char *) allocatedNode;
- int sizeLeft = allocatedNode->blockSize - (sizeof(Node) + bytes);
- freeNode = (Node *) (tempHead + sizeof(Node) + bytes); //create a new node at the address location that is =
- freeNode->free = true; //the start of the head + size of a node + requested bytes
- if(sizeLeft <= sizeof(Node) + 1){
- freeNode->blockSize = allocatedNode->blockSize;
- } else {
- freeNode->blockSize = allocatedNode->blockSize - bytes;
- }
- if(allocatedNode->next != NULL) {
- allocatedNode->next->previous = freeNode; //go into allocated node next and set the next nodes previous to the free node
- freeNode->next = allocatedNode->next; //set the freenodes next to the allocated nodes next
- allocatedNode->next = freeNode; //set the allocatednodes next to the free node
- freeNode->previous = allocatedNode;
- } else{
- allocatedNode->next = freeNode;
- freeNode->previous = allocatedNode;
- freeNode->next = NULL;
- }
- // freeNode->next = NULL;
- // freeNode->previous = allocatedNode;
- allocatedNode->next = freeNode;
- allocatedNode->free = false;
- printf("next Free Node address: %d\n", freeNode);
- return freeNode->previous + 1;
- }
- allocatedNode = allocatedNode->next;
- }
- return (NULL);
- }
- int main(int argc, const char* argv[]) {
- size_t heapSize = 200;
- STARTOFHEAP = (Node *) malloc(heapSize);
- printf("Size of requested heap: %d\n", heapSize);
- printf("Size of node: %d\n", sizeof(Node));
- initialise(STARTOFHEAP, heapSize);
- endofheap = (char *) STARTOFHEAP;
- printf("Start of heap address: %d\n", STARTOFHEAP);
- printf("End of heap address: %d\n", endofheap = (endofheap + heapSize));
- printf("Size of struct Node: %d\n\n", sizeof(Node));
- printf("Node 1: \n");
- Node *nodeOne = (Node *) allocate(18);
- printf("Memory Address location returned to no#include <stdio.h>
- #include <stdlib.h>
- #include "part1.h"
- #include <stdbool.h>
- Node * STARTOFHEAP;
- char * endofheap;
- void initialise(void *memory, size_t size){
- STARTOFHEAP = (Node *) memory;
- STARTOFHEAP->free = true;
- STARTOFHEAP->blockSize = size - sizeof(Node);
- STARTOFHEAP->previous = NULL;
- STARTOFHEAP->next = NULL;
- }
- void *allocate(size_t bytes){
- Node * allocatedNode = (Node *) STARTOFHEAP;
- Node * freeNode;
- while(allocatedNode != NULL) {
- if ((allocatedNode->free == true) && (allocatedNode->blockSize >= bytes)) {
- char *tempHead = (char *) allocatedNode;
- int sizeLeft = allocatedNode->blockSize - (sizeof(Node) + bytes);
- freeNode = (Node *) (tempHead + sizeof(Node) + bytes); //create a new node at the address location that is =
- freeNode->free = true; //the start of the head + size of a node + requested bytes
- if(sizeLeft <= sizeof(Node) + 1){
- freeNode->blockSize = allocatedNode->blockSize;
- } else {
- freeNode->blockSize = allocatedNode->blockSize - bytes;
- }
- if(allocatedNode->next != NULL) {
- allocatedNode->next->previous = freeNode; //go into allocated node next and set the next nodes previous to the free node
- freeNode->next = allocatedNode->next; //set the freenodes next to the allocated nodes next
- allocatedNode->next = freeNode; //set the allocatednodes next to the free node
- freeNode->previous = allocatedNode;
- } else{
- allocatedNode->next = freeNode;
- freeNode->previous = allocatedNode;
- freeNode->next = NULL;
- }
- // freeNode->next = NULL;
- // freeNode->previous = allocatedNode;
- allocatedNode->next = freeNode;
- allocatedNode->free = false;
- printf("next Free Node address: %d\n", freeNode);
- return freeNode->previous + 1;
- }
- allocatedNode = allocatedNode->next;
- }
- return (NULL);
- }
- int main(int argc, const char* argv[]) {
- size_t heapSize = 200;
- STARTOFHEAP = (Node *) malloc(heapSize);
- printf("Size of requested heap: %d\n", heapSize);
- printf("Size of node: %d\n", sizeof(Node));
- initialise(STARTOFHEAP, heapSize);
- endofheap = (char *) STARTOFHEAP;
- printf("Start of heap address: %d\n", STARTOFHEAP);
- printf("End of heap address: %d\n", endofheap = (endofheap + heapSize));
- printf("Size of struct Node: %d\n\n", sizeof(Node));
- printf("Node 1: \n");
- Node *nodeOne = (Node *) allocate(18);
- printf("Memory Address location returned to node 1: %d\n", nodeOne);
- printf("Node Address location returned to node 1: %d\n\n", nodeOne - 1);
- printf("Node 2: \n");
- Node *nodeTwo = (Node *) allocate(18);
- printf("Address returned to node 2: %d\n", nodeTwo);
- printf("Node Address location returned to node 2: %d\n\n", nodeTwo - 1);
- printf("Node 3: \n");
- Node *nodeThree = (Node *) allocate(68);
- printf("Address returned to node 3: %d\n", nodeThree);
- printf("Node Address location returned to node 3: %d\n\n", nodeThree - 1);
- printf("Node 4: \n");
- Node *nodeFour = (Node *) allocate(18
- );
- printf("Address returned to node 4: %d\n", nodeFour);
- printf("Node Address location returned to node 4: %d\n\n", nodeFour - 1);
- return 0;
- }
- de 1: %d\n", nodeOne);
- printf("Node Address location returned to node 1: %d\n\n", nodeOne - 1);
- printf("Node 2: \n");
- Node *nodeTwo = (Node *) allocate(18);
- printf("Address returned to node 2: %d\n", nodeTwo);
- printf("Node Address location returned to node 2: %d\n\n", nodeTwo - 1);
- printf("Node 3: \n");
- Node *nodeThree = (Node *) allocate(68);
- printf("Address returned to node 3: %d\n", nodeThree);
- printf("Node Address location returned to node 3: %d\n\n", nodeThree - 1);
- printf("Node 4: \n");
- Node *nodeFour = (Node *) allocate(18
- );
- printf("Address returned to node 4: %d\n", nodeFour);
- printf("Node Address location returned to node 4: %d\n\n", nodeFour - 1);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement