2019q1 Homework1 (lab0)
contributed by < TsundereChen
>
Report
queue.h & queue.c
struct queue_t
- Add
list_ele_t *tail
and int size
in order to track queue more easily
queue_t *q_new()
- It's just initializing queue, using malloc
- Check if malloc failed
void q_free(queue_t *q)
- Check if queue is valid, and free from the head to the tail
bool q_insert_head(queue_t *q, char *s)
- Insert an element at head of the queue
- I thought this was easy, until I got a problem
- When doing malloc to
new_h->value
, I allocated 1 more char's size to it
I did the same thing at strncpy(new_h->value...
- I thought the right way to copy the value is to allocate only the string's size,
and don't need one more char's space.
But that failed, and add one more char's space makes me passes all the tests
- I don't know why
Find out the problem
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
TsundereChen
bool q_insert_tail(queue_t *q, char *s)
- Basically the same as
q_insert_head
- And have the same issue just like
q_insert_head
Find out the problem
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
TsundereChen
bool q_remove_head(queue_t *q, char *sp, size_t bufsize)
- There's a function called
do_remove_head_quiet
inside qtest.c
,
what this function does is call q_remove_head(queue, NULL, 0)
- So need to check the value of
*sp
and bufsize
int q_size(queue_t *q)
- By adding
int size
in struct queue_t
,
this function just need to check if q
is valid, and return q->size
void q_reverse(queue_t *q)
qtest.c
- There are two kinds of
malloc
/free
/strdup
One is provided by <string.h>
, the other one is written by CMU
Latter is for tracking if your program behaves correctly or not
Check harness.c
& harness.h
for more information
report.c & report.h
harness.h
- In these files, there are some functions related to testing the program
void *test_malloc(size_t size)
void test_free(void *p)
char *test_strdup(const cahr *s)
Functions below are defined only if INTERNAL is defined
size_t allocation_check()
int fail_probability
void set_cautious_mode(bool cautious)
void set_noallocate_mode(bool noallocate)
bool error_check()
bool exception_setup(bool limit_time)
void exception_cancel()
void trigger_exception(char *msg)
harness.c
- Test program has designed few values for checking the program
MAGICHEADER
, value at start of every allocated block
MAGICFREE
, value when deallocate block
MAGICFOOTER
, value at end of every block
FILLCHAR
, value to fill every malloced space
- The
qtest
program validates the queue using these values
- Inside
harness.c
, there are boolean values being used to change
behavior while testing the queue
cautious_mode
noallocate_mode
- There's a comment called
Data for managing exceptions
, and there are
three variables
static jmp_buf env
static volatile sig_atomic_t jmp_ready = false
static bool time_limited
- According to this page C 語言 setjmp 與 longjmp 函數用法教學
- The testing program is using
static jmp_buf env
to jump back when error occured
- And according to C99 standard - 5.1.2.3 Program execution - Point 5
volatile
is keep the variable stable when previous acceses
are completed and subsequent accesses haven't yet occured
- In other words,
volatile
means when using this variable, program must get its value from its address
- And according to GNU libc Manual - 24.4.7.2 Atomic Types
sig_atomic_t
can guarantee that read/write to this data type must happen in a single instruction, so there's no chance for "interruption"
volatile sig_atomic_t
can ensure that this variable won't be optimized by compiler,
and operation related to this variable must be done in single instruction
console.c & console.h
TODO
Environment
Reference