malloc lab

垃圾实验

写挂了两版代码,白给十个小时

后面照着网上代码写了,再自己写就是狗

挂的代码1:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <unistd.h>
#include <string.h>

#include "mm.h"
#include "memlib.h"

/*********************************************************
* NOTE TO STUDENTS: Before you do anything else, please
* provide your team information in the following struct.
********************************************************/
team_t team = {
/* Team name */
"C0de Dancer",
/* First member's full name */
"Lyu Antong",
/* First member's email address */
"init.new.world@gmail.com",
/* Second member's full name (leave blank if none) */
"1183710217",
/* Second member's email address (leave blank if none) */
"0xdeadc0de"
};


/*
DataStructure:Balanced Tree(Treap)
4|4|4|4|4|2 : SIZE|LEFT|RIGHT|ZI_SIZE|PTR|WEIGHT

ZC_SEQ:
8|BLOCK_SIZE : SIZE|BLOCK_SIZE

FREE_SEQ:
8|8:PTR|NEXT
*/

/*SIZE OF DATA*/
#define HWORD 2
#define WORD 4
#define DWORD 8
#define SIZE_TREE_NODE 22
/*SIZE STRUCTURE OF DATA*/
#define SIZE_HWORD unsigned short
#define SIZE_WORD unsigned int
#define SIZE_DWORD unsigned long long
/*GET DATA*/
#define GET_HWORD(p) (*(unsigned short *)(p))
#define GET_WORD(p) (*(unsigned int *)(p))
#define GET_DWORD(p) (*(unsigned long long *)(p))
/*SET DATA*/
#define SET_HWORD(p,val) (*(unsigned short *)(p)=(val))
#define SET_WORD(p,val) (*(unsigned int *)(p)=(val))
#define SET_DWORD(p,val) (*(unsigned long long *)(p)=(val))
/*GET TREAP ARGVS*/
#define GET_TREE_SIZE(p) (GET_WORD(p))
#define GET_TREE_LEFT(p) (GET_WORD(p+WORD))
#define GET_TREE_RIGHT(p) (GET_WORD(p+WORD+WORD))
#define GET_TREE_ZI_SIZE(p) (GET_WORD(p+WORD+WORD*2))
#define GET_TREE_PTR(p) (GET_WORD(p+WORD+WORD*3))
#define GET_TREE_WEIGHT(p) (GET_HWORD(p+WORD+WORD*4))
/*SET TREAP ARGVS*/
#define SET_TREE_SIZE(p,val) (SET_WORD(p,val))
#define SET_TREE_LEFT(p,val) (SET_WORD(p+WORD,val))
#define SET_TREE_RIGHT(p,val) (SET_WORD(p+WORD+WORD,val))
#define SET_TREE_ZI_SIZE(p,val) (SET_WORD(p+WORD+WORD*2,val))
#define SET_TREE_PTR(p,val) (SET_WORD(p+WORD+WORD*3,val))
#define SET_TREE_WEIGHT(p,val) (SET_HWORD(p+WORD+WORD*4,val))
/*GET PTR ARGVS*/
#define GET_PTR_SIZE(p) GET_DWORD(p-DWORD)
/*SET PTR ARGVS*/
#define SET_PTR_SIZE(p,val) SET_DWORD(p-DWORD,val)
/*GET SEQ ARGVS*/
#define GET_SEQ_PTR(p) GET_DWORD(p)
#define GET_SEQ_NEXT(p) GET_DWORD(p+DWORD)
/*SET SEQ ARGVS*/
#define SET_SEQ_PTR(p,val) SET_DWORD(p,val)
#define SET_SEQ_NEXT(p,val) SET_DWORD(p+DWORD,val)
/*GET RANDOM HASH*/
#define RAND(p) ((GET_TREE_SIZE(p)^GET_TREE_PTR(p))+GET_TREE_WEIGHT(p))

