This commit is contained in:
Tien
2019-11-17 15:06:20 +07:00
parent 2ffb7a19a4
commit d753ae2fdc
54 changed files with 899 additions and 0 deletions
+8
View File
@@ -0,0 +1,8 @@
all: main.o ex1.o
gcc main.o ex1.o -o ex1 -lm
main.o: main.c ex1.h
gcc -c main.c
ex1.o: ex1.c ex1.h
gcc -c ex1.c
clean:
rm -f *.o ex1
+94
View File
@@ -0,0 +1,94 @@
#include "ex1.h"
/* Tim block trong dau tien co size thich hop */
blk *find_fit(unsigned int size)
{
blk *cur = head;
while (cur != NULL)
{
if (cur->is_free == 1 && cur->size >= size)
break;
cur = cur->next;
}
return cur;
}
void *aligned_malloc(unsigned int size, unsigned int align)
{
if (size == 0)
return NULL;
/* Kiem tra align la so mu cua 2 */
if (IS_POWER_OF_2(align) == 0)
return NULL;
/* Tong gia tri do lon cua block can xin */
unsigned int total_size = size + align - 1 + sizeof(blk) + sizeof(blk *);
/* Tim block trong thich hop (neu co) */
blk *blk_head = find_fit(total_size);
void *ret;
if (blk_head != NULL)
{
/* Gan gia tri "da duoc dung" */
blk_head->is_free = 0;
/* Tim vi tri tra ve thich hop */
ret = (void *)(ALIGN(blk_head + sizeof(blk) + sizeof(blk *), align));
/* Luu con tro den vi tri giu thong tin cua block */
*(blk **)((size_t)ret - sizeof(blk *)) = blk_head;
return ret;
}
void *block = sbrk(total_size);
if (block == (void *)-1)
return NULL;
/* Vi tri luu thong tin cua block */
blk_head = block;
blk_head->size = total_size;
blk_head->is_free = 0;
blk_head->next = NULL;
if (head == NULL)
head = blk_head;
if (tail != NULL)
tail->next = blk_head;
tail = blk_head;
/* Tim vi tri tra ve thich hop */
ret = (void *)(ALIGN(block + sizeof(blk) + sizeof(blk *), align));
/* Luu con tro den vi tri giu thong tin cua block */
*(blk **)((size_t)ret - sizeof(blk *)) = blk_head;
return ret;
}
/* Ham nay khong nhat thiet phai tra ve con tro (void *), tuy nhien em lam theo prototype de bai yeu cau */
void *aligned_free(void *ptr)
{
if (ptr == NULL)
return NULL;
/* Tro den vi tri luu thong tin cua block */
blk *blk_head = *((blk **)((size_t)ptr - sizeof(blk *)));
void *prg_brk = sbrk(0);
/* Neu block nam o sat diem break, (neu co thi block nay phai o tail) */
if ((size_t)blk_head + blk_head->size == (size_t)prg_brk)
{
if (head == tail)
{
head = NULL;
tail = NULL;
}
else
{
blk *tmp = head;
while (tmp != NULL)
{
if (tmp->next == tail)
{
tmp->next = NULL;
tail = tmp;
}
tmp = tmp->next;
}
}
sbrk(0 - (unsigned int)blk_head->size);
return NULL;
}
/* Neu khong giai phong duoc vung nho, danh dau block la "trong" */
blk_head->is_free = 1;
/* Tra ve con tro toi vi tri luu thong tin cua block (co the dung de debug) */
return (void *)blk_head;
}
+71
View File
@@ -0,0 +1,71 @@
#include "ex1.h"
memNode *findFree(size_t size)
{
memNode *cur = head;
while (cur != NULL) {
if (cur->isFree == 1 && cur->size >= size)
return cur;
cur = cur->next;
}
return NULL;
}
void *aligned_malloc(unsigned int size, unsigned int align)
{
if (size == 0)
return NULL;
if (ceil(log2((long)align)) != floor(log2((long)align)))
return NULL;
unsigned int total_size = size + align - 1 + sizeof(memNode);
memNode *header = findFree(total_size);
void *ret = NULL;
if (header != NULL) {
header->isFree = 0;
ret = (void *) (((size_t)header + sizeof(memNode) + align - 1) & ~((size_t) align - 1));
*((void **)((size_t)ret - sizeof(memNode))) = header;
return ret;
}
void *block = sbrk(total_size);
if (block == (void *) -1)
return NULL;
header = block;
header->size = total_size;
header->block = (size_t)block;
header->isFree = 0;
header->next = NULL;
if (head == NULL)
head = header;
if (tail != NULL)
tail->next = header;
tail = header;
ret = (void *) (((size_t)header + sizeof(memNode) + align - 1) & ~((size_t) align - 1));
*((void **)((size_t)ret - sizeof(memNode))) = header;
return ret;
}
void aligned_free(void *ptr)
{
if (ptr == NULL)
return;
memNode *header = (memNode *)((size_t)ptr - sizeof(memNode));
void *prgBrk = sbrk(0);
if (header->block + header->size == (size_t)prgBrk) {
if (head == tail) {
head = NULL;
tail = NULL;
} else {
memNode *tmp = head;
while (tmp != NULL) {
if (tmp->next == tail) {
tmp->next = NULL;
tail = tmp;
}
tmp = tmp->next;
}
}
sbrk(0 - (unsigned int)header->size - sizeof(memNode));
return;
}
header->isFree = 1;
}
+26
View File
@@ -0,0 +1,26 @@
#ifndef EX1_H
#define EX1_H
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#define ALIGN(size, align) ((((size_t)size) + (align - 1)) & ~((size_t)align - 1))
#define IS_POWER_OF_2(num) ((num != 0) && ((num & (num - 1)) == 0))
struct newStruct
{
unsigned int size;
unsigned int is_free;
struct newStruct *next;
};
typedef struct newStruct blk;
static blk *head = NULL, *tail = NULL;
blk *find_fit(unsigned int size);
void *aligned_malloc(unsigned int size, unsigned int align);
void *aligned_free(void *ptr);
#endif // EX1_H
BIN
View File
Binary file not shown.
+86
View File
@@ -0,0 +1,86 @@
#include "ex1.h"
/* Tim block trong */
memNode *findFree(size_t size)
{
memNode *cur = head;
while (cur != NULL)
{
if (cur->isFree == 1 && cur->size >= size)
return cur;
cur = cur->next;
}
return NULL;
}
void *aligned_malloc(unsigned int size, unsigned int align)
{
if (size == 0)
return NULL;
/* Kiem tra so mu cua 2 */
if (ceil(log2((float)align)) != floor(log2((float)align)))
return NULL;
/* Tong gia tri do lon cua block can xin */
unsigned int total_size = size + align - 1 + sizeof(memNode);
memNode *header = findFree(total_size);
void *ret = NULL;
if (header != NULL)
{
header->isFree = 0;
ret = (void *)(((size_t)header + sizeof(memNode) + align - 1) & ~((size_t)align - 1));
*((void **)((size_t)ret - sizeof(memNode))) = header;
return ret;
}
void *block = sbrk(total_size);
if (block == (void *)-1)
return NULL;
header = block;
header->size = total_size;
header->block = (size_t)block;
header->isFree = 0;
header->next = NULL;
if (head == NULL)
head = header;
if (tail != NULL)
tail->next = header;
tail = header;
/* Tim vi tri tra ve thoa man */
ret = (void *)(((size_t)header + sizeof(memNode) + align - 1) & ~((size_t)align - 1));
/* Luu tru thong tin block tai khong gian truoc gia tri tra ve */
*((void **)((size_t)ret - sizeof(memNode))) = header;
return ret;
}
void aligned_free(void *ptr)
{
if (ptr == NULL)
return;
/* Lay lai thong tin cua block */
memNode *header = (memNode *)((size_t)ptr - sizeof(memNode));
void *prgBrk = sbrk(0);
/* Neu block nam o sat diem break */
if (header->block + header->size == (size_t)prgBrk)
{
if (head == tail)
{
head = NULL;
tail = NULL;
}
else
{
memNode *tmp = head;
while (tmp != NULL)
{
if (tmp->next == tail)
{
tmp->next = NULL;
tail = tmp;
}
tmp = tmp->next;
}
}
sbrk(0 - (unsigned int)header->size - sizeof(memNode));
return;
}
header->isFree = 1;
}
+15
View File
@@ -0,0 +1,15 @@
#include "ex1.h"
#include <math.h>
int main(int argc, char *argv[])
{
printf("PID: %d\n", getpid());
for (unsigned int align = 2; align <= pow(2, 22); align *= 2)
{
void *p = aligned_malloc(1024, align);
printf("%10d %p %15ld %ld \n", align, p,
(unsigned long)p, ((unsigned long)p) % align);
aligned_free(p);
}
return 0;
}
BIN
View File
Binary file not shown.