-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdisastrOS_resource.c
70 lines (62 loc) · 1.59 KB
/
disastrOS_resource.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <assert.h>
#include <stdio.h>
#include "disastrOS_resource.h"
#include "disastrOS_descriptor.h"
#include "pool_allocator.h"
#include "linked_list.h"
#define RESOURCE_SIZE sizeof(Resource)
#define RESOURCE_MEMSIZE (sizeof(Resource)+sizeof(int))
#define RESOURCE_BUFFER_SIZE MAX_NUM_RESOURCES*RESOURCE_MEMSIZE
static char _resources_buffer[RESOURCE_BUFFER_SIZE];
static PoolAllocator _resources_allocator;
void Resource_init(){
int result=PoolAllocator_init(& _resources_allocator,
RESOURCE_SIZE,
MAX_NUM_RESOURCES,
_resources_buffer,
RESOURCE_BUFFER_SIZE);
assert(! result);
}
Resource* Resource_alloc(int id, int type){
Resource* r=(Resource*) PoolAllocator_getBlock(&_resources_allocator);
if (!r)
return 0;
r->list.prev=r->list.next=0;
r->id=id;
r->type=type;
List_init(&r->descriptors);
return r;
}
int Resource_free(Resource* r) {
assert(r->descriptors.first==0);
assert(r->descriptors.last==0);
return PoolAllocator_releaseBlock(&_resources_allocator, r);
}
Resource* ResourceList_byId(ResourceList* l, int id) {
ListItem* aux=l->first;
while(aux){
Resource* r=(Resource*)aux;
if (r->id==id)
return r;
aux=aux->next;
}
return 0;
}
void Resource_print(Resource* r) {
printf("id: %d, type:%d, pids:", r->id, r->type);
DescriptorPtrList_print(&r->descriptors);
}
void ResourceList_print(ListHead* l){
ListItem* aux=l->first;
printf("{\n");
while(aux){
Resource* r=(Resource*)aux;
printf("\t");
Resource_print(r);
if(aux->next)
printf(",");
printf("\n");
aux=aux->next;
}
printf("}\n");
}