/*Treap root,Treap seq*/
static char *root,*eight;
/*Initialize the tree*/
int mm_init(void){
root=NULL;
eight=NULL;
return 0;
}
/*Pushup a node*/
void Pushup(void *rt){
SIZE_WORD *l=GET_TREE_LEFT(rt),*r=GET_TREE_RIGHT(rt),lsize=0,rsize=0;
if(l!=NULL)lsize=GET_TREE_SIZE(l);
if(r!=NULL)rsize=GET_TREE_SIZE(r);
SIZE_HWORD weight=GET_TREE_WEIGHT(rt);
SET_TREE_SIZE(rt,lsize+rsize+weight);
}
/*Left rotate*/
void Lr(void **ptr){
SIZE_WORD q=GET_TREE_RIGHT(*ptr);
SET_TREE_RIGHT(*ptr,GET_TREE_LEFT(q));
SET_TREE_LEFT(q,*ptr);
SET_TREE_SIZE(q,GET_TREE_SIZE(*ptr));
Pushup(*ptr);
*ptr=q;
}
/*Right rotate*/
void Rr(void **ptr){
SIZE_WORD q=GET_TREE_LEFT(*ptr);
SET_TREE_LEFT(*ptr,GET_TREE_RIGHT(q));
SET_TREE_RIGHT(q,*ptr);
SET_TREE_SIZE(q,GET_TREE_SIZE(*ptr));
Pushup(*ptr);
*ptr=q;
}
/*Insert 8-seq*/
void Insert8(void *ptr){
SET_SEQ_PTR(ptr,ptr);
if(eight!=NULL)SET_SEQ_NEXT(ptr,eight);
else SET_SEQ_NEXT(ptr,0);
eight=ptr;
}
/*Get a 8-seq and delete it*/
void *Get8(){
if(eight==NULL)return NULL;
void *p=eight;
if(GET_SEQ_NEXT(eight)!=0)eight=GET_SEQ_NEXT(eight);
else eight=NULL;
return p;
}
/*Put a node into it*/
void *TreapNode(SIZE_WORD size,SIZE_WORD left,SIZE_WORD right,SIZE_WORD zi_size,SIZE_WORD ptr,SIZE_HWORD weight){
void *rt;
if(eight==NULL)rt=mem_sbrk(SIZE_TREE_NODE);
else {
rt=Get8();
}
return rt;
}

/*Insert ptr into treap*/
void Ins(void **rt,void *ptr,size_t size){
if(*rt==NULL){
*rt=TreapNode(0,NULL,NULL,size,ptr,1);
return;
}
SET_TREE_SIZE(*rt,GET_TREE_SIZE(*rt)+1);
if(GET_TREE_ZI_SIZE(*rt)>size){
Ins(&GET_TREE_LEFT(*rt),ptr,size);
if(RAND(GET_TREE_LEFT(*rt))<RAND(*rt))Rr(rt);
return;
}
else {
Ins(&GET_TREE_RIGHT(*rt),ptr,size);
if(RAND(GET_TREE_RIGHT(*rt))<RAND(*rt))Lr(rt);
return;
}
}
/*Find a size in the tree,return the ptr of tree*/
static char *FOUND;

void Find(void **rt,size_t x){
if(*rt==NULL)return NULL;
if(x<=GET_TREE_SIZE(*rt)){FOUND=*rt;Find(GET_TREE_LEFT(*rt),x);}
else Find(GET_TREE_RIGHT(*rt),x);
}

void* FOUNDIT(void **rt,size_t x){
FOUND=NULL;
Find(rt,x);
return FOUND;
}
/*Delete a ptr in the treap*/
void Del(void **rt){
if(GET_TREE_LEFT(*rt)==0 && GET_TREE_RIGHT(*rt)==0){Insert8(*rt);return;}
if(GET_TREE_LEFT(*rt)==0){Insert8(*rt);*rt=GET_TREE_RIGHT(*rt);return;}
if(GET_TREE_RIGHT(*rt)==0){Insert8(*rt);*rt=GET_TREE_LEFT(*rt);return;}
if(RAND(GET_TREE_LEFT(*rt))<RAND(GET_TREE_RIGHT(*rt)))Rr(*rt);
else Lr(*rt);
Del(rt);
}

void *mm_malloc(size_t size){
if(size<=0)return NULL;
size_t asize;
if(size<DWORD)asize=DWORD;
else asize=((size-1)/DWORD+1)*DWORD;
void *f=GET_TREE_PTR(FOUNDIT(root,asize+DWORD));
size_t siz=GET_TREE_SIZE(FOUNDIT(root,asize+DWORD));
if(f!=NULL){
Del(FOUNDIT(root,asize+DWORD));
SET_PTR_SIZE(f+DWORD,asize+DWORD);
return f+DWORD;
}
else {
void *p=mem_sbrk(asize+DWORD);
SET_PTR_SIZE(p+DWORD,asize+DWORD);
return p+DWORD;
}
}

void mm_free(void *ptr){
size_t siz=GET_PTR_SIZE(ptr);
Ins(root,ptr-DWORD,siz);
}

void *mm_realloc(void *ptr,size_t size){
if(ptr==NULL){return mm_malloc(size);}
if(size==0){mm_free(ptr);return NULL;}
size_t asize,qur=GET_PTR_SIZE(ptr);
if(size<DWORD)asize=DWORD;
else asize=((size-1)/DWORD+1)*DWORD;
void *p=mm_malloc(asize+DWORD);
SET_PTR_SIZE(p+DWORD,asize+DWORD);
memcpy(p+DWORD,ptr,size);
mm_free(ptr);
return p+DWORD;
}

挂的代码2:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/*
* mm-naive.c - The fastest, least memory-efficient malloc package.
*
* In this naive approach, a block is allocated by simply incrementing
* the brk pointer. A block is pure payload. There are no headers or
* footers. Blocks are never coalesced or reused. Realloc is
* implemented directly using mm_malloc and mm_free.
*
* NOTE TO STUDENTS: Replace this header comment with your own header
* comment that gives a high level description of your solution.
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <unistd.h>
#include <string.h>

#include "mm.h"
#include "memlib.h"

/*********************************************************
* NOTE TO STUDENTS: Before you do anything else, please
* provide your team information in the following struct.
********************************************************/
team_t team = {
/* Team name */
"C0de Dancer",
/* First member's full name */
"Lyu Antong",
/* First member's email address */
"init.new.world@gmail.com",
/* Second member's full name (leave blank if none) */
"Init_new_world",
/* Second member's email address (leave blank if none) */
"0xdeadc0de"
};

#define WSIZE 4
#define DSIZE 8
#define GET(p) (*(size_t *)(p))
#define PUT(p, val) (*(size_t *)(p) = (val))
#define NUM (1<<9)
static char *mem_addr[NUM];

int mm_choose(size_t size){
size /= 8;
if(size<102400)return size;
}

unsigned int mm_getlink(size_t size){
for(int i=mm_choose(size);i<NUM;i++){
if(GET(mem_addr[i])!=0)return i;
}
return 0;
}

/*
* mm_init - initialize the malloc package.
*/
int mm_init(void){
for(int i=1;i<NUM;i++){
mem_addr[i]=mem_sbrk(DSIZE);
PUT(mem_addr[i],0);
PUT(mem_addr[i]+WSIZE,0);
}
return 0;
}

void *mm_find(size_t size){
int val=mm_getlink(size);
if(val==0)return NULL;
char *p=mem_addr[val];
unsigned int real_size=GET(p),start=GET(p+WSIZE),ptr=GET(start),nxt_start=GET(start+WSIZE);
real_size--;
PUT(p,real_size);
PUT(p+WSIZE,nxt_start);
if(size==8)return ptr;
unsigned int size8=GET(mem_addr[1]);
size8++;
PUT(mem_addr[1],size8);
PUT(start+WSIZE,GET(mem_addr[1]+WSIZE));
PUT(start,start);
PUT(mem_addr[1]+WSIZE,start);
return ptr;
}

void mm_insert(void *ptr,size_t size){
if(size<=0 || ptr==NULL)return;
int addr=mm_choose(size);
int real_size=GET(mem_addr[addr]);
void *nxt_ptr=GET(mem_addr[addr]+WSIZE),*now_ptr=mm_find(8);
if(now_ptr==NULL)now_ptr=mem_sbrk(8);
real_size++;
PUT(mem_addr[addr],real_size);
PUT(now_ptr+WSIZE,GET(mem_addr[addr]+WSIZE));
PUT(now_ptr,nxt_ptr);
PUT(mem_addr[addr]+WSIZE,now_ptr);
}

/*
* mm_malloc - Allocate a block by incrementing the brk pointer.
* Always allocate a block whose size is a multiple of the alignment.
*/
void *mm_malloc(size_t size){
if(size<=0)return NULL;
size_t asize;
if(size<DSIZE)asize=2*DSIZE;
else asize=((size-1)/DSIZE+1)*DSIZE+DSIZE;
unsigned int real_size=mm_choose(asize+DSIZE);
void *ptr=mm_find(asize+DSIZE);
if(ptr!=NULL){
PUT(ptr,asize-DSIZE);
void *ptr_nxt=ptr+DSIZE+asize;
size_t lost_size=real_size-DSIZE-asize;
mm_insert(ptr_nxt,lost_size);
return ptr+DSIZE;
}
ptr=mem_sbrk(asize);
PUT(ptr,asize-DSIZE);
return ptr+DSIZE;
}

/*
* mm_free - Freeing a block does nothing.
*/
void mm_free(void *ptr){
if(ptr==NULL)return;
unsigned int real_size=GET(ptr-DSIZE)+DSIZE;
ptr -= DSIZE;
mm_insert(ptr,real_size);
}

/*
* mm_realloc - Implemented simply in terms of mm_malloc and mm_free
*/
void *mm_realloc(void *ptr, size_t size){
if(ptr==NULL){return mm_malloc(size);}
if(size==0){mm_free(ptr);return NULL;}
size_t asize;
if(size<DSIZE)asize=2*DSIZE;
else asize=((size-1)/DSIZE+1)*DSIZE+DSIZE;
char *p=mm_malloc(asize+DSIZE);
PUT(p,asize);
memcpy(p+DSIZE,ptr,size);
mm_free(ptr);
return p+DSIZE;
}

malloc lab
http://hexo.init-new-world.com/ics-malloc-lab
Author
John Doe
Posted on
December 17, 2019
Licensed under