summaryrefslogtreecommitdiff
path: root/src/kernel/lib/earray.h
blob: 69849c939b2e8053ebfbb1249b13a7d6518a0ac9 (plain) (blame)
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
#ifndef DEF_EARRAY_H
#define DEF_EARRAY_H

#include <sched.h>

/*
 * This class implements a simple extensible array structure.
 * An entry with a 0 value is considered free and can be allocated.
 * The array will free as much space when elements are removed.
 * The array will ALWAYS have array->data = 0 when uninitialized, !=0 when initialized and in use
 * To initialize an earray:
 * - allocate the struct earray
 * - set its ref_vect_init_len and vect_len to desired values
 * - set its data to 0
 * - all the rest doesn't matter
 * - call earray_init
 * Freeing an array does not free the struct earray, only the data it references.
 */

struct earray {
	int ref_vect_init_len;
	int vect_len;		// NEVER CHANGE THIS AFTER INITIALISATION!!!!

	int ref_vect_len;
	int elements;
	void ***data;

	mutex_t mutex;
};

void earray_init(struct earray *array);
void earray_free(struct earray *earray);				// frees everything

int earray_add(struct earray *array, void* ptr);		// return element number or -1 if fail
void *earray_at(struct earray *array, int num);			// returns 0 when nothing
void earray_set(struct earray *earray, int num, void* ptr);


#endif