contributed by < TsundereChen
>
sysprog
TsundereChen
- Please list your environment
- Pull the latest from the original repo also
TA
test_malloc
, free is correspond to test_free
fail_allocation
, which looks like this
static bool fail_allocation()
{
double weight = (double) random() / RAND_MAX;
return (weight / 0.01 * fail_probability);
}
Would this code fail ?
If so, why ?
static jmp_buf env;
, here, there's a keyword called "jmp_buf"static volatile sig_atomic_t jmp_ready = false;
, here, there's a keyword called "volatile"Need further explaination
head
is not enough for us to create functions in queue.c
, to trace queue easier, I added tail
q_size
function, I added int size
to queue_t
typedef struct {
/* Linked list of elements
You will need to add more fields to this structure
to efficiently implement q_size and q_insert_tail
*/
list_ele_t *head;
list_ele_t *tail;
int size;
} queue_t;
q_new
is a function to create empty queue, and return NULL if malloc failedif
statement here, to check if malloc
succeed, and because I added tail
and size
to queue_t, so I also initialized these values.
queue_t *q_new()
{
/* What if malloc returned NULL? */
queue_t *q = malloc(sizeof(queue_t));
if(q == NULL)
return NULL;
q->head = NULL;
q->tail = NULL;
q->size = 0;
return q;
}
list_ele_t
, but you should also free value
in every elementvoid q_free(queue_t *q)
{
if (q != NULL) {
/* How about freeing the list elements and the strings? */
/* Free queue structure */
while (q->head != q->tail) {
list_ele_t *del_t = q->head;
free(q->head->value);
q->head = q->head->next;
free(del_t);
}
if (q->head != NULL || q->tail != NULL) {
free(q->tail->value);
free(q->tail);
}
free(q);
}
}
q_insert_head
is a function that insert a new list_ele_t
to the head of the queuestrcpy
, I choose to use strncpy
, and add one more char byte to the destination array, to try to avoid buffer overflow
bool q_insert_head(queue_t *q, char *s)
{
/* What should you do if the q is NULL? */
/* Don't forget to allocate space for the string and copy it */
/* What if either call to malloc returns NULL? */
if(q == NULL)
return false;
list_ele_t *newh;
newh = malloc(sizeof(list_ele_t));
newh->value = malloc(sizeof(char) * (strlen(s) + 1));
if(newh == NULL || newh->value == NULL)
return false;
strncpy(newh->value, s, strlen(s) + 1);
newh->next = q->head;
q->head = newh;
if(q->tail == NULL)
q->tail = newh;
q->size = q->size + 1;
return true;
}
bool q_insert_tail(queue_t *q, char *s)
{
if (q == NULL)
return false;
list_ele_t *newt;
newt = malloc(sizeof(list_ele_t));
if (newt == NULL)
return false;
newt->value = malloc(sizeof(char) * (strlen(s) + 1));
if (newt->value == NULL) {
free(newt);
return false;
}
strncpy(newt->value, s, strlen(s) + 1);
newt->next = NULL;
q->tail->next = newt;
q->tail = newt;
if (q->head == NULL)
q->head = newt;
q->size = q->size + 1;
return true;
}
do_remove_head_quiet
in qtest.c, and it call q_remove_head in this format "q_remove_head(q, NULL, 0)"
sp
is pointed to NULL, and if it's pointed to NULL, don't copy the string, just remove the head.
bool q_remove_head(queue_t *q, char *sp, size_t bufsize)
{
if (q == NULL || q->size == 0)
return false;
if (sp == NULL && bufsize == 0) {
/* This is do_remove_head_quiet in qtest.c */
list_ele_t *delh = q->head;
q->size = q->size - 1;
if (q->size == 0) {
q->head = NULL;
q->tail = NULL;
} else {
q->head = q->head->next;
}
free(delh->value);
free(delh);
return true;
} else {
char buf_str[bufsize];
memset(buf_str, '\0', bufsize);
memset(sp, '\0', bufsize);
strncpy(buf_str, q->head->value, bufsize - 1);
strncpy(sp, buf_str, bufsize);
list_ele_t *delh = q->head;
q->size = q->size - 1;
if (q->size == 0) {
q->head = NULL;
q->tail = NULL;
} else {
q->head = q->head->next;
}
free(delh->value);
free(delh);
return true;
}
}
size
in queue_t, we can get the size of the queue just by accessing q->sizeint q_size(queue_t *q)
{
if (q != NULL)
return q->size;
else
return 0;
}
make test
, so a better solution is neededvoid q_reverse(queue_t *q)
{
if (q == NULL || q->size < 2)
return;
else {
list_ele_t *tmph = q->head;
list_ele_t *tmpt = q->tail;
list_ele_t *previous = NULL;
list_ele_t *current = q->head;
list_ele_t *preceding = q->head->next;
while (preceding != NULL) {
current->next = previous;
previous = current;
current = preceding;
preceding = preceding->next;
}
current->next = previous;
q->head = tmpt;
q->tail = tmph;
return;
}
}
tsundere:~/ $ uname -a
Linux Tsundere-X240s 4.18.10-zen1-1-zen #1 ZEN SMP PREEMPT Wed Sep 26 09:48:45 UTC 2018 x86_64 GNU/Linux
tsundere:~/ $ lscpu
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
CPU(s): 4
On-line CPU(s) list: 0-3
Thread(s) per core: 2
Core(s) per socket: 2
Socket(s): 1
NUMA node(s): 1
Vendor ID: GenuineIntel
CPU family: 6
Model: 69
Model name: Intel(R) Core(TM) i7-4500U CPU @ 1.80GHz
Stepping: 1
CPU MHz: 910.648
CPU max MHz: 3000.0000
CPU min MHz: 800.0000
BogoMIPS: 4790.56
Virtualization: VT-x
L1d cache: 32K
L1i cache: 32K
L2 cache: 256K
L3 cache: 4096K
NUMA node0 CPU(s): 0-3
Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm cpuid_fault epb invpcid_single pti ssbd ibrs ibpb stibp tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid xsaveopt dtherm ida arat pln pts flush_l1d
tsundere:~/ $ hostnamectl
Static hostname: Tsundere-X240s
Icon name: computer-laptop
Chassis: laptop
Machine ID: 35c34f0e79b049048648088e195686e6
Boot ID: 8f1ca5b31e8a4965a3d3a30feee9ec0e
Operating System: Arch Linux
Kernel: Linux 4.18.10-zen1-1-zen
Architecture: x86-64