Skip to content

Commit 7627c05

Browse files
committed
Initial project distribution import.
0 parents  commit 7627c05

34 files changed

+118213
-0
lines changed

.buildmode

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
nodebug

Makefile

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
TARGETS := mdriver
2+
3+
LOCKER=/afs/csail/proj/courses/6.172
4+
CC := icpc
5+
CXX := icpc
6+
# You can add -Werr to GCC to force all warnings to turn into errors
7+
CFLAGS := -g -Wall
8+
CXXFLAGS := -g -Wall
9+
LDFLAGS := -lpthread
10+
11+
HEADERS := \
12+
allocator_interface.h \
13+
config.h \
14+
fsecs.h \
15+
mdriver.h \
16+
memlib.h \
17+
validator.h
18+
19+
# Blank line ends list.
20+
21+
# If you add a new file called "filename.c", you should
22+
# add "filename.o \" to this list.
23+
OBJS := \
24+
memlib.o
25+
26+
MDRIVER_OBJS:= \
27+
allocator.o \
28+
bad_allocator.o \
29+
clock.o \
30+
fcyc.o \
31+
fsecs.o \
32+
ftimer.o \
33+
libc_allocator.o \
34+
mdriver.o
35+
36+
37+
# Blank line ends list.
38+
39+
OLDMODE := $(shell cat .buildmode 2> /dev/null)
40+
ifeq ($(DEBUG),1)
41+
CFLAGS := -DDEBUG -O0 $(CFLAGS)
42+
CXXFLAGS := -DDEBUG -O0 $(CXXFLAGS)
43+
ifneq ($(OLDMODE),debug)
44+
$(shell echo debug > .buildmode)
45+
endif
46+
else
47+
CFLAGS := -DNDEBUG -O3 $(CFLAGS)
48+
CXXFLAGS := -DNDEBUG -O3 $(CXXFLAGS)
49+
ifneq ($(OLDMODE),nodebug)
50+
$(shell echo nodebug > .buildmode)
51+
endif
52+
endif
53+
54+
# make all targets specified
55+
all: $(TARGETS)
56+
57+
.PHONY: pintool
58+
pintool:
59+
$(MAKE) -C pintool
60+
61+
mdriver: $(OBJS) $(MDRIVER_OBJS)
62+
$(CXX) $(LDFLAGS) $(OBJS) $(MDRIVER_OBJS) -o $@
63+
64+
# compile objects
65+
66+
# pattern rule for building objects
67+
%.o: %.cxx %.h $(HEADERS) .buildmode Makefile
68+
$(CXX) $(CXXFLAGS) -c $< -o $@
69+
%.o: %.c %.h $(HEADERS) .buildmode Makefile
70+
$(CC) $(CFLAGS) -c $< -o $@
71+
72+
73+
# run each of the targets
74+
run: $(TARGETS)
75+
for X in $(TARGETS) ; do \
76+
echo $$X -v ; \
77+
./$$X -v ; \
78+
echo ; \
79+
done
80+
81+
# remove targets and .o files as well as output generated by CQ
82+
clean:
83+
$(RM) $(TARGETS) $(OBJS) $(MDRIVER_OBJS) *.std* .buildmode
84+
$(RM) tmp/*.out

allocator.cpp

+152
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
/**
2+
* Copyright (c) 2012 MIT License by 6.172 Staff
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to
6+
* deal in the Software without restriction, including without limitation the
7+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8+
* sell copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20+
* IN THE SOFTWARE.
21+
**/
22+
23+
#include <stdio.h>
24+
#include <stdint.h>
25+
#include <cstdlib>
26+
#include <cstring>
27+
#include "./allocator_interface.h"
28+
#include "./memlib.h"
29+
30+
// All blocks must have a specified minimum alignment.
31+
#define ALIGNMENT 8
32+
33+
// Rounds up to the nearest multiple of ALIGNMENT.
34+
#define ALIGN(size) (((size) + (ALIGNMENT-1)) & ~(ALIGNMENT-1))
35+
36+
// The smallest aligned size that will hold a size_t value.
37+
#define SIZE_T_SIZE (ALIGN(sizeof(size_t)))
38+
39+
namespace my {
40+
// check - This checks our invariant that the size_t header before every
41+
// block points to either the beginning of the next block, or the end of the
42+
// heap.
43+
int allocator::check() {
44+
char *p;
45+
char *lo = (char*)mem_heap_lo();
46+
char *hi = (char*)mem_heap_hi() + 1;
47+
size_t size = 0;
48+
49+
p = lo;
50+
while (lo <= p && p < hi) {
51+
size = ALIGN(*(size_t*)p + SIZE_T_SIZE);
52+
p += size;
53+
}
54+
55+
if (p != hi) {
56+
printf("Bad headers did not end at heap_hi!\n");
57+
printf("heap_lo: %p, heap_hi: %p, size: %lu, p: %p\n", lo, hi, size, p);
58+
return -1;
59+
}
60+
61+
return 0;
62+
}
63+
64+
// init - Initialize the malloc package. Called once before any other
65+
// calls are made. Since this is a very simple implementation, we just
66+
// return success.
67+
int allocator::init() {
68+
return 0;
69+
}
70+
71+
// malloc - Allocate a block by incrementing the brk pointer.
72+
// Always allocate a block whose size is a multiple of the alignment.
73+
void * allocator::malloc(size_t size) {
74+
// We allocate a little bit of extra memory so that we can store the
75+
// size of the block we've allocated. Take a look at realloc to see
76+
// one example of a place where this can come in handy.
77+
int aligned_size = ALIGN(size + SIZE_T_SIZE);
78+
79+
// Expands the heap by the given number of bytes and returns a pointer to
80+
// the newly-allocated area. This is a slow call, so you will want to
81+
// make sure you don't wind up calling it on every malloc.
82+
void *p = mem_sbrk(aligned_size);
83+
84+
if (p == (void *)-1) {
85+
// Whoops, an error of some sort occurred. We return NULL to let
86+
// the client code know that we weren't able to allocate memory.
87+
return NULL;
88+
} else {
89+
// We store the size of the block we've allocated in the first
90+
// SIZE_T_SIZE bytes.
91+
*(size_t*)p = size;
92+
93+
// Then, we return a pointer to the rest of the block of memory,
94+
// which is at least size bytes long. We have to cast to uint8_t
95+
// before we try any pointer arithmetic because voids have no size
96+
// and so the compiler doesn't know how far to move the pointer.
97+
// Since a uint8_t is always one byte, adding SIZE_T_SIZE after
98+
// casting advances the pointer by SIZE_T_SIZE bytes.
99+
return (void *)((char *)p + SIZE_T_SIZE);
100+
}
101+
}
102+
103+
// free - Freeing a block does nothing.
104+
void allocator::free(void *ptr) {
105+
}
106+
107+
// realloc - Implemented simply in terms of malloc and free
108+
void * allocator::realloc(void *ptr, size_t size) {
109+
void *newptr;
110+
size_t copy_size;
111+
112+
// Allocate a new chunk of memory, and fail if that allocation fails.
113+
newptr = malloc(size);
114+
if (NULL == newptr)
115+
return NULL;
116+
117+
// Get the size of the old block of memory. Take a peek at malloc(),
118+
// where we stashed this in the SIZE_T_SIZE bytes directly before the
119+
// address we returned. Now we can back up by that many bytes and read
120+
// the size.
121+
copy_size = *(size_t*)((uint8_t*)ptr - SIZE_T_SIZE);
122+
123+
// If the new block is smaller than the old one, we have to stop copying
124+
// early so that we don't write off the end of the new block of memory.
125+
if (size < copy_size)
126+
copy_size = size;
127+
128+
// This is a standard library call that performs a simple memory copy.
129+
std::memcpy(newptr, ptr, copy_size);
130+
131+
// Release the old block.
132+
free(ptr);
133+
134+
// Return a pointer to the new block.
135+
return newptr;
136+
}
137+
138+
// call mem_reset_brk.
139+
void allocator::reset_brk() {
140+
mem_reset_brk();
141+
}
142+
143+
// call mem_heap_lo
144+
void * allocator::heap_lo() {
145+
return mem_heap_lo();
146+
}
147+
148+
// call mem_heap_hi
149+
void * allocator::heap_hi() {
150+
return mem_heap_hi();
151+
}
152+
};

