This repository has been archived on 2021-05-26. You can view files and clone it, but cannot push or open issues or pull requests.
OS/pintos-env/pintos/tests/threads/listpop.c

56 lines
1.4 KiB
C

#include "listpop.h"
#include <stdio.h>
#include "../lib/kernel/list.h"
#include "../threads/malloc.h"
static void populate(struct list* l, int* a, int n);
static bool compare_items(const struct list_elem * a, const struct list_elem * b, void * aux);
static void print_sorted(struct list* l);
static void free_populated(struct list* l);
struct item {
struct list_elem elem;
int priority;
};
void populate(struct list* l, int* a, int n) {
int i;
for (i = 0; i < n; i++) {
struct item* it = malloc(sizeof(struct item));
it->priority = a[i];
list_push_back(l, &(it->elem));
}
}
bool compare_items(const struct list_elem * a, const struct list_elem * b, void * aux) {
struct item * ia = list_entry(a, struct item, elem);
struct item * ib = list_entry(b, struct item, elem);
return (ia->priority < ib->priority);
}
void print_sorted(struct list* l) {
list_sort(l, compare_items, NULL);
struct list_elem* e;
for (e = list_begin(l); e != list_end(l); e = list_next(e)) {
struct item* i = list_entry(e, struct item, elem);
printf("%d\n", i->priority);
}
}
void free_populated(struct list* l) {
struct list_elem* e;
for (e = list_begin(l); e != list_end(l);) {
struct item* i = list_entry(e, struct item, elem);
e = list_next(e);
free(i);
}
}
void test_list(void) {
struct list l;
list_init(&l);
populate(&l, ITEMARRAY, ITEMCOUNT);
print_sorted(&l);
free_populated(&l);
}