forked from opsengine/cpulimit
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0a578f7
commit 9d2eb6e
Showing
5 changed files
with
150 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
#include <stdlib.h> | ||
#include <string.h> | ||
#include "process_table.h" | ||
|
||
void process_table_init(struct process_table *pt, int hashsize) | ||
{ | ||
pt->hashsize = hashsize; | ||
pt->table = (struct list **)malloc(sizeof(struct list *) * pt->hashsize); | ||
if (pt->table == NULL) | ||
{ | ||
exit(1); | ||
} | ||
memset(pt->table, 0, sizeof(struct list *) * pt->hashsize); | ||
} | ||
|
||
static int proc_hash(struct process_table *pt, struct process *p) | ||
{ | ||
return p->pid % pt->hashsize; | ||
} | ||
|
||
struct process *process_table_find(struct process_table *pt, struct process *p) | ||
{ | ||
int idx = proc_hash(pt, p); | ||
if (pt->table[idx] == NULL) | ||
{ | ||
return NULL; | ||
} | ||
return (struct process *)locate_elem(pt->table[idx], p); | ||
} | ||
|
||
void process_table_add(struct process_table *pt, struct process *p) | ||
{ | ||
int idx = proc_hash(pt, p); | ||
if (pt->table[idx] == NULL) | ||
{ | ||
pt->table[idx] = (struct list *)malloc(sizeof(struct list)); | ||
if (pt->table[idx] == NULL) | ||
{ | ||
exit(-1); | ||
} | ||
init_list(pt->table[idx], sizeof(pid_t)); | ||
} | ||
add_elem(pt->table[idx], p); | ||
} | ||
|
||
int process_table_del(struct process_table *pt, struct process *p) | ||
{ | ||
struct list_node *node; | ||
int idx = proc_hash(pt, p); | ||
if (pt->table[idx] == NULL) | ||
{ | ||
return 1; /* nothing to delete */ | ||
} | ||
node = (struct list_node *)locate_node(pt->table[idx], p); | ||
if (node == NULL) | ||
{ | ||
return 2; /* nothing to delete */ | ||
} | ||
delete_node(pt->table[idx], node); | ||
return 0; | ||
} | ||
|
||
int process_table_del_pid(struct process_table *pt, pid_t pid) | ||
{ | ||
struct process p; | ||
p.pid = pid; | ||
return process_table_del(pt, &p); | ||
} | ||
|
||
void process_table_destroy(struct process_table *pt) | ||
{ | ||
int i; | ||
for (i = 0; i < pt->hashsize; i++) | ||
{ | ||
if (pt->table[i] != NULL) | ||
{ | ||
destroy_list(pt->table[i]); | ||
free(pt->table[i]); | ||
} | ||
} | ||
free(pt->table); | ||
pt->table = NULL; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#ifndef __PROCESS_TABLE_H | ||
#define __PROCESS_TABLE_H | ||
|
||
#include "process_iterator.h" | ||
#include "list.h" | ||
|
||
struct process_table | ||
{ | ||
struct list **table; | ||
int hashsize; | ||
}; | ||
|
||
void process_table_init(struct process_table *pt, int hashsize); | ||
|
||
struct process *process_table_find(struct process_table *pt, struct process *p); | ||
|
||
void process_table_add(struct process_table *pt, struct process *p); | ||
|
||
int process_table_del(struct process_table *pt, struct process *p); | ||
|
||
int process_table_del_pid(struct process_table *pt, pid_t pid); | ||
|
||
void process_table_destroy(struct process_table *pt); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters