I need a simple singly-linked list to help implement some memory management functionality. I just finished writing it up and would really like a code review since I haven't written this particular data structure in a long time.
struct pid_node {
int PID;
struct pid_node* next;
};
struct pid_node* pid_node_create(int PID) {
struct pid_node* new;
new = kmalloc(sizeof(struct pid_node));
if (new == NULL) return NULL;
new->PID = PID;
new->next = NULL;
return new;
}
void add_pid_node(struct pid_node* head, struct pid_node* new) {
struct pid_node* temp;
temp = head;
while(temp->next != NULL) temp = temp->next;
temp->next = new;
}
void remove_pid_node(struct pid_node* head, struct pid_node* dead) {
struct pid_node* temp, other_part_of_list, delete_node;
temp = head;
while(temp->next != NULL) {
if (temp->next == dead) {
delete_node = temp->next;
other_part_of_list = temp->next->next;
temp->next = other_part_of_list;
kfree(delete_node); //don't leak memory
return;
}
temp = temp->next;
}
kprintf("Got to end of PID list, didn't remove 'dead'!\n");
}
//returns true or false (1 or 0) if a particular PID is within my list
int query_pid(int PID, struct pid_node* head) {
struct pid_node* temp;
temp = head;
while(temp->next != NULL) {
if (temp->PID == PID) return 1;
temp = temp->next;
}
return 0; //didn't find it
}
EDIT - I want to implement a small addition to the end of "remove", in case what I want to remove is the head of this list. Here's what I'm thinking:
if (head == dead) {
temp = head;
*head = &(head->next);
kfree(temp);
return;
}
////
It doesn't have to be super fancy or anything, just has to properly carry out the four functions.
Thanks a bunch!