Skip to content

Commit

Permalink
improve 9.14, 9.17, 9.18 test case
Browse files Browse the repository at this point in the history
  • Loading branch information
DreamAndDead committed Feb 1, 2018
1 parent 04dd49c commit c71498f
Show file tree
Hide file tree
Showing 10 changed files with 153 additions and 28 deletions.
8 changes: 4 additions & 4 deletions chapter9/code/9.14.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
#include <assert.h>
#include "vm/csapp.h"

int test(char* filename, char* content) {
void test(char* filename, char* content) {
int fd;
char buf[20];
fd = Open(filename, O_RDONLY, 0);
Read(fd, buf, strlen(content));
return !strncmp(buf, content, strlen(content));
assert( !strncmp(buf, content, strlen(content)) );
}

int touch(char* filename, char* content) {
Expand All @@ -23,7 +23,7 @@ int touch(char* filename, char* content) {

int main(int argc, char* argv[]) {
touch("hello.txt", "Hello, world!");
assert(test("hello.txt", "Hello, world!"));
test("hello.txt", "Hello, world!");

struct stat stat;
int fd;
Expand All @@ -38,7 +38,7 @@ int main(int argc, char* argv[]) {
*bufp = 'J';
Munmap(bufp, size);

assert(test("hello.txt", "Jello, world!"));
test("hello.txt", "Jello, world!");
return 0;
}

Expand Down
39 changes: 31 additions & 8 deletions chapter9/code/vm/9.17.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,46 @@
* 9.17.c
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "csapp.h"
#include "mm.h"
#include "memlib.h"

#define LOOP 1000
/*
* blk
* vvv
* 1 2 3 4
* +------+------+---------+---------+............
* |||||||| ||||||||||| |............
* +------+------+---------+---------+............
* 1 2
* ^^^
* new_blk
*/
int rand_num(void) {
return rand() % 10000;
}

int main(int argc, char* argv[]) {
int i;
for (i = 0; i < LOOP; i+=2) {
void* ptr_f = mm_malloc(i);
void* ptr = mm_malloc(i+1);
mm_free(ptr_f);
}

void* blk_1 = mm_malloc(rand_num());
void* blk_2 = mm_malloc(rand_num());
void* blk_3 = mm_malloc(rand_num());
void* blk_4 = mm_malloc(rand_num());
mm_free(blk_2);
mm_free(blk_4);

void* new_blk_1 = mm_malloc(rand_num());
void* new_blk_2 = mm_malloc(rand_num());

assert(new_blk_1 != blk_2);
assert(new_blk_1 == blk_4);
assert(new_blk_2 != blk_2);
assert(new_blk_2 != blk_4);

return 0;
}




59 changes: 52 additions & 7 deletions chapter9/code/vm/9.18.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,64 @@
* 9.18.c
*/
#include <stdio.h>
#include <assert.h>
#include "csapp.h"
#include "mm.h"
#include "memlib.h"

#define LOOP 1000
/*
* blk
* vvv
* 1 2 3
* +-------+-----------+---------+
* ||||||||| |||||||||||
* +-------+-----------+---------+
*/

// current block head
unsigned int blk_head(void* blk) {
unsigned int * header = (unsigned int *)blk - 1;
return *header;
}

// previous block tail
unsigned int prev_blk_tail(void* blk) {
unsigned int * prev_tail = (unsigned int *)blk - 2;
return *prev_tail;
}

// if block is allocated
int blk_alloc(void* blk) {
return blk_head(blk) & 0x1;
}

// if previous block is allocated
int blk_prev_alloc(void* blk) {
return (blk_head(blk) & 0x2) >> 1;
}

int rand_num(void) {
return rand() % 10000;
}

int main(int argc, char* argv[]) {
int i;
for (i = 0; i < LOOP; i+=2) {
void* ptr_f = mm_malloc(i);
void* ptr = mm_malloc(i+1);
mm_free(ptr_f);
}
void* blk_1 = mm_malloc(rand_num());
void* blk_2 = mm_malloc(rand_num());
void* blk_3 = mm_malloc(rand_num());
mm_free(blk_2);


assert(blk_alloc(blk_1));
assert(!blk_alloc(blk_2));
assert(blk_alloc(blk_3));

assert(blk_prev_alloc(blk_1));
assert(blk_prev_alloc(blk_2));
assert(!blk_prev_alloc(blk_3));


assert(blk_head(blk_1) != prev_blk_tail(blk_2));
assert(blk_head(blk_2) == prev_blk_tail(blk_3));

return 0;
}
Expand Down
3 changes: 3 additions & 0 deletions chapter9/code/vm/makefile
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@

CC = gcc
CFLAGS = -m64 -pthread -g
SRCS.example = mm.c memlib.c csapp.c mm_first_fit.c
SRCS.9.17 = mm.9.17.c memlib.c csapp.c 9.17.c
SRCS.9.18 = mm.9.18.c memlib.c csapp.c 9.18.c

all: diff
$(CC) $(CFLAGS) $(SRCS.example) -o mm_first_fit
$(CC) $(CFLAGS) $(SRCS.9.17) -o 9.17
$(CC) $(CFLAGS) $(SRCS.9.18) -o 9.18

test:
./mm_first_fit
./9.17
./9.18

Expand Down
1 change: 1 addition & 0 deletions chapter9/code/vm/mm.9.18.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ void mm_free(void *bp)
/* $begin mmfree */

PUT(HDRP(bp), PACK(size, 0, GET_PREV_ALLOC(HDRP(bp))));
PUT(FTRP(bp), PACK(size, 0, GET_PREV_ALLOC(HDRP(bp))));

if (GET_ALLOC(HDRP(NEXT_BLKP(bp))))
PUT(HDRP(NEXT_BLKP(bp)), PACK(GET_SIZE(HDRP(NEXT_BLKP(bp))), 1, 0));
Expand Down
13 changes: 7 additions & 6 deletions chapter9/code/vm/mm.9.18.diff
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
--- mm.c 2017-11-09 02:57:43.679935907 +0000
+++ mm.9.18.c 2017-11-09 02:57:43.679935907 +0000
+++ mm.9.18.c 2018-02-01 10:08:47.563747327 +0000
@@ -20,7 +20,7 @@
#define MAX(x, y) ((x) > (y)? (x) : (y))

Expand Down Expand Up @@ -44,13 +44,14 @@

/* Search the free list for a fit */
if ((bp = find_fit(asize)) != NULL) { //line:vm:mm:findfitcall
@@ -136,8 +137,15 @@
@@ -136,8 +137,16 @@
}
/* $begin mmfree */

- PUT(HDRP(bp), PACK(size, 0));
- PUT(FTRP(bp), PACK(size, 0));
+ PUT(HDRP(bp), PACK(size, 0, GET_PREV_ALLOC(HDRP(bp))));
+ PUT(FTRP(bp), PACK(size, 0, GET_PREV_ALLOC(HDRP(bp))));
+
+ if (GET_ALLOC(HDRP(NEXT_BLKP(bp))))
+ PUT(HDRP(NEXT_BLKP(bp)), PACK(GET_SIZE(HDRP(NEXT_BLKP(bp))), 1, 0));
Expand All @@ -62,7 +63,7 @@
coalesce(bp);
}

@@ -148,7 +156,7 @@
@@ -148,7 +157,7 @@
/* $begin mmfree */
static void *coalesce(void *bp)
{
Expand All @@ -71,7 +72,7 @@
size_t next_alloc = GET_ALLOC(HDRP(NEXT_BLKP(bp)));
size_t size = GET_SIZE(HDRP(bp));

@@ -158,22 +166,22 @@
@@ -158,22 +167,22 @@

else if (prev_alloc && !next_alloc) { /* Case 2 */
size += GET_SIZE(HDRP(NEXT_BLKP(bp)));
Expand Down Expand Up @@ -100,7 +101,7 @@
bp = PREV_BLKP(bp);
}
/* $end mmfree */
@@ -246,9 +254,9 @@
@@ -246,9 +255,9 @@
return NULL; //line:vm:mm:endextend

/* Initialize free block header/footer and the epilogue header */
Expand All @@ -113,7 +114,7 @@

/* Coalesce if the previous block was free */
return coalesce(bp); //line:vm:mm:returnblock
@@ -267,15 +275,14 @@
@@ -267,15 +276,14 @@
size_t csize = GET_SIZE(HDRP(bp));

if ((csize - asize) >= (2*DSIZE)) {
Expand Down
Binary file added chapter9/code/vm/mm_first_fit
Binary file not shown.
52 changes: 52 additions & 0 deletions chapter9/code/vm/mm_first_fit.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
* mm_first_fit.c
*
* test example code from book section 9.9.12
*/
#include <stdio.h>
#include <assert.h>
#include "csapp.h"
#include "mm.h"
#include "memlib.h"

/*
* blk
* vvv
* 1 2 3 4 5 6
* +----+----+----+----+--------+--------+
* | |||||| |||||| ||||||||||
* +----+----+----+----+--------+--------+
* 1 3 2
* ^^^
* new_blk
*/
int main(int argc, char* argv[]) {

// allocate 8 bytes because of alignment
void* blk_1 = mm_malloc(1);
void* blk_2 = mm_malloc(1);

void* blk_3 = mm_malloc(8);
void* blk_4 = mm_malloc(8);
void* blk_5 = mm_malloc(16);
void* blk_6 = mm_malloc(16);

mm_free(blk_1);
mm_free(blk_3);
mm_free(blk_5);


void* new_blk_1 = mm_malloc(6);
void* new_blk_2 = mm_malloc(12);
void* new_blk_3 = mm_malloc(7);

assert(new_blk_1 == blk_1);
assert(new_blk_2 == blk_5);
assert(new_blk_3 == blk_3);

return 0;
}




4 changes: 2 additions & 2 deletions chapter9/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ test way:
|9.14|9.14.c|assert|
|9.15|----|----|
|9.16|----|----|
|9.17|vm/(mm.9.17, 9.17.c)|malloc and free|
|9.18|vm/(mm.9.18, 9.18.c)|malloc and free|
|9.17|vm/(mm.9.17, 9.17.c)|assert|
|9.18|vm/(mm.9.18, 9.18.c)|assert|
|9.19|----|----|
|9.20|malloc/*|measure performance with std malloc|

2 changes: 1 addition & 1 deletion review.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
|6|----|----|
|7|----|----|
|8|----|----|
|9|test 9.17, 9.18; more detailed reason for 9.19; 9.20 is awful, sub with more professional way(such as segrated fit method) |ing|
|9|more detailed reason for 9.19; 9.20 is awful, sub with more professional way(such as segrated fit method) |ing|
|10|----|----|
|11|----|----|
|12|----|----|

0 comments on commit c71498f

Please sign in to comment.