Commit d20bc1b8 authored by ymelnyk21's avatar ymelnyk21
Browse files

done?

parent 08d8e9e8
CC = gcc
#SPECIAL_FLAGS = -ggdb -Wall -DDEBUG_ALLOC
SPECIAL_FLAGS = -ggdb -Wall
SPECIAL_FLAGS = -ggdb -Wall -DDEBUG_ALLOC
#SPECIAL_FLAGS = -ggdb -Wall
CFLAGS = -std=gnu99 $(SPECIAL_FLAGS)
all: libpb libbf memtest
......
File added
File added
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <stdbool.h>
int main (int argc, char **argv){
char* x = malloc(24);
char* y = malloc(19);
char* y = malloc(17);
char* z = malloc(32);
printf("x = %p\n", x);
printf("y = %p\n", y);
printf("z = %p\n", z);
printf("\n");
printf("testing whether a new block is allocated only when the size exceeds that of the old one:\n");
x = malloc(32);
printf("x = %p\n",x);
printf("reallocating x with SMALLER size\n");
y = realloc(x,24);
printf("new allocation goes to %p\n",y);
printf("reallocating x with LARGER size\n");
y = realloc(x,44);
printf("new allocation goes to %p\n",y);
printf("\n");
printf("testing whether the contents are copied correctly:\n");
char* str = (char*) malloc(5*sizeof(char));
strcpy(str,"abcd");
printf("allocated 5 bytes for the string: %s\n",str);
printf("reallocating 8 bytes\n");
str = realloc(str,(8*sizeof(char)));
printf("adding another two letter to the string...\n");
str[4]='e';
str[5]='f';
printf("result: %s\n",str);
printf("the original four letters were copied successfully\n");
printf("\n");
printf("testing block alignment\n");
bool aligned = false;
printf("allocating 5 bytes\n");
x=malloc(5);
aligned = ((intptr_t)x % 4 == 0);
printf("aligned = %s\n",aligned ? "true":"false");
printf("allocating 17 bytes\n");
x=malloc(17);
aligned = ((intptr_t)x % 4 == 0);
printf("aligned = %s\n",aligned ? "true":"false");
printf("allocating 4 bytes\n");
x=malloc(4);
aligned = ((intptr_t)x % 4 == 0);
printf("aligned = %s\n",aligned ? "true":"false");
printf("allocating 1 byte\n");
x=malloc(1);
aligned = ((intptr_t)x % 4 == 0);
printf("aligned = %s\n",aligned ? "true":"false");
}
File added
......@@ -125,28 +125,32 @@ void init () {
*/
void* malloc (size_t size) {
init();
init(); //initalize the heap
if (size == 0) {
return NULL;
return NULL; //if block of zero size was requested, return null
}
size_t total_size = size + sizeof(header_s);
header_s* header_ptr = (header_s*)free_addr;
void* block_ptr = (void*)(free_addr + sizeof(header_s));
intptr_t new_free_addr = free_addr + total_size;
size_t total_size = size + sizeof(header_s); //calculate the total size allocated, which includes both header and the requested block
header_s* header_ptr = (header_s*)free_addr; //set the header pointer to the first free address in the heap
void* block_ptr = (void*)(free_addr + sizeof(header_s)); //calculate the block pointer using header size
int align =(4 - (intptr_t)block_ptr %4)%4;
// DEBUG("remainder mod 4, offset:",(intptr_t) block_ptr%4 ,align);
header_ptr = (header_s*)(header_ptr+align);
block_ptr=(void*)(block_ptr+align);
intptr_t new_free_addr = free_addr + align + total_size ; //calculate the new free address to use for subsequent allocations
// DEBUG("allocation size: ",size);
if (new_free_addr > end_addr) {
return NULL;
return NULL; //if this new address exceeds the bounds of the heap, return an error
} else {
free_addr = new_free_addr;
free_addr = new_free_addr; //else amend the free address pointer
}
header_ptr->size = size;
header_ptr->size = size; //populate the size property of the header
return block_ptr;
} // malloc()
......@@ -207,30 +211,31 @@ void* calloc (size_t nmemb, size_t size) {
*
* \param ptr The block to be assigned a new size.
* \param size The new size that the block should assume.
* \return A pointer to the resultant block, which may be `ptr` itself, or
x \return A pointer to the resultant block, which may be `ptr` itself, or
* may be a newly allocated block.
*/
void* realloc (void* ptr, size_t size) {
if (ptr == NULL) {
return malloc(size);
return malloc(size); //if no origin address is given, simply allocate new space
}
if (size == 0) {
free(ptr);
return NULL;
return NULL; //if requested block of size 0, "free" the origin block and return no new address
}
header_s* old_header = (header_s*)((intptr_t)ptr - sizeof(header_s));
size_t old_size = old_header->size;
header_s* old_header = (header_s*)((intptr_t)ptr - sizeof(header_s)); //calculate the location of the original block header
size_t old_size = old_header->size; //and retrieve the size
// DEBUG("realloc detected old size: ",(int) old_size);
if (size <= old_size) {
return ptr;
return ptr; //if original size exceeds the requested size, do nothing
}
void* new_ptr = malloc(size);
void* new_ptr = malloc(size); //else, allocate a new block in the "front" of the heap
if (new_ptr != NULL) {
memcpy(new_ptr, ptr, old_size);
memcpy(new_ptr, ptr, old_size); //if this new allocation does not exceed the boundary, copy the contents of the old block into the new
free(ptr);
}
return new_ptr;
......
File added
File added
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment