Skip to content

Instantly share code, notes, and snippets.

@antonioalmeidab
Created October 13, 2019 17:10
Show Gist options
  • Save antonioalmeidab/353ab8f9821e6c084c97d31b40f38e07 to your computer and use it in GitHub Desktop.
Save antonioalmeidab/353ab8f9821e6c084c97d31b40f38e07 to your computer and use it in GitHub Desktop.
Memory allocation algorithm
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFER_LENGTH 20
void slice(char *dest, char *str, int begin, int num_of_chars);
void handle_request(char* command, short (*array)[]);
void insert_process(char* command, short (*array)[], short process);
int parse_process(char* token);
void request_worst(short (*array)[], long num_of_bytes, int pid);
void request_best(short (*array)[], long num_of_bytes, int pid);
void request_first(short (*array)[], long num_of_bytes, int pid);
void traverse_array(short (*array)[]);
void release(short(*array)[],long tam[],int pid_re);
void compact (short (*array)[]);
int find_hole_start(short (*array)[], int start_index);
int find_hole_end (short (*array)[], int start_index);
void show_status(short (*array)[]);
long total_memory;
int main(int argc, char const *argv[])
{
long number_of_process[4];
long tam[4];
// ./allocator 1048576
const char blank[2] = " ";
if (argc < 2) {
printf("Too few arguments... Exit\n");
exit(1);
}
total_memory = atol(argv[1]);
short array[total_memory];
short (*ptr)[];
ptr = &array;
memset(array, 0, sizeof array);
printf("Allocated %ld bytes on the memory\n", total_memory);
char line[BUFFER_LENGTH];
while (1) {
traverse_array(ptr);
printf("allocator>");
char *command = fgets(line, BUFFER_LENGTH, stdin);
if (command != NULL) printf("command typed = %s", command);
char *token = strtok(command, blank);
if (strncmp(token, "RQ", 2) == 0) {
token = strtok(NULL, blank);
if (strncmp(token, "P", 1)) {
continue;
}
int pid = parse_process(token);
token = strtok(NULL, blank);
if (token == NULL) continue;
long num_of_bytes = atol(token);
tam[pid] = num_of_bytes;
if (token == NULL) continue;
token = strtok(NULL, blank);
if (token == NULL) continue;
char method = *token;
switch (method) {
case 'W':
request_worst(ptr, num_of_bytes, pid);
break;
case 'B':
request_best(ptr, num_of_bytes, pid);
break;
case 'F':
request_first(ptr, num_of_bytes, pid);
break;
default:
printf("%c method is undefined...\n", method);
}
} else if (strncmp(token, "RL", 2) == 0) {
token = strtok(NULL, blank);
if (strncmp(token, "P", 1)) {
continue;
}
int pid_re = parse_process(token);
release(ptr,tam,pid_re);
printf("release sucess \n");
} else if (strncmp(token, "C", 1) == 0) {
printf("Handle compact...\n");
compact(ptr);
} else if (strncmp(token, "STAT",4) == 0) {
printf("Handle status...\n");
show_status(ptr);
} else if (strncmp(token, "X", 1) == 0) {
printf("Exiting...\n");
break;
} else {
printf("Command not found...\n");
}
}
//printf("Address of code now = %p\n", code);
printf("Program normally terminated...\n");
return 0;
}
void release(short(*array)[],long tam[],int pid_re){
int address = 0;
int begin = 0;
while (address < total_memory && address - begin <= tam[pid_re]) {
if ((*array)[address]) {
begin = address + 1;
}
++address;
}
--address;
printf("begin: %ld\n",begin - tam[pid_re]);
printf("address: %ld\n",address -tam[pid_re]);
if ((address -tam[pid_re]) - (begin - tam[pid_re]) == tam[pid_re]) {
printf("process %d released with sucess\n",pid_re);
for (int i = (begin -tam[pid_re]); i < (address - tam[pid_re]); ++i) {
(*array)[i] = (*array)[i] -(pid_re + 1);
}
}
else {
printf("unable to release\n");
}
}
void request_first(short (*array)[], long num_of_bytes, int pid)
{
int address = 0;
int begin = 0;
while (address < total_memory && address - begin <= num_of_bytes) {
if ((*array)[address]) {
begin = address + 1;
}
++address;
}
--address;
if (address - begin == num_of_bytes) {
printf("Able to allocate\n");
for (int i = begin; i < address; ++i) {
(*array)[i] = pid + 1;
}
}
else {
printf("Unable to allocate\n");
}
}
void request_worst(short (*array)[], long num_of_bytes, int pid)
{
int address = 0;
int begin = 0;
while (address < total_memory && begin - address < num_of_bytes) {
if ((*array)[address]) {
begin = address;
}
++address;
}
if (begin - address >= num_of_bytes) {
printf("Able to allocate\n");
}
else {
printf("unable to allocate\n");
}
}
void request_best(short (*array)[], long num_of_bytes, int pid)
{
int address = 0;
int begin = 0;
while (address < total_memory && begin - address < num_of_bytes) {
if ((*array)[address]) {
begin = address;
}
++address;
}
if (begin - address >= num_of_bytes) {
printf("Able to allocate\n");
}
else {
printf("unable to allocate\n");
}
}
void compact (short (*array)[])
{
int destination_hole_end;
int index = 0;
int destination_hole_start = find_hole_start(array, index);
index = destination_hole_start;
while(index < total_memory) {
destination_hole_end = find_hole_end(array, index);
index = destination_hole_end;
if (index == total_memory) break;
int pid = (*array)[index];
while ((*array)[index] == pid) {
(*array)[destination_hole_start] = (*array)[index];
(*array)[index] = 0;
destination_hole_start++;
index++;
}
}
}
int find_hole_start (short (*array)[], int start_index)
{
int hole_start = start_index;
while ((*array)[hole_start] && hole_start < total_memory) hole_start++;
return hole_start;
}
int find_hole_end (short (*array)[], int start_index)
{
int hole_end = start_index;
while((*array)[hole_end] == 0 && hole_end < total_memory) hole_end++;
return hole_end;
}
void show_status(short (*array)[])
{
int index = 0;
int process_start;
int hole_end;
while (index < total_memory) {
printf("Addresses ");
if ((*array)[index] == 0) {
hole_end = find_hole_end(array, index);
printf("[%d:%d] Unused\n", index, hole_end-1);
index = hole_end;
} else {
process_start = index;
while((*array)[index] == (*array)[process_start]) index++;
printf("[%d:%d] Process P%d\n", process_start, index-1, (*array)[process_start] - 1);
}
}
}
int parse_process(char* token)
{
char p[4];
char p1[4];
int i = 1;
while (*(token + i) >= 48 && *(token + i) <= 57) p[i++-1] = *(token + i);
printf("Process passed is %s\n", p);
return atoi(p);
}
void traverse_array(short (*array)[])
{
printf("--------------------------------------------------MEMORY--------------------------------------------------\n");
printf(">>> [");
int i = 0;
for (; i < total_memory - 1; ++i) {
printf("%d, ", (*array)[i]);
}
printf("%d]\n", (*array)[i]);
printf("----------------------------------------------------------------------------------------------------------\n");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment