@@ -36,13 +36,13 @@ See "membench.h" for function semantics.
36
36
37
37
// A `free_chunk` was allocated by the arena, but isn't currently in
38
38
// use. You'll likely put some bookkeeping information into such chunks.
39
- typedef struct {
39
+ typedef struct free_chunk {
40
40
// YOUR CODE HERE
41
41
} free_chunk ;
42
42
43
43
// A `chunk_or_free` object is *either* an allocated chunk, *or* a
44
44
// free chunk. That calls for a union!
45
- typedef union {
45
+ typedef union chunk_or_free {
46
46
chunk c ;
47
47
free_chunk f ;
48
48
} chunk_or_free ;
@@ -54,27 +54,27 @@ typedef struct membench_group {
54
54
55
55
struct membench_arena {
56
56
membench_group first_group ;
57
- chunk_or_free * free_chunk ;
57
+ free_chunk * free ;
58
58
};
59
59
60
60
61
61
membench_arena * membench_arena_new (void ) {
62
62
// An arena initially contains a single chunk, which is free.
63
63
// TODO: Change this!
64
64
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 ;
66
66
return arena ;
67
67
}
68
68
69
69
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 ;
73
73
return result ;
74
74
}
75
75
76
76
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
78
78
}
79
79
80
80
void membench_arena_free (membench_arena * arena ) {
0 commit comments