Skip to content

Commit 6ead001

Browse files
committed
Updates
1 parent 1653638 commit 6ead001

27 files changed

+164
-163
lines changed

Assignment-2/a

-28.7 KB
Binary file not shown.

Readme.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This repo contains various Operating System Modules such as a shell, process scheduler, memory management module, reader writer problem and filesearch. These are all made for Unix systems.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line numberDiff line numberDiff line change
@@ -1,123 +1,123 @@
1-
#include <unistd.h>
2-
#include <errno.h>
3-
#include <stdio.h>
4-
#include <stdlib.h>
5-
#include <sys/mman.h>
6-
7-
8-
9-
//Initialize freelist
10-
struct header{
11-
size_t size;
12-
struct header* next;
13-
};
14-
static struct header* freelist=NULL;
15-
void init(){
16-
if(freelist==NULL){
17-
freelist=mmap(NULL,4096,PROT_READ|PROT_WRITE,MAP_PRIVATE|MAP_ANONYMOUS,-1,0);
18-
freelist->size=4096-sizeof(struct header);
19-
freelist->next=NULL;
20-
}
21-
}
22-
//Function to show state of freelist
23-
void info(){
24-
struct header* current = freelist;
25-
int blocknumber = 1;
26-
27-
fprintf(stderr,"------------------------------------\n");
28-
fprintf(stderr,"Free List Contents:\n");
29-
while (current) {
30-
fprintf(stderr,"Block %d: Address=%p, Size=%zu\n", blocknumber, current, current->size);
31-
current = current->next;
32-
blocknumber++;
33-
}
34-
fprintf(stderr,"------------------------------------\n");
35-
}
36-
37-
38-
39-
//Function to check int overflow on multiplication
40-
int checkoverflow(int m, int n){
41-
if(m==0)return 0;
42-
int p=m*n;
43-
if(p/n==m)return 0;
44-
else return 1;
45-
}
46-
47-
48-
49-
// Own implemetnation of malloc
50-
void* my_malloc(size_t size) {
51-
52-
if(size==0)return NULL;
53-
init();
54-
55-
struct header* prev = NULL;
56-
struct header* current = freelist;
57-
58-
// First Fit
59-
while (current) {
60-
if (current->size >= size + sizeof(struct header)) {
61-
if (current->size > size + sizeof(struct header)) {
62-
63-
struct header* new_block = (struct header*)((char*)current + size + sizeof(struct header));
64-
new_block->size = current->size - size - sizeof(struct header);
65-
new_block->next = current->next;
66-
current->size = size;
67-
current->next = (struct header*)123456; //magic
68-
if (prev)
69-
prev->next = new_block;
70-
else
71-
freelist = new_block;
72-
return (void*)(current + 1);
73-
}
74-
else{
75-
if (prev)
76-
prev->next = current->next;
77-
else
78-
freelist = current->next;
79-
current->next = (struct header*)123456; //magic
80-
current->size = size;
81-
return (void*)(current + 1);
82-
}
83-
}
84-
prev = current;
85-
current = current->next;
86-
}
87-
88-
// If no suitable block is found, request more memory.
89-
struct header* new_block = sbrk(size+sizeof(struct header));
90-
if (new_block == (void*)-1) {
91-
errno = ENOMEM;
92-
return NULL; // Out of memory
93-
}
94-
new_block->size = size;
95-
new_block->next = (struct header*)123456; //magic
96-
return (void*)(new_block + 1);
97-
}
98-
99-
// Own implementation of calloc
100-
void* my_calloc(size_t nelem, size_t size) {
101-
102-
if(checkoverflow(nelem,size)==1){
103-
errno=EOVERFLOW;
104-
return NULL;
105-
}
106-
size_t finalsize = nelem * size;
107-
void* ptr = my_malloc(finalsize);
108-
if(ptr==NULL)return NULL;
109-
memset(ptr, 0, finalsize);
110-
return ptr;
111-
112-
}
113-
114-
// Own implementation of free
115-
void my_free(void* ptr) {
116-
117-
if(ptr==NULL)return;
118-
struct header* block = (struct header*)ptr - 1;
119-
if(block->next!=(struct header*)123456) return;
120-
block->next = freelist;
121-
freelist = block;
122-
1+
#include <unistd.h>
2+
#include <errno.h>
3+
#include <stdio.h>
4+
#include <stdlib.h>
5+
#include <sys/mman.h>
6+
7+
8+
9+
//Initialize freelist
10+
struct header{
11+
size_t size;
12+
struct header* next;
13+
};
14+
static struct header* freelist=NULL;
15+
void init(){
16+
if(freelist==NULL){
17+
freelist=mmap(NULL,4096,PROT_READ|PROT_WRITE,MAP_PRIVATE|MAP_ANONYMOUS,-1,0);
18+
freelist->size=4096-sizeof(struct header);
19+
freelist->next=NULL;
20+
}
21+
}
22+
//Function to show state of freelist
23+
void info(){
24+
struct header* current = freelist;
25+
int blocknumber = 1;
26+
27+
fprintf(stderr,"------------------------------------\n");
28+
fprintf(stderr,"Free List Contents:\n");
29+
while (current) {
30+
fprintf(stderr,"Block %d: Address=%p, Size=%zu\n", blocknumber, current, current->size);
31+
current = current->next;
32+
blocknumber++;
33+
}
34+
fprintf(stderr,"------------------------------------\n");
35+
}
36+
37+
38+
39+
//Function to check int overflow on multiplication
40+
int checkoverflow(int m, int n){
41+
if(m==0)return 0;
42+
int p=m*n;
43+
if(p/n==m)return 0;
44+
else return 1;
45+
}
46+
47+
48+
49+
// Own implemetnation of malloc
50+
void* my_malloc(size_t size) {
51+
52+
if(size==0)return NULL;
53+
init();
54+
55+
struct header* prev = NULL;
56+
struct header* current = freelist;
57+
58+
// First Fit
59+
while (current) {
60+
if (current->size >= size + sizeof(struct header)) {
61+
if (current->size > size + sizeof(struct header)) {
62+
63+
struct header* new_block = (struct header*)((char*)current + size + sizeof(struct header));
64+
new_block->size = current->size - size - sizeof(struct header);
65+
new_block->next = current->next;
66+
current->size = size;
67+
current->next = (struct header*)123456; //magic
68+
if (prev)
69+
prev->next = new_block;
70+
else
71+
freelist = new_block;
72+
return (void*)(current + 1);
73+
}
74+
else{
75+
if (prev)
76+
prev->next = current->next;
77+
else
78+
freelist = current->next;
79+
current->next = (struct header*)123456; //magic
80+
current->size = size;
81+
return (void*)(current + 1);
82+
}
83+
}
84+
prev = current;
85+
current = current->next;
86+
}
87+
88+
// If no suitable block is found, request more memory.
89+
struct header* new_block = sbrk(size+sizeof(struct header));
90+
if (new_block == (void*)-1) {
91+
errno = ENOMEM;
92+
return NULL; // Out of memory
93+
}
94+
new_block->size = size;
95+
new_block->next = (struct header*)123456; //magic
96+
return (void*)(new_block + 1);
97+
}
98+
99+
// Own implementation of calloc
100+
void* my_calloc(size_t nelem, size_t size) {
101+
102+
if(checkoverflow(nelem,size)==1){
103+
errno=EOVERFLOW;
104+
return NULL;
105+
}
106+
size_t finalsize = nelem * size;
107+
void* ptr = my_malloc(finalsize);
108+
if(ptr==NULL)return NULL;
109+
memset(ptr, 0, finalsize);
110+
return ptr;
111+
112+
}
113+
114+
// Own implementation of free
115+
void my_free(void* ptr) {
116+
117+
if(ptr==NULL)return;
118+
struct header* block = (struct header*)ptr - 1;
119+
if(block->next!=(struct header*)123456) return;
120+
block->next = freelist;
121+
freelist = block;
122+
123123
}
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,42 @@
1-
#include <stdio.h>
2-
#include <stdlib.h>
3-
#include <sys/mman.h>
4-
// Include your Headers below
5-
#include <unistd.h>
6-
7-
8-
// You are not allowed to use the function malloc and calloc directly .
9-
10-
struct header{
11-
size_t size;
12-
};
13-
14-
// Function to allocate memory using mmap
15-
void* my_malloc(size_t size) {
16-
// Your implementation of my_malloc goes here
17-
if(size==0)return NULL;
18-
struct header* ptr=mmap(NULL,size+sizeof(struct header),PROT_READ|PROT_WRITE,MAP_PRIVATE|MAP_ANONYMOUS,-1,0);
19-
if(ptr==NULL)return NULL;
20-
ptr->size=size;
21-
return (void*)(ptr+1);
22-
}
23-
24-
// Function to allocate and initialize memory to zero using mmap
25-
void* my_calloc(size_t nelem, size_t size) {
26-
// Your implementation of my_calloc goes here
27-
void* ptr = my_malloc(nelem*size);
28-
if(ptr==NULL)return NULL;
29-
memset(ptr,0,nelem*size);
30-
return (void*)(ptr);
31-
}
32-
33-
// Function to release memory allocated using my_malloc and my_calloc
34-
void my_free(void* ptr) {
35-
// Your implementation of my_free goes here
36-
if(ptr==NULL)return;
37-
munmap((struct header*)ptr-1,((struct header*)ptr-1)->size+sizeof(struct header));
38-
}
39-
40-
void info(){
41-
printf("This is a custom memory allocator\n");
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <sys/mman.h>
4+
// Include your Headers below
5+
#include <unistd.h>
6+
7+
8+
// You are not allowed to use the function malloc and calloc directly .
9+
10+
struct header{
11+
size_t size;
12+
};
13+
14+
// Function to allocate memory using mmap
15+
void* my_malloc(size_t size) {
16+
// Your implementation of my_malloc goes here
17+
if(size==0)return NULL;
18+
struct header* ptr=mmap(NULL,size+sizeof(struct header),PROT_READ|PROT_WRITE,MAP_PRIVATE|MAP_ANONYMOUS,-1,0);
19+
if(ptr==NULL)return NULL;
20+
ptr->size=size;
21+
return (void*)(ptr+1);
22+
}
23+
24+
// Function to allocate and initialize memory to zero using mmap
25+
void* my_calloc(size_t nelem, size_t size) {
26+
// Your implementation of my_calloc goes here
27+
void* ptr = my_malloc(nelem*size);
28+
if(ptr==NULL)return NULL;
29+
memset(ptr,0,nelem*size);
30+
return (void*)(ptr);
31+
}
32+
33+
// Function to release memory allocated using my_malloc and my_calloc
34+
void my_free(void* ptr) {
35+
// Your implementation of my_free goes here
36+
if(ptr==NULL)return;
37+
munmap((struct header*)ptr-1,((struct header*)ptr-1)->size+sizeof(struct header));
38+
}
39+
40+
void info(){
41+
printf("This is a custom memory allocator\n");
4242
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)