Skip to content

Commit db351ac

Browse files
committed
Add files for second datarep section.
1 parent aed8fc4 commit db351ac

File tree

20 files changed

+355
-0
lines changed

20 files changed

+355
-0
lines changed

datareps1/flapmap1.cc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#include "flapmap.hh"
2+
3+
int main() {
4+
printf("Hi! This program is a placeholder.\n");
5+
printf("If you want to experiment with `flapmap` code, either edit `flapmap1.cc` and `make`,\n");
6+
printf("or make a file called `flapmapNN.cc` and `make`.\n");
7+
}

datareps2/.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
membug[0-9]
2+
membug[0-9][0-9]
3+
[sbl][0-9]
4+
[sbl][0-9][0-9]
5+
ll[0-9]
6+
ll[0-9][0-9]
7+
hulk

datareps2/GNUmakefile

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# build all programs with names like `[sbl][0-9] [sbl][0-9][0-9]`
2+
XPROGRAMS = $(patsubst %.cc,%,$(wildcard [sbl][0-9].cc [sbl][0-9][0-9].cc ll[0-9].cc ll[0-9][0-9].cc))
3+
PROGRAMS = $(XPROGRAMS) hulk
4+
all: $(PROGRAMS)
5+
6+
ALLPROGRAMS = $(PROGRAMS) inv testinsert0 greet1
7+
8+
include ../common/rules.mk
9+
10+
LIBS = -lm
11+
12+
13+
# Rules for making object files (i.e., parts of executables)
14+
# from source files
15+
16+
%.o: %.cc $(BUILDSTAMP)
17+
$(CXX) $(CPPFLAGS) $(CXXFLAGS) $(DEPCFLAGS) $(O) -o $@ -c $<
18+
19+
20+
# Rules for making executables (runnable programs) from object files
21+
22+
$(XPROGRAMS): \
23+
%: %.o hexdump.o
24+
$(CXX) $(CXXFLAGS) $(LDFLAGS) $(O) -o $@ $^ $(LIBS)
25+
26+
hulk: \
27+
%: %.o hexdump.o
28+
$(CXX) $(CXXFLAGS) $(LDFLAGS) $(O) -o $@ $^ $(LIBS)
29+
30+
31+
clean:
32+
rm -rf $(ALLPROGRAMS) *.o $(DEPSDIR)
33+
rm -rf $(wildcard [sbl][0-9] [sbl][0-9][0-9] ll[0-9] ll[0-9][0-9])
34+
35+
.PHONY: all clean

datareps2/b1.cc

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include <cstdio>
2+
3+
const char* f(int) {
4+
return "example";
5+
}
6+
7+
int main() {
8+
printf("%s\n", f(2));
9+
}

datareps2/b2.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include <cstdio>
2+
3+
const char* f(int) {
4+
char buf[100] = "example";
5+
return buf;
6+
}
7+
8+
int main() {
9+
printf("%s\n", f(2));
10+
}

datareps2/b3.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include <cstdio>
2+
3+
const char* f(int) {
4+
char buf[100] = "example";
5+
return buf;
6+
}
7+
8+
int main() {
9+
printf("%p\n", f(2));
10+
}

datareps2/b4.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <cstdio>
2+
#include <cstring>
3+
4+
const char* f(int) {
5+
char* buf = new char[100];
6+
strcpy(buf, "example");
7+
return buf;
8+
}
9+
10+
int main() {
11+
const char* s = f(2);
12+
printf("%s\n", s);
13+
delete[] s;
14+
}

datareps2/b5.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <cstdio>
2+
#include <cstring>
3+
4+
const char* f(int) {
5+
char* buf = new char[100];
6+
strcpy(buf, "example");
7+
return buf;
8+
}
9+
10+
int main() {
11+
const char* s = f(2);
12+
delete[] s;
13+
printf("%s\n", s);
14+
}

datareps2/b6.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <cstdio>
2+
#include <cstring>
3+
4+
const char* f(int) {
5+
char* buf = new char[strlen("example")];
6+
strcpy(buf, "example");
7+
return buf;
8+
}
9+
10+
int main() {
11+
const char* s = f(2);
12+
printf("%s\n", s);
13+
delete[] s;
14+
}

datareps2/hexdump.cc

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include "hexdump.hh"
2+
#include <cassert>
3+
4+
static void fhexdump_ascii(FILE* f, const unsigned char* p, size_t pos);
5+
6+
void hexdump(const void* ptr, size_t size, const char* objname) {
7+
fhexdump_at(stdout, (size_t) ptr, ptr, size, objname);
8+
}
9+
10+
void fhexdump(FILE* f, const void* ptr, size_t size, const char* objname) {
11+
fhexdump_at(f, (size_t) ptr, ptr, size, objname);
12+
}
13+
14+
void fhexdump_at(FILE* f, size_t first_offset, const void* ptr, size_t size,
15+
const char* objname) {
16+
const char* addrfmt = objname ? " %08zx" : "%08zx";
17+
const unsigned char* p = (const unsigned char*) ptr;
18+
if (objname != nullptr) {
19+
fprintf(f, "%s:\n", objname);
20+
}
21+
for (size_t i = 0; i != size; ++i) {
22+
if (i % 16 == 0) {
23+
fprintf(f, addrfmt, first_offset + i);
24+
}
25+
fprintf(f, "%s%02x", (i % 8 == 0 ? " " : " "), (unsigned) p[i]);
26+
if (i % 16 == 15 || i == size - 1) {
27+
fhexdump_ascii(f, p, i);
28+
}
29+
}
30+
}
31+
32+
static void fhexdump_ascii(FILE* f, const unsigned char* p, size_t pos) {
33+
// Print an ASCII report that ends with byte p[pos].
34+
// The first byte printed is p[first], where first is the max multiple
35+
// of 16 having `first < pos`. The report starts at column 51.
36+
size_t first = pos - (pos % 16); // first char to print
37+
int n = pos + 1 - first; // # chars to print
38+
char buf[17];
39+
for (size_t i = first; i != first + n; ++i) {
40+
buf[i - first] = (p[i] >= 32 && p[i] < 127 ? p[i] : '.');
41+
}
42+
fprintf(f, "%*s|%.*s|\n", 51 - (3 * n + (n > 8)), "", n, buf);
43+
}

0 commit comments

Comments
 (0)