Skip to content

Commit 8cd1c47

Browse files
committed
Exercise 3 updates.
1 parent 2398f91 commit 8cd1c47

File tree

3 files changed

+13
-8
lines changed

3 files changed

+13
-8
lines changed

common/rules.mk

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ ISCLANG := $(shell if $(CC) --version | grep LLVM >/dev/null; then echo 1; else
44
CC += -std=gnu11 -W -Wall -Wshadow
55
CFLAGS ?= -g $(DEFS)
66
O ?= -O3
7+
ifeq ($(filter 0 1 2 3 s,$(O)),$(strip $(O)))
8+
override O := -O$(O)
9+
endif
710

811
# these rules ensure dependencies are created
912
DEPCFLAGS = -MD -MF $(DEPSDIR)/$*.d -MP

fundamentals3x/.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@ membench-malloc
22
membench-jemalloc
33
membench-tcmalloc
44
membench-arena
5+
membench-fullarena
6+
membench-cheat

fundamentals3x/mb-arena.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ See "membench.h" for function semantics.
3636

3737
// A `free_chunk` was allocated by the arena, but isn't currently in
3838
// use. You'll likely put some bookkeeping information into such chunks.
39-
typedef struct {
39+
typedef struct free_chunk {
4040
// YOUR CODE HERE
4141
} free_chunk;
4242

4343
// A `chunk_or_free` object is *either* an allocated chunk, *or* a
4444
// free chunk. That calls for a union!
45-
typedef union {
45+
typedef union chunk_or_free {
4646
chunk c;
4747
free_chunk f;
4848
} chunk_or_free;
@@ -54,27 +54,27 @@ typedef struct membench_group {
5454

5555
struct membench_arena {
5656
membench_group first_group;
57-
chunk_or_free* free_chunk;
57+
free_chunk* free;
5858
};
5959

6060

6161
membench_arena* membench_arena_new(void) {
6262
// An arena initially contains a single chunk, which is free.
6363
// TODO: Change this!
6464
membench_arena* arena = (membench_arena*) malloc(sizeof(membench_arena));
65-
arena->free_chunk = &arena->first_group.chunks[0];
65+
arena->free = &arena->first_group.chunks[0].f;
6666
return arena;
6767
}
6868

6969
chunk* membench_alloc(membench_arena* arena) {
70-
assert(arena->free_chunk != NULL);
71-
chunk* result = &arena->free_chunk->c;
72-
arena->free_chunk = NULL;
70+
assert(arena->free != NULL);
71+
chunk* result = (chunk*) arena->free; // OK because of the union
72+
arena->free = NULL;
7373
return result;
7474
}
7575

7676
void membench_free(membench_arena* arena, chunk* x) {
77-
arena->free_chunk = (chunk_or_free*) x;
77+
arena->free = (free_chunk*) x; // OK because of the union
7878
}
7979

8080
void membench_arena_free(membench_arena* arena) {

0 commit comments

Comments
 (0)