allocator_interface.h

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/**
2+
* Copyright (c) 2012 MIT License by 6.172 Staff
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to
6+
* deal in the Software without restriction, including without limitation the
7+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
8+
* sell copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in
12+
* all copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
20+
* IN THE SOFTWARE.
21+
**/
22+
23+
#include <cstdlib>
24+
25+
#ifndef _ALLOCATOR_INTERFACE_H
26+
#define _ALLOCATOR_INTERFACE_H
27+
28+
namespace my {
29+
class allocator_interface { // NOLINT
30+
public:
31+
virtual void reset_brk() = 0;
32+
virtual void * heap_lo() = 0;
33+
virtual void * heap_hi() = 0;
34+
};
35+
36+
class libc_allocator : public virtual allocator_interface {
37+
public:
38+
static int init();
39+
static void * malloc(size_t size);
40+
static void * realloc(void *ptr, size_t size);
41+
static void free(void *ptr);
42+
static int check();
43+
void reset_brk();
44+
void * heap_lo();
45+
void * heap_hi();
46+
};
47+
48+
class allocator : public virtual allocator_interface {
49+
public:
50+
static int init();
51+
static void * malloc(size_t size);
52+
static void * realloc(void *ptr, size_t size);
53+
static void free(void *ptr);
54+
static int check();
55+
void reset_brk();
56+
void * heap_lo();
57+
void * heap_hi();
58+
};
59+
60+
61+
class bad_allocator : public virtual allocator_interface {
62+
public:
63+
static int init();
64+
static void * malloc(size_t size);
65+
static void * realloc(void *ptr, size_t size);
66+
static void free(void *ptr);
67+
static int check();
68+
void reset_brk();
69+
void * heap_lo();
70+
void * heap_hi();
71+
};
72+
};
73+
#endif // _ALLOCATOR_INTERFACE_H

0 commit comments

Comments
 (0)